Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * HTTPS with follow-redirect example
- * Created by Sujay S. Phadke, 2016
- *
- * Based on the WifiClientSecure example by
- * Ivan Grokhotkov
- *
- *
- * This example is in public domain.
- *
- * Modified by PDAControl
- * http://pdacontrolenglish.blogspot.com.co
- * http://pdacontrol.blogspot.com.co
- * https://youtu.be/5f7wCeD4gB4 -- TUTORIAL
- */
- #include <ESP8266WiFi.h>
- #include "HTTPSRedirect.h"
- // Replace with your network credentials
- const char* ssid = "hooshmandsazan3";
- const char* password = "!q@w#e4r5t6y";
- const char* host = "script.google.com";
- const char* googleRedirHost = "script.googleusercontent.com";
- const char *GScriptId = "AKfycbyOZ2agkz7Udy7L43aitJP0DgM1dlaAsAnJ7tudm9_wod7ZdNM";
- const int httpsPort = 443;
- HTTPSRedirect client(httpsPort);
- // http://askubuntu.com/questions/156620/how-to-verify-the-ssl-fingerprint-by-command-line-wget-curl/
- // echo | openssl s_client -connect script.google.com:443 |& openssl x509 -fingerprint -noout
- // www.grc.com doesn't seem to get the right fingerprint
- // SHA1 fingerprint of the certificate
- //const char* fingerprint = "94 2F 19 F7 A8 B4 5B 09 90 34 36 B2 2A C4 7F 17 06 AC 6A 2E";
- const char* fingerprint = "F0 5C 74 77 3F 6B 25 D7 3B 66 4D 43 2F 7E BC 5B E9 28 86 AD";
- const char* fingerprint2 = "94 64 D8 75 DE 5D 3A E6 3B A7 B6 15 52 72 CC 51 7A BA 2B BE";
- // Write to Google Spreadsheet
- String url = String("/macros/s/") + GScriptId + "/exec?value=Hello";
- // Fetch Google Calendar events for 1 week ahead
- String url2 = String("/macros/s/") + GScriptId + "/exec?cal";
- // Read from Google Spreadsheet
- String url3 = String("/macros/s/") + GScriptId + "/exec?read";
- void setup() {
- Serial.begin(115200);
- Serial.println();
- Serial.print("Connecting to wifi: ");
- Serial.println(ssid);
- // flush() is needed to print the above (connecting...) message reliably,
- // in case the wireless connection doesn't go through
- Serial.flush();
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- // Use HTTPSRedirect class to create TLS connection
- Serial.print("Connecting to ");
- Serial.println(host);
- bool flag = false;
- for (int i=0; i<5; i++){
- int retval = client.connect(host, httpsPort);
- if (retval == 1) {
- flag = true;
- break;
- }
- else
- Serial.println("Connection failed. Retrying...");
- }
- Serial.flush();
- if (!flag){
- Serial.print("Could not connect to server: ");
- Serial.println(host);
- Serial.println("Exiting...");
- return;
- }
- Serial.flush();
- if (client.verify(fingerprint, host)) {
- Serial.println("Certificate match.");
- } else {
- Serial.println("Certificate mis-match");
- }
- // Note: setup() must finish within approx. 1s, or the the watchdog timer
- // will reset the chip. Hence don't put too many requests in setup()
- // ref: https://github.com/esp8266/Arduino/issues/34
- //Serial.print("Requesting URL: ");
- //Serial.println(url);
- client.printRedir(url, host, googleRedirHost);
- Serial.println("==============================================================================");
- // client.printRedir(url2, host, googleRedirHost);
- // Serial.println("==============================================================================");
- }
- void loop() {
- HTTPSRedirect client(httpsPort);
- if (!client.connected())
- client.connect(host, httpsPort);
- client.printRedir(url3, host, googleRedirHost);
- //Serial.println(client.myLine);
- // In my testing on a ESP-01, a delay of less than 1500 resulted
- // in a crash and reboot after about 50 loop runs.
- delay(1500);
- }
Add Comment
Please, Sign In to add comment