#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
#include <WiFiManager.h>
#include <AceButton.h>
using namespace ace_button;
// define the GPIO connected with Relays and switches
#define RelayPin1 5 //D1
#define RelayPin2 4 //D2
#define RelayPin3 14 //D5
#define RelayPin4 12 //D6
#define SwitchPin1 1 //TX
#define SwitchPin2 0 //D3
#define SwitchPin3 13 //D7
#define SwitchPin4 3 //RX
#define wifiLed 16 //D0
//Change the virtual pins according the rooms
#define VPIN_BUTTON_1 V5
#define VPIN_BUTTON_2 V6
#define VPIN_BUTTON_3 V7
#define VPIN_BUTTON_4 V8
int toggleState_1 = 1; //Define integer to remember the toggle state for relay 1
int toggleState_2 = 1; //Define integer to remember the toggle state for relay 2
int toggleState_3 = 1; //Define integer to remember the toggle state for relay 3
int toggleState_4 = 1; //Define integer to remember the toggle state for relay 4
int wifiFlag = 0;
ButtonConfig config1;
AceButton button1(&config1);
ButtonConfig config2;
AceButton button2(&config2);
ButtonConfig config3;
AceButton button3(&config3);
ButtonConfig config4;
AceButton button4(&config4);
void handleEvent1(AceButton*, uint8_t, uint8_t);
void handleEvent2(AceButton*, uint8_t, uint8_t);
void handleEvent3(AceButton*, uint8_t, uint8_t);
void handleEvent4(AceButton*, uint8_t, uint8_t);
char auth[] = "rDc0CXAgW_PUfFjYT910etN3h-wy6KCP";
char server[] = "blynk.iot-cm.com";
int port = 8080;
BlynkTimer timer;
// When App button is pushed - switch the state
BLYNK_WRITE(VPIN_BUTTON_1) {
toggleState_1 = param.asInt();
digitalWrite(RelayPin1, toggleState_1);
}
BLYNK_WRITE(VPIN_BUTTON_2) {
toggleState_2 = param.asInt();
digitalWrite(RelayPin2, toggleState_2);
}
BLYNK_WRITE(VPIN_BUTTON_3) {
toggleState_3 = param.asInt();
digitalWrite(RelayPin3, toggleState_3);
}
BLYNK_WRITE(VPIN_BUTTON_4) {
toggleState_4 = param.asInt();
digitalWrite(RelayPin4, toggleState_4);
}
void checkBlynkStatus() { // called every 3 seconds by SimpleTimer
bool isconnected = Blynk.connected();
if (isconnected == false) {
wifiFlag = 1;
Serial.println("Blynk Not Connected");
}
if (isconnected == true) {
wifiFlag = 0;
Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1);
Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2);
Blynk.virtualWrite(VPIN_BUTTON_3, toggleState_3);
Blynk.virtualWrite(VPIN_BUTTON_4, toggleState_4);
Serial.println("Blynk Connected");
}
}
void setup()
{
Serial.begin(115200);
WiFiManager wifiManager;
wifiManager.autoConnect("AutoConnectAP"); // Create an Access Point named "AutoConnectAP"
Serial.println("Connected to Wi-Fi!");
Blynk.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str(), server, port);
Serial.println("Blynk connected!");
pinMode(RelayPin1, OUTPUT);
pinMode(RelayPin2, OUTPUT);
pinMode(RelayPin3, OUTPUT);
pinMode(RelayPin4, OUTPUT);
pinMode(wifiLed, OUTPUT);
pinMode(SwitchPin1, INPUT_PULLUP);
pinMode(SwitchPin2, INPUT_PULLUP);
pinMode(SwitchPin3, INPUT_PULLUP);
pinMode(SwitchPin4, INPUT_PULLUP);
//During Starting all Relays should TURN OFF
digitalWrite(RelayPin1, toggleState_1);
digitalWrite(RelayPin2, toggleState_2);
digitalWrite(RelayPin3, toggleState_3);
digitalWrite(RelayPin4, toggleState_4);
digitalWrite(wifiLed, HIGH);
config1.setEventHandler(button1Handler);
config2.setEventHandler(button2Handler);
config3.setEventHandler(button3Handler);
config4.setEventHandler(button4Handler);
button1.init(SwitchPin1);
button3.init(SwitchPin3);
button4.init(SwitchPin4);
timer.setInterval(3000L, checkBlynkStatus); // check if Blynk server is connected every 3 seconds
delay(1000);
Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1);
Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2);
Blynk.virtualWrite(VPIN_BUTTON_3, toggleState_3);
Blynk.virtualWrite(VPIN_BUTTON_4, toggleState_4);
}
void loop()
{
if (WiFi.status() != WL_CONNECTED)
{
//Serial.println("WiFi Not Connected");
digitalWrite(wifiLed, HIGH);
}
else
{
//+Serial.println("WiFi Connected");
digitalWrite(wifiLed, LOW); //Turn on WiFi LED
Blynk.run();
}
timer.run(); // Initiates SimpleTimer
button1.check();
button2.check();
button3.check();
button4.check();
}
void button1Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
Serial.println("EVENT1");
switch (eventType) {
case AceButton::kEventPressed:
Serial.println("kEventPressed");
toggleState_1 = 0;
Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1); // Update Button Widget
digitalWrite(RelayPin1, LOW);
break;
case AceButton::kEventReleased:
Serial.println("kEventReleased");
toggleState_1 = 1;
Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1); // Update Button Widget
digitalWrite(RelayPin1, HIGH);
break;
}
}
void button2Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
Serial.println("EVENT2");
switch (eventType) {
case AceButton::kEventPressed:
Serial.println("kEventPressed");
toggleState_2 = 0;
Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2);
digitalWrite(RelayPin2, LOW);
break;
case AceButton::kEventReleased:
Serial.println("kEventReleased");
toggleState_2 = 1;
Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2);
digitalWrite(RelayPin2, HIGH);
break;
}
}
void button3Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
Serial.println("EVENT3");
switch (eventType) {
case AceButton::kEventPressed:
Serial.println("kEventPressed");
toggleState_3 = 0;
Blynk.virtualWrite(VPIN_BUTTON_3, toggleState_3);
digitalWrite(RelayPin3, LOW);
break;
case AceButton::kEventReleased:
Serial.println("kEventReleased");
toggleState_3 = 1;
Blynk.virtualWrite(VPIN_BUTTON_3, toggleState_3);
digitalWrite(RelayPin3, HIGH);
break;
}
}
void button4Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
Serial.println("EVENT4");
switch (eventType) {
case AceButton::kEventPressed:
Serial.println("kEventPressed");
toggleState_4 = 0;
Blynk.virtualWrite(VPIN_BUTTON_4, toggleState_4);
digitalWrite(RelayPin4, LOW);
break;
case AceButton::kEventReleased:
Serial.println("kEventReleased");
toggleState_4 = 1;
Blynk.virtualWrite(VPIN_BUTTON_4, toggleState_4);
digitalWrite(RelayPin4, HIGH);
break;
}
}
เพิ่มฟังก์ชั่นการตั้งค่า token ใน WiFiManager เราสามารถใช้พารามิเตอร์แบบกำหนดเองใน WiFiManager โดยเพิ่มโค้ดสำหรับการตั้งค่า token ในฟังก์ชั่น setup และใช้พารามิเตอร์ WiFiManager เพื่อตั้งค่า token ที่จะใช้ในการเชื่อมต่อกับ Blynk
ตัวอย่างโค้ด
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
#include <WiFiManager.h>
#include <AceButton.h>
using namespace ace_button;
// define the GPIO connected with Relays and switches
#define RelayPin1 5 //D1
#define RelayPin2 4 //D2
#define RelayPin3 14 //D5
#define RelayPin4 12 //D6
#define SwitchPin1 1 //TX
#define SwitchPin2 0 //D3
#define SwitchPin3 13 //D7
#define SwitchPin4 3 //RX
#define wifiLed 16 //D0
//Change the virtual pins according the rooms
#define VPIN_BUTTON_1 V5
#define VPIN_BUTTON_2 V6
#define VPIN_BUTTON_3 V7
#define VPIN_BUTTON_4 V8
int toggleState_1 = 1; //Define integer to remember the toggle state for relay 1
int toggleState_2 = 1; //Define integer to remember the toggle state for relay 2
int toggleState_3 = 1; //Define integer to remember the toggle state for relay 3
int toggleState_4 = 1; //Define integer to remember the toggle state for relay 4
int wifiFlag = 0;
ButtonConfig config1;
AceButton button1(&config1);
ButtonConfig config2;
AceButton button2(&config2);
ButtonConfig config3;
AceButton button3(&config3);
ButtonConfig config4;
AceButton button4(&config4);
void handleEvent1(AceButton*, uint8_t, uint8_t);
void handleEvent2(AceButton*, uint8_t, uint8_t);
void handleEvent3(AceButton*, uint8_t, uint8_t);
void handleEvent4(AceButton*, uint8_t, uint8_t);
char auth[34] = "rDc0CXAgW_PUfFjYT910etN3h-wy6KCP"; // default token
char server[] = "blynk.iot-cm.com";
int port = 8080;
BlynkTimer timer;
// When App button is pushed - switch the state
BLYNK_WRITE(VPIN_BUTTON_1) {
toggleState_1 = param.asInt();
digitalWrite(RelayPin1, toggleState_1);
}
BLYNK_WRITE(VPIN_BUTTON_2) {
toggleState_2 = param.asInt();
digitalWrite(RelayPin2, toggleState_2);
}
BLYNK_WRITE(VPIN_BUTTON_3) {
toggleState_3 = param.asInt();
digitalWrite(RelayPin3, toggleState_3);
}
BLYNK_WRITE(VPIN_BUTTON_4) {
toggleState_4 = param.asInt();
digitalWrite(RelayPin4, toggleState_4);
}
void checkBlynkStatus() { // called every 3 seconds by SimpleTimer
bool isconnected = Blynk.connected();
if (isconnected == false) {
wifiFlag = 1;
Serial.println("Blynk Not Connected");
}
if (isconnected == true) {
wifiFlag = 0;
Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1);
Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2);
Blynk.virtualWrite(VPIN_BUTTON_3, toggleState_3);
Blynk.virtualWrite(VPIN_BUTTON_4, toggleState_4);
Serial.println("Blynk Connected");
}
}
void setup()
{
Serial.begin(115200);
WiFiManager wifiManager;
// Custom parameters
WiFiManagerParameter custom_blynk_token("token", "Blynk Token", auth, 34);
// Add custom parameters to WiFiManager
wifiManager.addParameter(&custom_blynk_token);
// Create an Access Point named "AutoConnectAP"
wifiManager.autoConnect("AutoConnectAP");
// Save token from WiFiManager
strcpy(auth, custom_blynk_token.getValue());
Serial.println("Connected to Wi-Fi!");
Blynk.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str(), server, port);
Serial.println("Blynk connected!");
pinMode(RelayPin1, OUTPUT);
pinMode(RelayPin2, OUTPUT);
pinMode(RelayPin3, OUTPUT);
pinMode(RelayPin4, OUTPUT);
pinMode(wifiLed, OUTPUT);
pinMode(SwitchPin1, INPUT_PULLUP);
pinMode(SwitchPin2, INPUT_PULLUP);
pinMode(SwitchPin3, INPUT_PULLUP);
pinMode(SwitchPin4, INPUT_PULLUP);
//During Starting all Relays should TURN OFF
digitalWrite(RelayPin1, toggleState_1);
digitalWrite(RelayPin2, toggleState_2);
digitalWrite(RelayPin3, toggleState_3);
digitalWrite(RelayPin4, toggleState_4);
digitalWrite(wifiLed, HIGH);
config1.setEventHandler(button1Handler);
config2.setEventHandler(button2Handler);
config3.setEventHandler(button3Handler);
config4.setEventHandler(button4Handler);
button1.init(SwitchPin1);
button2.init(SwitchPin2);
button3.init(SwitchPin3);
button4.init(SwitchPin4);
timer.setInterval(3000L, checkBlynkStatus); // check if Blynk server is connected every 3 seconds
delay(1000);
Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1);
Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2);
Blynk.virtualWrite(VPIN_BUTTON_3, toggleState_3);
Blynk.virtualWrite(VPIN_BUTTON_4, toggleState_4);
}
void loop()
{
if (WiFi.status() != WL_CONNECTED)
{
digitalWrite(wifiLed, HIGH);
}
else
{
digitalWrite(wifiLed, LOW); //Turn on WiFi LED
Blynk.run();
}
timer.run(); // Initiates SimpleTimer
button1.check();
button2.check();
button3.check();
button4.check();
}
void button1Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
Serial.println("EVENT1");
switch (eventType) {
case AceButton::kEventPressed:
Serial.println("kEventPressed");
toggleState_1 = 0;
Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1); // Update Button Widget
digitalWrite(RelayPin1, LOW);
break;
case AceButton::kEventReleased:
Serial.println("kEventReleased");
toggleState_1 = 1;
Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1); // Update Button Widget
digitalWrite(RelayPin1, HIGH);
break;
}
}
void button2Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
Serial.println("EVENT2");
switch (eventType) {
case AceButton::kEventPressed:
Serial.println("kEventPressed");
toggleState_2 = 0;
Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2);
digitalWrite(RelayPin2, LOW);
break;
case AceButton::kEventReleased:
Serial.println("kEventReleased");
toggleState_2 = 1;
Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2);
digitalWrite(RelayPin2, HIGH);
break;
}
}
void button3Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
Serial.println("EVENT3");
switch (eventType) {
case AceButton::kEventPressed:
Serial.println("kEventPressed");
toggleState_3 = 0;
Blynk.virtualWrite(VPIN_BUTTON_3, toggleState_3);
digitalWrite(RelayPin3, LOW);
break;
case AceButton::kEventReleased:
Serial.println("kEventReleased");
toggleState_3 = 1;
Blynk.virtualWrite(VPIN_BUTTON_3, toggleState_3);
digitalWrite(RelayPin3, HIGH);
break;
}
}
void button4Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
Serial.println("EVENT4");
switch (eventType) {
case AceButton::kEventPressed:
Serial.println("kEventPressed");
toggleState_4 = 0;
Blynk.virtualWrite(VPIN_BUTTON_4, toggleState_4);
digitalWrite(RelayPin4, LOW);
break;
case AceButton::kEventReleased:
Serial.println("kEventReleased");
toggleState_4 = 1;
Blynk.virtualWrite(VPIN_BUTTON_4, toggleState_4);
digitalWrite(RelayPin4, HIGH);
break;
}
}
ในโค้ดนี้ผมเพิ่มพารามิเตอร์แบบกำหนดเองสำหรับ token ของ Blynk โดยใช้ WiFiManagerParameter
และเพิ่มพารามิเตอร์นี้ลงใน WiFiManager
ในฟังก์ชั่น setup
และบันทึก token จาก WiFiManager
เพื่อใช้ในการเชื่อมต่อกับ Blynk