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

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

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

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

NodeMCU Esp8266สายไฟRelay 4ch แบบ Active LOWจอแสดงผลLCD I2C

สำหรับน้องๆที่ยังไม่ติดตั้ง 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://9arduino.nisit.net/download/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

 

*/

#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://9arduino.nisit.net/download/esp8266.apk

หรือจะ Scan QR Code

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

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

ผลการทดลอง 

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

Flowchart

โปรเจค Arduino ควบคุมพัดลมตามอุณหภูมิ

โปรเจคเครื่องรดน้ำต้นไม้อัตโนมัติ