-
Notifications
You must be signed in to change notification settings - Fork 1
/
esp8266.ino
112 lines (103 loc) · 3.55 KB
/
esp8266.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* This Code is for the ESP8266 WiFi Module. It builds a website running AJAX (Asynchronous JavaScript and XML)
* which allows this webpage to update itself without having to refresh itself which may not be resource friendly
*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#define DEVICE_ID 1
ESP8266WebServer server(80);
const char* ssid="COLDSPOT"; // WiFi Access Point to connect to
const char* password="1234567890"; // Password for the Access Point
String webSite,javaScript,XML;
String coming;
void buildWebsite() {
buildJavascript();
webSite = "<!DOCTYPE HTML>\n";
webSite += "<HEAD><META charset = 'UTF-8'></HEAD>\n";
webSite += "<html lang = 'en-US'>\n";
webSite += javaScript;
webSite += "<BODY onload='process()'>\n";
webSite += "<h1 align='center'><BR>ULTRASONIC SENSOR DATA:<BR><h1>\n";
webSite += "STATUS = <A charset='UTF-8' id='runtime'></A>\n";
webSite += "</BODY>\n";
webSite += "</HTML>\n";
}
void buildJavascript() {
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 += "function process(){\n";
javaScript += " if(xmlHttp.readyState==0 || xmlHttp.readyState==4){\n";
javaScript += " xmlHttp.open('PUT','xml',true);\n";
javaScript += " xmlHttp.onreadystatechange=handleServerResponse;\n"; // no brackets?????
javaScript += " xmlHttp.send(null);\n";
javaScript += " }\n";
javaScript += " setTimeout('process()',1000);\n";
javaScript += "}\n";
javaScript += "function handleServerResponse(){\n";
javaScript += " if(xmlHttp.readyState==4 && xmlHttp.status==200){\n";
javaScript += " xmlResponse=xmlHttp.responseXML;\n";
javaScript += " xmldoc = xmlResponse.getElementsByTagName('response');\n";
javaScript += " message = xmldoc[0].firstChild.nodeValue;\n";
javaScript += " document.getElementById('runtime').innerHTML=message;\n";
javaScript += " }\n";
javaScript += "}\n";
javaScript += "</SCRIPT>\n";
}
void buildXML() {
XML = "<?xml version='1.0' encoding='UTF-8'?>";
XML += "<response>";
XML += DataFromArduino(); //incoming data from Arduino. It means it is the value of mapping function.
XML += "</response>";
}
String DataFromArduino() {
if(Serial.available()) {
while(Serial.available()) {
coming = Serial.readStringUntil('\n').toInt();
delay(1);
} // it is getting via serial port.
return coming;
} else {
return coming;
}
}
void handleWebsite() {
buildWebsite();
server.send(200,"text/html",webSite);
}
void handleXML() {
buildXML();
server.send(200,"text/xml",XML);
}
void setup() {
Serial.begin(9600);
WiFi.begin(ssid,password); //trying to connect the modem
while(WiFi.status() != WL_CONNECTED) {
delay(500);
}
WiFi.mode(WIFI_STA);
Serial.println("\n. . . BOOTING ESP8266 . . .\n");
Serial.print("DEVICE ID :");
Serial.print(DEVICE_ID);
Serial.print("\n");
Serial.print("CONNECTED TO ACCESS POINT :");
Serial.println(ssid);
Serial.flush();
Serial.println(WiFi.localIP());
//it is sending the IP to Arduino's port. So I can see the IP on Serial Monitor
Serial.flush();
server.on("/", handleWebsite);
//it is all about setting of server.
server.on("/xml",handleXML);
server.begin();
}
void loop() {
server.handleClient();
}