Connecting Blynk and ESP32

Hi,

I have two force sensors connected to an ESP32 microcontroller. When the data from the force sensor is printed in an Arduino IDE, I get the output. But the same when it is connected to through Blynk Application, I am not getting output in the Arduino IDE and in the Blynk Application. My code is as mentioned below.

#define BLYNK_TEMPLATE_ID "TMPL*&^*&*^&*"
#define BLYNK_TEMPLATE_NAME "Smart Seating Detection System"
#define BLYNK_AUTH_TOKEN "C26g*******%%^%^&******"
#define SENSOR_PIN1 4 // The ESP32 pin connected to the first sensor
#define SENSOR_PIN2 2  // The ESP32 pin connected to the second sensor

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

char auth[] = "C26g*******%%^%^&******"; // Get this from the Blynk App
char ssid[] = "<My SSID>"; // Your WiFi SSID
char pass[] = "<My Password>"; // Your WiFi password

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

int condition(int s1, int s2)
{
 int cnt;
 if(s1 > 30)
 {
   if(s2 > 30)
   {
     cnt = 0;
   }
   else
   {
     cnt = 1;
   }
 }
 else
 {
       if(s2 > 30)
   {
     cnt = 1;
   }
   else
   {
     cnt = 2;
   }
 }
 return cnt;
}

void loop() {
  int OccupancyCount;
  int ForceSensor_1 = analogRead(SENSOR_PIN1);
  int ForceSensor_2 = analogRead(SENSOR_PIN2);
  OccupancyCount = condition(ForceSensor_1, ForceSensor_2);
  Serial.println(ForceSensor_1); // Print sensor value to serial monitor
  Serial.println(ForceSensor_2); // Print sensor value to serial monitor
  Serial.println(OccupancyCount);
  
  Blynk.virtualWrite(V1, ForceSensor_1); // Send sensor value to Blynk app
  Blynk.virtualWrite(V2, ForceSensor_2); // Send sensor value to Blynk app
  Blynk.virtualWrite(V3, OccupancyCount); // Send sensor value to Blynk app
  Blynk.run();
  delay(1000); // Adjust delay as needed
}

I have tried debugging the code. When " Blynk.begin(auth, ssid, pass);" line is compiled and executed, I am getting an error. Could you please advice what could be done to sort this issue out. I have tried with the below also and still is does not work.

I have tried with the below code instead of " Blynk.begin(auth, ssid, pass);"

Alternate code which I have tried:

WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
  delay(500);
  Serial.print(".");
}
Blynk.config(auth);
Blynk.connect();

@venguru95 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.

Thank you for advice @PeteKnight. I have edited the above. Please do help me out with the query.

The reason that your sensors work without Blynk, but won’t work with Blynk is that you’ve chosen analog pins that are connected to the ESP33’s ADC2 analog to digital converter.

The ESP32 architecture doesn’t allow ADC2 to be used at the same time as WiFi.

You should choose pins that are connected to ADC1 instead…

  • 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)

Read this for more info…

But, your void loop isn’t compatible with Blynk either. Read this for info about how to use BlynkTimer instead of a blocking delay(), and declutter your loop…

I found this part of your post very confusing…

Without more information about this error, and clarification about whether is a compilation error or an execution error, we can’t help with this issue.

Pete.