shurillu / CTBot

A simple (and easy to use) Arduino Telegram BOT Library for ESP8266/ESP32
MIT License
147 stars 34 forks source link

DeepSleep esp8266 #65

Closed planer-pro closed 3 years ago

planer-pro commented 3 years ago

Hello, how to periodic public message, and after do deep sleep for some time (4000ms)? d0-rst pin connected. Always say that "Starting TelegramBot...FAIL"

code: `#include

include "CTBot.h"

define pirPin D1//pir sens pin

define durationSleep 4 // secondes

CTBot myBot;

String ssid = "wifi"; String pass = "12345678"; String token = "my token here";//token right, checked

uint32_t tId;

void setup() { Serial.begin(115200);

pinMode(pirPin, INPUT);

Serial.print("\nStarting TelegramBot...");

if (ESP.getResetReason() != "Deep-Sleep Wake")
{
    myBot.wifiConnect(ssid, pass);
    myBot.setTelegramToken(token);
}
if (myBot.testConnection())
    Serial.println("PASS");
else
{
    Serial.println("FAIL");
    Serial.println("Go to deep sleep!");
    ESP.deepSleep(0);//sleep forever due save battery
}

Serial.print("Sensor calibrate...");
delay(3000);
Serial.println("COMPLETE");

TBMessage msg;

tId = "Here my ID";//ID right, checked

if (digitalRead(pirPin))
    myBot.sendMessage(tId, "Motion detected!");

ESP.deepSleep(durationSleep * 1000000);

}

void loop() { }`

Who know?? Anybody have example?

planer-pro commented 3 years ago

Because this my code work fine but have not deepSleep and eating more battery power; `#include

include "CTBot.h"

define pirPin D1

CTBot myBot;

String ssid = "wifi"; String pass = "12345678"; String token = "my ID here";

uint32_t tm; uint32_t tId;

void setup() { Serial.begin(115200);

pinMode(pirPin, INPUT);

Serial.print("\nStarting TelegramBot...");

myBot.wifiConnect(ssid, pass);
myBot.setTelegramToken(token);

if (myBot.testConnection())
    Serial.println("PASS");
else
    Serial.println("FAIL");

Serial.print("Sensor calibrate...");
delay(3000);
Serial.println("COMPLETE");

}

void loop() { if (millis() - tm > 1000) { static uint8_t part; TBMessage msg;

    switch (part)
    {
    case 0:
        if (myBot.getNewMessage(msg))
        {
            tId = msg.sender.id;
            myBot.sendMessage(tId, "ID defined. Tracking started.");
            part++;
        }
        break;
    case 1:
        if (digitalRead(pirPin))
            myBot.sendMessage(tId, "Motion detected!");
        break;
    }
    tm = millis();
}

}`

shurillu commented 3 years ago

Hello, why do you do this check if (ESP.getResetReason() != "Deep-Sleep Wake")?

When you put the ESP8266 into deepsleep, the radio is powered down so when the ESP woke up, it needs to reconnect itself to the WiFi network. So, delete the if mentioned above and leave the code inside his brackets and the sketch will work. In other words, you have to connect to the WiFi network and set the telegram token every time the ESP reboot.

Let me know if my solution solve your issue.

Regards,

Stefano

planer-pro commented 3 years ago

if (ESP.getResetReason() != "Deep-Sleep Wake") needs to do not do sensor caliblation every wakeup. Ok i fix it (move in right place), and feedback here. Thanks.

shurillu commented 3 years ago

Hello, could I close the issue?

planer-pro commented 3 years ago

Hi,yes close please. I change library on UniversalTelegramBot in code. Thanks