ESP8266 Water Level Monitor

Carousel imageCarousel imageCarousel image

//Libraries

#include <ESP8266WiFi.h>

#include <ESP8266WebServer.h>

#include <ESP8266mDNS.h>

#include <WiFiUdp.h>

#include <ArduinoOTA.h>


const char* ssid = "xxxxxxxxx";

const char* password = "xxxxxxxxxx";

char host[] = "api.thingspeak.com";

String ApiKey = "xxxxxxxxxxxxxx";

String path = "/update?key=" + ApiKey + "&field1=";


#define ECHOPIN 5 //Pin to receive echo pulse

#define TRIGPIN 4 //Pin to send trigger pulse


#define PI 3.1415926535898

#define SUPERFICE_BASE (R_POZZO * R_POZZO * PI)

#define R_POZZO 0.66 //raggio pozzo (m)

#define H_POZZO 100.05 //Height of tank cm


long distance;

int litres;

float percentage = 0;


//unsigned long updateSensor = 0;

int updatePeriod = 15;


//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

void setup() {


Serial.begin(115200);


pinMode(ECHOPIN, INPUT);

pinMode(TRIGPIN, OUTPUT);

digitalWrite(ECHOPIN, HIGH);


WiFi.begin(ssid, password);

// Wait for connection

while (WiFi.status() != WL_CONNECTED) {

delay(500);

Serial.print(".");

}


ArduinoOTA.onStart([]() {

String type;

if (ArduinoOTA.getCommand() == U_FLASH) {

type = "sketch";

} else { // U_SPIFFS

type = "filesystem";

}


// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()

Serial.println("Start updating " + type);

});

ArduinoOTA.onEnd([]() {

Serial.println("\nEnd");

});

ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {

Serial.printf("Progress: %u%%\r", (progress / (total / 100)));

});

ArduinoOTA.onError([](ota_error_t error) {

Serial.printf("Error[%u]: ", error);

if (error == OTA_AUTH_ERROR) {

Serial.println("Auth Failed");

} else if (error == OTA_BEGIN_ERROR) {

Serial.println("Begin Failed");

} else if (error == OTA_CONNECT_ERROR) {

Serial.println("Connect Failed");

} else if (error == OTA_RECEIVE_ERROR) {

Serial.println("Receive Failed");

} else if (error == OTA_END_ERROR) {

Serial.println("End Failed");

}

});

Serial.println("");

Serial.print("Connected to ");

Serial.println(ssid);

Serial.print("IP address: ");

Serial.println(WiFi.localIP());

Serial.println();

Serial.print("MAC: ");

Serial.println(WiFi.macAddress());

}


//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

void loop() {


// if (millis() - updateSensor > 180 * 1000UL) {

// updateSensor = millis();

// delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.

//String v = String(distance); //Is this needed at all? Freddie to find out


ArduinoOTA.handle();


digitalWrite(TRIGPIN, LOW); //Set the trigger pin to low for 2uS

delayMicroseconds(2);

digitalWrite(TRIGPIN, HIGH); //Send a 10uS high to trigger ranging

delayMicroseconds(15);

digitalWrite(TRIGPIN, LOW); //Send pin low again


distance = pulseIn(ECHOPIN, HIGH, 26000); //Read in times pulse

distance = distance / 58;

Serial.print(distance);

float distance2 = (H_POZZO - distance) + 20; //Height of tank

Serial.println(" cm");


litres = floor(SUPERFICE_BASE * distance2 * 10);

Serial.println(litres);


percentage = (litres / 1380.0) * 100.0;


Serial.println(percentage);


// make TCP connections

WiFiClient client;

const int httpPort = 80;

if (!client.connect(host, httpPort)) {

return;

}


String url = "/update?key=";

url += ApiKey;

url += "&field1=";

url += String(distance);

url += "&field2=";

url += String(litres);

url += "&field3=";

url += String(percentage);

url += "\r\n";


// Request to the server

client.print(String("GET ") + url + " HTTP/1.1\r\n" +

"Host: " + host + "\r\n" +

"Connection: keep-alive\r\n\r\n");


delay(updatePeriod * 1000);

}