Advertisement
mattrix

Untitled

Jun 18th, 2018
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #include "ESP8266WiFi.h"
  2. #include "HTTPSRedirect.h"
  3.  
  4. const char* ssid = "SSID";
  5. const char* password = "PASSWORD";
  6. const char* host = "script.google.com";
  7. const char *url = "/macros/s/ID/exec";
  8. const int httpsPort = 443;
  9.  
  10. void setup() {
  11.     Serial.begin(9600);
  12.  
  13.     WiFi.mode(WIFI_STA);
  14.     WiFi.begin(ssid, password);
  15.  
  16.     while (WiFi.status() != WL_CONNECTED) {
  17.         delay(500);
  18.         Serial.print(".");
  19.     }
  20.  
  21.     Serial.println("");
  22.     Serial.println("WiFi connected");  
  23.     Serial.println("IP address: ");
  24.     Serial.println(WiFi.localIP());
  25. }
  26.  
  27. void loop() {
  28.     HTTPSRedirect* client = new HTTPSRedirect(httpsPort);
  29.     client->setPrintResponseBody(false);
  30.     client->setContentTypeHeader("application/json");
  31.  
  32.     bool flag = false;
  33.     for (int i=0; i<5; i++){
  34.         int retval = client->connect(host, httpsPort);
  35.         if (retval == 1) {
  36.             flag = true;
  37.             break;
  38.         }
  39.         else
  40.             Serial.println("Connection failed. Retrying...");
  41.     }
  42.  
  43.     if (!flag){
  44.         Serial.print("Could not connect to server: ");
  45.         Serial.println(host);
  46.         Serial.println("Exiting...");
  47.         return;
  48.     }
  49.  
  50.     String payload = "{}";
  51.     client->POST(url, host, payload, false);
  52.     String response = client->getResponseBody();
  53.     Serial.print(response);
  54.  
  55.     delete client;
  56.     client = nullptr;
  57.  
  58.     delay(5000);
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement