Untitled

 avatar
unknown
plain_text
3 years ago
3.1 kB
2
Indexable
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <SFE_BMP180.h>
#include <Wire.h>
#include "DHTesp.h"
#include "index.h" //Our HTML webpage contents with javascripts


//----------------------------------------Host & httpsPort
const char* host = "script.google.com";
const int httpsPort = 443;
//----------------------------------------

WiFiClientSecure client; //--> Create a WiFiClientSecure object.


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


DHTesp dht;

SFE_BMP180 pressure;

#define ALTITUDE 1655.0 // Altitude in meters


#define LED 2 //On board LED
#define DHTpin 14 //D5 of NodeMCU is GPIO14

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

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
}

float humidity, temperature;

void handleADC() {
  char status;
  double T, P, p0, a;
  double Tdeg, Tfar, phg, pmb, tempF;


  status = pressure.startTemperature();
  if (status != 0)
  {
    // Wait for the measurement to complete:
    delay(status);
    status = pressure.getTemperature(T);
    if (status != 0)
    {
      // Print out the measurement:
      Serial.print("temperature: ");
      Serial.print(T, 2);
      Tdeg = T;
      Serial.print(" deg C, ");
      Tfar = (9.0 / 5.0) * T + 32.0;
      Serial.print((9.0 / 5.0)*T + 32.0, 2);
      tempF = (9.0 / 5.0) * T + 32.0;
      Serial.println(" deg F");

    }

    int soil = analogRead(A0);
    //Create JSON data
    String data = "{\"Temperature\":\"" + String(soil) + "\"}";

    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

    Serial.print("T:");
    Serial.println(T); //dht.toFahrenheit(temperature));

  }

}

void setup()
{
  Serial.begin(115200);
  Serial.println();
  pinMode(LED, OUTPUT);


   //BMP180 Sensor
  if (pressure.begin())
    Serial.println("BMP180 init success");
  else
  {
    Serial.println("BMP180 init fail\n\n");
    while (1); // Pause forever.
  }

  
  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
}