takes longer time to connect with wifi after a deep sleep mode for 10minutes
Posted: Fri Apr 02, 2021 1:47 am
hi I'm ivan . I'm try to build a smart temperature&Humidity to collect a reading. I'm using thingsboard as a platform to receive the data . Now i face a problem by doing a deep sleep-mode method , sometimes i get the accurate reading after deep sleep mode or sometimes late 10-30 seconds and after successfully deep sleep and wakeup for 1hour++ ,the next 10 minutes it take to long to reconnect sometimes its just reconnect only .. can anyone help about this ?
#include "DHT.h"
#include "WiFi.h" // WiFi control for ESP32
#include <ThingsBoard.h> // ThingsBoard SDK
#define SERIAL_DEBUG_BAUD 115200
#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
#define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 10 /* Time ESP32 will go to sleep (in seconds) */
#define DHTTYPE DHT11
#define DHTPIN 4
// WiFi access point
#define WIFI_AP_NAME ""
// WiFi password
#define WIFI_PASSWORD ""
#define TOKEN ""
#define THINGSBOARD_SERVER ""
int status = WL_IDLE_STATUS;
uint8_t leds_cycling[] = { 25, 26, 32 };
uint8_t leds_control[] = { 19, 22, 21 };
RTC_DATA_ATTR int bootCount = 0;
DHT dht(DHTPIN, DHTTYPE);
WiFiClient espClient;
ThingsBoard tb(espClient);
bool subscribed = false;
int led_delay = 1000;
int send_delay = 2000;
int led_passed = 0;
int send_passed = 0;
RPC_Response processDelayChange(const RPC_Data &data)
{
Serial.println("Received the set delay RPC method");
led_delay = data;
Serial.print("Set new delay: ");
Serial.println(led_delay);
return RPC_Response(NULL, led_delay);
}
RPC_Response processGetDelay(const RPC_Data &data)
{
Serial.println("Received the get value method");
return RPC_Response(NULL, led_delay);
}
RPC_Response processSetGpioState(const RPC_Data &data)
{
Serial.println("Received the set GPIO RPC method");
int pin = data["pin"];
bool enabled = data["enabled"];
if (pin < COUNT_OF(leds_control)) {
Serial.print("Setting LED ");
Serial.print(pin);
Serial.print(" to state ");
Serial.println(enabled);
digitalWrite(leds_control[pin], enabled);
}
return RPC_Response(data["pin"], (bool)data["enabled"]);
}
RPC_Callback callbacks[] = {
{ "setValue", processDelayChange },
{ "getValue", processGetDelay },
{ "setGpioStatus", processSetGpioState },
};
void wakeup_TIMER()
{
dht.begin();
float h = dht.readHumidity();
float t = dht.readTemperature();
//if (isnan(h) || isnan(t)) {
// Serial.println("Failed to read from DHT sensor!");
//}else{
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
WiFi.forceSleepWake();
Serial.println(" Start Data Send ... ");
tb.sendTelemetryFloat("humidity", h);
Serial.println(" Send tb.Humidity ");
tb.sendTelemetryFloat("temperature", t);
Serial.println(" Send tb.Temperature ");
connectTB();
// }
tb.loop();
}
void connectTB()
{
if (!tb.connected()) {
subscribed = false;
// Connect to the ThingsBoard
Serial.print("Connecting to: ");
Serial.print(THINGSBOARD_SERVER);
Serial.print(" with token ");
Serial.println(TOKEN);
if (!tb.connect(THINGSBOARD_SERVER, TOKEN)) {
Serial.println("Failed to connect");
return;
}
}
}
void InitWiFi()
{
Serial.println("Connecting to AP ...");
// attempt to connect to WiFi network
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WEP network, SSID: ");
Serial.println(WIFI_AP_NAME);
status = WiFi.begin(WIFI_AP_NAME, WIFI_PASSWORD);
// wait 15 seconds for connection:
delay(50000);
Serial.println(status);
}
// once you are connected :
Serial.print("You're connected to the network");
/*
while (WiFi.status() != WL_CONNECTED) {
delay(5000);
Serial.print(WiFi.status());
Serial.print("i.");
Serial.print(WL_CONNECTED);
}*/
Serial.println("Connected to AP");
}
void reconnect() {
// Loop until we're reconnected
status = WiFi.status();
if ( status != WL_CONNECTED) {
WiFi.begin(WIFI_AP_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("r.");
}
Serial.println("Connected to AP");
}
}
void setup(){
Serial.begin(SERIAL_DEBUG_BAUD);
delay(1000); //Take some time to open up the Serial Monitor
//Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
//Print the wakeup reason for ESP32
wakeup_TIMER();
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
" Seconds");
Serial.println("Going to sleep now");
Serial.flush();
esp_deep_sleep_start();
}
void loop(){
}
#include "DHT.h"
#include "WiFi.h" // WiFi control for ESP32
#include <ThingsBoard.h> // ThingsBoard SDK
#define SERIAL_DEBUG_BAUD 115200
#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
#define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 10 /* Time ESP32 will go to sleep (in seconds) */
#define DHTTYPE DHT11
#define DHTPIN 4
// WiFi access point
#define WIFI_AP_NAME ""
// WiFi password
#define WIFI_PASSWORD ""
#define TOKEN ""
#define THINGSBOARD_SERVER ""
int status = WL_IDLE_STATUS;
uint8_t leds_cycling[] = { 25, 26, 32 };
uint8_t leds_control[] = { 19, 22, 21 };
RTC_DATA_ATTR int bootCount = 0;
DHT dht(DHTPIN, DHTTYPE);
WiFiClient espClient;
ThingsBoard tb(espClient);
bool subscribed = false;
int led_delay = 1000;
int send_delay = 2000;
int led_passed = 0;
int send_passed = 0;
RPC_Response processDelayChange(const RPC_Data &data)
{
Serial.println("Received the set delay RPC method");
led_delay = data;
Serial.print("Set new delay: ");
Serial.println(led_delay);
return RPC_Response(NULL, led_delay);
}
RPC_Response processGetDelay(const RPC_Data &data)
{
Serial.println("Received the get value method");
return RPC_Response(NULL, led_delay);
}
RPC_Response processSetGpioState(const RPC_Data &data)
{
Serial.println("Received the set GPIO RPC method");
int pin = data["pin"];
bool enabled = data["enabled"];
if (pin < COUNT_OF(leds_control)) {
Serial.print("Setting LED ");
Serial.print(pin);
Serial.print(" to state ");
Serial.println(enabled);
digitalWrite(leds_control[pin], enabled);
}
return RPC_Response(data["pin"], (bool)data["enabled"]);
}
RPC_Callback callbacks[] = {
{ "setValue", processDelayChange },
{ "getValue", processGetDelay },
{ "setGpioStatus", processSetGpioState },
};
void wakeup_TIMER()
{
dht.begin();
float h = dht.readHumidity();
float t = dht.readTemperature();
//if (isnan(h) || isnan(t)) {
// Serial.println("Failed to read from DHT sensor!");
//}else{
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
WiFi.forceSleepWake();
Serial.println(" Start Data Send ... ");
tb.sendTelemetryFloat("humidity", h);
Serial.println(" Send tb.Humidity ");
tb.sendTelemetryFloat("temperature", t);
Serial.println(" Send tb.Temperature ");
connectTB();
// }
tb.loop();
}
void connectTB()
{
if (!tb.connected()) {
subscribed = false;
// Connect to the ThingsBoard
Serial.print("Connecting to: ");
Serial.print(THINGSBOARD_SERVER);
Serial.print(" with token ");
Serial.println(TOKEN);
if (!tb.connect(THINGSBOARD_SERVER, TOKEN)) {
Serial.println("Failed to connect");
return;
}
}
}
void InitWiFi()
{
Serial.println("Connecting to AP ...");
// attempt to connect to WiFi network
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WEP network, SSID: ");
Serial.println(WIFI_AP_NAME);
status = WiFi.begin(WIFI_AP_NAME, WIFI_PASSWORD);
// wait 15 seconds for connection:
delay(50000);
Serial.println(status);
}
// once you are connected :
Serial.print("You're connected to the network");
/*
while (WiFi.status() != WL_CONNECTED) {
delay(5000);
Serial.print(WiFi.status());
Serial.print("i.");
Serial.print(WL_CONNECTED);
}*/
Serial.println("Connected to AP");
}
void reconnect() {
// Loop until we're reconnected
status = WiFi.status();
if ( status != WL_CONNECTED) {
WiFi.begin(WIFI_AP_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("r.");
}
Serial.println("Connected to AP");
}
}
void setup(){
Serial.begin(SERIAL_DEBUG_BAUD);
delay(1000); //Take some time to open up the Serial Monitor
//Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
//Print the wakeup reason for ESP32
wakeup_TIMER();
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
" Seconds");
Serial.println("Going to sleep now");
Serial.flush();
esp_deep_sleep_start();
}
void loop(){
}