0

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;
}
  • 2
    Do you actually have a question? And is it related to Arduino, and not Javascript or HTML? – Majenko Nov 21 '18 at 12:20
  • I already edited it sir :) sir do you have any idea about it? – Xiaomy Nov 21 '18 at 12:24
  • Looks like you're confusing ESP8266 native code for Arduino code. – Majenko Nov 21 '18 at 12:25
  • try to add a include ESP8266WebServer server(80); – abu-ahmed al-khatiri Nov 21 '18 at 12:31
  • @abu-ahmedal-khatiri I also add that earlier but still error – Xiaomy Nov 21 '18 at 12:32
1

You're trying to mix code that is intended to run directly on an ESP8266 using ESP8266-specific libraries on an Arduino. It can't ever work.

Likes like:

server.on("/",WebsiteContent);
server.on("/xml",XMLcontent);
server.begin();

are for use with the ESP8266's ESP8266WebServer library which doesn't run on an Arduino. To use these kind of constructs you have to program the ESP8266, not the Arduino, and then you don't have the AT commands available.

So, either:

  • Do it all with AT commands only, or
  • Ditch the Arduino and program the ESP8266 directly instead.
  • can you send me a link about that in AT commands? – Xiaomy Nov 21 '18 at 12:38
  • You already use them in your code. – Majenko Nov 21 '18 at 13:16
  • I thought there will be for AJAX or what – Xiaomy Nov 21 '18 at 13:17
  • can you help me sir? – Xiaomy Nov 21 '18 at 14:59
  • Do you know what the AT commands that you have in your sketch actually do? – Majenko Nov 21 '18 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.