Esp32 not reconnecting to server

I’m a new user and just picked up an example to read temperature and a couple of sensors. After a few hours esp32 is trying to connect to server and looping there. Time to time I also get an “Packet too big: 1280” message. The major problem is the disconnection that makes the project unusable.
After hw rest the program starts to work normally.
Any help?
many thanks

Well, based on what we can see of your programme… sorry, not much help available :stuck_out_tongue_winking_eye:

Understand but when I inserted the code into the post I blocked because a newcomer.
Anyhow now I try again:

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <OneWire.h>
#include<DallasTemperature.h>
#define ONE_WIRE_BUS 32

int sensorPin = 35;
int sensorPin2 = 33;
int sensorValue1 = 0;  // variable to store the value coming from the sensor
int sensorValue2 = 0;


char auth[] = "xxxxxxxxxxxxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxx";

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);

float temp;
float buff;


void setup() {

  Serial.begin(115200);
  Serial.println();
  Serial.println();
 analogReadResolution(10);
  DS18B20.begin();

  xTaskCreatePinnedToCore(WifiTask, "WifiTask ", 4000, NULL, 2, NULL, 0); //Task for Core 0 priority set at 2
  // xTaskCreatePinnedToCore(####Task,"####Task ",4000,NULL,1,NULL,0); //Example of another task pinned to Core 0 at a lower priority

  xTaskCreatePinnedToCore(tempTask, "tempTask ", 4000, NULL, 5, NULL, 1); //Task for Core 1 priority set at 5
  // xTaskCreatePinnedToCore(####Task,"####Task ",4000,NULL,1,NULL,1); //Example of another task pinned to Core 1 at a lower priority
}

void loop() {
  vTaskDelete( NULL );
}

void tempTask( void * pvParameters )  //Task for reading Temperature on Core 1
{

  while (1) {                         //Use whatever code you use to measure temperature
    DS18B20.requestTemperatures();
    buff = DS18B20.getTempCByIndex(0);
 //   Serial.println (buff);
    if (buff != -127.00) {
      temp = buff;
    }
    Blynk.virtualWrite(V10, temp);
//    Serial.println (temp);
 
    sensorValue1 = analogRead(sensorPin);
//    sensorValue2 = analogRead(sensorPin2);
//    Serial.print (" sensor ");
//    Serial.print (sensorValue1);Serial.print ("  ");
//    Serial.println (sensorValue2);
  Blynk.virtualWrite(V11,  sensorValue1);
//    Blynk.virtualWrite(V12, sensorValue2);
   vTaskDelay( 5000 / portTICK_PERIOD_MS);  //Delay for this task allows the core to do other tasks



  }
}

void WifiTask( void * pvParameters )  //Wifi&Blynk tasks on Core 0
{
  WiFi.disconnect();                  //Code here runs once same as setup
  Blynk.begin(auth, ssid, pass);
  while (Blynk.connect() == false) {}

  while (1) {                         //Code here runs continously same as loop
    Blynk.run();
  
    vTaskDelay( 1500 / portTICK_PERIOD_MS);  //Little delay here is necessary
  }
}

Not sure what you have in your void loop()… but I don’t see anything that will allow Blynk to run :wink:

This is all that you should have in your void loop()… and run all other functions from timer calls

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

Thanks for your help, but I still have the problem. In effect 2 different problems.
First: compiler doesn’t recognize timer.run instruction. I also included simpletimer.h but compiler doesn’t find it. I bypassed the problem by commenting the instruction, but it is not a solution
Second I receive watchdog alert for the wifi task.
I originally copied the code from a forum for ds18b20 and esp32 problem where it was claimed it was working, anyhow…

and the code


> #define BLYNK_PRINT Serial
> #include <WiFi.h>
> #include <BlynkSimpleEsp32.h>
> #include <OneWire.h>
> #include<DallasTemperature.h>
> #include <SimpleTimer.h>
> #define ONE_WIRE_BUS 32
> 
> int sensorPin = 35;
> int sensorPin2 = 33;
> int sensorValue1 = 0;  // variable to store the value coming from the sensor
> int sensorValue2 = 0;
> 
> 
> char auth[] = "xxxxxxxxxxxxx";
> 
> // Your WiFi credentials.
> // Set password to "" for open networks.
> char ssid[] = "Vodafone-roberto";
> char pass[] = "xxxxxxxxx";
> 
> OneWire oneWire(ONE_WIRE_BUS);
> DallasTemperature DS18B20(&oneWire);
> 
> float temp;
> float buff;
> 
> 
> void setup() {
> 
>   Serial.begin(115200);
>   Serial.println();
>   Serial.println();
>   analogReadResolution(10);
>   DS18B20.begin();
> 
>   xTaskCreatePinnedToCore(WifiTask, "WifiTask ", 4000, NULL, 2, NULL, 0); //Task for Core 0 priority set at 2
>   // xTaskCreatePinnedToCore(####Task,"####Task ",4000,NULL,1,NULL,0); //Example of another task pinned to Core 0 at a lower priority
> 
>   xTaskCreatePinnedToCore(tempTask, "tempTask ", 4000, NULL, 5, NULL, 1); //Task for Core 1 priority set at 5
>   // xTaskCreatePinnedToCore(####Task,"####Task ",4000,NULL,1,NULL,1); //Example of another task pinned to Core 1 at a lower priority
> 
> 
> 
> }
> void loop() {
>   Blynk.run();
> //  timer.run();
> }
> 
> //  vTaskDelete( NULL );
> 
> 
> void tempTask( void * pvParameters )  //Task for reading Temperature on Core 1
> {
> 
>   while (1) {                         //Use whatever code you use to measure temperature
>     DS18B20.requestTemperatures();
>     buff = DS18B20.getTempCByIndex(0);
>     //   Serial.println (buff);
>     if (buff != -127.00) {
>       temp = buff;
>     }
>     Blynk.virtualWrite(V10, temp);
>     //    Serial.println (temp);
> 
>     sensorValue1 = analogRead(sensorPin);
>     //    sensorValue2 = analogRead(sensorPin2);
>     //    Serial.print (" sensor ");
>     //    Serial.print (sensorValue1);Serial.print ("  ");
>     //    Serial.println (sensorValue2);
>     Blynk.virtualWrite(V11,  sensorValue1);
>     //    Blynk.virtualWrite(V12, sensorValue2);
>     vTaskDelay( 5000 / portTICK_PERIOD_MS);  //Delay for this task allows the core to do other tasks
> 
> 
> 
>   }
> }
> 
> void WifiTask( void * pvParameters )  //Wifi&Blynk tasks on Core 0
> {
>   WiFi.disconnect();                  //Code here runs once same as setup
>   Blynk.begin(auth, ssid, pass);
>   while (Blynk.connect() == false) {}
> 
>    while (1) {                         //Code here runs continously same as loop
> 
> 
> 
> 
>   vTaskDelay( 500 / portTICK_PERIOD_MS);  //Little delay here is necessary
> }
> }

Use three backticks, not commas or apostrophes.

Blynk - FTFC

Ensure you have latest Blynk library installed in your IDE. and remember to initialise Blynk timer in your pre-setup

BlynkTimer timer;

Then you need to actually create a timer call in your setup… check out this example

Don’t use the void loop() for running stuff if you have two seperate threads already for your stuff. Put Blynk.run in one thread and do all other in the other one.

I’ve also noticed a tendency in the ESp32 that it needs a certain delay in the created loops. Try adding vTaskDelay(10); in your vTask loops.

There is an example of how I did here: Three LEDs with Dirty Delay (ESP32)

Hope it helps :slight_smile:

-edit-

vTaskDelay(500) looks a bit too much, change that to 1 and see what happens. Just let that task run with the Blynk.run() in it :slight_smile:

Ha… I totally forgot the whole dual core thing with the ESP32 :blush: I think I am under utilising mine :slight_smile:

2 Likes

I’m a newbie, I do not see the timer object being defined in your sketch. You should’ve “BlynkTimer timer;” right on top in the sketch and see if it compiles.

many thanks to all
your suggestions were really useful and I now proceeding forward new problems! thanks:wink:

Will be useful to know what solved the problems you had…

5 posts were split to a new topic: Servo & Stepper Ramplings

here is my code now running since 8 hours. Time to time I’ve a server disconnection but I think it could be a network problem. I read values from gas sensors and a termometer

:wink:

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <OneWire.h>
#include<DallasTemperature.h>
#include <SimpleTimer.h>
#define ONE_WIRE_BUS 32
int setTimer(long d, timer_callback f, int n);
int sensorPin1 = 35;
int sensorPin2 = 34;
int sensorValue1 = 0;  // variable to store the value coming from the sensor
int sensorValue2 = 0;
BlynkTimer timer;

char auth[] = "xxxxxxxxxxxxxxxxxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Vodafone-roberto";
char pass[] = "xxxxxxxxxxxxx";

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);

float temp;
float buff;


void setup() {

  Serial.begin(115200);
  Serial.println();
  Serial.println();
  analogReadResolution(10);
  DS18B20.begin();

  xTaskCreatePinnedToCore(WifiTask, "WifiTask ", 4000, NULL, 2, NULL, 0); //Task for Core 0 priority set at 2
  // xTaskCreatePinnedToCore(####Task,"####Task ",4000,NULL,1,NULL,0); //Example of another task pinned to Core 0 at a lower priority

  xTaskCreatePinnedToCore(tempTask, "tempTask ", 4000, NULL, 5, NULL, 1); //Task for Core 1 priority set at 5
  // xTaskCreatePinnedToCore(####Task,"####Task ",4000,NULL,1,NULL,1); //Example of another task pinned to Core 1 at a lower priority

  Blynk.email("statocaldaia@yahoo.com", "partenza controllo", " Ho incominciato");

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


void tempTask( void * pvParameters )  //Task for reading Temperature on Core 1
{

  while (1) {                         //Use whatever code you use to measure temperature
    DS18B20.requestTemperatures();
    buff = DS18B20.getTempCByIndex(0);
    //   Serial.println (buff);
    if (buff != -127.00) {
      temp = buff;
    }
    Blynk.virtualWrite(V10, temp);
    //    Serial.println (temp);

    sensorValue1 = analogRead(sensorPin1);
    if (sensorValue1 >> 300)
    { Blynk.email("statocaldaia@yahoo.com", "supero livello gas", " sensor 1");
    }
    Blynk.virtualWrite(V11,  sensorValue1);
    sensorValue2 = analogRead(sensorPin2);
    Blynk.virtualWrite(V12,  sensorValue2);
    if (sensorValue2 >> 600)
    { Blynk.email("statocaldaia@yahoo.com", "supero livello gas", " sensor 2");
    }
    vTaskDelay( 10 / portTICK_PERIOD_MS);  //Delay for this task allows the core to do other tasks
  }
}

void WifiTask( void * pvParameters )  //Wifi&Blynk tasks on Core 0
{
  WiFi.disconnect();                  //Code here runs once same as setup
  Blynk.begin(auth, ssid, pass);
  Blynk.email("statocaldaia@yahoo.com", "partenza controllo", " Ho incominciato");

  while (Blynk.connect() == false) {}

  while (1) {                         //Code here runs continously same as loop
        vTaskDelay( 1 / portTICK_PERIOD_MS);  //Little delay here is necessary
  }
}
1 Like

FreeRTOS is actually a whole lot better than all that arduino stuff, but it’s not as user friendly. I set it up now and it can compile/upload stuff to the ESP (8266 for now). I’m planning on utilizing only HTTP REST api for Blynk using FreeRTOS. That should work like a charm.

1 Like

I’m not only new to Blynk, I’m new to coding. I’m learning fast but FreeRTOS is well out of my reach atm. :disappointed:

2 Likes

Yeah, it’s a very big leap of faith to step in to that world. If you try and learn the arduino you’ll eventually venture out in that world because it’s very adaptable and fast. I’ve just compiled some examples and learn by looking at others people’s work (like with Blynk btw :wink: )

1 Like

That’s exactly what I’ve been doing, Blynk too. Not sure if it works for everyone but I learn fastest by “doing”. Having code in front of me and solving any issues when adapting it for my own use/gratification. Unfortunately not everyone is willing to share code, some protect it with their lives, which I totally understand if it’s some sort of commercial enterprise but sharing helps others learn so much faster.

I find the dual cores of the ESP32 fascinating and really want to learn how to use them to their full potential. Hopefully when I think of a project to use the 4 ESP32’s I have, code for them will be a bit more accessible …

1 Like

Sadly, not everyone takes your approach. Often offering people code on a plate results in comments like “I now want it to do this as well, can you add that feature for me”.

How is the motorbike alarm going? Did you ever get the accelerometer thing working?

Pete.