项目分享| WIFI&手动,两种方式控制8路继电器
-
本次项目分享如何使用 Blynk 和 ESP32 通过手动开关和智能手机控制 8 通道继电器模块,实现物联网智能家居自动化。
这个项目只需要一个 8 通道继电器模块和 ESP32 板就可以完成这个智能家居项目(如果为了方便可以把LED、按键和供电接口像视频里一样整合到一块PCB上):
1. 通过 WiFi(Blynk App)控制家用电器
2. 通过手动开关控制家用电器
3. 在 Blynk 应用程序中监控实时反馈
4. 无需联网即可手动控制家电
✔ 硬件连接
硬件电路非常简单,如下图:
使用ESP32 的GPIO引脚D23、D22、D21、D19、D18、D5、D25和D26来控制8个继电器。而GPIO 引脚D13、D12、D14、D27、D33、D32、D15和D4连接按钮以手动控制 8 个继电器。
在 Arduino IDE 中使用了INPUT_PULLUP函数,而不是使用上拉电阻。用5V手机充电器给智能继电器模块供电。
✔使用 Blynk 通过无线网络控制继电器
如果 ESP32 模块与 WiFi 连接了,就可以通过 Blynk App 和按钮控制家用电器了。使用 Blynk 应用程序,就意味着可以在任何地方控制、监控继电器的实时状态。
如果 WiFi 不可用,可以通过按钮控制继电器。ESP32 每 3 秒检查一次 WiFi。当 WiFi 可用时,ESP32就会自动连接WiFi。
✔ 为 ESP32 配置 Blynk 应用程序
Blynk是开源的物联网app平台,在我们之前分享的项目中经常出现。
1.从 Google Play 商店或 App Store安装Blynk 应用程序。然后创建一个帐户并点击新项目。
2.为项目命名,选择ESP32 Dev Board ,连接类型为 WiFi。然后点击创建。3. Blynk 将向注册的电子邮件 ID 发送身份验证令牌。点击确定。
在 Blynk 应用程序中添加按钮小部件,8个按钮小部件来控制8个继电器。在这里,我为 8 个按钮使用了虚拟引脚 V1、V2、V3、V4、V5、V6、V7、V8。模式则是切换。
✔ Blynk ESP32 家庭自动化代码
使用Arduino IDE对ESP32 DEV KIT V1进行编程,用过ESP32的人都会进行操作。
在上传代码之前,必须安装 ESP32 开发板和 Blynk 库。然后在代码中输入 WiFi 名称、WiFi 密码和 Blynk Auth Token。
选择DOIT ESP32 DEVKIT V1板和适当的PORT 。然后将代码上传到 ESP32 Board。
✔ 使用 Arduino IDE 对 ESP32 进行编程
在将代码上传到 ESP32 时,如果看到“ Connecting....___ ”文本,则按下ESP32的BOOT按钮。
ESP32代码如下:/**********************************************************************************
* TITLE: ESP32 Home Automation project using Blynk | Internet & Manual control with Realtime Feedback | 8-CHANNEL RELAY
* Click on the following links to learn more.
* YouTube Video: https://youtu.be/o1e5s_5p3FU
* Related Blog : https://iotcircuithub.com/blynk-esp32-home-automation/
* by Tech StudyCell
**********************************************************************************/
/*************************************************************
Update the Preferences > Aditional boards Manager URLs:
https://dl.espressif.com/dl/package_esp32_index.json, http://arduino.esp8266.com/stable/package_esp8266com_index.json
Note: This requires ESP32 support package:
https://github.com/espressif/arduino-esp32
Download latest Blynk library here:
https://github.com/blynkkk/blynk-library/releases/latest
*************************************************************
Change WiFi ssid, pass, and Blynk auth token to run :)
Please be sure to select the right ESP32 module
in the Tools -> Board menu!
*************************************************************/
#include <BlynkSimpleEsp32.h>
BlynkTimer timer;
// define the GPIO connected with Relays and switches
#define RelayPin1 23 //D23
#define RelayPin2 22 //D22
#define RelayPin3 21 //D21
#define RelayPin4 19 //D19
#define RelayPin5 18 //D18
#define RelayPin6 5 //D5
#define RelayPin7 25 //D25
#define RelayPin8 26 //D26
#define SwitchPin1 13 //D13
#define SwitchPin2 12 //D12
#define SwitchPin3 14 //D14
#define SwitchPin4 27 //D27
#define SwitchPin5 33 //D33
#define SwitchPin6 32 //D32
#define SwitchPin7 15 //D15
#define SwitchPin8 4 //D4
#define wifiLed 2 //D2
#define VPIN_BUTTON_1 V1
#define VPIN_BUTTON_2 V2
#define VPIN_BUTTON_3 V3
#define VPIN_BUTTON_4 V4
#define VPIN_BUTTON_5 V5
#define VPIN_BUTTON_6 V6
#define VPIN_BUTTON_7 V7
#define VPIN_BUTTON_8 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 toggleState_5 = 1; //Define integer to remember the toggle state for relay 5
int toggleState_6 = 1; //Define integer to remember the toggle state for relay 6
int toggleState_7 = 1; //Define integer to remember the toggle state for relay 7
int toggleState_8 = 1; //Define integer to remember the toggle state for relay 8
int wifiFlag = 0;
#define AUTH "AUTH TOKEN" // You should get Auth Token in the Blynk App.
#define WIFI_SSID "WIFI NAME" //Enter Wifi Name
#define WIFI_PASS "WIFI PASSWORD" //Enter wifi Password
void relayOnOff(int relay){
switch(relay){
case 1:
if(toggleState_1 == 1){
digitalWrite(RelayPin1, LOW); // turn on relay 1
toggleState_1 = 0;
Serial.println("Device1 ON");
}
else{
digitalWrite(RelayPin1, HIGH); // turn off relay 1
toggleState_1 = 1;
Serial.println("Device1 OFF");
}
delay(100);
break;
case 2:
if(toggleState_2 == 1){
digitalWrite(RelayPin2, LOW); // turn on relay 2
toggleState_2 = 0;
Serial.println("Device2 ON");
}
else{
digitalWrite(RelayPin2, HIGH); // turn off relay 2
toggleState_2 = 1;
Serial.println("Device2 OFF");
}
delay(100);
break;
case 3:
if(toggleState_3 == 1){
digitalWrite(RelayPin3, LOW); // turn on relay 3
toggleState_3 = 0;
Serial.println("Device3 ON");
}
else{
digitalWrite(RelayPin3, HIGH); // turn off relay 3
toggleState_3 = 1;
Serial.println("Device3 OFF");
}
delay(100);
break;
case 4:
if(toggleState_4 == 1){
digitalWrite(RelayPin4, LOW); // turn on relay 4
toggleState_4 = 0;
Serial.println("Device4 ON");
}
else{
digitalWrite(RelayPin4, HIGH); // turn off relay 4
toggleState_4 = 1;
Serial.println("Device4 OFF");
}
delay(100);
break;
case 5:
if(toggleState_5 == 1){
digitalWrite(RelayPin5, LOW); // turn on relay 5
toggleState_5 = 0;
Serial.println("Device5 ON");
}
else{
digitalWrite(RelayPin5, HIGH); // turn off relay 5
toggleState_5 = 1;
Serial.println("Device5 OFF");
}
delay(100);
break;
case 6:
if(toggleState_6 == 1){
digitalWrite(RelayPin6, LOW); // turn on relay 6
toggleState_6 = 0;
Serial.println("Device6 ON");
}
else{
digitalWrite(RelayPin6, HIGH); // turn off relay 6
toggleState_6 = 1;
Serial.println("Device6 OFF");
}
delay(100);
break;
case 7:
if(toggleState_7 == 1){
digitalWrite(RelayPin7, LOW); // turn on relay 7
toggleState_7 = 0;
Serial.println("Device7 ON");
}
else{
digitalWrite(RelayPin7, HIGH); // turn off relay 7
toggleState_7 = 1;
Serial.println("Device7 OFF");
}
delay(100);
break;
case 8:
if(toggleState_8 == 1){
digitalWrite(RelayPin8, LOW); // turn on relay 8
toggleState_8 = 0;
Serial.println("Device8 ON");
}
else{
digitalWrite(RelayPin8, HIGH); // turn off relay 8
toggleState_8 = 1;
Serial.println("Device8 OFF");
}
delay(100);
break;
default : break;
}
}
void with_internet(){
//Manual Switch Control
if (digitalRead(SwitchPin1) == LOW){
delay(200);
relayOnOff(1);
Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1); // Update Button Widget
}
else if (digitalRead(SwitchPin2) == LOW){
delay(200);
relayOnOff(2);
Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2); // Update Button Widget
}
else if (digitalRead(SwitchPin3) == LOW){
delay(200);
relayOnOff(3);
Blynk.virtualWrite(VPIN_BUTTON_3, toggleState_3); // Update Button Widget
}
else if (digitalRead(SwitchPin4) == LOW){
delay(200);
relayOnOff(4);
Blynk.virtualWrite(VPIN_BUTTON_4, toggleState_4); // Update Button Widget
}
else if (digitalRead(SwitchPin5) == LOW){
delay(200);
relayOnOff(5);
Blynk.virtualWrite(VPIN_BUTTON_5, toggleState_5); // Update Button Widget
}
else if (digitalRead(SwitchPin6) == LOW){
delay(200);
relayOnOff(6);
Blynk.virtualWrite(VPIN_BUTTON_6, toggleState_6); // Update Button Widget
}
else if (digitalRead(SwitchPin7) == LOW){
delay(200);
relayOnOff(7);
Blynk.virtualWrite(VPIN_BUTTON_7, toggleState_7); // Update Button Widget
}
else if (digitalRead(SwitchPin8) == LOW){
delay(200);
relayOnOff(8);
Blynk.virtualWrite(VPIN_BUTTON_8, toggleState_8); // Update Button Widget
}
}
void without_internet(){
//Manual Switch Control
if (digitalRead(SwitchPin1) == LOW){
delay(200);
relayOnOff(1);
}
else if (digitalRead(SwitchPin2) == LOW){
delay(200);
relayOnOff(2);
}
else if (digitalRead(SwitchPin3) == LOW){
delay(200);
relayOnOff(3);
}
else if (digitalRead(SwitchPin4) == LOW){
delay(200);
relayOnOff(4);
}
else if (digitalRead(SwitchPin5) == LOW){
delay(200);
relayOnOff(5);
}
else if (digitalRead(SwitchPin6) == LOW){
delay(200);
relayOnOff(6);
}
else if (digitalRead(SwitchPin7) == LOW){
delay(200);
relayOnOff(7);
}
else if (digitalRead(SwitchPin8) == LOW){
delay(200);
relayOnOff(8);
}
}
BLYNK_CONNECTED() {
// Request the latest state from the server
Blynk.syncVirtual(VPIN_BUTTON_1);
Blynk.syncVirtual(VPIN_BUTTON_2);
Blynk.syncVirtual(VPIN_BUTTON_3);
Blynk.syncVirtual(VPIN_BUTTON_4);
Blynk.syncVirtual(VPIN_BUTTON_5);
Blynk.syncVirtual(VPIN_BUTTON_6);
Blynk.syncVirtual(VPIN_BUTTON_7);
Blynk.syncVirtual(VPIN_BUTTON_8);
}
// 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);
}
BLYNK_WRITE(VPIN_BUTTON_5) {
toggleState_5 = param.asInt();
digitalWrite(RelayPin5, toggleState_5);
}
BLYNK_WRITE(VPIN_BUTTON_6) {
toggleState_6 = param.asInt();
digitalWrite(RelayPin6, toggleState_6);
}
BLYNK_WRITE(VPIN_BUTTON_7) {
toggleState_7 = param.asInt();
digitalWrite(RelayPin7, toggleState_7);
}
BLYNK_WRITE(VPIN_BUTTON_8) {
toggleState_8 = param.asInt();
digitalWrite(RelayPin8, toggleState_8);
}
void checkBlynkStatus() { // called every 3 seconds by SimpleTimer
bool isconnected = Blynk.connected();
if (isconnected == false) {
wifiFlag = 1;
digitalWrite(wifiLed, LOW); //Turn off WiFi LED
}
if (isconnected == true) {
wifiFlag = 0;
digitalWrite(wifiLed, HIGH); //Turn on WiFi LED
}
}
void setup()
{
Serial.begin(9600);
pinMode(RelayPin1, OUTPUT);
pinMode(RelayPin2, OUTPUT);
pinMode(RelayPin3, OUTPUT);
pinMode(RelayPin4, OUTPUT);
pinMode(RelayPin5, OUTPUT);
pinMode(RelayPin6, OUTPUT);
pinMode(RelayPin7, OUTPUT);
pinMode(RelayPin8, OUTPUT);
pinMode(wifiLed, OUTPUT);
pinMode(SwitchPin1, INPUT_PULLUP);
pinMode(SwitchPin2, INPUT_PULLUP);
pinMode(SwitchPin3, INPUT_PULLUP);
pinMode(SwitchPin4, INPUT_PULLUP);
pinMode(SwitchPin5, INPUT_PULLUP);
pinMode(SwitchPin6, INPUT_PULLUP);
pinMode(SwitchPin7, INPUT_PULLUP);
pinMode(SwitchPin8, 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(RelayPin5, toggleState_5);
digitalWrite(RelayPin6, toggleState_6);
digitalWrite(RelayPin7, toggleState_7);
digitalWrite(RelayPin8, toggleState_8);
WiFi.begin(WIFI_SSID, WIFI_PASS);
timer.setInterval(3000L, checkBlynkStatus); // check if Blynk server is connected every 3 seconds
Blynk.config(AUTH);
}
void loop()
{
if (WiFi.status() != WL_CONNECTED)
{
Serial.println("WiFi Not Connected");
}
else
{
Serial.println("WiFi Connected");
Blynk.run();
}
timer.run(); // Initiates SimpleTimer
if (wifiFlag == 0)
with_internet();
else
without_internet();
}
项目来源:hackster.io
制作者:Subhajit -