//Include required libraries
#include "WiFi.h"
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
const char* ssid = "Wokwi-GUEST"; // change SSID
const char* password = ""; // change password
//----------------------------------------Host & httpsPort
const char* host = "script.google.com";
const int httpsPort = 443;
//----------------------------------------
float temperature = 20;
String GAS_ID = "AKfycbx6nr3T2WeX8qeLT4brnhNg1lweoHECsHYcM87TkWs3mrJo-S6RtCqQWw_-CBhHbxsQ";
WiFiClientSecure google; //--> Create a WiFiClientSecure object.
long now = millis();
long lastMeasure = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
WiFi.begin(ssid, password); //--> Connect to your WiFi router
Serial.println("");
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(250);
}
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
google.setInsecure();
}
void loop() {
now = millis();
if (now - lastMeasure > 6000) {
lastMeasure = now;
sendData(temperature,30, 40,1.5,1.5);
temperature++;
}
}
void sendData(float temperature, float humidity, float pressure, float usage, float cylinder) {
Serial.print("connecting to ");
Serial.println(host);
if (!google.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
String url = "/macros/s/" + GAS_ID + "/exec?temp=" + String(temperature) + "&humi=" + String(humidity) + "&pressure=" + String(pressure) + "&cylinder=" + String(cylinder) + "&usage=" + String(usage); // 2 variables
Serial.print("requesting URL: ");
Serial.println(url);
google.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");
//----------------------------------------
//---------------------------------------
while (google.connected()) {
String line = google.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
String line = google.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();
}