electronicsguy / HTTPSRedirect

A library for seamless data logging, communication and control for Internet of Things
GNU Affero General Public License v3.0
57 stars 25 forks source link

Problem with connection to the google script server #15

Closed bastian187 closed 3 months ago

bastian187 commented 3 months ago

Hi, I want to use your library to get the appointments from my google calendar. This is the code I'm using

`#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.

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");

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

Serial.println(timeClient.getFormattedTime()); syncCalendar(); } void loop() { }

void syncCalendar() { HTTPSRedirect* client = nullptr;

client = new HTTPSRedirect(dstPort); client->setPrintResponseBody(false); client->setContentTypeHeader("application/json");

// 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; }

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

In the Serial monitor I get this response:

wm:AutoConnect: SUCCESS wm:STA IP Address: 192.168.178.122 17:12:31 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 working so it's no wifi connection problem.

Thanks for your help, Bastian

github-actions[bot] commented 3 months ago

Welcome to HTTPSRedirect! Please provide enough info to debug the issue.

bastian187 commented 3 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.