doctormord / ESP8266_EPD_Weather_Google_Calendar

Linking an ESP8266 with an EPD to dispay weather data and appointments from Google calendar
50 stars 10 forks source link

Could not connect to google server #3

Closed bastian187 closed 7 months ago

bastian187 commented 7 months ago

Hi, I want to use your Program only to get the appointments from my google calendar, so I simplified it to that code:

`#include

include

include

include

include

include "HTTPSRedirect.h"

include

include

// Params needed to fetch events from Google Calendar char const const dstHost = "script.google.com"; char const const dstPath = "/macros/s/AKfycb[...]2S8fGWc3b-Q/exec"; // script path including key int const dstPort = 443; int32_t const timeout = 2000; int const GoogleServerMaxRetry = 1; //maximum tries to reach google server.

define DEBUG

ifdef DEBUG

define DPRINT(...) Serial.print(__VA_ARGS__)

define DPRINTLN(...) Serial.println(__VA_ARGS__)

else

define DPRINT(...) //now defines a blank line degugging is turned off

define DPRINTLN(...) //now defines a blank line

endif

char const const weekDay0 = "Montag"; char const const weekDay1 = "Dienstag"; char const const weekDay2 = "Mittwoch"; char const const weekDay3 = "Donnerstag"; char const const weekDay4 = "Freitag"; char const const weekDay5 = "Samstag"; char const * const weekDay6 = "Sonntag";

String appointment0, appointment1, appointment2, appointment3; String appdate0, appdate1, appdate2, appdate3; String appDateString0, appDateString1, appDateString2, appDateString3;

WiFiClient client; // wifi client object WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP);

void configModeCallback (WiFiManager *myWiFiManager) { DPRINTLN(F("failed to connect and hit timeout")); DPRINT(F("Entered config mode at: ")); DPRINT(myWiFiManager->getConfigPortalSSID()); DPRINT(F(" IP: ")); DPRINTLN(WiFi.softAPIP()); }

void setup() { unsigned long startTime = millis();

WiFiManager wifiManager;

wifiManager.setTimeout(30); wifiManager.setBreakAfterConfig(true); wifiManager.setAPCallback(configModeCallback);

Serial.begin(115200);

wifiManager.setConnectTimeout(5); wifiManager.autoConnect("SmartEPD");

DPRINTLN(F("connected...yeey :)"));

timeClient.begin(); timeClient.update();

Serial.println(timeClient.getFormattedTime()); syncCalendar(); DPRINTLN(F("Fetching data is done now.")); } void loop() { }

void syncCalendar() { DPRINT(F("Free heap at sync start .. ")); DPRINTLN(ESP.getFreeHeap());

HTTPSRedirect* client = nullptr; // Setup a new HTTPS client

// Use HTTPSRedirect class to create a new TLS connection client = new HTTPSRedirect(dstPort); client->setPrintResponseBody(false); client->setContentTypeHeader("application/json");

DPRINT("Connecting to "); DPRINTLN(dstHost);

// Try to connect for a maximum of 5 times bool flag = false; for (int i = 0; i < GoogleServerMaxRetry; i++) { int retval = client->connect(dstHost, dstPort); if (retval == 1) { flag = true; break; } else Serial.println(F("Connection failed. Retrying...")); }

if (!flag) { Serial.print(F("Could not connect to server: ")); Serial.println(dstHost); Serial.println("Exiting..."); delete client; client = nullptr; return; }

DPRINTLN(F("GET: Fetch Google Calendar Data:"));

ESP.wdtDisable();

// fetch spreadsheet data client->GET(dstPath, dstHost); String googleCalData = client->getResponseBody();

DPRINT(F("data fetched from google: ")); DPRINTLN(googleCalData);

// build output strings from received data

// appointment titles appointment0 = getValue(googleCalData, ';', 1); appointment1 = getValue(googleCalData, ';', 4); appointment2 = getValue(googleCalData, ';', 7); appointment3 = getValue(googleCalData, ';', 10);

//appointment dates appdate0 = getValue(googleCalData, ';', 0); appdate1 = getValue(googleCalData, ';', 3); appdate2 = getValue(googleCalData, ';', 6); appdate3 = getValue(googleCalData, ';', 9);

DPRINT(F("Next appointment: ")); DPRINTLN(appointment0 + " at " + appdate0); DPRINT(F("Next appointment: ")); DPRINTLN(appointment1 + " at " + appdate1); DPRINT(F("Next appointment: ")); DPRINTLN(appointment2 + " at " + appdate2); DPRINT(F("Next appointment: ")); DPRINTLN(appointment3 + " at " + appdate3);

// Appointment date strings // "Thu Jul 19 2018 02:00:00 GMT+0200 (MESZ)" - substring starts at 1, not 0

String scratchpad = appdate0.substring(0, 3); appDateString0 = getGermanWeekdayName(scratchpad); if (!appDateString0.startsWith("null")) appDateString0 += (" " + getValue(appdate0, ' ', 2) + ". " + getValue(appdate0, ' ', 1) + " um " + appdate0.substring(16, 21)); else appDateString0 = "";

scratchpad = appdate1.substring(0, 3); appDateString1 = getGermanWeekdayName(scratchpad); if (!appDateString1.startsWith("null")) appDateString1 += (" " + getValue(appdate1, ' ', 2) + ". " + getValue(appdate1, ' ', 1) + " um " + appdate1.substring(16, 21)); else appDateString1 = "";

scratchpad = appdate2.substring(0, 3); appDateString2 = getGermanWeekdayName(scratchpad); if (!appDateString2.startsWith("null")) appDateString2 += (" " + getValue(appdate2, ' ', 2) + ". " + getValue(appdate2, ' ', 1) + " um " + appdate2.substring(16, 21)); else appDateString2 = "";

scratchpad = appdate3.substring(0, 3); appDateString3 = getGermanWeekdayName(scratchpad); if (!appDateString3.startsWith("null")) appDateString3 += (" " + getValue(appdate3, ' ', 2) + ". " + getValue(appdate3, ' ', 1) + " um " + appdate3.substring(16, 21)); else appDateString3 = "";

DPRINT(F("appDateString0: ")); DPRINTLN(appDateString0); DPRINT(F("appDateString1: ")); DPRINTLN(appDateString1); DPRINT(F("appDateString2: ")); DPRINTLN(appDateString2); DPRINT(F("appDateString3: ")); DPRINTLN(appDateString3);

// delete HTTPSRedirect object delete client; client = nullptr; DPRINT(F("Free heap at sync end .. ")); DPRINTLN(ESP.getFreeHeap());

}

String getGermanWeekdayName(String &weekd) {

String weekdg;

if (weekd == "Mon") weekdg = weekDay0; else if (weekd == "Tue") weekdg = weekDay1; else if (weekd == "Wed") weekdg = weekDay2; else if (weekd == "Thu") weekdg = weekDay3; else if (weekd == "Fri") weekdg = weekDay4; else if (weekd == "Sat") weekdg = weekDay5; else if (weekd == "Sun") weekdg = weekDay6; else weekdg = "null";

return weekdg; }

String getValue(String data, char separator, int index) { int found = 0; int strIndex[] = {0, -1}; int maxIndex = data.length() - 1;

for (int i = 0; i <= maxIndex && found <= index; i++) { if (data.charAt(i) == separator || i == maxIndex) { found++; strIndex[0] = strIndex[1] + 1; strIndex[1] = (i == maxIndex) ? i + 1 : i; } }

return found > index ? data.substring(strIndex[0], strIndex[1]) : ""; }`

In the Serial monitor I get this response

wm:AutoConnect: SUCCESS wm:STA IP Address: 192.168.178.122 connected...yeey :) 17:12:31 Free heap at sync start .. 47944 Connecting to script.google.com Connection failed. Retrying... Could not connect to server: script.google.com Exiting... Fetching data is done now.

When I copy the Link from google script into my browser, I get the data. The ntp time in the monitor is also working so I think the problem is maybe in the syncCalendar function? Thanks for your help

doctormord commented 7 months ago

Hi mate,

could be the server is not responding on port 443?

It had been a long time since i used this setup, so I can't provide a direct solution to this.

Best regards

Christian

Am 22. März 2024 20:29:37 schrieb bastian187 @.***>:

Hi, I want to use your Program only to get the appointments from my google calendar, so I simplified it to that code:`#include

include

include

include

include

include "HTTPSRedirect.h"

include

include // Params needed to fetch events from Google Calendar

char const const dstHost = "script.google.com"; char const const dstPath = "/macros/s/AKfycb[...]2S8fGWc3b-Q/exec"; // script path including key int const dstPort = 443; int32_t const timeout = 2000; int const GoogleServerMaxRetry = 1; //maximum tries to reach google server.#define DEBUG

ifdef DEBUG

define DPRINT(...) Serial.print(VA_ARGS)

define DPRINTLN(...) Serial.println(VA_ARGS)

else

define DPRINT(...) //now defines a blank line degugging is turned off

define DPRINTLN(...) //now defines a blank line

endifchar const * const weekDay0 = "Montag";

char const const weekDay1 = "Dienstag"; char const const weekDay2 = "Mittwoch"; char const const weekDay3 = "Donnerstag"; char const const weekDay4 = "Freitag"; char const const weekDay5 = "Samstag"; char const const weekDay6 = "Sonntag";String appointment0, appointment1, appointment2, appointment3; String appdate0, appdate1, appdate2, appdate3; String appDateString0, appDateString1, appDateString2, appDateString3;WiFiClient client; // wifi client object WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP);void configModeCallback (WiFiManager myWiFiManager) { DPRINTLN(F("failed to connect and hit timeout")); DPRINT(F("Entered config mode at: ")); DPRINT(myWiFiManager->getConfigPortalSSID()); DPRINT(F(" IP: ")); DPRINTLN(WiFi.softAPIP()); }void setup() { unsigned long startTime = millis();WiFiManager wifiManager; wifiManager.setTimeout(30); wifiManager.setBreakAfterConfig(true); wifiManager.setAPCallback(configModeCallback);Serial.begin(115200); wifiManager.setConnectTimeout(5); wifiManager.autoConnect("SmartEPD");DPRINTLN(F("connected...yeey :)")); timeClient.begin(); timeClient.update();Serial.println(timeClient.getFormattedTime()); syncCalendar(); DPRINTLN(F("Fetching data is done now.")); } void loop() { }void syncCalendar() { DPRINT(F("Free heap at sync start .. ")); DPRINTLN(ESP.getFreeHeap());HTTPSRedirect client = nullptr; // Setup a new HTTPS client // Use HTTPSRedirect class to create a new TLS connection client = new HTTPSRedirect(dstPort); client->setPrintResponseBody(false); client->setContentTypeHeader("application/json");DPRINT("Connecting to "); DPRINTLN(dstHost);// Try to connect for a maximum of 5 times bool flag = false; for (int i = 0; i < GoogleServerMaxRetry; i++) { int retval = client->connect(dstHost, dstPort); if (retval == 1) { flag = true; break; } else Serial.println(F("Connection failed. Retrying...")); }if (!flag) { Serial.print(F("Could not connect to server: ")); Serial.println(dstHost); Serial.println("Exiting..."); delete client; client = nullptr; return; }DPRINTLN(F("GET: Fetch Google Calendar Data:")); // Sometimes fetching data hangs here, causing high battery drain which renders the system useless // Trigger HW watchdog on to long delay

ESP.wdtDisable(); //ESP.wdtEnable(10); //while (1){};// fetch spreadsheet data client->GET(dstPath, dstHost); String googleCalData = client->getResponseBody();DPRINT(F("data fetched from google: ")); DPRINTLN(googleCalData); // build output strings from received data // appointment titles appointment0 = getValue(googleCalData, ';', 1); appointment1 = getValue(googleCalData, ';', 4); appointment2 = getValue(googleCalData, ';', 7); appointment3 = getValue(googleCalData, ';', 10);//appointment dates appdate0 = getValue(googleCalData, ';', 0); appdate1 = getValue(googleCalData, ';', 3); appdate2 = getValue(googleCalData, ';', 6); appdate3 = getValue(googleCalData, ';', 9);DPRINT(F("Next appointment: ")); DPRINTLN(appointment0 + " at " + appdate0); DPRINT(F("Next appointment: ")); DPRINTLN(appointment1 + " at " + appdate1); DPRINT(F("Next appointment: ")); DPRINTLN(appointment2 + " at " + appdate2); DPRINT(F("Next appointment: ")); DPRINTLN(appointment3 + " at " + appdate3);// Appointment date strings // "Thu Jul 19 2018 02:00:00 GMT+0200 (MESZ)" - substring starts at 1, not 0String scratchpad = appdate0.substring(0, 3); appDateString0 = getGermanWeekdayName(scratchpad); if (!appDateString0.startsWith("null")) appDateString0 += (" " + getValue(appdate0, ' ', 2) + ". " + getValue(appdate0, ' ', 1) + " um " + appdate0.substring(16, 21)); else appDateString0 = "";scratchpad = appdate1.substring(0, 3); appDateString1 = getGermanWeekdayName(scratchpad); if (!appDateString1.startsWith("null")) appDateString1 += (" " + getValue(appdate1, ' ', 2) + ". " + getValue(appdate1, ' ', 1) + " um " + appdate1.substring(16, 21)); else appDateString1 = "";scratchpad = appdate2.substring(0, 3); appDateString2 = getGermanWeekdayName(scratchpad); if (!appDateString2.startsWith("null")) appDateString2 += (" " + getValue(appdate2, ' ', 2) + ". " + getValue(appdate2, ' ', 1) + " um " + appdate2.substring(16, 21)); else appDateString2 = "";scratchpad = appdate3.substring(0, 3); appDateString3 = getGermanWeekdayName(scratchpad); if (!appDateString3.startsWith("null")) appDateString3 += (" " + getValue(appdate3, ' ', 2) + ". " + getValue(appdate3, ' ', 1) + " um " + appdate3.substring(16, 21)); else appDateString3 = "";DPRINT(F("appDateString0: ")); DPRINTLN(appDateString0); DPRINT(F("appDateString1: ")); DPRINTLN(appDateString1); DPRINT(F("appDateString2: ")); DPRINTLN(appDateString2); DPRINT(F("appDateString3: ")); DPRINTLN(appDateString3);// delete HTTPSRedirect object delete client; client = nullptr; DPRINT(F("Free heap at sync end .. ")); DPRINTLN(ESP.getFreeHeap());} String getGermanWeekdayName(String &weekd) { String weekdg; if (weekd == "Mon") weekdg = weekDay0; else if (weekd == "Tue") weekdg = weekDay1; else if (weekd == "Wed") weekdg = weekDay2; else if (weekd == "Thu") weekdg = weekDay3; else if (weekd == "Fri") weekdg = weekDay4; else if (weekd == "Sat") weekdg = weekDay5; else if (weekd == "Sun") weekdg = weekDay6; else weekdg = "null";return weekdg; }String getValue(String data, char separator, int index) { int found = 0; int strIndex[] = {0, -1}; int maxIndex = data.length() - 1;for (int i = 0; i <= maxIndex && found <= index; i++) { if (data.charAt(i) == separator || i == maxIndex) { found++; strIndex[0] = strIndex[1] + 1; strIndex[1] = (i == maxIndex) ? i + 1 : i; } }return found > index ? data.substring(strIndex[0], strIndex[1]) : ""; }`In the Serial monitor I get this response wm:AutoConnect: SUCCESS wm:STA IP Address: 192.168.178.122 connected...yeey :) 17:12:31 Free heap at sync start .. 47944 Connecting to script.google.com Connection failed. Retrying... Could not connect to server: script.google.com Exiting... Fetching data is done now.When I copy the Link from google script into my browser, I get the data. The ntp time in the monitor is also working so I think the problem is maybe in the syncCalendar function? Thanks for your help— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>

bastian187 commented 7 months ago

Hi Christian, Thank you for your response, so the problem is the HTTPSRedirect library, am I guessing this right? So I will open an Issue there.

bastian187 commented 7 months ago

SOLVED: I finally solved the problem by hardcoding the wifi credentials into the code and using the direct wifi.begin function. And in syncCalendar I added client->setInsecure();. Now it is working without problems.