/*
Read RC voltage circuit
Plot data chart.js
Condensator is connected to
analog pin A0
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#define LED 16 //set to digital pin 16 variable LED
#include "index.h"
float val;
float ddp;
//SSID and Password of your WiFi router
const char* ssid = "nome_rete";
const char* password = "password";

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

//===============================================================
// This routine is executed when you open its IP in browser
//===============================================================
void handleRoot() {
//
String webpage=MAIN_page;
server.send(200, "text/html", webpage); //Send web page
}

void handleADC() {
val = analogRead(A0);
ddp=(val-10)*0.002682;
Serial.println(val);
String adcValue = String(ddp);
//digitalWrite(LED,!digitalRead(LED)); //Toggle LED on data request ajax
server.send(200, "text/plane", adcValue); //Send ADC value only to client ajax request
}
//==============================================================
// SETUP
//==============================================================
void setup(void){
Serial.begin(115200);

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

//Onboard LED port Direction output
pinMode(LED,OUTPUT);
digitalWrite(LED,HIGH);

// 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
server.on("/readADC",handleADC);

server.begin(); //Start server
Serial.println("HTTP server started");
}
//==============================================================
// LOOP
//==============================================================
void loop(void){
server.handleClient(); //Handle client requests
}