Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //String coba;
- #include <ESP8266WiFi.h>
- #include <WiFiClientSecure.h>
- #define ON_Board_LED 2 //--> Defining an On Board LED, used for indicators when the process of connecting to a wifi router
- const char* ssid = ""; //--> Your wifi name or SSID.
- const char* password = ""; //--> Your wifi password.
- //----------------------------------------Host & httpsPort
- const char* host = "script.google.com";
- const int httpsPort = 443;
- //----------------------------------------
- WiFiClientSecure client; //--> Create a WiFiClientSecure object.
- // Ganti ID Spreadsheet
- String GAS_ID = "AKfycbz_PbiQezN1UkAdhRnjyCZVnuDepQnuoQmf5u0H_Bv_xY99SfL7-l5a35I_lkKCEDLA"; //--> spreadsheet script ID
- // end
- void setup() {
- Serial.begin(9600);
- WiFi.begin(ssid, password); //--> Connect to your WiFi router
- Serial.println("");
- pinMode(ON_Board_LED,OUTPUT); //--> On Board LED port Direction output
- digitalWrite(ON_Board_LED, HIGH); //--> Turn off Led On Board
- //----------------------------------------Wait for connection
- Serial.print("Connecting");
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- //----------------------------------------Make the On Board Flashing LED on the process of connecting to the wifi router.
- digitalWrite(ON_Board_LED, LOW);
- delay(250);
- digitalWrite(ON_Board_LED, HIGH);
- delay(250);
- //----------------------------------------
- }
- //----------------------------------------
- digitalWrite(ON_Board_LED, HIGH); //--> Turn off the On Board LED when it is connected to the wifi router.
- Serial.println("");
- Serial.print("Successfully connected to : ");
- Serial.println(ssid);
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- Serial.println();
- //----------------------------------------
- WiFi.setAutoReconnect(true);
- WiFi.persistent(true);
- client.setInsecure();
- }
- void loop() {
- tagId = 'halo';
- sendData(tagId);
- }
- // Subroutine for sending data to Google Sheets
- void sendData(String tagId) {
- Serial.println("==========");
- Serial.print("connecting to ");
- Serial.println(host);
- //----------------------------------------Connect to Google host
- if (!client.connect(host, httpsPort)) {
- Serial.println("connection failed");
- return;
- }
- //----------------------------------------
- //----------------------------------------Processing data and sending data
- String string_cardid = String(tagId);
- String url = "/macros/s/" + GAS_ID + "/exec?cardid=" + string_cardid;
- Serial.print("requesting URL: ");
- Serial.println(url);
- client.print(String("GET ") + url + " HTTP/1.1\r\n" +
- "Host: " + host + "\r\n" +
- "User-Agent: BuildFailureDetectorESP8266\r\n" +
- "Connection: close\r\n\r\n");
- Serial.println("request sent");
- //----------------------------------------
- //----------------------------------------Checking whether the data was sent successfully or not
- while (client.connected()) {
- String line = client.readStringUntil('\n');
- if (line == "\r") {
- Serial.println("headers received");
- break;
- }
- }
- String line = client.readStringUntil('\n');
- if (line.startsWith("{\"state\":\"success\"")) {
- Serial.println("esp8266/Arduino CI successfull!");
- } else {
- Serial.println("esp8266/Arduino CI has failed");
- }
- Serial.print("reply was : ");
- Serial.println(line);
- Serial.println("closing connection");
- Serial.println("==========");
- Serial.println();
- //----------------------------------------
- }
- //==============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement