Home > Blockchain >  Not able to use GET method in WiFi initialization of program
Not able to use GET method in WiFi initialization of program

Time:12-22

I have a function to send logs to Telegram. This function works fine, when I call it from void setup() or void loop() function or some function defined by me. I would also like to get a message to Telegram, when my esp32 connects to Wifi.

This is how my code looks.


void telegram_report(String error_message) {
    String url = "";
    url  = "https://api.telegram.org/bot";
    url  = TELEGRAM_TOKEN;
    url  = "/sendMessage?chat_id=";
    url  = TELEGRAM_CHAT_ID;
    url  = "&parse_mode=Markdown&text=";
    url  = "[ ESP32(1) ] ";
    url  = error_message;

    HTTPClient http;
    http.begin(url);
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");

    int countTries = 0;
    int httpCode = -1;

    while(httpCode == -1){
        if (countTries > 3) {
            Serial.println("[ ERR ] Could not send Error Report to Telegram. Max number of tries reached");
            http.end();
            Serial.println(error_message);
            return;
        }
        httpCode = http.GET();
        countTries  ;
    }
}



void connectToWiFi() {
    Serial.println(" ");
    Serial.print("[ INF ] Connencting to WiFi");
    Serial.print(" ");
    WiFi.mode(WIFI_STA);
    WiFi.begin(WIFI_NETWORK, WIFI_PASSWORD);

    unsigned long startAttemptTime = millis();

    while(WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < 20000) {
        Serial.print(".");
        delay(500);
    }

    if (WiFi.status() != WL_CONNECTED) {
        Serial.println("[ ERR ] Failed connect to WiFi!");
        delay(5000);

    }

    else {
        String connected = "";
        connected  = "[ SUCC ] Connected to WiFi:";
        connected  = String(WIFI_NETWORK);
        connected  = " - with IP address ";
        connected  = String(WiFi.localIP());
        telegram_report(connected); // This is where I'm struggling
    }
}

When I call the function telegram_report() somewhere from my code Im able to get a response to Telegram. But I'm getting nothing when I try to call the function from Wifi connection function. My Wifi connection is already established when I call the telegram_report(connected);.

Could it be that I'm passing String to function and not reference? Also I'm getting weird output from String(WiFi.localIP()) when I try to print it. Is it because I'm converting it to the String?

CodePudding user response:

The secure connection (https) requires to check validity of certificates. This includes checking of the dates. The ESP32 SDK retrieves internet time right after it connects to Internet, but it takes some milliseconds.

You can check if the time has been retrieved by comparing it to some timestamp larger than 1970-01-01 (which is the default 'unset' time).

time_t now = time(nullptr);
uint8_t timeoutCounter = 0;
while (now < SECS_YR_2000 && timeoutCounter < 10) {
   timeoutCounter   
   delay(100);
   now = time(nullptr);
}

#define SECS_YR_2000 ((time_t)(946684800UL))

  • Related