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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
| #include <soc/rtc_cntl_reg.h> #include <WiFi.h> #include <WebServer.h> #include <ESPmDNS.h> #include <esp_wifi.h> #include <HTTPClient.h> #include "text.h" #include <Wire.h>
#include <dht11.h> dht11 DHT11; #define DHT11PIN 2 int DHT11_STATUS = 0; int DHT11_COUNT = 0;
const char* AP_SSID = "ESP32_Config"; String wifi_ssid = ""; String wifi_pass = ""; String scanNetworksID = "";
#define ROOT_HTML "<!DOCTYPE html><html><head><title>WIFI Config by lwang</title><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"></head><style type=\"text/css\">.input{display: block; margin-top: 10px;}.input span{width: 100px; float: left; float: left; height: 36px; line-height: 36px;}.input input{height: 30px;width: 200px;}.btn{width: 120px; height: 35px; background-color: #000000; border:0px; color:#ffffff; margin-top:15px; margin-left:100px;}</style><body><form method=\"GET\" action=\"connect\"><label class=\"input\"><span>WiFi SSID</span><input type=\"text\" name=\"ssid\"></label><label class=\"input\"><span>WiFi PASS</span><input type=\"text\" name=\"pass\"></label><input class=\"btn\" type=\"submit\" name=\"submit\" value=\"Submie\"> <p><span> Nearby wifi:</P></form>"
String header;
String LED= "off"; String ATOMIZER= "off"; String FAN = "off"; String NEW = "REFRESH";
const int output26 = 23; const int output27 = 27;
unsigned long currentTime = millis();
unsigned long previousTime = 0;
const long timeoutTime = 2000;
typedef struct tool { int ledinput; int faninput; int wuinput; }tools; tools ntools; bool resetesp = 0;
WiFiServer server1(80); WebServer server(80);
#define RESET_PIN 13
#include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64
#define OLED_RESET 4 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); const char *ntpServer = "pool.ntp.org"; const long gmtOffset_sec = 8 * 3600; const int daylightOffset_sec = 0;
void Task1code( void *pvParameters ); void Task2code( void *pvParameters );
void setup() { Serial.begin(115200); pinMode(RESET_PIN, INPUT);
pinMode(33, OUTPUT); pinMode(5, OUTPUT);
pinMode(25, OUTPUT); pinMode(22, OUTPUT);
pinMode(26, OUTPUT); pinMode(23, OUTPUT);
pinMode(34, INPUT); pinMode(35, INPUT); pinMode(32, INPUT);
pinMode(17, OUTPUT);
digitalWrite(25, HIGH); digitalWrite(22, LOW);
digitalWrite(33, HIGH); digitalWrite(5, LOW);
digitalWrite(26, HIGH); digitalWrite(23, LOW);
pinMode(DHT11PIN,OUTPUT);
digitalWrite(17, HIGH);
delay(100);
if (!AutoConfig()) { wifi_Config(); Serial.println("esp32 reset"); } Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP()); WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); xTaskCreatePinnedToCore(Task1code, "Task1", 10000, NULL, 1, NULL, 1); xTaskCreatePinnedToCore(Task2code, "Task2", 10000, NULL, 0, NULL, 0); server1.begin(); }
int value = 0; void loop(){ } void Task1code(void *pvParameters) { for (;;) { digitalWrite(25, HIGH); digitalWrite(22, LOW); digitalWrite(26, HIGH); digitalWrite(23, LOW);
digitalWrite(33, HIGH); digitalWrite(5, LOW);
digitalWrite(17, HIGH);
ntools.ledinput = digitalRead(35); ntools.wuinput = digitalRead(34); ntools.faninput = digitalRead(32); delay(2000); if(ntools.ledinput == HIGH) { Serial.println("ntools.ledinput high "); LED = "on"; } if(ntools.ledinput == LOW) { Serial.println("ntools.ledinput low "); LED = "off"; }
if(ntools.faninput == HIGH) { Serial.println("ntools.faninput high "); FAN = "on"; } if(ntools.faninput == LOW) { Serial.println("ntools.faninput low "); FAN = "off"; } if(ntools.wuinput == HIGH) { Serial.println("ntools.wuinput high "); ATOMIZER = "on"; } if(ntools.wuinput == LOW) { Serial.println("ntools.wuinput low "); ATOMIZER = "off"; }
if(resetesp == 1) { resetesp =0; ESP.restart(); Serial.println("esp32 restart!!!!"); } Serial.println(" no no esp32 restart!!!!"); if (digitalRead(RESET_PIN) == HIGH) { delay(1000); esp_wifi_restore(); delay(10); Serial.println(" esp32 esp_wifi_restore()!!!!"); ESP.restart(); } server.handleClient(); WiFiClient client = server1.available(); delay(500); if (client) { currentTime = millis(); previousTime = currentTime; Serial.println("New Client."); String currentLine = ""; while (client.connected() && currentTime - previousTime <= timeoutTime) { currentTime = millis(); if (client.available()) { char c = client.read(); Serial.write(c); header += c; if (c == '\n') { if (currentLine.length() == 0) { client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println("Connection: close"); client.println(); if (header.indexOf("GET /LED/on") >= 0) { Serial.println("GPIO 26 on"); LED = "on"; digitalWrite(22, HIGH); } else if (header.indexOf("GET /LED/off") >= 0) { Serial.println("GPIO 26 off"); LED = "off"; digitalWrite(25, LOW); } else if (header.indexOf("GET /at/on") >= 0) { ATOMIZER = "on"; digitalWrite(5, HIGH); } else if (header.indexOf("GET /at/off") >= 0) { ATOMIZER = "off"; digitalWrite(33, LOW); } else if(header.indexOf("GET /fan/on")>= 0){ FAN = "on"; digitalWrite(23, HIGH); }else if(header.indexOf("GET /fan/off")>= 0){ FAN= "off"; digitalWrite(26, LOW); }
else if(header.indexOf("GET /new") >= 0) { Serial.println("new"); }
client.println("<!DOCTYPE html><html>"); client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"); client.println("<link rel=\"icon\" href=\"data:,\">"); client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}"); client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;"); client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}"); client.println(".button2 {background-color: #555555;}</style></head>"); client.println("<body><h1>桌面精灵</h1>"); client.println("<p>LED - State " + LED + "</p>"); if (LED=="off") { client.println("<p><a href=\"/LED/on\"><button class=\"button\">ON</button></a></p>"); } else { client.println("<p><a href=\"/LED/off\"><button class=\"button button2\">OFF</button></a></p>"); } client.println("<p>atomizer - State " + ATOMIZER + "</p>"); if (ATOMIZER=="off") { client.println("<p><a href=\"/at/on\"><button class=\"button\">ON</button></a></p>"); } else { client.println("<p><a href=\"/at/off\"><button class=\"button button2\">OFF</button></a></p>"); } client.println("<p>fan - State " + FAN + "</p>"); if (FAN=="off") { client.println("<p><a href=\"/fan/on\"><button class=\"button\">ON</button></a></p>"); } else { client.println("<p><a href=\"/fan/off\"><button class=\"button button2\">OFF</button></a></p>"); } if(FAN=="off" || DHT11_STATUS == 0) { client.println("<a ><button class=\"button top1\">温湿度显示</button>"); client.println("</a><center style=\"position: relative;\"></br><a><button type=\"button\" class=\"button\" value=\"temp\">温度<span style=\"color: red;font-size: 25px;\">"); client.println((int)DHT11.temperature); client.println("°C</span></button></a><a><button type=\"button\" class=\"button\" value=\"humi\">湿度<span style=\"color: green;font-size: 25px;\">"); client.println((int)DHT11.humidity); client.println("%</span></button></a>"); client.println("</span></button></a></br>"); } client.println("<p>" + NEW + "</p>"); client.println("<p><a href=\"/new\"><button class=\"button\">NEW</button></a></p>"); client.println("</body></html>"); client.println(); break; } else { currentLine = ""; } } else if (c != '\r') { currentLine += c; } } } header = ""; client.stop(); Serial.println("Client disconnected."); Serial.println(""); } } } void Task2code(void *pvParameters) { Wire.setPins(18,19); display.begin(SSD1306_SWITCHCAPVCC, 0x3C); display.clearDisplay(); display.setTextColor(WHITE); display.setTextSize(1.5); display.setCursor(0, 0); WiFi.begin(wifi_ssid.c_str(), wifi_pass.c_str()); configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); for (;;) {
delay(100); display.clearDisplay(); if(FAN=="off") { bmp_display(); display.clearDisplay(); delay(500);
printLocalTime(); display.display(); delay(3000); Serial.println("ononon"); }else { display.setCursor(0, 0); display.print("The fan is turned on and cannot be used."); display.display(); delay(3000); Serial.println("offoff"); } } } void printLocalTime() { struct tm timeinfo; if (!getLocalTime(&timeinfo)) { display.println("Failed to obtain time"); return; } int chk = DHT11.read(DHT11PIN);
Serial.print("Read sensor: "); switch (chk) { case DHTLIB_OK: Serial.println("OK"); DHT11_STATUS = 0; break; case DHTLIB_ERROR_CHECKSUM: Serial.println("Checksum error"); break; case DHTLIB_ERROR_TIMEOUT: Serial.println("Time out error"); break; default: Serial.println("Unknown error"); break; } if((int)DHT11.humidity > 100 || (int)DHT11.temperature > 100 ) { for(int q = 0 ;q < 10; q++) { int chk = DHT11.read(DHT11PIN); Serial.print("Humidity1 (%): "); Serial.println((float)DHT11.humidity, 2);
Serial.print("Temperature1 °C): "); Serial.println((float)DHT11.temperature, 2); if((int)DHT11.humidity < 100 && (int)DHT11.temperature < 100 ) { DHT11_STATUS = 0; break; } DHT11_STATUS = 1; } DHT11_COUNT++; if(DHT11_COUNT == 5 ) { digitalWrite(17, LOW); delay(4000); digitalWrite(17, HIGH); DHT11_COUNT =0; } }
Serial.print("Humidity (%): "); Serial.println((float)DHT11.humidity, 2);
Serial.print("Temperature °C): "); Serial.println((float)DHT11.temperature, 2); display.setCursor(0, 0); display.println(&timeinfo, "%F"); display.setCursor(42, 8); display.println(&timeinfo, "%A"); display.setCursor(0, 16); if( DHT11_STATUS == 1) { display.print("Humidity:"); display.println("error"); display.setCursor(0, 24); display.print("Temperature:"); display.println("error"); }else { display.print("Humidity:"); display.println((float)DHT11.humidity); display.setCursor(0, 24); display.print("Temperature:"); display.println((float)DHT11.temperature); } display.setCursor(80, 0); display.println(&timeinfo, "%T"); display.display(); display.setCursor(0, 32); display.print("IP:"); display.println(WiFi.localIP()); display.drawBitmap(0,40,Heart_16x16,16,16,1); display.setCursor(30, 44); display.print("JGX Desktop"); display.drawBitmap(112,40,Heart_16x16,16,16,1); }
void bmp_display(void) { display.stopscroll(); display.clearDisplay(); display.drawBitmap(0,0,BMP_5, 128, 64, 1); display.display(); delay(100); display.clearDisplay(); display.drawBitmap(0,0,BMP_6, 128, 64, 1); display.display(); delay(100); display.clearDisplay(); display.drawBitmap(0,0,BMP_7, 128, 64, 1); display.display(); delay(100); display.clearDisplay(); display.drawBitmap(0,0,BMP_8,128, 64, 1); display.display(); delay(100);
display.clearDisplay(); display.drawBitmap(0,0,BMP_9, 128, 64, 1); display.display(); delay(100); display.clearDisplay(); display.drawBitmap(0,0,BMP_10, 128, 64, 1); display.display(); delay(100); display.clearDisplay(); display.drawBitmap(0,0,BMP_11,128, 64, 1); display.display(); delay(100);
display.clearDisplay(); display.drawBitmap(0,0,BMP_12, 128, 64, 1); display.display(); delay(100);
display.clearDisplay(); display.drawBitmap(0,0,BMP_13,128, 64, 1); display.display(); delay(100);
display.clearDisplay(); display.drawBitmap(0,0,BMP_14, 128, 64, 1); display.display(); delay(100); display.clearDisplay(); display.drawBitmap(0,0,BMP_15, 128, 64, 1); display.display(); delay(100);
display.clearDisplay(); display.drawBitmap(0,0,BMP_16, 128, 64, 1); display.display(); delay(100);
display.clearDisplay(); display.drawBitmap(0,0,BMP_17, 128, 64, 1); display.display(); delay(100); }
void wifi_Config() { Serial.println("scan start"); int n = WiFi.scanNetworks(); Serial.println("scan done"); if (n == 0) { Serial.println("no networks found"); scanNetworksID = "no networks found"; } else { Serial.print(n); Serial.println(" networks found"); for (int i = 0; i < n; ++i) { Serial.print(i + 1); Serial.print(": "); Serial.print(WiFi.SSID(i)); Serial.print(" ("); Serial.print(WiFi.RSSI(i)); Serial.print(")"); Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*"); scanNetworksID += "<P>" + WiFi.SSID(i) + "</P>"; delay(10); } } Serial.println("");
WiFi.mode(WIFI_AP); boolean result = WiFi.softAP(AP_SSID, ""); if (result) { IPAddress myIP = WiFi.softAPIP(); Serial.println(""); Serial.print("Soft-AP IP address = "); Serial.println(myIP); Serial.println(String("MAC address = ") + WiFi.softAPmacAddress().c_str()); Serial.println("waiting ..."); } else { Serial.println("WiFiAP Failed"); delay(3000); ESP.restart(); }
if (MDNS.begin("esp32")) { Serial.println("MDNS responder started"); }
server.on("/", []() { server.send(200, "text/html", ROOT_HTML + scanNetworksID + "</body></html>"); });
server.on("/connect", []() {
server.send(200, "text/html", "<html><body><font size=\"10\">successd,wifi connecting...<br />Please close this page manually.</font></body></html>");
WiFi.softAPdisconnect(true); wifi_ssid = server.arg("ssid"); wifi_pass = server.arg("pass"); server.close(); WiFi.softAPdisconnect(); Serial.println("WiFi Connect SSID:" + wifi_ssid + " PASS:" + wifi_pass);
WiFi.mode(WIFI_STA); WiFi.begin(wifi_ssid.c_str(), wifi_pass.c_str()); uint8_t Connect_time = 0; while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); Connect_time ++; resetesp = 1; if (Connect_time > 80) { Serial.println("Connection timeout, check input is correct or try again later!"); delay(3000); ESP.restart(); } } Serial.println(""); Serial.println("WIFI Config Success"); Serial.printf("SSID:%s", WiFi.SSID().c_str()); Serial.print(" LocalIP:"); Serial.print(WiFi.localIP()); Serial.println(""); }); server.begin(); }
bool AutoConfig() { WiFi.begin(); for (int i = 0; i < 20; i++) { int wstatus = WiFi.status(); if (wstatus == WL_CONNECTED) { Serial.println("WIFI SmartConfig Success"); Serial.printf("SSID:%s", WiFi.SSID().c_str()); Serial.printf(", PSW:%s\r\n", WiFi.psk().c_str()); Serial.print("LocalIP:"); Serial.print(WiFi.localIP()); Serial.print(" ,GateIP:"); Serial.println(WiFi.gatewayIP()); return true; } else { Serial.print("WIFI AutoConfig Waiting......"); Serial.println(wstatus); delay(1000); } } Serial.println("WIFI AutoConfig Faild!" ); return false; }
|