#include [COLOR="#000000"]<[/COLOR]ESP8266WiFi.h>
#include [COLOR="#000000"]<[/COLOR]WiFiClientSecure.h>
#include [COLOR="#000000"]<[/COLOR]Wire.h>
String readString;
unsigned long Timer = 0;
unsigned long Interval = 5000;
#define Addr 0x40
const char* ssid = "DcubeAirtel";
const char* password = "D@Airtel190";
const char* host = "script.google.com";
const int httpsPort = 443;
float Ctemp,Ftemp,humid;
// Use WiFiClientSecure class to create TLS connection
WiFiClientSecure client;
// SHA1 fingerprint of the certificate, don't care with your GAS service
const char* fingerprint = "fd 85 80 08 94 28 7b 0e 2f 13 06 09 d7 fd f0 23 40 7c e4 34";
String SCRIPT_ID = "AKfycbyj1wW2B_a0iazJVj43-FHZCScxZX4MMpSHdkHIgMYma-11weQE"; // Replace by your Gscript service id
void setup()
{
// Initialise I2C communication as MASTER
Wire.begin(2,14);
Serial.begin(115200);
Serial.println();
Serial.print("connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop()
{
delay(300);
Timer = millis();
while(millis()- Timer<=Interval)
{
temptask();
delay(500);
}
sendData();
// while (Serial.available()) {
// char c = Serial.read(); //gets one byte from serial buffer
// readString += c; //makes the string readString
// delay(2); //slow looping to allow buffer to fill with next character
// }
//
// if (readString.length() >0) {
// Serial.println(readString); //so you can see the captured string
// int n = readString.toInt(); //convert readString into a number
//
// // auto select appropriate value, copied from someone elses code.
// sendData(analogRead(A0),n);
// readString=""; //empty for next input
// }
}
// Function for Send data into Google Spreadsheet
void sendData()
{
Serial.print("connecting to ");
Serial.println(host);
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
if (client.verify(fingerprint, host)) {
Serial.println("certificate matches");
}
else {
Serial.println("certificate doesn't match");
}
String tempC = String(Ctemp, 1);
String tempF = String(Ftemp, 1);
String humiD = String(humid, 1);
String url = "/macros/s/" + SCRIPT_ID + "/exec?tempC=" + tempC + "&tempF=" + tempF + "&humiD=" + humiD;
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");
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.println("reply was:");
Serial.println("==========");
Serial.println(line);
Serial.println("==========");
Serial.println("closing connection");
}
void temptask(){
unsigned int data[2];
// Start I2C transmission
Wire.beginTransmission(Addr);
// Send humidity measurement command, NO HOLD master
Wire.write(0xF5);
// Stop I2C transmission
Wire.endTransmission();
delay(500);
// Request 2 bytes of data
Wire.requestFrom(Addr, 2);
// Read 2 bytes of data
// humidity msb, humidity lsb
if(Wire.available() == 2)
{
data[0] = Wire.read();
data[1] = Wire.read();
// Convert the data
float humidity = (((data[0] * 256.0 + data[1]) * 125.0) / 65536.0) - 6;
// Output data to Serial Monitor
Serial.print("Relative Humidity :");
Serial.print(humidity);
Serial.println(" %RH");
humidity = humid;
}
// Start I2C transmission
Wire.beginTransmission(Addr);
// Send temperature measurement command, NO HOLD master
Wire.write(0xF3);
// Stop I2C transmission
Wire.endTransmission();
delay(500);
// Request 2 bytes of data
Wire.requestFrom(Addr, 2);
// Read 2 bytes of data
// temp msb, temp lsb
if(Wire.available() == 2)
{
data[0] = Wire.read();
data[1] = Wire.read();
// Convert the data
float cTemp = (((data[0] * 256.0 + data[1]) * 175.72) / 65536.0) - 46.85;
float fTemp = (cTemp * 1.8) + 32;
// Output data to Serial Monitor
Serial.print("Temperature in Celsius :");
Serial.print(cTemp);
Serial.println(" C");
Serial.print("Temperature in Fahrenheit :");
Serial.print(fTemp);
Serial.println(" F");
Ctemp = cTemp;
Ftemp = fTemp;
}
delay(300);
}