Hello!
I am trying to make a simple water level meter and connect to Blynk.
The hardware is Nodemcu V3 and JSN SR04T 2.0
It connects and no errors during compilation of code, but the readings from the sensor is very unstable. I have read in other forums about the unstable readings from the JSN SR04T 2.0, but so far no solutons.
Does anyone have any ideas how to make it work properly ?
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <NewPing.h>
//#include <ArduinoOTA.h> //added for OTA
#define SONAR_NUM 2 // Number of sensors. Change to suit your requirements.
#define PI 3.1415926535897932384626433832795
//** CHANGE TO SUIT TANK DIMENSIONS
const int MAX_DISTANCE = 200; //max distance to measure
const int Diameter1 = 160; //internal Diameter of tank 1 in cm
const int Diameter2 = 160; //internal Diameter of tank 2 in cm
const int Depth1 = 220; //total depth of tank 1 in cm , from sensor to base inside
const int Depth2 = 200; //total depth of tank 2 in cm, from sensor to base inside
const unsigned int Period = 1000; //period between pings, in milliseconds. i.e 1 minute = 60,000. max 65535. Want longer? use unsigned long
//** SENSOR PINS
const int PingPin1 = 5; // GPIO5, D1
const int EchoPin1 = 4; // GPIO4, D2
const int PingPin2 = 14; // GPIO14, D5
const int EchoPin2 = 12; // GPIO12, D6
const int Area1 = PI * ((Diameter1 / 2) * (Diameter1 / 2)); //area of base of tank 1
const int Area2 = PI * ((Diameter2 / 2) * (Diameter2 / 2)); //area of base of tank 2
// Global variables
int Litres1, Litres2, Distance1, Distance2, WaterDepth1, WaterDepth2;
// Set password to "" for open networks.
char ssid[] = "xxxxxxxxxxx"; //local wifi network SSID
char pass[] = "xxxxxxxxxx"; //local network password
char auth[] = "xxxxxxxxxxxx";
BlynkTimer 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.
NewPing(PingPin2, EchoPin2, MAX_DISTANCE)
};
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
//************Readings Tank 2
Distance2 = sonar[1].ping_cm(); //get distance to the top of the water tank 2
if (Distance2 >= Depth2 || Distance2 == 0) Distance2 = Depth2; //check it does not go negative
WaterDepth2 = Depth2 - Distance2; //calculate the depth of the water
Litres2 = (Area2 * WaterDepth2) / 1000; //calculate the volume of the water
//***********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 / 10); //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);
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
Serial.println();
Serial.println("Tank 2 water distance: " + String(Distance2)); //print depth
Serial.println("Tank 2 water depth: " + String(WaterDepth2)); //print depth
Serial.println("Tank 2 Litres: " + String(Litres2)); //print litres
}
void setup() {
pinMode(13, OUTPUT); //LED D7
timer.setInterval(Period, sendSensorReadings); // Setup a function to be called every ten seconds
delay(10);
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);
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
}