#include #ifdef ESP8266 #include #include #include #define WIFIMCU "ESP8266" #endif #ifdef ESP32 #include #include #define WIFIMCU "ESP32" // Note: ESP32 has LittleFS built-in #endif // WiFi settings const char* essid = SSID_NAME; const char* psks = SSID_WPAKEY; const int httpsPort = 443; // Google scripting API settings const char* GScriptHost = GSCRIPT_HOST; const char* GScriptIdRead = GSCRIPT_URL; // WiFi IP address setup IPAddress ipaddr = {0, 0, 0, 0}; char ipaddrstr[18] = {'\0'}; WiFiClientSecure httpsClient; const uint16_t httpPort = 443; bool keepAlive = false; String httpResponse = ""; // Forward declarations bool startWiFi(const char * ssid, const char * psks); bool connectHost(const char * url, const char * host, String& response); uint16_t getResponseStatus(String& response); String getRedirectLocation(String& response); String getRedirectHost(String& response); String getResponseBody(); void setup() { uint16_t statusCode = 0; String redirectHost = ""; String redirectURL = ""; Serial.begin(115200); delay(800); // Allow serial time to start Serial.println(F("Starting WiFi ")); // WiFi setup Serial.print("MAC address: "); Serial.println(WiFi.macAddress()); if (startWiFi(essid, psks)) { ipaddr = WiFi.localIP(); Serial.print(F("IP address: ")); sprintf(ipaddrstr, "%d.%d.%d.%d", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); Serial.println(ipaddrstr); Serial.println(F("RequestURL:")); Serial.println(GScriptIdRead); if (connectHost(GScriptIdRead, GScriptHost, httpResponse)) { Serial.println(F("Response:")); Serial.println(httpResponse); Serial.print(F("Request status code: ")); statusCode = getResponseStatus(httpResponse); Serial.println(statusCode); if (statusCode == 302){ Serial.println(F("Re-direct host:")); redirectHost = getRedirectHost(httpResponse); Serial.println(redirectHost); Serial.println(F("Re-direct location:")); redirectURL = getRedirectLocation(httpResponse); Serial.println(redirectURL); if (connectHost(redirectURL.c_str(), redirectHost.c_str(), httpResponse)) { Serial.println(F("Response:")); Serial.println(httpResponse); String responsebody = getResponseBody(); Serial.println(F("Response body:")); Serial.println(responsebody); } } } // connectHost(alturl, althost); } else { Serial.println(F("WiFi not connected!")); } Serial.flush(); } void loop() { // put your main code here, to run repeatedly: } bool startWiFi(const char * ssid, const char * psks) { uint16_t tmo = 60000; uint16_t dly = 500; WiFi.config(0u, 0u, 0u); WiFi.begin(ssid, psks); while ((WiFi.status() != WL_CONNECTED) && (tmo > 0)) { delay(dly); tmo = tmo - dly; Serial.print("."); }; Serial.println(); if (WiFi.status() == WL_CONNECTED) return true; return false; } bool connectHost(const char * url, const char * host, String& response) { // String rqpath = String("/macros/s/") + url + "/exec"; String request = ("GET ") + String(url) + " HTTP/1.1\r\n" + "Host: " + String(host) + "\r\n" + "User-Agent: " + WIFIMCU + "\r\n" + "Connection: " + (keepAlive ? "keep-alive" : "close") + "\r\n" + "\r\n"; // String response = ""; char c; String line = ""; Serial.println(F("GET request:")); Serial.println(request); Serial.print(F("Connecting to ")); Serial.println(host); // Insecure mode httpsClient.setInsecure(); // Try to connect for a maximum of 3 times for (int i = 0; i < 3; i++) { // Serial.print(F("Attempt: ")); // Serial.println(i); int retval = httpsClient.connect(host, httpsPort); if (retval == 1) { Serial.print(F("Connected to ")); Serial.println(host); Serial.print(F("Sending GET request to: ")); Serial.print("https://"); Serial.print(host); Serial.print(":"); Serial.println(url); // Perform the actual request httpsClient.print(request); Serial.println(F("Sent GET request.")); Serial.println(F("Reading response...")); response = ""; while (httpsClient.connected()) { // String response = httpsClient.readStringUntil('\n'); // c = httpsClient.read(); line = httpsClient.readStringUntil('\n'); response += (line + '\n'); if (line == "\r") { Serial.println(F("Headers received.")); // break; return true; } } } Serial.println(F("Connection failed. Retrying...")); } Serial.print(F("Could not connect to server: ")); Serial.println(host); return false; } uint16_t getResponseStatus(String& response){ uint16_t statusCode = 0; uint16_t pos1 = 0; uint16_t pos2 = 0; if (response.length() > 0) { pos1 = response.indexOf(0x20) + 1; pos2 = response.indexOf(0x20, pos1); statusCode = response.substring(pos1, pos2).toInt(); } return statusCode; } String getRedirectLocation(String& response){ uint16_t pos1 = 0; uint16_t pos2 = 0; String location = ""; if (response.length() > 0) { pos1 = response.indexOf("Location: ") + 10; pos1 = response.indexOf("//", pos1) + 2; pos1 = response.indexOf('/', pos1); pos2 = response.indexOf('\n', pos1+1); location = response.substring(pos1, pos2); } return location; } String getRedirectHost(String& response){ uint16_t pos1 = 0; uint16_t pos2 = 0; String location = ""; if (response.length() > 0) { pos1 = response.indexOf("Location: ") + 10; pos1 = response.indexOf("//", pos1) + 2; pos2 = response.indexOf('/', pos1); location = response.substring(pos1, pos2); } return location; } String getResponseBody(){ String responseBody = ""; char c; while (httpsClient.available()) { c = httpsClient.read(); responseBody += c; } return responseBody; }