Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with connection to the google script server #15

Closed
bastian187 opened this issue Mar 23, 2024 · 2 comments
Closed

Problem with connection to the google script server #15

bastian187 opened this issue Mar 23, 2024 · 2 comments

Comments

@bastian187
Copy link

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

`#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiManager.h>
#include <DNSServer.h>
#include <ArduinoJson.h>
#include "HTTPSRedirect.h"
#include <NTPClient.h>
#include <WiFiUdp.h>

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

Copy link

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

@bastian187
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant