Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.7 kB
3
Indexable
Never
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include "index.h" //Our HTML webpage contents with javascripts
int sense_Pin = A0; // sensor input at Analog pin A0

int value = 0;

//----------------------------------------Host & httpsPort
const char* host = "script.google.com";
const int httpsPort = 443;
//----------------------------------------
//
WiFiClientSecure client; //--> Create a WiFiClientSecure object.
//
//String GAS_ID = "AKfycbyBbb7Or-vdlkT6mTuq5D2ht7ohLy89HvCFsPcQ4DoLHG_petNPsYOiyBiM7ye58FIl"; //--> spreadsheet script ID

//----------------------------------------


//SSID and Password of your WiFi router
//const char* ssid = "Sammy";
//const char* password = "flyingcars970";

const char* ssid = "SammyLaptop";
const char* password = "flyingcars970";

ESP8266WebServer server(80); //Server on port 80

void handleRoot() {
  String s = MAIN_page; //Read HTML contents
  server.send(200, "text/html", s); //Send web page
}


void handleADC() {
  char status;

  int soilvalue = analogRead(sense_Pin);

  Serial.print("Soil Value: ");
  Serial.println(soilvalue);

  //Create JSON data
  String data = "{\"Soil\":\"" + String(soilvalue) + "\"}";

  //  digitalWrite(LED, !digitalRead(LED)); //Toggle LED on data request ajax
  server.send(200, "text/plane", data); //Send ADC value, temperature and humidity JSON to client ajax request

  // sendData(T, tempF, humidity, rain, P, phg); //--> Calls the sendData Subroutine
}

//void sendData(float temInC, double temInF, int hum, int rainfall, double presInMB, double presInHG) {
//  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_temperatureInC =  String(temInC);
//  String string_temperatureInF =  String(temInF);
//  // String string_temperature =  String(tem, DEC);
//  String string_humidity =  String(hum, DEC);
//  String string_rain = String(rainfall);
//  String string_pressureInMB = String(presInMB);
//  String string_pressureInHG = String(presInHG);
//  String url = "/macros/s/" + GAS_ID + "/exec?temperatureInC=" + string_temperatureInC + "&temperatureInF=" + string_temperatureInF + "&humidity=" + string_humidity + + "&rain=" + string_rain + "&pressureInMB=" + string_pressureInMB + "&pressureInHG=" + string_pressureInHG;
//  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();
//  //----------------------------------------
//}

void setup()
{
  Serial.begin(115200);
  Serial.println();

  WiFi.begin(ssid, password); //Connect to your WiFi router
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }


  //If connection successful show IP address in serial monitor
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP()); //IP address assigned to your ESP

  server.on("/", handleRoot); //Which routine to handle at root location. This is display page
  server.on("/readADC", handleADC); //This page is called by java Script AJAX

  server.begin(); //Start server
  Serial.println("HTTP server started");

  client.setInsecure();

}

void loop()
{
  server.handleClient(); //Handle client requests

  handleADC();
}