...
ESP-01 모듈에 서버를 실행시키고
핸드폰으로 웹페이지를 열어서
ESP모듈에 부착된 LED를 켜고 끄는
프로그램
주의 : BuiltinLED = 1 이면 TxD가 죽으므로 시리얼모니터링이 안된다!!
// ============================================================================================
// ============================================================================================ // IOT based home automation project // https://circuits4you.com/2016/05/19/iot-based-home-automation-project/ // ============================================================================================ // ESP8266 보드 설치하기 // https://learn.sparkfun.com/tutorials/esp8266-thing-hookup-guide/installing-the-esp8266-arduino-addon // To begin, we’ll need to update the board manager with a custom URL. Open up Arduino, // then go to the Preferences (File > Preferences). Then, towards the bottom of the window, // copy this URL into the “Additional Board Manager URLs” text box: // // Hit OK. Then navigate to the Board Manager by going to Tools > Boards > Boards Manager. // There should be a couple new entries in addition to the standard Arduino boards. // Look for esp8266. Click on that entry, then select Install. // SSD1306 Lib for ESP8266 // https://randomnerdtutorials.com/esp8266-0-96-inch-oled-display-with-arduino-ide/ // https://github.com/squix78/esp8266-oled-ssd1306/archive/master.zip // unzip to : C:\Users\turbocrazy\Documents\Arduino\libraries // other lib not tested // https://github.com/ThingPulse/esp8266-oled-ssd1306 // ============================================================================================ // ESP-01 pin usage : GPIO 0/2 - OLED, RxD(GPIO 3)=DHT, TxD(GPIO 1) = BLUE LED or Serial Comm // ============================================================================================#include
#include #include #include "HomePage.h" // Include the correct display library // For a connection via I2C using Wire include #include // Only needed for Arduino 1.6.5 and earlier #include "SSD1306.h" // alias for #include "SSD1306Wire.h" #include #define DHTPIN 3 // what pin we're connected to RxD = GPIO 3 #define DHTTYPE DHT11 // DHT 11 // ============================================================================================ const char *ssid = "iptimexxxx"; // Set these to your desired credentials. const char *password = "xxxxxxxxx"; const int BuiltInLED = 1; // built in BLUE = TxD --> GPIO 1, RxD = GPIO 3 int long counter = 0; String L1Status, Temperature, Humidity, myLocalIP; ESP8266WebServer server(80); // server on port 80 IPAddress myIP; SSD1306 display(0x3c, 0, 2); // Init OLED - ESP-01 : GPIO 0 = SDA, GPIO 2 = SCL DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor for normal 16mhz Arduino // ============================================================================================ void FunctionRoot() { // handles main page 192.168.4.1 String s = MAIN_page; s.replace("@@TEMP@@", Temperature); // only update at Home reflash s.replace("@@HUMID@@", Humidity); s.replace("@@L1@@", L1Status); server.send(200, "text/html", s); } // ============================================================================================ void FunctionForm() { // Handle Set Date/Time Settings String t_state = server.arg("submit"); // Temperature = String(t); // it is already set in 'displayTempHumid' if(t_state=="ON1") { // Change Load-1 State as per request L1Status="ON"; digitalWrite(BuiltInLED, LOW); // Load1 Turned on } if(t_state=="OFF1") { L1Status="OFF"; digitalWrite(BuiltInLED, HIGH); // Load1 Turned off } server.sendHeader("Location", "/"); // This Line Keeps It on Same Page server.send(302, "text/plain", "Updated-- Press Back Button"); delay(500); } // ============================================================================================ void displayTempHumid(){ // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); float t = dht.readTemperature(); // Read temperature as Celsius float f = dht.readTemperature(true); // Read temperature as Fahrenheit Temperature = String(t); Humidity = String(h); if (isnan(h) || isnan(t) || isnan(f)){ // Check if any reads failed and exit to try again display.clear(); // clearing the display display.drawString(5,0, "Failed DHT"); return; } Serial.println("Humidity: " + String(h) + " Temp: " + String(t)); //Serial.println(myLocalIP); // IP address assigned to your ESP display.clear(); display.drawString(0, 0, "Server up !! " + String(counter++) + "<>"); display.drawString(0, 16, "IP :" + myLocalIP); display.drawString(0, 24, "Humidity: " + String(h) + "%\t"); display.drawString(0, 32, "Temp: " + Temperature + "C"); display.drawString(0, 40, "Temp: " + String(f) + "F"); display.display(); } // ============================================================================================ void setup() { // Power on setup delay(1000); display.init(); // Initialising the UI will init the display too. display.flipScreenVertically(); display.setFont(ArialMT_Plain_10); // small size font display.setTextAlignment(TEXT_ALIGN_LEFT); display.clear(); display.drawString(0, 0, "Server Init ..."); display.display(); Serial.begin(115200); // Arduino monitor dht.begin(); // initialize dht WiFi.begin(ssid, password); // Connect to WiFi router : orginal code not work while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } //myLocalIP = String(WiFi.localIP()); // IP address assigned to your ESP - strange ??? myIP = WiFi.localIP(); char buf[18]; // https://gist.github.com/loosak/76019faaefd5409fca67 sprintf(buf, "%d.%d.%d.%d", myIP[0], myIP[1], myIP[2], myIP[3]); myLocalIP = String(buf); Serial.println(""); Serial.println("IP: " + myLocalIP); server.on("/", FunctionRoot); server.on("/form", FunctionForm); server.begin(); display.clear(); display.drawString(0, 0, "Server up !!"); display.drawString(0, 16, "IP: " + myLocalIP); display.display(); delay(1000); pinMode(BuiltInLED, OUTPUT); // use Built in BLUE LED digitalWrite(LED_BUILTIN, LOW); delay(500); digitalWrite(LED_BUILTIN, HIGH); delay(500); } // ============================================================================================ void loop() { // Main Program Loop server.handleClient(); displayTempHumid(); delay(1000); } // ============================================================================================
...
실행한 모습
ESP-01모듈에 연결된 DHT-11 센서에서
온도와 습도를 읽어서 표시하고
화면에서 버튼을 누르면
모듈에 내장된 BLUE LED가 ON/OFF 하게 된다
'IoT_ESP8266' 카테고리의 다른 글
ESp-01 : 원격 TV ON/OFF 리모콘 제어 (1) | 2019.01.02 |
---|---|
ESP-01 : 서버 LED ON/OFF - 고정 IP (0) | 2019.01.01 |
ESP-8266 : 서버 - LED/Relay 제어 (0) | 2019.01.01 |
ESP8266 - ESP-01 : 온도및 습도 모니터링 OLED/Web (0) | 2018.12.30 |
ESP-01 with OLED (1) | 2018.12.30 |