Commit 67f8fbc1 authored by D.L.Alexander's avatar D.L.Alexander

Updated version of Spreadsheet_data

parent 2d735eaf
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include "DHT.h"
#define DHTTYPE DHT22 //--> Defines the type of DHT sensor used,in this project the sensor used is DHT2.
const int DHTPin = 4; //--> Pin D1 = GPIO4
DHT dht(DHTPin, DHTTYPE);
#define ON_Board_LED 2
int relayInput = 12;
//----------------------------------------SSID and Password of your WiFi router.
const char* ssid = "derick2";
const char* password = "derickgeroutereka1";
//----------------------------------------Google spreadsheet host & httpsPort
const char* host = "script.google.com";
const int httpsPort = 443;
//----------------------------------------
WiFiClientSecure client; //--> Create a WiFiClientSecure object.
String GAS_ID = "AKfycbwETCQ_Kz6KOTWnQj5y1_xczrK6FVbYuhko9UZMM_kHUblLl-lddju9wz30J5p7EkHfpg"; //--> Google spreadsheet script ID
// Include the Servo library
#include <Servo.h>
// Declare the Servo pin
int servoPin = 13;
// Create a servo object
Servo Servo1;
//==============================================================================
void setup() {
Servo1.attach(servoPin);
//-----Pin define for the moisture sensor
pinMode(A0,INPUT);
Serial.begin(115200);
delay(500);
dht.begin(); //--> Start reading DHT22 sensors
delay(500);
WiFi.begin(ssid, password); //--> Connect to your WiFi router
Serial.println("");
pinMode(ON_Board_LED,OUTPUT); //--> On Board LED on for board testing
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, LOW);
delay(250);
}
//----------------------------------------
digitalWrite(ON_Board_LED, HIGH); //--> Turn off the On Board LED when it is connected to the wifi router.
//----------------------------------------If successfully connected to the wifi router, the IP Address will display
Serial.println("");
Serial.print("Successfully connected to : ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
client.setInsecure();
// We need to attach the servo to the used pin number
Servo1.attach(servoPin);
// Make servo go to 90 degrees
Servo1.write(90);
delay(1000);
Servo1.write(0);
delay(1000);
pinMode(relayInput, OUTPUT);
for(int i=0; i<6;i++)
{
int moisture = analogRead(A0);
// Make servo go to 0 degrees
Serial.println(moisture);
delay(1000);
if (moisture>700)
{
digitalWrite(relayInput, HIGH); // turn relay on
delay(1000);
}
}
digitalWrite(relayInput, LOW); // turn relay off
// delay(1000);
Servo1.write(90);
delay(1000);
}
//==============================================================================
void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
int h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
//Read moisture level
int m = analogRead(A0);
digitalWrite(relayInput, LOW);
// Check if any reads failed and exit
if (isnan(h) || isnan(t) || isnan(m)) {
Serial.println("Failed to read from DHT sensor !");
delay(500);
return;
}
String Temp = "Temperature : " + String(t) + " °C";
String Humi = "Humidity : " + String(h) + " %";
String mois = "Humidity : " + String(m) + " %";
Serial.println(Temp);
Serial.println(Humi);
Serial.println(mois);
sendData(t, h, m); //--> Calls the sendData Subroutine
digitalWrite(relayInput, LOW);
}
//==============================================================================
// Sending data to Google Sheets
void sendData(float tem, int hum, int mois) {
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, DEC);
String string_moisture = String(mois, DEC);
String url = "/macros/s/" + GAS_ID + "/exec?temperature=" + string_temperature + "&humidity=" + string_humidity + "&moisture=" + string_moisture;
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();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment