NodeMCU Esp8266 เปิดปิดไฟควบคุมผ่าน App มือถือ
สวัสดีครับวันนี้ ทางร้านก็มี โปรเจคเล็กๆ มาแจกน้องๆน่ะครับเกี่ยวกับการเขียน App ควบคุมระบบ เปิดปิดไฟ แบบง่ายสุดๆ (ต้มน้ำฉีกซองทานได้เลย) ด้วย Nodemcu Esp8266 และ App บน Android มาเริ่มกันเลย
อุปกรณ์ มีดังต่อไปนี้ครับ
มาเริ่มต้นกันเลยครับ
1. ต่อวงจรดังนี้
ตำแหน่งขา NodeMCU Esp8266 | อุปกรณ์ |
ขา D1 | SCL |
ขา D2 | SDA |
ขา Vin | VCC ของจอ LCD |
ขา GND | GND ของจอ LCD |
ขา Vin | VCC ของ Relay |
ขา GND | GND ของ Relay |
ขา D0 | IN1 ของ Relay |
ขา D5 | IN2 ของ Relay |
ขา D6 | IN3 ของ Relay |
ขา D7 | IN4 ของ Relay |
2. เมื่อน้องๆ ประกอบก็เริ่มเขียนโปรแกรม ครับ
Add Library ตัวนี้ด้วยน่ะครับ
Code ตัวอย่าง
| /* ตัวอย่าง โดย 9Arduino.com พัฒนาเอง |
| การต่อสาย |
| ขา D1 SCL |
| ขา D2 SDA |
| ขา Vin VCC ของจอ LCD |
| ขา GND GND ของจอ LCD |
| ขา Vin VCC ของ Relay |
| ขา GND GND ของ Relay |
| ขา D0 IN1 ของ Relay |
| ขา D5 IN2 ของ Relay |
| ขา D6 IN3 ของ Relay |
| ขา D7 IN4 ของ Relay |
| จากบทความ Link https://www.ab.in.th/b/11 |
| |
| สำหรับน้องๆที่ต้องการไฟล์ App ไปพัฒนาต่อสามารถ ขอไฟล์เพิ่มเติมได้ที่ https://www.ab.in.th/contactus |
| */ |
|
|
| #include <ESP8266WiFi.h> |
| #include <Wire.h> |
| #include <LiquidCrystal_I2C.h> |
|
|
| LiquidCrystal_I2C lcd(0x3f, 16, 02); |
|
|
| const char* ssid = "9arduino"; // ชื่อ SSID Wifi |
| const char* password = "1234567890"; // รหัส Password Wifi |
|
|
| WiFiServer server(80); |
|
|
| int val1 = 1; |
| int val2 = 1; |
| int val3 = 1; |
| int val4 = 1; |
|
|
| void setup() { |
| Serial.begin(9600); |
| delay(10); |
| lcd.begin(); |
| lcd.backlight(); |
| lcd.print("Server Off.."); |
| lcd.setCursor(0, 1); |
| |
| pinMode(D0, OUTPUT); |
| pinMode(D5, OUTPUT); |
| pinMode(D6, OUTPUT); |
| pinMode(D7, OUTPUT); |
| |
| digitalWrite(D0, 1); |
| digitalWrite(D5, 1); |
| digitalWrite(D6, 1); |
| digitalWrite(D7, 1); |
|
|
| // Connect to WiFi network |
| Serial.println(); |
| Serial.println(); |
| Serial.print("Connecting to "); |
| Serial.println(ssid); |
| |
| WiFi.begin(ssid, password); |
| while (WiFi.status() != WL_CONNECTED) { |
| delay(500); |
| Serial.print("."); |
| } |
|
|
| Serial.println(""); |
| Serial.println("WiFi connected"); |
|
|
| // Start the server |
| server.begin(); |
| Serial.println("Server started"); |
| lcd.clear(); |
| lcd.print("Server On IP :"); |
| lcd.setCursor(0, 1); |
|
|
| // Print the IP address |
| Serial.println(WiFi.localIP()); |
| lcd.print(WiFi.localIP()); // แสดง IP ผ่านจอ |
| lcd.setCursor(0, 2); |
| } |
|
|
| void loop() { |
| // Check if a client has connected |
| WiFiClient client = server.available(); |
| if (!client) { |
| return; |
| } |
|
|
| Serial.println("new client"); |
| while(!client.available()){ |
| delay(1); |
| } |
|
|
| // Read the first line of the request |
| String req = client.readStringUntil('\r'); |
| Serial.println(req); |
| client.flush(); |
|
|
| // Match the request |
| // ch1 |
| if (req.indexOf("/?ch1=0") != -1) |
| val1 = 1; |
| else if (req.indexOf("/?ch1=1") != -1) |
| val1 = 0; |
| // ch2 |
| else if (req.indexOf("/?ch2=0") != -1) |
| val2 = 1; |
| else if (req.indexOf("/?ch2=1") != -1) |
| val2 = 0; |
| // ch3 |
| else if (req.indexOf("/?ch3=0") != -1) |
| val3 = 1; |
| else if (req.indexOf("/?ch3=1") != -1) |
| val3 = 0; |
| // ch4 |
| else if (req.indexOf("/?ch4=0") != -1) |
| val4 = 1; |
| else if (req.indexOf("/?ch4=1") != -1) |
| val4 = 0; |
| else { |
| Serial.println("invalid request"); |
| client.stop(); |
| return; |
| } |
|
|
| // Set GPIO2 according to the request |
| digitalWrite(D0, val1); |
| digitalWrite(D5, val2); |
| digitalWrite(D6, val3); |
| digitalWrite(D7, val4); |
|
|
| client.flush(); |
| // Prepare the response |
| String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n\r\nGPIO is now "; |
| s += "\n"; |
|
|
| client.print(s); |
| delay(1); |
| Serial.println("Client disonnected"); |
| } |
ไม่มีความคิดเห็น:
แสดงความคิดเห็น