I am just new on using AJAX in web server, I got an error on declaring "server" I am confused about this because I just used only a AT commands to used the ESP8266-01 (wifi-module) I hope you can help me, I want to get the data (distance) to print in the web server(page) in real-time(no refresh). So I used the AJAX for it but this code is still error. Advance thanks on anyhelp :)
#include "NewPing.h"
#include <SoftwareSerial.h>
#define DEBUG true
SoftwareSerial esp8266(10,11);
#define TRIGGER_PIN 4
#define ECHO_PIN 3
#define MAX_DISTANCE 400
NewPing sonar(TRIGGER_PIN,ECHO_PIN,MAX_DISTANCE);
void setup() {
Serial.begin (9600);
Serial.println("START");
esp8266.begin(9600);
server.on("/",WebsiteContent);
server.on("/xml",XMLcontent);
server.begin();
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
sendData("AT+RST\r\n",2000,DEBUG);
sendData("AT+CWMODE=2\r\n",1000,DEBUG);
sendData("AT+CIFSR\r\n",1000,DEBUG);
sendData("AT+CIPMUX=1\r\n",1000,DEBUG);
sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG);
}
void javascriptContent(){
Javascript ="<SCRIPT>\n";
Javascript+="var xmlHttp=createXmlHttpObject();\n";
Javascript+="function createXmlHttpObject(){\n";
Javascript+="if(window.XMLHttpRequest){\n";
Javascript+="xmlHttp=new XMLHttpRequest();\n";
Javascript+="}else{\n";
Javascript+="xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');\n";
Javascript+="}\n";
Javascript+="return xmlHttp;\n";
Javascript+="}\n";
Javascript+="\n";
Javascript+="function response(){\n";
Javascript+="xmlResponse=xmlHttp.responseXML;\n";
Javascript+="xmldoc = xmlResponse.getElementsByTagName('data');\n";
Javascript+="message = xmldoc[0].firstChild.nodeValue;\n";
Javascript+="document.getElementById('div1').innerHTML=message;\n";
Javascript+="}\n";
Javascript+="function process(){\n";
Javascript+="xmlHttp.open('PUT','xml',true);\n";
Javascript+="xmlHttp.onreadystatechange=response;\n";
Javascript+="xmlHttp.send(null);\n";
Javascript+="setTimeout('process()',200);\n";
Javascript+="}\n";
Javascript+="</SCRIPT>\n";
}
void WebsiteContent(){
javascriptContent();
Website="<html>\n";
Website+="<style>\n";
Website+="#div1{\n";
Website+="width:400px;\n";
Website+="margin:0 auto;\n";
Website+="margin-top:130px;\n";
Website+="font-size:900%;\n";
Website+="color:powderblue;\n";
Website+="}\n";
Website+="</style>\n";
Website+="<body onload='process()'>";
Website+="<div id='div1'>"+data+"</div></body></html>";
Website+=Javascript;
server.send(200,"text/html",Website);
}
void XMLcontent(){
XML ="<?xml version='1.0'?>";
XML+="<data>";
XML+=data;
XML+="</data>";
server.send(200,"text/xml",XML);
}
void loop()
{
delay(2000);
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
long duration,distance;
duration= pulseIn(ECHO_PIN,HIGH);
distance = duration/58.2;
data=(String)distance;
delay(500);
server.handleClient();
if(esp8266.available())
{
if(esp8266.find("+IPD,"))
{
delay(1000);
int connectionId = esp8266.read()-48;
String cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend +=webpage.length();
cipSend +="\r\n";
sendData(cipSend,1000,DEBUG);
sendData(webpage,1000,DEBUG);
String closeCommand = "AT+CIPCLOSE=";
closeCommand+=connectionId;
closeCommand+="\r\n";
sendData(closeCommand,3000,DEBUG);
}
}
}
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
esp8266.print(command);
long int time = millis();
while( (time+timeout) > millis())
{
while(esp8266.available())
{
char c = esp8266.read();
response+=c;
}
}
if(debug)
{
Serial.print(command);
Serial.print("> ");
Serial.print(response);
}
return response;
}
ESP8266WebServer server(80);