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();
}