Problem sending sensor data to Blynk

The idea is to read data from LDR, MQ135 and DHT11 sensors with an ESP-32 board, and send them to Blynk. However, the program only sends data from the DHT11 sensor to Blynk. It is worth mentioning that the program reads all data normally when it is not associated with the cloud. Can someone help me?

#define BLYNK_TEMPLATE_ID           "TMPLarC887WR"
#define BLYNK_DEVICE_NAME           "Quickstart Device"
#define BLYNK_AUTH_TOKEN            "8R4MTmvaxY_97P5GuKOfVUlLz6nTsGs_"
#define BLYNK_PRINT Serial
#include "MQ135.h"
//inclusao cĂłdigo
#include "DHT.h"
#define DHTTYPE DHT11 // DHT 11
#define PinoGasAnalogica 15
#define PinoLDR 2
#define DHTPIN 4
#define DHTTYPE DHT11

int sensor_gas = 0;
float sensor_lumi;
float indice_temp;
float indice_fogo;

MQ135 mq135_sensor(PinoGasAnalogica);
DHT dht ( DHTPIN, DHTTYPE ) ; 

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = BLYNK_AUTH_TOKEN;

char ssid[] = "OLIVEIRA_2.4G";
char pass[] = "061125104";

BlynkTimer timer;

BLYNK_WRITE(V0)
{
  // Set incoming value from pin V0 to a variable
  int value = param.asInt();
  // Update state
  Blynk.virtualWrite(V1, value);
}

BLYNK_CONNECTED()
{
  // Change Web Link Button message to "Congratulations!"
  Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
  Blynk.setProperty(V3, "onImageUrl",  "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
  Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
}

void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V2, millis() / 1000);
}
void leituras(){
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);
  
  sensor_gas = analogRead(PinoGasAnalogica);
  sensor_gas = (sensor_gas / 4096);
  sensor_lumi = analogRead(PinoLDR);
  sensor_lumi = (sensor_lumi / 4096);
  indice_temp = t / 50;

  indice_fogo = sensor_gas * 2 + sensor_lumi * 0.5 + indice_temp * 1;

  Blynk.virtualWrite(V4,t);
  Blynk.virtualWrite(V5,h);
  Blynk.virtualWrite(V6,sensor_gas);
  Blynk.virtualWrite(V7,sensor_lumi);
  Blynk.virtualWrite(V8,indice_fogo);
}
void setup()
{
  // Debug console
  Serial.begin(115200);
  dht.begin();

  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L, myTimerEvent);
  timer.setInterval(1000L, leituras);
}

void loop()
{
  Blynk.run();
  timer.run();
}

GPIO2 and GPIO15 are analog pins attached to ADC2

You can’t use the ADC2 pins in conjunction with WiFi, so you should swap to ADC1 pins…

Analog to Digital Converter (ADC)

The ESP32 has 18 x 12 bits ADC input channels (while the ESP8266 only has 1x 10 bits ADC). These are the GPIOs that can be used as ADC and respective channels:

  • ADC1_CH0 (GPIO 36)
  • ADC1_CH1 (GPIO 37)
  • ADC1_CH2 (GPIO 38)
  • ADC1_CH3 (GPIO 39)
  • ADC1_CH4 (GPIO 32)
  • ADC1_CH5 (GPIO 33)
  • ADC1_CH6 (GPIO 34)
  • ADC1_CH7 (GPIO 35)
  • ADC2_CH0 (GPIO 4)
  • ADC2_CH1 (GPIO 0)
  • ADC2_CH2 (GPIO 2)
  • ADC2_CH3 (GPIO 15)
  • ADC2_CH4 (GPIO 13)
  • ADC2_CH5 (GPIO 12)
  • ADC2_CH6 (GPIO 14)
  • ADC2_CH7 (GPIO 27)
  • ADC2_CH8 (GPIO 25)
  • ADC2_CH9 (GPIO 26)

ESP32 Pinout Reference: Which GPIO pins should you use? | Random Nerd Tutorials

Pete.

1 Like

I changed the pins to GPIO34 and GPIO 35, but the problem remains. Data from LDR and MQ135 sensors is not sent to Blynk.


#define BLYNK_TEMPLATE_ID           "TMPLarC887WR"
#define BLYNK_DEVICE_NAME           "Quickstart Device"
#define BLYNK_AUTH_TOKEN            "8R4MTmvaxY_97P5GuKOfVUlLz6nTsGs_"
#define BLYNK_PRINT Serial
#include "MQ135.h"
//inclusao cĂłdigo
#include "DHT.h"
#define DHTTYPE DHT11 // DHT 11
#define PinoGasAnalogica 35
#define PinoLDR 34
#define DHTPIN 4
#define DHTTYPE DHT11

int sensor_gas = 0;
float sensor_lumi;
float indice_temp;
float indice_fogo;

MQ135 mq135_sensor(PinoGasAnalogica);
DHT dht ( DHTPIN, DHTTYPE ) ; 

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = BLYNK_AUTH_TOKEN;

char ssid[] = "OLIVEIRA_2.4G";
char pass[] = "061125104";

BlynkTimer timer;

BLYNK_WRITE(V0)
{
  // Set incoming value from pin V0 to a variable
  int value = param.asInt();
  // Update state
  Blynk.virtualWrite(V1, value);
}

BLYNK_CONNECTED()
{
  // Change Web Link Button message to "Congratulations!"
  Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
  Blynk.setProperty(V3, "onImageUrl",  "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
  Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
}

void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V2, millis() / 1000);
}
void leituras(){
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);
  
  sensor_gas = analogRead(PinoGasAnalogica);
  sensor_gas = (sensor_gas / 4096);
  sensor_lumi = analogRead(PinoLDR);
  sensor_lumi = (sensor_lumi / 4096);
  indice_temp = t / 50;

  indice_fogo = sensor_gas * 2 + sensor_lumi * 0.5 + indice_temp * 1;

  Blynk.virtualWrite(V4,t);
  Blynk.virtualWrite(V5,h);
  Blynk.virtualWrite(V6,sensor_gas);
  Blynk.virtualWrite(V7,sensor_lumi);
  Blynk.virtualWrite(V8,indice_fogo);
}
void setup()
{
  // Debug console
  Serial.begin(115200);
  dht.begin();


  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L, myTimerEvent);
  timer.setInterval(1000L, leituras);
}

void loop()
{
  Blynk.run();
  timer.run();
}

First, I’ll add some Serial.print to see values in the serial monitor , don’t you think? :thinking:

1 Like

I checked the read without uploading to the cloud and everything works normally. The problem is not in the reading, but in sending it to Blynk, as the “indice_fogo”, which takes data from the LDR and MQ135 sensor, is changed when some modification is made to the sensors.

We can’t help you without seeing your serial monitor output.

I put a Serial.println on the LDR and M135 readings, but it appears kind of buggy. No information appears, other than strange symbols.

change the speed of your serial monitor to 115200
and post the text , not a screenshot

1 Like

This line of code…

tells your ESP32 to output data to the serial monitor at 115200 baud, but your screenshot shows that you have your serial monitor set to 8600 baud, hence the garbage in the serial monitor.

For us to be able to help we need to see your full updates code, so we can see what debug code you have added and where, and we also need to see the text that appears in your serial monitor - including what is shown at boot-up and the Blynk connection phase. Please copy the serial monitor text and paste it between triple backticks the same as for code. Please DO NOT post screenshots of your code or serial monitor output.

Pete.

9600*

1 Like

I’m having a “fat finger” day today!

Pete.

1 Like

:joy:

I think it would be better to change the monitor speed instead of uploading a new code :thinking:

1 Like

Follow the changed code and the serial monitor.

#define BLYNK_TEMPLATE_ID           "TMPLarC887WR"
#define BLYNK_DEVICE_NAME           "Quickstart Device"
#define BLYNK_AUTH_TOKEN            "8R4MTmvaxY_97P5GuKOfVUlLz6nTsGs_"
#define BLYNK_PRINT Serial
#include "MQ135.h"
//inclusao cĂłdigo
#include "DHT.h"
#define DHTTYPE DHT11 // DHT 11
#define PinoGasAnalogica 34
#define PinoLDR 35
#define DHTPIN 4
#define DHTTYPE DHT11

float sensor_gas = 0;
float sensor_lumi;
float indice_temp;
float indice_fogo;

MQ135 mq135_sensor(PinoGasAnalogica);
DHT dht ( DHTPIN, DHTTYPE ) ; 

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = BLYNK_AUTH_TOKEN;

char ssid[] = "OLIVEIRA_2.4G";
char pass[] = "061125104";

BlynkTimer timer;

BLYNK_WRITE(V0)
{
  // Set incoming value from pin V0 to a variable
  int value = param.asInt();
  // Update state
  Blynk.virtualWrite(V1, value);
}

BLYNK_CONNECTED()
{
  // Change Web Link Button message to "Congratulations!"
  Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
  Blynk.setProperty(V3, "onImageUrl",  "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
  Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
}

void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V2, millis() / 1000);
}
void leituras(){
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);
  
  sensor_gas = analogRead(PinoGasAnalogica);
  sensor_gas = (sensor_gas/4096);
  sensor_lumi = analogRead(PinoLDR);
  sensor_lumi = (sensor_lumi / 4096);
  indice_temp = t / 50;

  indice_fogo = sensor_gas * 2 + sensor_lumi * 0.5 + indice_temp * 1;

  Serial.println(sensor_lumi);
  Serial.println(sensor_gas);

  Blynk.virtualWrite(V4,t);
  Blynk.virtualWrite(V5,h);
  Blynk.virtualWrite(V6,sensor_gas);
  Blynk.virtualWrite(V7,sensor_lumi);
  Blynk.virtualWrite(V8,indice_fogo);
}
void setup()
{
  // Debug console
  Serial.begin(115200);
  dht.begin();

  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L, myTimerEvent);
  timer.setInterval(1000L, leituras);
}

void loop()
{
  Blynk.run();
  timer.run();
}

ets Jun  8 2016 00:22:57

rst:0x10 (RTCWDT_RTC_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:10944
load:0x40080400,len:6388
entry 0x400806b4
[24] Connecting to OLIVEIRA_2.4G
[2642] Connected to WiFi
[2642] IP: 192.168.0.5
[2642] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.1.0 on ESP32

 #StandWithUkraine    https://bit.ly/swua


[2652] Connecting to blynk.cloud:80
[3032] Ready (ping: 133ms).
0.13
0.26

did you configure V6 and V7 datastream as double , and set min and max value ? (web dashboard )

It would be far better if you did something like this:

  Serial.print("sensor_lumi = ");
  Serial.println(sensor_lumi);
  Serial.print("sensor_gas = ");
  Serial.println(sensor_gas);

[/quote]

and also turned-on the timestamp option in the serial monitor and let it run for a few seconds worth of readings.

How are your V6 and V7 datastreams configured (especially the data type and Min/Max values) and what type of widgets do you have these datastreams connected to? Did you remember to choose a datastream when you added the widgets?

Pete.

1 Like

For some reason it now worked. The “sensor_gas” was declared as “int”, but it must be “float”, because when dividing by 4096, the value remained at zero. After I fixed that and the serial monitor (I don’t know if it has any relationship) it is sending all the data to Blynk.

2 Likes

Yep, sending zero to Blynk will show zero in the widget display.

It’s really useful to add lots of debug messages that track the value of variables and show you when certain conditions are met and sections of the sketch are executed, especially when you’re debugging code that isn’t producing the expected results.

Pete.

1 Like

Thank you so much for the support!

1 Like

Hello there.
I’ve been having the same problem with the LDR sensor, when I’m not using Blynk it measures normally; however, as soon as I begin using Blynk it begins reading 0.
I’ve been trying everything on this thread yet I’ve got no results, could you help me?
Here’s my code:

#define BLYNK_TEMPLATE_ID "TMPL5BKXT9ld3"
#define BLYNK_TEMPLATE_NAME "Estação de Metreologia"
//#define BLYNK_AUTH_TOKEN "ifxsHtXL-ktNNrBEVF7ZmPn0BjkD7jLJ"

#include <DHT.h>
#include <Wire.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

#define LDR 4
#define Rain 36
DHT dht(5,DHT11);
//BlynkTimer timer;

//Token de autentificação Blynk
char auth[] = "ifxsHtXL-ktNNrBEVF7ZmPn0BjkD7jLJ";

//Home
char ssid[ ] = "";
char pass[] = "";


void setup() {
  Serial.begin(115200);
  dht.begin();
  Blynk.begin(auth,ssid,pass);
}


void loop() {
  Blynk.run();
  readDHT();
  readLDR();
  readRain();
  Serial.println("");
}

void readDHT(){
  float temperature = dht.readTemperature(); //Não se inserem parâmetros para que a temperatura seja medida em Celsius.
  float humidity = dht.readHumidity();
  Blynk.virtualWrite(V0, temperature);
  Blynk.virtualWrite(V1, humidity);
  Serial.print("Temp: ");
  Serial.print(temperature);
  Serial.println(" ÂşC");
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");
}

void readLDR(){
  float ldr = analogRead(LDR);
  //ldr = map(ldr,4095,0,0,100);
  Blynk.virtualWrite(V2, ldr);
  Serial.print("Luminosity: ");
  Serial.print(ldr);
  Serial.println(" %");
}

void readRain(){
  float rain = analogRead(Rain);
  rain = map(rain,4095,0,0,100);
  Blynk.virtualWrite(V3, rain);
  Serial.print("Rain: ");
  Serial.print(rain);
  Serial.println(" %");
}

Well I did it thanks anyway.
Upon close inspection of this thread I was able to do it.
Thank you so much, I’ve been trying to this over a week.
Thank you so much once again.