I have water level sensor (A02YYUW) that I want to connect with ESP32 and send the data to Blynk. But after my device is connected with blynk server, the sensor data is not there so I can not see the data in my blynk app. I really appreciate any help you may offer. Here is the code:
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPxxxxxxxxxxx"
#define BLYNK_TEMPLATE_NAME "Water Level Monitoring System"
#define BLYNK_AUTH_TOKEN "XFGnxxxxxxxxxxxxxxxxxxxxxxxxxx"
#include <Wire.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <HardwareSerial.h>
HardwareSerial Ultrasonic_Sensor(2); // TX2 (pin 17), RX2 (pin 16)
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Capstone";
char pass[] = "testxxxxx";
unsigned char data[4] = {};
float distance;
BlynkTimer timer;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup()
{
Serial.begin(115200); // Initialize the serial monitor
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
Ultrasonic_Sensor.begin(9600); // Initialize the hardware serial
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
delay(2000);
display.clearDisplay();
display.setTextColor(WHITE);
delay(1000);
// Setup a function to be called every second
timer.setInterval(1000L, loop);
}
void loop()
{
do
{
for (int i = 0; i < 4; i++)
{
data[i] = Ultrasonic_Sensor.read();
}
} while (Ultrasonic_Sensor.read() == 0xff);
Ultrasonic_Sensor.flush();
if (data[0] == 0xff)
{
int sum;
sum = (data[0] + data[1] + data[2]) & 0x00FF;
if (sum == data[3])
{
distance = (data[1] << 8) + data[2];
Serial.print("distance=");
distance=distance / 10;
Serial.println(distance);
display.clearDisplay();
display.setCursor(10,0);
display.setTextSize(2);
display.setTextColor(WHITE);
display.print("Distance");
display.setCursor(10,30);
display.setTextSize(2);
display.print(String(distance)+" cm");
display.display();
Blynk.virtualWrite(V0, distance);
return;
}
}
Blynk.run();
timer.run();
}
The serial monitor view:
@syahril Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```
Copy and paste these if you can’t find the correct symbol on your keyboard.
Pete.
You’re not using BlynkTimer correctly.
You void loop should contain just this code…
and everything else should be in a different function that is called by your interval timer.
However, I suspect that you have other issues, maybe with the connections of your sensor or maybe caused by trying to power your display from the ESP32. It’s difficult to say without more info.
The best approach would be to simplify things and get your hardware working without Blynk first.
Pete.
I’ve changed the code. I ran ESP32 with sensor and OLED display and it works fine.
But when I try to connect it to Blynk, it does not work well.
Here is the new code:
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPLxxxxx"
#define BLYNK_TEMPLATE_NAME "Water Level Monitoring System"
#define BLYNK_AUTH_TOKEN "XFGnpPxxxxxxxxxxxxxxxx"
#include <Wire.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <HardwareSerial.h>
HardwareSerial Ultrasonic_Sensor(2); // TX2 (pin 17), RX2 (pin 16)
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Capstone";
char pass[] = "testxxxx";
unsigned char data[4] = {};
float distance;
BlynkTimer timer;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void sendSensor()
{
do
{
for (int i = 0; i < 4; i++)
{
data[i] = Ultrasonic_Sensor.read();
}
} while (Ultrasonic_Sensor.read() == 0xff);
Ultrasonic_Sensor.flush();
if (data[0] == 0xff)
{
int sum;
sum = (data[0] + data[1] + data[2]) & 0x00FF;
if (sum == data[3])
{
distance = (data[1] << 8) + data[2];
Serial.print("distance=");
distance=distance / 10;
Serial.println(distance);
display.clearDisplay();
display.setCursor(10,0);
display.setTextSize(2);
display.setTextColor(WHITE);
display.print("Distance");
display.setCursor(10,30);
display.setTextSize(2);
display.print(String(distance)+" cm");
display.display();
Blynk.virtualWrite(V0, distance);
return;
}
}
}
void setup()
{
Serial.begin(115200); // Initialize the serial monitor
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
Ultrasonic_Sensor.begin(9600); // Initialize the hardware serial
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
delay(2000);
display.clearDisplay();
display.setTextColor(WHITE);
delay(1000);
// Setup a function to be called every second
timer.setInterval(1000L, sendSensor);
}
void loop()
{
Blynk.run();
timer.run();
}
It does not display the sensor data on dashboard, just one data and sometimes none and after that it is does not work well even the device is connected to blynk server…
The serial monitor:
@PeteKnight and the dashboard only shows once the sensor data, just like in the serial monitor.
Okay.
Drip-feeding information like…
and…
isn’t helpful when trying to assist you with your problem.
Your description doesn’t indicate if your OLED display is continuing to display data, or how long elapses between Blynk connecting and the one data point being displayed.
Straightforward debugging practices such as adding serial print messages to strategic areas of your code is probably the simplest method of identifying the program flow and where the issue lies.
In future, when you post serial output to the forum please copy/paste the text from your serial monitor and post it with triple backticks, tge same was as when posting code.
It would also help if you turned-on timestamps in your serial monitor.
Pete.
ESP32 with OLED is continuing display the sensor data. But when it’s connected to Blynk, the sensor data is not continuing display the data either in OLED or Blynk app.
Without connection to Blynk, it works fine because the sensor data is displayed in OLED continuously.
By the way thank you, Pete. I am afraid there is something wrong with my code that I can get help here. There’s nothing wrong with the wiring connection tho.
Syahril