#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <NewPing.h>
#include <ArduinoOTA.h>
//added for OTA
#include <Wire.h> // I2C library already built in Arduino IDE
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7);
//0x3F is i2c address, EN,RW ,RS ,D4,D5,D6,D7
#define SONAR_NUM 1 // Number
of sensors. Change to suit your requirements.
#define PI 3.1415926535897932384626433832795
//** CHANGE TO SUIT TANK DIMENSIONS
const int MAX_DISTANCE = 300; //max distance
to measure
const int Diameter1 = 25.2;
//internal Diameter of tank 1 in cm
const int Depth1 = 30; //total
depth of tank 1 in cm, from sensor to base inside
const unsigned int Period = 2000; //period between pings, in
milliseconds. i.e 1 munute = 60,000. max 65535. Want longer?
use unsigned long
//** SENSOR PINS
const int PingPin1 = 14; // GPIO14, D5
const int EchoPin1 = 12; // GPIO12, D6
const int Area1 = PI * ((Diameter1 / 2) * (Diameter1 / 2));
//area of base of tank 1
// Global variables
int Litres1, Distance1, WaterDepth1;
// Set password to "" for open networks.
char ssid[] = "TROJAN";
//local wifi network SSID
char pass[] = "technomancer";
//local network password
char auth[] = "735ef2ac5a8c476a982c4b4482f05ad5";
// You should get Authority Token in your email.
BlynkTimer timer;
//config timer
NewPing sonar[SONAR_NUM] = {
// Sensor object array.
NewPing(PingPin1, EchoPin1, MAX_DISTANCE) //
Each sensor's trigger pin, echo pin, and max distance to ping.
};
void sendSensorReadings()
{
//***********Readings Tank 1
Distance1 = sonar[0].ping_cm(); //get distance to the top of the water tank 1
if (Distance1 >= Depth1 || Distance1 == 0 ) Distance1 = Depth1; //check it does not go negative
WaterDepth1 = Depth1 - Distance1; //calculate the depth of the
water
Litres1 = (Area1 * WaterDepth1) / 1000; //calculate the volume of the water in litres
//***********SEND INFO TO BLYNK
Blynk.virtualWrite(V1, WaterDepth1); //send depth to Blynk server
Blynk.virtualWrite(V2, Litres1); //send litres to Blynk server
Blynk.virtualWrite(V3, Litres1); //send litres to Blynk server. for vertical level widget &
chart,
//scaled to 1/10 as Blynk only goes up to 9999
and we need up to 16000
//delay(50);
//Blynk.virtualWrite(V4, WaterDepth2); //send depth to Blynk server
//Blynk.virtualWrite(V5, Litres2); //send real litres to Blynk server
//Blynk.virtualWrite(V6, Litres2 / 10); //send litres/10 to Blynk server
digitalWrite(13, HIGH); //flash the LED on D7, just to let us know it's running
delay(50);
digitalWrite(13, LOW);
//************************* can be commented out, test use only
Serial.println();
Serial.println();
Serial.println("Tank 1 water distance: " + String(Distance1)); //print depth
Serial.println("Tank 1 water depth: " + String(WaterDepth1)); //print depth
Serial.println("Tank 1 Litres: " + String(Litres1)); //print litres
//***********************************************
}
void setup() {
ArduinoOTA.onError([](ota_error_t error) { ESP.restart(); }); //added for OTA
ArduinoOTA.begin(); //added for OTA
pinMode(13, OUTPUT); //LED D7
timer.setInterval(Period, sendSensorReadings); // Setup a function to be called every n seconds
delay(10);
//***********************************
lcd.begin(16,2); // initializing the LCD 16 x 2
lcd.setBacklightPin(3,POSITIVE); // Enable or Turn On the backlight
lcd.setBacklight(HIGH);
//** can be commented out, test only
Serial.begin(115200); // Open serial console.
Serial.println();
Serial.println("Connecting to " + String(ssid)); // Connected to WiFi network
//**
Blynk.begin(auth, ssid, pass);
delay(20);
//** can be commented out, test only
Serial.println(WiFi.localIP()); //this is local IP for this board
Serial.println("WiFi connected");
}
void loop() {
Blynk.run();
timer.run();
ArduinoOTA.handle(); //added for OTA
lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Water Depth:"); // Prints string "Distance" on the LCD
lcd.print(WaterDepth1); // Prints the distance value from the sensor
delay(10);
lcd.setCursor(0,1);
lcd.print("Liters: ");
lcd.print(Litres1);
lcd.print(" L");
delay(10);
}
to get started follow this tutorial Adding a New Device using MQTT