Skip to content

Commit f7fe2e8

Browse files
author
gemi254
committed
Added connection control example
1 parent b5d054f commit f7fe2e8

File tree

1 file changed

+204
-0
lines changed

1 file changed

+204
-0
lines changed
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
#if defined(ESP32)
2+
#include <ESPmDNS.h>
3+
#include <WebServer.h>
4+
#define WEB_SERVER WebServer
5+
#define ADC_PIN 36
6+
#else
7+
#include <ESP8266mDNS.h>
8+
#include <ESP8266WebServer.h>
9+
#define WEB_SERVER ESP8266WebServer
10+
#define ADC_PIN A0
11+
#endif
12+
13+
#define LOGGER_LOG_LEVEL 5 // Define log level for this module
14+
#include <ControlAssist.h> // Control assist class
15+
16+
const char st_ssid[]="mdk3"; // Put connection SSID here. On empty an AP will be started
17+
const char st_pass[]="2843028858"; // Put your wifi passowrd.
18+
unsigned long pingMillis = millis(); // Ping millis
19+
unsigned long disconnMillis = millis(); // Wifi disconnect millis
20+
21+
ControlAssist ctrl; // Control assist class
22+
WEB_SERVER server(80); // Web server on port 80
23+
24+
time_t disconTime = 10 * 1000; // Wifi disconnection duration
25+
26+
PROGMEM const char HTML_BODY[] = R"=====(
27+
<style>
28+
body {text-align: center;}
29+
30+
#conLed {
31+
width: 10px;
32+
height: 10px;
33+
background-color: lightGray;
34+
border-radius: 10px;
35+
margin-left: 95%;
36+
}
37+
#conLed.on {
38+
background-color: lightGreen;
39+
border: 1px solid #4CAF50;
40+
}
41+
42+
th{ text-align: right;}
43+
</style>
44+
<body>
45+
<div style="text-align:left;display:inline-block;min-width:340px;">
46+
<div style="text-align:center;color:#eaeaea;">
47+
<h1>ControlAssist Websockets connection</h1>
48+
</div>
49+
<div>
50+
<table border="0" style="width: 100%;" cellspacing="10">
51+
<tr>
52+
<th>MacAddress</th>
53+
<td><span id="mac_address">AA:DD:CC:EE:FF:KK</span></td>
54+
</tr>
55+
<tr>
56+
<th>Wifi RSSI </th>
57+
<td><input title="Range control" type="range" id="wifi_rssi" name="wifi_rssi" min="-120" max="0" value="0"></td>
58+
</tr>
59+
<tr>
60+
<th>Disconnect</th>
61+
<td>
62+
<button type="button" title="Disconnect WiFi" id="disconnect_button">Disconnect</button>&nbsp;WiFi for
63+
<input type="text" title="Enter wifi disconnect seconds" id="sleep_time" value="" style="width: 30px">&nbsp;Seconds
64+
</td>
65+
</tr>
66+
<tr><td>&nbsp;</td></tr>
67+
<tr>
68+
<td><div id="conLed">&nbsp;</td>
69+
<td></div><span id="wsStatus" style="display: none1;">Disconnected</span></td>
70+
</tr>
71+
</table>
72+
</div>
73+
</div>
74+
</body>
75+
76+
<script>
77+
document.getElementById("wsStatus").addEventListener("change", (event) => {
78+
conLed = document.getElementById("conLed")
79+
if(event.target.innerHTML.startsWith("Connected: ")){
80+
conLed.classList.add("on");
81+
}else{
82+
conLed.classList.remove("on");
83+
}
84+
conLed.title = event.target.innerHTML
85+
});
86+
</script>
87+
)=====";
88+
89+
// Disconnect WiFi
90+
void disconnect(){
91+
LOG_D("Disconnect WiFi for %s seconds\n", String(disconTime / 1000L).c_str());
92+
ctrl.put("wifi_rssi", -120 );
93+
ctrl.loop();
94+
ctrl.sendSystemMsg("C");
95+
time_t s = millis();
96+
while(millis() - s < 100 )
97+
ctrl.loop();
98+
WiFi.disconnect();
99+
disconnMillis = millis();
100+
}
101+
102+
// Change handler to handle websockets changes
103+
void changeHandler(uint8_t ndx){
104+
String key = ctrl[ndx].key;
105+
if(key == "sleep_time" )
106+
disconTime = ctrl[key].toInt() * 1000;
107+
else if(key == "disconnect_button")
108+
disconnect();
109+
110+
LOG_D("changeHandler: ndx: %02i, key: %s = %s\n",ndx, key.c_str(), ctrl[key].c_str());
111+
}
112+
// Connect WiFi
113+
void connect(){
114+
if(WiFi.status() == WL_CONNECTED ) return;
115+
// Connect WIFI ?
116+
if(strlen(st_ssid)>0){
117+
LOG_E("Connect Wifi to %s.\n", st_ssid);
118+
WiFi.mode(WIFI_STA);
119+
WiFi.begin(st_ssid, st_pass);
120+
uint32_t startAttemptTime = millis();
121+
while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < 20000) {
122+
Serial.print(".");
123+
delay(500);
124+
Serial.flush();
125+
}
126+
Serial.println();
127+
}
128+
129+
// Check connection
130+
if(WiFi.status() == WL_CONNECTED ){
131+
LOG_I("Wifi AP SSID: %s connected, use 'http://%s' to connect\n", st_ssid, WiFi.localIP().toString().c_str());
132+
}else{
133+
LOG_E("Connect failed.\n");
134+
LOG_I("Starting AP.\n");
135+
String mac = WiFi.macAddress();
136+
mac.replace(":","");
137+
String hostName = "ControlAssist_" + mac.substring(6);
138+
WiFi.mode(WIFI_AP);
139+
WiFi.softAP(hostName.c_str(),"",1);
140+
LOG_I("Wifi AP SSID: %s started, use 'http://%s' to connect\n", WiFi.softAPSSID().c_str(), WiFi.softAPIP().toString().c_str());
141+
if (MDNS.begin(hostName.c_str())) LOG_V("AP MDNS responder Started\n");
142+
}
143+
}
144+
145+
void setup() {
146+
Serial.begin(115200);
147+
Serial.print("\n\n\n\n");
148+
Serial.flush();
149+
LOG_I("Starting..\n");
150+
// Connect WiFi
151+
connect();
152+
153+
// Control assist setup
154+
ctrl.setHtmlBody(HTML_BODY);
155+
ctrl.bind("mac_address", WiFi.macAddress().c_str() );
156+
ctrl.bind("wifi_rssi");
157+
ctrl.bind("sleep_time", disconTime / 1000);
158+
ctrl.bind("disconnect_button");
159+
ctrl.setAutoSendOnCon(true);
160+
// Every time a variable changed changeHandler will be called
161+
ctrl.setGlobalCallback(changeHandler);
162+
163+
// Start web sockets
164+
ctrl.begin();
165+
//ctrl.put("sleep_time", disconTime / 1000,true);
166+
LOG_V("ControlAssist started.\n");
167+
168+
// Setup webserver
169+
server.on("/", []() {
170+
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
171+
String res = "";
172+
res.reserve(CTRLASSIST_STREAM_CHUNKSIZE);
173+
while( ctrl.getHtmlChunk(res)){
174+
server.sendContent(res);
175+
}
176+
});
177+
178+
// Start webs server
179+
server.begin();
180+
LOG_V("HTTP server started\n");
181+
}
182+
183+
void loop() {
184+
// Change html control values
185+
if (millis() - pingMillis >= 3000){
186+
// Update control assist variables
187+
ctrl.put("wifi_rssi", WiFi.RSSI() );
188+
189+
pingMillis = millis();
190+
}
191+
// Change html control values
192+
if ( WiFi.status() != WL_CONNECTED && millis() - disconnMillis >= disconTime){
193+
// Re connect
194+
connect();
195+
disconnMillis = millis();
196+
}
197+
#if not defined(ESP32)
198+
if(MDNS.isRunning()) MDNS.update(); // Handle MDNS
199+
#endif
200+
// Handler webserver clients
201+
server.handleClient();
202+
// Handle websockets
203+
ctrl.loop();
204+
}

0 commit comments

Comments
 (0)