[esp32]esp8266连接小爱同学控制继电器

By Heanny
2022-05-26
1948 read

前言

接 https://www.heanny.cn/post-520.html

步骤

  1. 配置arduino
# 该内容来自https://bbs.bemfa.com/29
1、安装ArduinoIDE,官网下载:https://www.arduino.cc/en/Main/Software ,下载windows安装版,常规安装即可。
2、下载安装esp32 安装包
巴法下载:https://file.bemfa.com/bemfa_com/esp/esp32V2_0_3.zip 。
百度网盘:https://pan.baidu.com/s/1tqRbFQqT8m0KdQwxOcrJlQ ,提取码1234
3、解压下载好的esp32安装包,关闭arduino ide 软件,点击安装包中的”一键安装“,安装大约需要5分钟时间,请耐心等待。
4、打开arduino IDE。“工具”--”开发板“,选择自己的开发板类型即可。
5、烧录设置默认即可,插上开发板,在“工具”--“开发板”--“端口”选中自己开发板com口。返回IDE,点击上方指向右的箭头即可烧录。

******************************************************************************
方法二

1、安装ArduinoIDE,官网下载:点击跳转 ,下载windows安装版,常规安装即可。
2、下载安装后打开arduino IDE 。打开右上角 “文件”--“首选项” --“附加开发板管理器网址” 填入`https://ai.bemfa.com/esp32/stable/package_esp32_index.json` 完成后点击“好”确认即可。
或 `https://ai.bemfa.com/esp8266/stable/package_esp8266com_index.json,https://ai.bemfa.com/esp32/stable/package_esp32_index.json`
3、重启arduino IDE。上方工作条打开“工具”--“开发板”--“开发板管理器”,搜索esp32,找到安装即可(如果失败,多点几次重试或继续,等待安装完毕)。。
4、打开arduino IDE。“工具”--”开发板“,选择自己的开发板类型即可。
5、烧录设置默认即可,插上开发板,在“工具”--“开发板”--“端口”选中自己开发板com口。返回IDE,点击上方指向右的箭头即可烧录。
  1. 使用arduino刷入脚本
    先配置esp8266相关,选择对应com端口,开发版选择Generic ESP8266 Module,并将GPIO0接地

微信图片_20231223212230.png

进入工具->管理库,安装blinker,然后上传代码

#define BLINKER_MIOT_LIGHT #define BLINKER_WIFI #include <Blinker.h> char auth[] = "XXXXXXXXXX"; //输入密钥 char ssid[] = "XXXXXXXXXXX"; //wifi名称 char pswd[] = "XXXXXXXX"; //wifi密码 int GPIO=0;//定义GPIO口用于控制继电器 #define BUTTON_1 "ButtonKey" BlinkerButton Button1("btn-abc");//这里需要根据自己在BLINKER里面设置的名字进行更改 //按下按键后就会进行此函数 void button1_callback(const String & state) { BLINKER_LOG("get button state: ", state); digitalWrite(GPIO,!digitalRead(GPIO)); Blinker.vibrate(); } //接入小爱同学的代码,此部分代码根据BLINKER官网进行修改得到 void miotPowerState(const String & state) { BLINKER_LOG("need set power state: ",state); if (state == BLINKER_CMD_OFF) {//如果语音接收到是关闭灯就设置GPIO口为高电平 digitalWrite(GPIO, HIGH); BlinkerMIOT.powerState("off"); BlinkerMIOT.print(); } else if (state == BLINKER_CMD_ON) { digitalWrite(GPIO, LOW); BlinkerMIOT.powerState("on"); BlinkerMIOT.print(); } } void setup() { Serial.begin(115200); pinMode(GPIO,OUTPUT); digitalWrite(GPIO,HIGH);//初始化,由于继电器是低电平触发。所以刚开始设为高电平 Blinker.begin(auth, ssid, pswd); Button1.attach(button1_callback); BlinkerMIOT.attachPowerState(miotPowerState);//这段代码一定要加,不加小爱同学控制不了,务必在回调函数中反馈该控制状态 } void loop() { Blinker.run(); }
  1. 手机安装电灯 blinker
    https://diandeng.tech/home 注册,添加设备,独立设备,复制密钥到上面的代码里面

  2. 接入小爱同学
    打开米家,我的,其他平台设备,选择电灯设备,绑定

  3. 喊你的小爱同学开灯啦

其他

据说电灯设备不稳定,而且最多5个免费设备,还不能获取设备状态

巴法云代码

/* * 智能语言控制控制,支持同时天猫、小爱、小度、google Assistent控制 * 也同时支持web控制、小程序控制、app控制,定时控制等 * QQ群:566565915 * 项目示例:通过发送on或off控制开关 * 官网:bemfa.com */ #include <ESP8266WiFi.h> //默认,加载WIFI头文件 #include "PubSubClient.h" //默认,加载MQTT库文件 //********************需要修改的部分*******************// const char* ssid = ""; //修改,你的路由去WIFI名字 const char* password = ""; //你的WIFI密码 #define ID_MQTT "" //用户私钥,控制台获取 const char* topic = "xs003"; //主题名字,可在巴法云控制台自行创建,名称随意 const int B_led = 0; //单片机LED引脚值,D系列是NodeMcu引脚命名方式,其他esp8266型号将D2改为自己的引脚 //**************************************************// const char* mqtt_server = "bemfa.com"; //默认,MQTT服务器 const int mqtt_server_port = 9501; //默认,MQTT服务器 WiFiClient espClient; PubSubClient client(espClient); //灯光函数及引脚定义 void turnOnLed(); void turnOffLed(); void setup_wifi() { delay(10); 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"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Topic:"); Serial.println(topic); String msg = ""; for (int i = 0; i < length; i++) { msg += (char)payload[i]; } Serial.print("Msg:"); Serial.println(msg); if (msg == "on") {//如果接收字符on,亮灯 turnOnLed();//开灯函数 } else if (msg == "off") {//如果接收字符off,亮灯 turnOffLed();//关灯函数 } msg = ""; } void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect(ID_MQTT)) { Serial.println("connected"); Serial.print("subscribe:"); Serial.println(topic); //订阅主题,如果需要订阅多个主题,可发送多条订阅指令client.subscribe(topic2);client.subscribe(topic3); client.subscribe(topic); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } void setup() { pinMode(B_led, OUTPUT); //设置引脚为输出模式 digitalWrite(B_led, LOW);//默认引脚上电高电平 Serial.begin(115200); //设置波特率115200 setup_wifi(); //设置wifi的函数,连接wifi client.setServer(mqtt_server, mqtt_server_port);//设置mqtt服务器 client.setCallback(callback); //mqtt消息处理 } void loop() { if (!client.connected()) { reconnect(); } client.loop(); } //打开灯泡 void turnOnLed() { Serial.println("turn on light"); digitalWrite(B_led, HIGH); } //关闭灯泡 void turnOffLed() { Serial.println("turn off light"); digitalWrite(B_led, LOW); }
[esp32]ESP8266使用micropython连接MQTT控制家电
[ESP32]ESP8266连接小爱同学控制继电器二

Comments

暂无评论,还不快来坐沙发...

Leave a Reply