ควบคุมบอร์ดรีเลย์ ESP8266 (HW-622) ผ่าน Server Blynk |
|
|
|
วงจรบอร์ด ESP8266 WIFI
รายละเอียดขั้วต่อใช้งาน
ที่มา: ucexperimen
การต่อสายกับบอร์ดโปรแกรม YP-05
การต่อสายกับบอร์ดโปรแกรม CP2102
* ขาจั๊มเปอร์เมื่อทำการโปรแกรมเสร็จให้ถอดออกก่อนนำไปใช้งาน
ตัวอย่างโปรแกรมควบคุมบอร์ดรีเลย์ #1
#define BLYNK_PRINT Serial #include <ESP8266WiFi.h> #include <BlynkSimpleEsp8266.h> // Blynk app authentication code. char auth[] = "***"; char ssid[] = "***"; //<---SSID name char pass[] = "***"; //<---Password WiFiServer server(80); void setup() { Serial.begin(9600); Blynk.begin( auth, ssid, pass); // Initiate Blynk conection. // This function runs every time Blynk connection is established. Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(50); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP()); server.begin(); } // Main program loop. void loop() { Blynk.run(); } ตัวอย่างโปรแกรมควบคุมบอร์ดรีเลย์ FIX IP #2
#define BLYNK_PRINT Serial #include <ESP8266WiFi.h> #include <BlynkSimpleEsp8266.h> // Blynk app authentication code. char auth[] = "***"; char ssid[] = "***"; //<---SSID name char pass[] = "***"; //<---Password /************************| * ตั้งค่า Static IP ตรงนี้ | ************************/ IPAddress local_IP(192, 168, 1, 5); IPAddress gateway(192, 168, 1, 1); IPAddress subnet(255, 255, 255, 0); IPAddress primaryDNS(8, 8, 8, 8); IPAddress secondaryDNS(8, 8, 4, 4); void setup() { Serial.begin(9600); if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) { Serial.println("STA Failed to configure"); } // This function runs every time Blynk connection is established. Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(50); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP()); Blynk.config(auth); while (Blynk.connect(1000) == false) { } } // Main program loop. void loop() { Blynk.run(); } ตัวอย่างโปรแกรมควบคุมบอร์ดรีเลย #3
* Notes: * V0: button configured as a switch. * V1: led. * V2: led. #include <ESP8266WiFi.h> #include <BlynkSimpleEsp8266.h> #define ESP8266_GPIO2 2 // Blue LED. #define ESP8266_GPIO4 4 // Relay control. #define ESP8266_GPIO5 5 // Optocoupler input. #define LED_PIN ESP8266_GPIO2 // Blynk app authentication code. char auth[] = "***"; const char ssid[] = "***"; // Wifi SSID. const char password[] = "***"; // Wifi password. // Flag for sync on re-connection. bool isFirstConnect = true; volatile int relayState = LOW; // Blynk app pushbutton status. volatile int inputState = LOW; // Input pin state. void setup() { pinMode( ESP8266_GPIO4, OUTPUT ); // Relay control pin. pinMode( ESP8266_GPIO5, INPUT_PULLUP ); // Input pin. pinMode( LED_PIN, OUTPUT ); // ESP8266 module blue LED. digitalWrite( LED_PIN, LOW ); // Turn on LED. Blynk.begin( auth, ssid, password ); // Initiate Blynk conection. digitalWrite( LED_PIN, HIGH ); // Turn off LED. } // This function runs every time Blynk connection is established. BLYNK_CONNECTED() { if ( isFirstConnect ) { Blynk.syncAll(); isFirstConnect = false; } } // Sync input LED. BLYNK_READ( V2 ) { CheckInput(); } // Blynk app relay command. BLYNK_WRITE( V0 ) { if ( param.asInt() != relayState ) { relayState = !relayState; // Toggle state. digitalWrite( ESP8266_GPIO4, relayState ); // Relay control pin. Blynk.virtualWrite( V1, relayState*255 ); // Set Blynk app LED. } } // Debounce input pin. int DebouncePin( void ) { // Read input pin. if ( digitalRead( ESP8266_GPIO5 ) == HIGH ) { // Debounce input. delay( 25 ); if ( digitalRead( ESP8266_GPIO5 ) == HIGH ) return HIGH; } return LOW; } // Set LED based upon state of input pin. void CheckInput( void ) { if ( DebouncePin() != inputState ) { Blynk.virtualWrite( V2, inputState*255 ); inputState = !inputState; } } // Main program loop. void loop() { Blynk.run(); CheckInput(); } วิดีโอทดสอบสั่งงาน CP1202: USB 2.0 to TTL UART Module Serial Converter www.lazada.co.th
HW-622: ESP8266 Relay Module HW-622 www.lazada.co.th
YP-05: FTDI FT232RL 3.3V 5.0V USB to TTL Serial www.lazada.co.th
Blynking an IoT Yunshan ESP8266 250V 10A AC/DC WIFI Network Relay Module
How to do Simple Home Automation with Amazon Alexa || ESP8266