-
Notifications
You must be signed in to change notification settings - Fork 0
/
ADB_ESP8266_webServer.ino
138 lines (113 loc) · 3.46 KB
/
ADB_ESP8266_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
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
extern "C" {
#include <user_interface.h>
}
#define RELAY_Pin D1
#define LED_Pin 2
#define WLAN_SSID "roterSSID"
#define WLAN_PASS "rooterPass"
struct softap_config config;
uint8_t monitoringMAC1[6] = {0x34 , 0xD2, 0x70, 0x1A, 0x80, 0x15};
boolean detectingMAC1 = false;
int detectInterval = 8000; //msec
unsigned long lastDetectedMillis = 0;
ESP8266WebServer server(80);
void ICACHE_FLASH_ATTR wifi_handle_event_cb(System_Event_t *evt) {
if (evt->event != EVENT_SOFTAPMODE_PROBEREQRECVED) {
return;
}
uint8_t* mac = evt->event_info.ap_probereqrecved.mac;
if (memcmp(mac, monitoringMAC1, 6) == 0 && detectInterval < (millis() - lastDetectedMillis)) {
lastDetectedMillis = millis();
detectingMAC1 = true;
}
}
void setup() {
delay(1000);
Serial.begin(115200);
Serial.println();
Serial.print("Configuring access point...");
connectWiFi();
initializePage();
server.on("/", initializePage);
server.on("/RELE", webOnOff);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
if (detectingMAC1) {
webOnOff();
}
}
void connectWiFi() {
WiFi.disconnect();
delay(10);
WiFi.mode(WIFI_AP_STA);
WiFi.begin(WLAN_SSID, WLAN_PASS);
delay(100);
Serial.print("\nConnecting to ");
Serial.print(WLAN_SSID);
// Wait for connection
int i = 0;
while (WiFi.status() != WL_CONNECTED && i++ <= 10) {//wait 10 seconds
Serial.print(".");
delay(1000);
}
if (i == 11) {
Serial.print("\nCould not connect to network...");
while (1) delay(500);
}
WiFi.softAP("ESPap","thereisnospoon");
Serial.print("\nSoftAP IP address: ");
Serial.println(WiFi.softAPIP());
Serial.print("Local IP address: ");
Serial.println(WiFi.localIP());
wifi_set_event_handler_cb(wifi_handle_event_cb);
}
void initializePage() {
updateHTML(checkClient());
}
String checkClient() {
WiFiClient clientConnected = server.client();
String ipClient = clientConnected.remoteIP().toString();
return ipClient;
}
//Aggiorna la pagina del webserver
void updateHTML(String IP) {
pinMode(RELAY_Pin, OUTPUT);
String webpage = "";
webpage += "<div id=\"header\"><h1>MiniServer ESP8266</h1></div>";
webpage += "<h2>Client connected :</h2>";
webpage += IP;
webpage += "<div id=\"section\"><h2>RELE Status</h2>";
webpage += "RELE has been switched : " + String((digitalRead(RELAY_Pin) == HIGH) ? "ON" : "OFF");
webpage += "<br><br>";
webpage += "<input type=\"button\" value=\"RELE " + String((digitalRead(RELAY_Pin) == HIGH) ? "ON" : "OFF") + " \" + onclick=\"window.location.href=\'/RELE\'\">";
webpage += "</div>";
server.send(200, "text/html", webpage);
delay(1000);
}
void initializePins() {
pinMode(LED_Pin, OUTPUT);
pinMode(RELAY_Pin, OUTPUT);
digitalWrite(LED_Pin, HIGH); // turn off LED with voltage HIGH
digitalWrite(RELAY_Pin, LOW); // turn off relay with voltage LOW
}
//Gestione degli eventi quando si accede dalla pagina web
void webOnOff() {
String requestUrl = server.uri();
Serial.println("Connected client: " + checkClient());
updateHTML(checkClient());
if (requestUrl.indexOf("RELE") != -1 || detectingMAC1) {
pinMode(RELAY_Pin, OUTPUT);
pinMode(LED_Pin, OUTPUT);
digitalWrite(RELAY_Pin, !digitalRead(RELAY_Pin));
digitalWrite(LED_Pin, !digitalRead(LED_Pin));
Serial.println("Switched ON/OFF");
detectingMAC1 = false;
}
initializePage();
}