I am using the ESP32 Board with the JSN-SR40T sensor. I have the sensor wired correctly and it is getting 4.84V to the sensor from the VIN pin on the Esp32. The issue I’m having is with the reading being zero every other time the function sendSensorReadings() is called via the blynk timer. I have added an exponential filter to try and smooth the data but it is still not good enough. I also tried using the BLYNK_CONNECTED() function incase it was a sync issue. None of that has helped and I cannot find anyone that has had a similar problem. I currently have it taking a reading every 5 minutes but I was originally using shorter intervals with the same issue. I am using the Blynk app on an Iphone 11 and Adruino IDE to program the ESP32. I am also using the blynk servers and not a local one. I have also tried multiple sensors, power sources, and esp32’s with the same result.
#include <WiFi.h>
#include <WiFiClient.h>
#include <Blynk.h>
#include <BlynkSimpleEsp32.h>
#include <MegunoLink.h>
#include <Filter.h>
//#include <ArduinoOTA.h>
#define trigPin 25
#define echoPin 26
char ssid[] = "TMax"; //local wifi network SSID
char pass[] = "charger1"; // local network password
char auth[] = "GaKxoyGpUVF7kmi7LBtExrDW2DS081_l"; // Authority Token
float Distance_cm;
float Distance_ft;
float duration;
float Duration_clean;
long FilterWeight = 55;
ExponentialFilter<long> ADCFilter(FilterWeight, 0);
BlynkTimer timer; //config timer
BLYNK_CONNECTED() {
//get data stored in virtual pins V1 and V2 from server
Blynk.syncVirtual(V1);
Blynk.syncVirtual(V2);
}
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// ArduinoOTA.onError([](ota_error_t error) { ESP.restart(); });
// ArduinoOTA.begin();
Blynk.begin(auth, ssid, pass);
timer.setInterval(300000L, sendSensorReadings);
}
void sendSensorReadings()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
Distance_cm = duration *0.034/2;
Distance_ft = 0.0328084 * Distance_cm;
ADCFilter.Filter(Distance_ft);
Blynk.virtualWrite(V1,ADCFilter.Current());
Blynk.virtualWrite(V2, Distance_ft);
}
void loop() {
Blynk.run();
timer.run();
//ArduinoOTA.handle();
}