-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.ino
156 lines (127 loc) · 4.61 KB
/
webserver.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <ESP8266WiFi.h> // Include the Wi-Fi library
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
WiFiClient client;
const char* hostGet = "192.168.2.3";
ESP8266WebServer server;
const char* ssid = "DIGISOL"; // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = "shreeram1998"; // The password of the Wi-Fi network
//const char* ssid = "realmeXT"; // The SSID (name) of the Wi-Fi network you want to connect to
//const char* password = "helloworld98"; // The password of the Wi-Fi network
uint8_t pin_led = 16;
char sessionPage[] PROGMEM = R"=====(
<html>
<head>
<script>
function changeState(){
var xhr = new XMLHttpRequest();
var url = "/session";
xhr.onreadystatechange = function(){
if(this.readyState == 4 && this.status == 200){
document.getElementById("state").innerText = this.responseText;
}
};
xhr.open("GET",url,true);
xhr.send();
}
function sendIP(){
var xhr = new XMLHttpRequest();
var url = "/getIP";
xhr.onreadystatechange = function(){
if(this.readyState == 4 && this.status == 200){
document.getElementById("ip").innerText = this.responseText;
}
};
xhr.open("GET",url,true);
xhr.send();
}
</script>
</head>
<body>
<center>
<h1>Start or end the session</h1><br>
<button onclick="changeState()" id="state"> start </button>
<button onclick="sendIP()" id="ip"> Send my ip</button>
</body>
<html>
)=====";
//===============================================POST---DATA---==========================================================================
void postData(String ip) {
WiFiClient clientGet;
const int httpGetPort = 8080;
//the path and file to send the data to:
String urlGet = "/teacher/class";
// We now create and add parameters:
urlGet += "?ip=" + ip+"&classroom=C1";
Serial.println("urlget:"+urlGet);
Serial.print(">>> Connecting to host: ");
Serial.println(hostGet);
if (!clientGet.connect(hostGet, httpGetPort)) {
Serial.print("Connection failed: ");
Serial.print(hostGet);
} else {
clientGet.println("GET " + urlGet + " HTTP/1.1");
clientGet.print("Host: ");
clientGet.println(hostGet);
clientGet.println("User-Agent: ESP8266/1.0");
clientGet.println("Connection: close\r\n\r\n");
unsigned long timeoutP = millis();
while (clientGet.available() == 0) {
if (millis() - timeoutP > 10000) {
Serial.print(">>> Client Timeout: ");
Serial.println(hostGet);
clientGet.stop();
return;
}
}
//just checks the 1st line of the server response. Could be expanded if needed.
while(clientGet.available()){
String retLine = clientGet.readStringUntil('\r');
Serial.println(retLine);
break;
}
} //end client connection if else
Serial.print(">>> Closing host: ");
Serial.println(hostGet);
clientGet.stop();
}
void checkSession() {
digitalWrite(pin_led,!digitalRead(pin_led));
String state = digitalRead(pin_led) ? "On" : "Off";
server.send(200,"text/plain",state);
}
void ip() {
String ip =WiFi.localIP().toString().c_str();
server.send(200,"text/plain",ip);
postData(ip);
}
void setup() {
Serial.begin(9600); // Start the Serial communication to send messages to the computer
delay(10);
Serial.println('\n');
WiFi.begin(ssid, password); // Connect to the network
Serial.print("Connecting to ");
Serial.print(ssid); Serial.println(" ...");
int i = 0;
while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(1000);
Serial.print(++i); Serial.print(' ');
}
Serial.println('\n');
Serial.println("Connection established!");
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
pinMode(pin_led, OUTPUT);
//===========Server Config===========================
server.on("/",[](){server.send_P(200,"text/html",sessionPage);});
server.begin();
server.on("/session",checkSession);
server.on("/getIP",ip);
postData(WiFi.localIP().toString().c_str());
}
void loop() {
server.handleClient();
}
void welcome(){
server.send(200,"text/html","<b>Hello you are on nodemcu server</b>");
}