Could some one help me to modify sketch to work through AJAX requests, no over updating page each time by sending GET requests, quite annoing...
#include <SoftwareSerial.h>
#include <ESP8266pro.h>
#include <ESP8266proServer.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define DEBUG true
const int RX=10;
const int TX=11;
const int ts=9; //temp sensor DS18B20
const int il=13; //indicator led
const int ms=12; //master switch relay
#define ONE_WIRE_BUS ts
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
const int TN = 1400; //tone
float t; //temperature
unsigned long previousMillis = 0; //check sensors
const long interval = 60000; //Sampling interval time mSec
const long COM_BAUD = 38400; //ESP UART baudrate
const String mode = "1"; //1 - Client, 2 - Access Point, 3 - Both
const String ssid = "sazz Wireless Network"; //Network SSID
const String password = "Samir2012A"; //Network Password
const String ipaddress = "192.168.0.201"; //Desired IP address
const String subnetworkmask = "255.255.255.0";
const String gateway = "192.168.0.1";
SoftwareSerial esp8266(RX, TX); // RX, TX
ESP8266pro wifi(esp8266, Serial);
ESP8266proServer server(wifi, onClientRequest);
String requestPaths[ESP_MAX_CONNECTIONS];
void setup() {
pinMode(ts, INPUT);
esp8266.begin(COM_BAUD);
Serial.begin(COM_BAUD); //for debug
sendData("AT+RST\r\n",3400,DEBUG); // reset module
sendData("AT+GMR\r\n",2000,DEBUG); // Sofware version
sendData("AT+CWMODE="+mode+"\r\n",1000,DEBUG); // configure as access point
sendData("AT+CWJAP=\""+ssid+"\",\""+password+"\"\r\n",10000,DEBUG); // connect to AP
sendData("AT+CIPSTA_DEF=\""+ipaddress+"\",\""+gateway+"\",\""+subnetworkmask+"\"\r\n",1000,DEBUG); // set ip address
sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
//sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80
// Start server on port 80
server.start(80);
Serial.println();
Serial.println("=================================");
Serial.println("Server started: http://" + ipaddress);
Serial.println("=================================");
sensors.begin(); //for dallas temp sensor
checkSensors(); //check all available sensors and save values to RAM
digitalWrite(il,HIGH);
delay(1000);
digitalWrite(il,LOW);
}
void loop() { // Process incoming requests
server.processRequests();
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
checkSensors();
}
}
void onClientRequest(ESP8266proConnection* connection,
char* buffer, int length, boolean completed) {
Serial.print(buffer);
if (strncmp(buffer, "GET ", 4) == 0) {
// We found GET HTTP request
char* p = strstr(buffer + 4, " ");
*p = '\0'; // erase string after path
requestPaths[connection->getId()] = (String)((char*)buffer + 4); }
if (completed) { String path = requestPaths[connection->getId()];
if (path == "/")
{
checkSensors(); //check all available sensors and save values to RAM
connection->send(F(
"HTTP/1.0 200 OK\r\n\r\n"
"<head>"
"<meta http-equiv='refresh' content='300'>"
"<title>ESP8266 + ARDUINO</title>"
"</head>"
"<body>"
"<div id=\"changedDiv\" style=\" width: 250px; height: 250px; border-radius: 14px; box-shadow: rgb(0, 0, 0) 0px 0px 20px; opacity: 1;"
"<div style=\"padding-right:20px; padding-left:20px; padding-top:20px; padding-bottom:20px;\">"
"<h1 align=\"center\">ESP8266 + ARDUINO</h1>
"));
connection->send(F(
"<h2> Temp = "
));
connection->send(String(t));
if(digitalRead(ms)==LOW){
connection->send(F(
"
<input type=\"button\" onclick=\"window.location.href='/led2'\" value=\"Switch is OFF\" />"
));
}else{
connection->send(F(
"
<input type=\"button\" onclick=\"window.location.href='/led3'\" value=\"Switch is ON\" />"
));
}
connection->send(F(
"</div>"
"</div>"
"</body>"
"</html>"
));
}
else if (path.startsWith("/led"))
{
if (path.substring(4) == "0"){
masterswitchOn();
Serial.println("Turning masterswitch ON");
connection->send(F(
"HTTP/1.0 200 OK\r\n\r\n"
"<html>"
"<meta http-equiv='refresh' content='1; url=/' />"
"</html>"
));
}
if (path.substring(4) == "1")
{
masterswitchOff();
Serial.println("Turning masterswitch OFF");
connection->send(F(
"HTTP/1.0 200 OK\r\n\r\n"
"<html>"
"<meta http-equiv='refresh' content='1; url=/' />"
"</html>"
));
}
}
else {
connection->send(F("HTTP/1.0 404 Not Found\r\n\r\n"));
connection->send(F("<h1>4 0 4</h1>"));
connection->send(F("Yes, this is true. :("));
}
connection->close();
}
}
void checkSensors(){
sensors.requestTemperatures(); // Send the command to get temperatures
t = sensors.getTempCByIndex(0);
Serial.print("Temperature = ");
Serial.print(t);
Serial.println(" Celsius");
}
void masterswitchOn(){
digitalWrite(ms,HIGH);
}
void masterswitchOff(){
digitalWrite(ms,LOW);
}
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
esp8266.print(command); // send the read character to the esp8266
long int time = millis();
while( (time+timeout) > millis())
{
while(esp8266.available())
{
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
response+=c;
}
}
if(debug)
{
Serial.print(response);
}
return response;
}
libraries here