Skip to content

Commit b16a9c2

Browse files
committed
Added json mode and worker to editor
Example - updates
1 parent 00b4fba commit b16a9c2

15 files changed

+55
-44
lines changed
1.21 KB
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

examples/SmartSwitch/SmartSwitch.ino

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ float t = 0;
166166
float h = 0;
167167
bool udht = false;
168168
bool heat_enabled_prev = false;
169-
int ledState = LED_OFF;
169+
bool ledState = LED_OFF;
170+
bool ledOut = LED_OFF;
170171

171172
struct EE_bl {
172173
byte memid; //here goes the EEMARK stamp
@@ -180,15 +181,15 @@ struct EE_bl {
180181
EE_bl ee = {0,0,0,0,0,0.1}; //populate as initial
181182

182183
// SUBS
183-
void writeEE() {
184+
void writeEE(){
184185
ee.memid = EEMARK;
185186
//EEPROM.put(EESC, sched); // only separately when needed with commit()
186187
//EEPROM.put(EECH, memch); // not need to store and retrieve memch
187188
EEPROM.put(EEBEGIN + memch*sizeof(ee), ee);
188189
EEPROM.commit(); //needed for ESP8266?
189190
}
190191

191-
void readEE() {
192+
void readEE(){
192193
byte ChkEE;
193194
if (memch > MEMMAX) memch = 0;
194195
EEPROM.get(EEBEGIN + memch*sizeof(ee), ChkEE);
@@ -199,6 +200,15 @@ void readEE() {
199200
}
200201
}
201202

203+
void doOut(){
204+
if (ledOut != ledState){ // only if changed
205+
digitalWrite(ledPin, ledState); //consolidated here
206+
ledOut = ledState; //update
207+
if (ledState == LED_OFF) ws.textAll("led,ledoff");
208+
else ws.textAll("led,ledon");
209+
}
210+
}
211+
202212
void showTime()
203213
{
204214
byte tmpch = 0;
@@ -208,21 +218,21 @@ void showTime()
208218
now = time(nullptr);
209219
const tm* tm = localtime(&now);
210220
ws.printfAll("Now,Clock,%02d:%02d,%d", tm->tm_hour, tm->tm_min, tm->tm_wday);
211-
if ((2==tm->tm_hour )&&(2==tm->tm_min)) {
221+
if ((2==tm->tm_hour )&&(2==tm->tm_min)){
212222
configTzTime(MYTZ, "pool.ntp.org");
213223
Serial.print(F("Sync Clock at 02:02\n"));
214224
}
215225
Serial.printf("RTC: %02d:%02d\n", tm->tm_hour, tm->tm_min);
216226

217-
if (sched == 0) { // automatic
227+
if (sched == 0){ // automatic
218228
if ((tm->tm_wday > 0)&&(tm->tm_wday < 6)) tmpch = 0; //Mon - Fri
219229
else if (tm->tm_wday == 6) tmpch = 1; //Sat
220230
else if (tm->tm_wday == 0) tmpch = 2; //Sun
221231
} else { // manual
222232
tmpch = sched - 1; //and stays
223233
}
224234

225-
if (tmpch != memch) { // update if different
235+
if (tmpch != memch){ // update if different
226236
memch = tmpch;
227237
readEE();
228238
ws.printfAll("Now,Setting,%02d:%02d,%02d:%02d,%+2.1f", ee.hstart, ee.mstart, ee.hstop, ee.mstop, ee.tempe);
@@ -237,20 +247,18 @@ void showTime()
237247
else { //enable smart if different
238248

239249
if (((bmi < emi)&&(bmi <= xmi)&&(xmi < emi))||
240-
((emi < bmi)&&((bmi <= xmi)||(xmi < emi)))) {
250+
((emi < bmi)&&((bmi <= xmi)||(xmi < emi)))){
241251
heat_enabled = true;
242252
} else heat_enabled = false;
243253
}
244254

245-
if (heat_enabled_prev) { // smart control (delayed one cycle)
246-
if (((t + HYST) < ee.tempe)&&(ledState == LED_OFF)) { // OFF->ON once
255+
if (heat_enabled_prev){ // smart control (delayed one cycle)
256+
if (((t + HYST) < ee.tempe)&&(ledState == LED_OFF)){ // OFF->ON once
247257
ledState = LED_ON;
248-
digitalWrite(ledPin, ledState); // apply change
249258
ws.textAll("led,ledon");
250259
}
251-
if ((((t - HYST) > ee.tempe)&&(ledState == LED_ON))||(!heat_enabled)) { // ON->OFF once, also turn off at end of period.
260+
if ((((t - HYST) > ee.tempe)&&(ledState == LED_ON))||(!heat_enabled)){ // ON->OFF once, also turn off at end of period.
252261
ledState = LED_OFF;
253-
digitalWrite(ledPin, ledState); // apply change
254262
ws.textAll("led,ledoff");
255263
}
256264

@@ -263,7 +271,7 @@ void showTime()
263271
void updateDHT(){
264272
float h1 = dht.readHumidity();
265273
float t1 = dht.readTemperature(); //Celsius or dht.readTemperature(true) for Fahrenheit
266-
if (isnan(h1) || isnan(t1)) {
274+
if (isnan(h1) || isnan(t1)){
267275
Serial.println(F("Failed to read from DHT sensor!"));
268276
} else {
269277
h = h1 + DHT_H_CORR;
@@ -279,11 +287,10 @@ void analogSample()
279287

280288
void checkPhysicalButton()
281289
{
282-
if (digitalRead(btnPin) == LOW) {
283-
if (btnState != LOW) { // btnState is used to avoid sequential toggles
290+
if (digitalRead(btnPin) == LOW){
291+
if (btnState != LOW){ // btnState is used to avoid sequential toggles
284292
ledState = !ledState;
285-
digitalWrite(ledPin, ledState);
286-
if (ledState == LED_OFF) {
293+
if (ledState == LED_OFF){
287294
ws.textAll("led,ledoff");
288295
Serial.println(F("LED-OFF"));
289296
} else {
@@ -297,17 +304,17 @@ void checkPhysicalButton()
297304
}
298305
}
299306

300-
void mytimer() {
307+
void mytimer(){
301308
++count; //200ms increments
302309
checkPhysicalButton();
303-
if ((count % 25) == 1) { // update temp every 5 seconds
310+
if ((count % 25) == 1){ // update temp every 5 seconds
304311
analogSample();
305312
udht = true;
306313
}
307-
if ((count % 50) == 0) { // update temp every 10 seconds
314+
if ((count % 50) == 0){ // update temp every 10 seconds
308315
ws.cleanupClients();
309316
}
310-
if (count >= 150) { // cycle every 30 sec
317+
if (count >= 150){ // cycle every 30 sec
311318
showTime();
312319
count = 0;
313320
}
@@ -385,22 +392,21 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
385392
Serial.printf("ws[%s][%u] %s-message[%llu]: ", server->url(), client->id(), (info->opcode == WS_TEXT)?"text":"binary", info->len);
386393

387394
if(info->opcode == WS_TEXT){
388-
for(size_t i=0; i < info->len; i++) { //debug
395+
for(size_t i=0; i < info->len; i++){ //debug
389396
msg += (char) data[i];
390397
}
391-
if(data[0] == 'L') { // LED
392-
if(data[1] == '1') {
398+
if(data[0] == 'L'){ // LED
399+
if(data[1] == '1'){
393400
ledState = LED_ON;
394401
ws.textAll("led,ledon"); // for others
395402
}
396-
else if(data[1] == '0') {
403+
else if(data[1] == '0'){
397404
ledState = LED_OFF;
398405
ws.textAll("led,ledoff");
399406
}
400-
digitalWrite(ledPin, ledState); // apply change
401407

402-
} else if(data[0] == 'T') { // timeset
403-
if (len > 11) {
408+
} else if(data[0] == 'T'){ // timeset
409+
if (len > 11){
404410
data[3] = data[6] = data[9] = data[12] = 0; // cut strings
405411
ee.hstart = (uint8_t) atoi((const char *) &data[1]);
406412
ee.mstart = (uint8_t) atoi((const char *) &data[4]);
@@ -411,8 +417,8 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
411417
memch = 255; // to force showTime()to send Setting
412418
showTime();
413419
}
414-
} else if(data[0] == 'W') { // temperatureset
415-
if (len > 3) {
420+
} else if(data[0] == 'W'){ // temperatureset
421+
if (len > 3){
416422
if (ee.tempe != (float) atof((const char *) &data[1])){
417423
ee.tempe = (float) atof((const char *) &data[1]);
418424
Serial.printf("[%u] Temp set %+2.1f\n", client->id(), ee.tempe);
@@ -421,7 +427,7 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
421427
showTime();
422428
}
423429
}
424-
} else if ((data[0] == 'Z')&&(len > 2)) { // sched
430+
} else if ((data[0] == 'Z')&&(len > 2)){ // sched
425431
data[2] = 0;
426432
if (sched != (uint8_t) atoi((const char *) &data[1])){
427433
sched = (uint8_t) atoi((const char *) &data[1]);
@@ -434,7 +440,7 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
434440

435441
} else {
436442
char buff[3];
437-
for(size_t i=0; i < info->len; i++) {
443+
for(size_t i=0; i < info->len; i++){
438444
sprintf(buff, "%02x ", (uint8_t) data[i]);
439445
msg += buff ;
440446
}
@@ -457,12 +463,12 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
457463
Serial.printf("ws[%s][%u] frame[%u] %s[%llu - %llu]: ", server->url(), client->id(), info->num, (info->message_opcode == WS_TEXT)?"text":"binary", info->index, info->index + len);
458464

459465
if(info->opcode == WS_TEXT){
460-
for(size_t i=0; i < len; i++) {
466+
for(size_t i=0; i < len; i++){
461467
msg += (char) data[i];
462468
}
463469
} else {
464470
char buff[3];
465-
for(size_t i=0; i < len; i++) {
471+
for(size_t i=0; i < len; i++){
466472
sprintf(buff, "%02x ", (uint8_t) data[i]);
467473
msg += buff ;
468474
}
@@ -488,7 +494,7 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
488494

489495
void setup(){
490496
Serial.begin(115200);
491-
Serial.setDebugOutput(true);
497+
//Serial.setDebugOutput(true);
492498

493499
//Wifi
494500
#ifdef USE_WFM
@@ -511,7 +517,7 @@ void setup(){
511517
//WiFi.softAP(hostName); // Core SVN 5179 use STA as default interface in mDNS (#7042)
512518
WiFi.mode(WIFI_STA); // Core SVN 5179 use STA as default interface in mDNS (#7042)
513519
WiFi.begin(ssid, password);
514-
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
520+
if (WiFi.waitForConnectResult() != WL_CONNECTED){
515521
Serial.print(F("STA: Failed!\n"));
516522
WiFi.disconnect(false);
517523
delay(1000);
@@ -545,15 +551,14 @@ void setup(){
545551
EEPROM.begin(EEALL);
546552
//EEPROM.get(EECH, memch); //current channel, no need
547553
readEE(); // populate structure if healthy
548-
digitalWrite(ledPin, ledState);
549554
Serial.printf("Timer set %02d:%02d - %02d:%02d\n", ee.hstart, ee.mstart, ee.hstop, ee.mstop);
550555
Serial.printf("Temp set %+2.1f\n", ee.tempe);
551556

552557
//FS
553558
#ifdef USE_FatFS
554-
if (MYFS.begin(false,"/ffat",3)) { //limit the RAM usage, bottom line 8kb + 4kb takes per each file, default is 10
559+
if (MYFS.begin(false,"/ffat",3)){ //limit the RAM usage, bottom line 8kb + 4kb takes per each file, default is 10
555560
#else
556-
if (MYFS.begin()) {
561+
if (MYFS.begin()){
557562
#endif
558563
Serial.print(F("FS mounted\n"));
559564
} else {
@@ -654,7 +659,7 @@ void setup(){
654659
#ifdef USE_AUTH_STAT
655660
if(!request->authenticate(http_username, http_password)) return request->requestAuthentication();
656661
#endif
657-
request->onDisconnect([]() {
662+
request->onDisconnect([](){
658663
#ifdef ESP32
659664
ESP.restart();
660665
#elif defined(ESP8266)
@@ -672,7 +677,7 @@ void setup(){
672677
#ifdef USE_AUTH_STAT
673678
if(!request->authenticate(http_username, http_password)) return request->requestAuthentication();
674679
#endif
675-
request->onDisconnect([]() {
680+
request->onDisconnect([](){
676681
#ifdef ESP32
677682
/*
678683
//https://github.com/espressif/arduino-esp32/issues/400#issuecomment-499631249
@@ -726,7 +731,7 @@ void setup(){
726731

727732
//OTA
728733
ArduinoOTA.setHostname(hostName);
729-
ArduinoOTA.onStart([]() {
734+
ArduinoOTA.onStart([](){
730735
Serial.print(F("OTA Started ...\n"));
731736
MYFS.end(); // Clean FS
732737
ws.textAll("Now,OTA"); // for all clients
@@ -742,5 +747,6 @@ void loop(){
742747
updateDHT();
743748
udht = false;
744749
}
750+
doOut();
745751
ArduinoOTA.handle();
746752
}
1.21 KB
Binary file not shown.

examples/SmartSwitch/data/edit.htm

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)