I have a weird issue I could really use help with.
I have a simple sketch below:
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// You should get Auth Token in the Blynk App.
char auth[] = "";
// Conversion factor for micro seconds to seconds
#define uS_TO_S_FACTOR 1000000
// Time ESP32 will go to sleep (in seconds)
#define TIME_TO_SLEEP 15
// holds the total boot counts
RTC_DATA_ATTR int bootCount = 0;
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "-()-";
// Moisture sensor is connected to GPIO 34 (Analog ADC1_CH6)
const int sensorPin = 34;
// variable for storing the moisture sensor value
int sensorValue = 0;
// this function reads the sensor values and send them to blynk
void publishSensorValues()
{
// Reading moisture sensor value
sensorValue = analogRead(sensorPin);
// Sending to blynk
Blynk.virtualWrite(V0, sensorValue);
while (Blynk.connect() == false) {
// Wait until connected
}
// Send uptime
Blynk.virtualWrite(V1, bootCount);
}
void setup()
{
// Debug console
Serial.begin(115200);
delay(500); //Take some time to open up the Serial Monitor
//Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
// schedule a wakeup in the future
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
// connect to blynk
Blynk.begin(auth, ssid, pass);
// read and publish the sensor values
publishSensorValues();
Serial.println("Going to sleep now");
Serial.flush();
esp_deep_sleep_start();
}
void loop()
{
}
I realized if I take out this piece:
while (Blynk.connect() == false) {
// Wait until connected
}
then only the first Blynk.virtualWrite(V0, sensorValue) succeeds. If I swap V1 and V0, then V1 succeeds.
Which one is the first, that one succeeds.
But if I put while (Blynk.connect() == false) back, then both succeed.
BUTāI get a double connection statements in the console:
Coding for deep sleep is very different to regular Blynk coding - where the void loop needs to be kept clean. With deep sleep, the opposite is true, you want the void loop to execute once, so you put everything in there.
I have no experience of coding ESP32s for deep sleep, but i think the general principals should be the same.
Your code contains no Blynk.run(); commands, which in my experience are needed to get most Blynk functions to work correctly. Youāre also trying to use Blynk.connect when I suspect that you should be using Blynk.connected()```
Also, using Blynk.begin, which is a blocking function, isnāt good for battery powered devices as a failure to connect to Wi-Fi or Blynk will result in the device continually trying to connect until it either succeeds or the batteries are flat.
Itās better to use do your own Wi-Fi connection management then use Blynk.config and Blynk.connect.
Hereās a bit of code that I contributed to in the earlier stages when it was running on an ESP8266, but was then ported over to ESP32 by @christophebl and he reports that itās working well for himā¦
// this function reads the sensor values and send them to blynk
void publishSensorValues()
{
Blynk.connect();
// Reading moisture sensor value
sensorValue = analogRead(sensorPin);
// Sending to blynk
Blynk.virtualWrite(V0, sensorValue);
// Send uptime
Blynk.virtualWrite(V1, bootCount);
}
I think the ādouble connectionsā are because your device isnāt talking to the Blynk server after the first connection, so it then tries to re-connect.
This is because of the lack of Blynk.run statements.
Any āwhile/wendā command is also likely to cause disconnections unless thereās a Blynk.run command in the loop.
āDouble Connectionsā happen because Blynk.connect() is called again. Use Blynk.connected() to check if there is connection.
Try this code
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// You should get Auth Token in the Blynk App.
char auth[] = "****************";
// Conversion factor for micro seconds to seconds
#define uS_TO_S_FACTOR 1000000
// Time ESP32 will go to sleep (in seconds)
#define TIME_TO_SLEEP 15
// holds the total boot counts
RTC_DATA_ATTR int bootCount = 0;
// Your WiFi credentials.
// Set password to "" for open networks.
char cloudBlynkServer[] = "****";
char ssid[] = "****";
char pass[] = "****";
// Moisture sensor is connected to GPIO 34 (Analog ADC1_CH6)
const int sensorPin = 34;
// variable for storing the moisture sensor value
int sensorValue = 0;
// this function reads the sensor values and send them to blynk
void publishSensorValues()
{
#define KAVG 10
sensorValue = 0;
// Reading moisture sensor value
for (int i = 0; i < KAVG; i++) //
{
sensorValue += analogRead(sensorPin);
delay(20);
}
sensorValue = sensorValue / KAVG;
// Sending to blynk
Blynk.virtualWrite(V5 /*V0*/, sensorValue);
// Send uptime
Blynk.virtualWrite(V3 /*V1*/, bootCount);
}
#define BLYNK_SERVER_HARDWARE_PORT 8080
void setup()
{
// Debug console
Serial.begin(115200);
//Increment boot number and print it every reboot
++bootCount;
// schedule a wakeup in the future
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
// connect to blynk
Blynk.begin(auth, ssid, pass, cloudBlynkServer, BLYNK_SERVER_HARDWARE_PORT);
if (Blynk.connected())
{
// read and publish the sensor values
publishSensorValues();
}
Serial.println("Boot number: " + String(bootCount));
Serial.println("Going to sleep now");
Serial.flush();
Blynk.disconnect();
esp_deep_sleep_start();
}
void loop()
{
}
I think that dispensing with Blynk.run is a risky strategy, and the effects arenāt really well understood.
Iāve seen other discussions where a timer can call a function that contains Blynk.run, but Iām not really convinced and it doesnāt have a place in the code that youāre using.
As far as what youāre trying to achieve, Iād read through the comments that are in beehive code that I linked to. Itās well commented (I know because I put them in there ) and Iām told that it the code works well.
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// You should get Auth Token in the Blynk App.
char auth[] = "";
// Conversion factor for micro seconds to seconds
#define uS_TO_S_FACTOR 1000000
// Time ESP32 will go to sleep (in seconds)
#define TIME_TO_SLEEP 15
// holds the total boot counts
RTC_DATA_ATTR int bootCount = 0;
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "-()-";
// Moisture sensor is connected to GPIO 34 (Analog ADC1_CH6)
const int sensorPin = 34;
// this function reads the sensor values and send them to blynk
void publishSensorValues()
{
// how many readings to take
#define MAX_SENSOR_READS 10
// variable for storing the moisture sensor value
int sensorValue = 0;
// Reading moisture sensor values
for (int i = 0; i < MAX_SENSOR_READS; i++)
{
sensorValue += analogRead(sensorPin);
delay(20);
}
sensorValue = sensorValue / MAX_SENSOR_READS;
// Sending to blynk
Blynk.virtualWrite(V0, sensorValue);
// Send uptime
Blynk.virtualWrite(V1, bootCount);
}
void setup()
{
// Debug console
Serial.begin(115200);
delay(500); //Take some time to open up the Serial Monitor
//Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
// schedule a wakeup in the future
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
// setup the connection to blynk
Blynk.config(auth);
// setup WiFi
Blynk.connectWiFi(ssid, pass);
//will continue trying to connect to Blynk server.
//Returns true when connected, false if timeout has been reached.
//Default timeout is 30 seconds.
bool result = Blynk.connect();
if (Blynk.connected())
{
// read and publish the sensor values
publishSensorValues();
}
Serial.println("Going to sleep now");
Serial.flush();
Blynk.disconnect();
esp_deep_sleep_start();
}
void loop()
{
}
Iām not sure why youāre saving the outcome of the Blynk.connect() test to the variable āresultā if youāre not going to use it in the next stage. Wouldnāt it be better to do this:?
//will continue trying to connect to Blynk server.
//Returns true when connected, false if timeout has been reached.
//Default timeout is 30 seconds.
bool result = Blynk.connect();
if (result)
{
// read and publish the sensor values
publishSensorValues();
}