Advertisement
BimoSora

Spreadsheet

May 6th, 2024 (edited)
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.61 KB | None | 0 0
  1. //String coba;
  2.  
  3. #include <ESP8266WiFi.h>
  4. #include <WiFiClientSecure.h>
  5.  
  6. #define ON_Board_LED 2  //--> Defining an On Board LED, used for indicators when the process of connecting to a wifi router
  7.  
  8. const char* ssid = ""; //--> Your wifi name or SSID.
  9. const char* password = ""; //--> Your wifi password.
  10.  
  11. //----------------------------------------Host & httpsPort
  12. const char* host = "script.google.com";
  13. const int httpsPort = 443;
  14. //----------------------------------------
  15. WiFiClientSecure client; //--> Create a WiFiClientSecure object.
  16.  
  17. // Ganti ID Spreadsheet
  18. String GAS_ID = "AKfycbz_PbiQezN1UkAdhRnjyCZVnuDepQnuoQmf5u0H_Bv_xY99SfL7-l5a35I_lkKCEDLA"; //--> spreadsheet script ID
  19. // end
  20.  
  21. void setup() {
  22.   Serial.begin(9600);
  23.  
  24.   WiFi.begin(ssid, password); //--> Connect to your WiFi router
  25.   Serial.println("");
  26.    
  27.   pinMode(ON_Board_LED,OUTPUT); //--> On Board LED port Direction output
  28.   digitalWrite(ON_Board_LED, HIGH); //--> Turn off Led On Board
  29.  
  30.   //----------------------------------------Wait for connection
  31.   Serial.print("Connecting");
  32.   while (WiFi.status() != WL_CONNECTED) {
  33.     Serial.print(".");
  34.     //----------------------------------------Make the On Board Flashing LED on the process of connecting to the wifi router.
  35.     digitalWrite(ON_Board_LED, LOW);
  36.     delay(250);
  37.     digitalWrite(ON_Board_LED, HIGH);
  38.     delay(250);
  39.     //----------------------------------------
  40.   }
  41.   //----------------------------------------
  42.   digitalWrite(ON_Board_LED, HIGH); //--> Turn off the On Board LED when it is connected to the wifi router.
  43.   Serial.println("");
  44.   Serial.print("Successfully connected to : ");
  45.   Serial.println(ssid);
  46.   Serial.print("IP address: ");
  47.   Serial.println(WiFi.localIP());
  48.   Serial.println();
  49.   //----------------------------------------
  50.  
  51.   WiFi.setAutoReconnect(true);
  52.   WiFi.persistent(true);
  53.  
  54.   client.setInsecure();
  55. }
  56.  
  57. void loop() {
  58.   tagId = 'halo';
  59.   sendData(tagId);
  60. }
  61.  
  62. // Subroutine for sending data to Google Sheets
  63. void sendData(String tagId) {
  64.   Serial.println("==========");
  65.   Serial.print("connecting to ");
  66.   Serial.println(host);
  67.  
  68.   //----------------------------------------Connect to Google host
  69.   if (!client.connect(host, httpsPort)) {
  70.     Serial.println("connection failed");
  71.     return;
  72.   }
  73.   //----------------------------------------
  74.  
  75.   //----------------------------------------Processing data and sending data
  76.   String string_cardid =  String(tagId);
  77.   String url = "/macros/s/" + GAS_ID + "/exec?cardid=" + string_cardid;
  78.   Serial.print("requesting URL: ");
  79.   Serial.println(url);
  80.  
  81.   client.print(String("GET ") + url + " HTTP/1.1\r\n" +
  82.          "Host: " + host + "\r\n" +
  83.          "User-Agent: BuildFailureDetectorESP8266\r\n" +
  84.          "Connection: close\r\n\r\n");
  85.  
  86.   Serial.println("request sent");
  87.   //----------------------------------------
  88.  
  89.   //----------------------------------------Checking whether the data was sent successfully or not
  90.   while (client.connected()) {
  91.     String line = client.readStringUntil('\n');
  92.     if (line == "\r") {
  93.       Serial.println("headers received");
  94.       break;
  95.     }
  96.   }
  97.   String line = client.readStringUntil('\n');
  98.   if (line.startsWith("{\"state\":\"success\"")) {
  99.     Serial.println("esp8266/Arduino CI successfull!");
  100.   } else {
  101.     Serial.println("esp8266/Arduino CI has failed");
  102.   }
  103.   Serial.print("reply was : ");
  104.   Serial.println(line);
  105.   Serial.println("closing connection");
  106.   Serial.println("==========");
  107.   Serial.println();
  108.   //----------------------------------------
  109. }
  110. //==============================================================================
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement