#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
//for BME280
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define ON_Board_LED 2 // On board LED, indicator when connecting to a wifi router
Adafruit_BME280 bme; // Sensor temperature, humid, pressure
const char* ssid = "Gtx 16"; // Your wifi name
const char* password = "dsre4415"; // Your wifi password
//----------------------------------------Host & httpsPort
const char* host = "script.google.com";
const int httpsPort = 443;
//----------------------------------------
WiFiClientSecure client; // Create a WiFiClientSecure object
// Google spreadsheet script ID
String GAS_ID = "AKfycbw9BgGoruju-N_5CHi9pOQMXFm7zhxhKtFRSMC_ZjgMornaK3KRq2xyki9Qjzq4bUfU";
void setup() {
Serial.begin(115200);
// for sensor BME280
bool status;
status = bme.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
WiFi.begin(ssid, password); // Connect to your WiFi router
Serial.println("");
pinMode(ON_Board_LED,OUTPUT); // On board LED port as 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 LED flashing when connecting to the wifi router
digitalWrite(ON_Board_LED, LOW);
delay(200);
digitalWrite(ON_Board_LED, HIGH);
delay(200);
//----------------------------------------
}
//----------------------------------------
digitalWrite(ON_Board_LED, HIGH); // Turn off the 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();
//----------------------------------------
client.setInsecure();
}
void loop() {
float h = bme.readHumidity();
float t = bme.readTemperature(); // default: unit Celsius
float p = bme.readPressure();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from BME sensor !");
delay(500);
return;
}
String Temp = "Temperature : " + String(t) + " °C";
String Humi = "Humidity : " + String(h) + " %";
String Pres = "Pressure : " + String(p) + " hPa";
Serial.println(Temp);
Serial.println(Humi);
Serial.println(Pres);
sendData(t, h, p); // Call the sendData subroutine
delay(5000);
}
// Subroutine for sending data to Google Sheets
void sendData(float tem, float hum, float pres) {
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_temperature = String(tem);
String string_humidity = String(hum);
String string_pres = String(pres);
String url = "/macros/s/" + GAS_ID + "/exec?temperature=" + string_temperature + "&humidity=" + string_humidity + "&pressure="+ string_pres ;
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();
//----------------------------------------
}