งานที่ 11

 

NodeMCU Esp8266 เปิดปิดไฟควบคุมผ่าน App มือถือ

สวัสดีครับวันนี้ ทางร้านก็มี โปรเจคเล็กๆ มาแจกน้องๆน่ะครับเกี่ยวกับการเขียน App ควบคุมระบบ เปิดปิดไฟ แบบง่ายสุดๆ (ต้มน้ำฉีกซองทานได้เลย) ด้วย Nodemcu Esp8266 และ App บน Android มาเริ่มกันเลย

อุปกรณ์ มีดังต่อไปนี้ครับ

สำหรับน้องๆที่ยังไม่ติดตั้ง Arduino ให้สามารถใช้งาน NodeMCU คลิกเลยครับ

มาเริ่มต้นกันเลยครับ

1. ต่อวงจรดังนี้

ตำแหน่งขา NodeMCU Esp8266อุปกรณ์
ขา D1SCL
ขา D2SDA 
ขา VinVCC ของจอ LCD
ขา GNDGND ของจอ LCD
ขา VinVCC ของ Relay
ขา GNDGND ของ Relay
ขา D0IN1 ของ Relay
ขา D5IN2 ของ Relay
ขา D6IN3 ของ Relay
ขา D7IN4 ของ Relay

2. เมื่อน้องๆ ประกอบก็เริ่มเขียนโปรแกรม ครับ

Add Library ตัวนี้ด้วยน่ะครับ 

Download Library LiquidCrystal_I2C : http://download.ab.in.th/download.php?file=Arduino-LiquidCrystal-I2C-library-master.zip

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");
}

3. จากนั้นติดตั้ง App โดย Download ได้ที่ : http://download.ab.in.th/download.php?file=App-nodemcu-build620821.apk

หากไม่สามารถโหลด App ให้ทำการ Save ไฟล์ apk ลง Sd card ก่อนแล้วค่อยติดตั้ง

หน้าตาโปรแกรมของทางร้านครับ

หากน้องๆ ต้องการ ไฟล์ต้นฉบับ นามสกุล aia ของ App Invertor สามารถติดต่อทางร้านได้เลยครับ ยินดีให้นำไปพัฒนาต่อได้ครับ

ความคิดเห็น