1 Star 1 Fork 0

AGeek/esp32c3_bemfa_mqtt

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
LGPL-3.0

物联智能开关灯工程(一个极客AGeek开源)

该工程是基于esp32C3使用IDF v5.2开发的一个物联开关灯模块。 使用esp32c3联网后对接巴法云MQTT服务器再对接天猫精灵。 通过巴法云mqtt通信控制一个mg90s的舵机来转动对应角度来替代手指去开灯或者关灯。 可以和天猫精灵联动,对天猫精灵说“天猫精灵 开灯/关灯”来控制舵机开关灯。

如何使用

硬件需求

首先准备硬件如下:

  1. ESP32C3开发板或者自己绘制的板子,需要能进行烧录。
  2. mg90s的舵机一个,需带一个塑料转子。
  3. 2.54公对母杜邦线。
  4. 502胶水或者强力泡棉双面胶等或其他结构固定。
  5. 一条USB线和5v充电插头。
  6. 家用的2.4G wifi或者热点。

esp32c3开发板

舵机

杜邦线

泡棉双面胶

组装图如下 组装图

如何配置工程?

配置idf开发环境

首先需要下载vscode vscode下载

然后安装vscode,注意需要勾选这几个框方便打开文件夹工程。 vscode安装 可以下载中文插件再重新打开就是中文页面 vscode中文插件

在vsdode中下载idf插件

vscode下载idf1

在vscode左侧乐鑫图标打开idf vscode下载idf2

选择安装idf开发环境

选择乐鑫服务器
选择版本V5.2

vscode下载idf3

vscode下载idf4

不同电脑环境不一样,有可能会出现idf安装失败的情况
那就只能按报错信息去百度查找原因
然后需要先删除原本的idf下载目录的东西,卸载插件重新安装,如果还是不行那就安装旧版本或者另一个版本

安装成功后可以选择工程文件夹打开工程

vscode打开工程

注册巴法云物联平台

巴法云

注册巴法云

巴法云注册

参考:

巴法云MQTT设备云对接: https://cloud.bemfa.com/docs/#/?id=_43-mqtt设备云

巴法云参考天猫精灵对接: https://cloud.bemfa.com/docs/#/?id=p-stylefont-weight700margin0px9、天猫精灵支持

选择MQtt设备云创建主题 roomLight002 appSend appReceive

巴法云mqtt设备云

打开mqtt_app.c 复制私钥到 .credentials.client_id


mqtt_app_t mqtt_bemfa=
 {
    .mqtt_cfg = {

        .broker.address.uri = "mqtt://bemfa.com:9501/",
        .credentials.client_id = "私钥",
        .network.disable_auto_reconnect = true,

    },
    .event_handler=mqtt_event_handler,
    .topics={"roomLight002","appSend","appReceive"},
    .state=mqtt_state_stop
 };

打开wifi_connect_default.c

填写家里或手机的wifi热点名称和密码


#define CONFIG_EXAMPLE_WIFI_SSID "2.4G wifi名称"
#define CONFIG_EXAMPLE_WIFI_PASSWORD "wifi密码"

mg90s舵机默认是90度,安装塑料的转子在90度时贴合到灯的开关侧边
默认开灯是先转动到90度300ms后转到45度500ms后转动回90度
默认关闭是先转动到90度300ms后转到135度500ms后转动回90度

如果这个控制逻辑和你实际安装到灯的开关不一样时需自行修改

打开 mg90s.c

修改 mg90s_180_switch_room_light函数


/**
 * @brief  功能介绍:设置舵机转动实现开关灯
 * @param  参数:mg90s 
 * @param  参数:is_on 
 */
  void mg90s_180_switch_room_light(mg90s_180_t* mg90s ,uint8_t is_on)
  {

    if(is_on)
    {
        ESP_LOGI(TAG, "room_light_on");
        mg90s_180_set_mode(mg90s,mg90s_angle_90);
        vTaskDelay(pdMS_TO_TICKS(300));
        mg90s_180_set_mode(mg90s,mg90s_angle_45);
        vTaskDelay(pdMS_TO_TICKS(500));
        mg90s_180_set_mode(mg90s,mg90s_angle_90);
    }else
    {
     
        ESP_LOGI(TAG, "room_light_off");
        mg90s_180_set_mode(mg90s,mg90s_angle_90);
        vTaskDelay(pdMS_TO_TICKS(300));
        mg90s_180_set_mode(mg90s,mg90s_angle_135);
        vTaskDelay(pdMS_TO_TICKS(500));
        mg90s_180_set_mode(mg90s,mg90s_angle_90);
    }

  }


尝试编译

然后串口下载到开发板

把开发板的 IO5 5V GND

和舵机的线接上

IO5-->橙色线
5V-->红线
GND-->棕线

连接图

调试运行

打开串口监控:

以下为正确运行的日志


I (26) boot: ESP-IDF v5.2 2nd stage bootloader
I (26) boot: compile time Mar  8 2024 18:13:36   
I (26) boot: chip revision: v0.3
I (29) boot.esp32c3: SPI Speed      : 80MHz      
I (33) boot.esp32c3: SPI Mode       : DIO        
I (38) boot.esp32c3: SPI Flash Size : 2MB        
I (43) boot: Enabling RNG early entropy source...
I (48) boot: Partition Table:
I (52) boot: ## Label            Usage          Type ST Offset  
 Length
I (59) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (67) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (74) boot:  2 factory          factory app      00 00 00010000 00100000
I (81) boot: End of partition table
I (86) esp_image: segment 0: paddr=00010020 vaddr=3c0b0020 size=264d0h (156880) map
I (119) esp_image: segment 1: paddr=000364f8 vaddr=3fc91400 size=02d08h ( 11528) load
I (121) esp_image: segment 2: paddr=00039208 vaddr=40380000 size=06e10h ( 28176) load
I (130) esp_image: segment 3: paddr=00040020 vaddr=42000020 size=ad1dch (709084) map
I (244) esp_image: segment 4: paddr=000ed204 vaddr=40386e10 size=0a3fch ( 41980) load
I (257) boot: Loaded app from partition at offset 0x10000
I (257) boot: Disabling RNG early entropy source...
I (269) cpu_start: Unicore app
I (277) cpu_start: Pro cpu start user code
I (277) cpu_start: cpu freq: 160000000 Hz
I (278) cpu_start: Application information:
I (280) cpu_start: Project name:     mqtt_tcp
I (286) cpu_start: App version:      4ab17e1-dirty
I (291) cpu_start: Compile time:     Mar  8 2024 18:11:38       
I (297) cpu_start: ELF file SHA256:  aaf448587...
I (302) cpu_start: ESP-IDF:          v5.2
I (307) cpu_start: Min chip rev:     v0.3
I (312) cpu_start: Max chip rev:     v1.99 
I (317) cpu_start: Chip rev:         v0.3
I (321) heap_init: Initializing. RAM available for dynamic allocation:
I (329) heap_init: At 3FC988B0 len 00027750 (157 KiB): RAM      
I (335) heap_init: At 3FCC0000 len 0001C710 (113 KiB): Retention RAM
I (342) heap_init: At 3FCDC710 len 00002950 (10 KiB): Retention 
RAM
I (349) heap_init: At 50000010 len 00001FD8 (7 KiB): RTCRAM     
I (356) spi_flash: detected chip: generic
I (360) spi_flash: flash io: dio
W (364) spi_flash: Detected size(4096k) larger than the size in 
the binary image header(2048k). Using the size in the binary image header.
I (377) sleep: Configure to isolate all GPIO pins in sleep stateI (383) sleep: Enable automatic switching of GPIO sleep configuration
I (391) main_task: Started on CPU0
I (391) main_task: Calling app_main()
I (391) app_main: [APP] Startup..
I (401) app_main: [APP] Free memory: 280448 bytes
I (401) app_main: [APP] IDF version: v5.2
I (421) wifi_connect_deault: Start example_connect.
I (421) pp: pp rom version: 9387209
I (421) net80211: net80211 rom version: 9387209
I (431) wifi:wifi driver task: 3fca1e8c, prio:23, stack:6656, core=0
I (431) wifi:wifi firmware version: cc1dd81
I (431) wifi:wifi certification version: v7.0
I (441) wifi:config NVS flash: enabled
I (441) wifi:config nano formating: disabled
I (441) wifi:Init data frame dynamic rx buffer num: 32
I (451) wifi:Init static rx mgmt buffer num: 5
I (451) wifi:Init management short buffer num: 32
I (461) wifi:Init dynamic tx buffer num: 32
I (461) wifi:Init static tx FG buffer num: 2
I (471) wifi:Init static rx buffer size: 1600
I (471) wifi:Init static rx buffer num: 10
I (471) wifi:Init dynamic rx buffer num: 32
I (481) wifi_init: rx ba win: 6
I (481) wifi_init: tcpip mbox: 32
I (481) wifi_init: udp mbox: 6
I (491) wifi_init: tcp mbox: 6
I (491) wifi_init: tcp tx win: 5760
I (501) wifi_init: tcp rx win: 5760
I (501) wifi_init: tcp mss: 1440
I (501) wifi_init: WiFi IRAM OP enabled
I (511) wifi_init: WiFi RX IRAM OP enabled
I (511) phy_init: phy_version 1130,b4e4b80,Sep  5 2023,11:09:30 
I (551) wifi:mode : sta (60:55:f9:79:cb:98)
I (551) wifi:enable tsf
I (561) wifi_connect_deault: Connecting to TP-LINK_xwj...       
I (561) wifi_connect_deault: Waiting for IP(s)
I (1171) wifi:new:<6,2>, old:<1,0>, ap:<255,255>, sta:<6,2>, prof:1
I (1471) wifi:state: init -> auth (b0)
I (1471) wifi:state: auth -> assoc (0)
I (1511) wifi:state: assoc -> run (10)
I (1821) wifi:<ba-add>idx:0 (ifx:0, a4:1a:3a:65:48:f6), tid:0, ssn:2706, winSize:64
I (1821) wifi:connected with TP-LINK_xwj, aid = 2, channel 6, 40D, bssid = a4:1a:3a:65:48:f6
I (1821) wifi:security: WPA2-PSK, phy: bgn, rssi: -40
I (1831) wifi:pm start, type: 1

I (1831) wifi:dp: 1, bi: 102400, li: 3, scale listen interval from 307200 us to 307200 us
I (1841) wifi:set rx beacon pti, rx_bcn_pti: 0, bcn_timeout: 25000, mt_pti: 0, mt_time: 10000
I (1851) wifi_connect_deault: ESP_WIFI_CONNECT!

I (1851) wifi:AP's beacon interval = 102400 us, DTIM period = 1
W (1961) wifi:[ADDBA]rx delba, code:39, delete tid:0
I (1961) wifi:<ba-del>idx:0, tid:0
W (1961) wifi:[ADDBA]rx delba, code:39, delete tid:0
I (2851) esp_netif_handlers: example_netif_sta ip: 192.168.1.100, mask: 255.255.255.0, gw: 192.168.1.1
I (2851) wifi_connect_deault: Got IPv4 event: Interface "example_netif_sta" address: 192.168.1.100
I (2861) mqtt_example: mqtt start!
I (2861) wifi_connect_deault: ESP_MQTT_start!

I (2861) mqtt_example: Other event id:7
I (3411) wifi_connect_deault: Got IPv6 event: Interface "example_netif_sta" address: fe80:0000:0000:0000:6255:f9ff:fe79:cb98, 
type: ESP_IP6_ADDR_IS_LINK_LOCAL
I (3411) main_task: Returned from app_main()
I (5151) mqtt_example: MQTT_EVENT_CONNECTED
I (5151) mqtt_example: sent subscribe successful, msg_id=62532
I (5151) mqtt_example: sent subscribe successful, msg_id=61793
I (5161) mqtt_example: sent subscribe successful, msg_id=18018
I (5171) mqtt_example: MQTT_EVENT_SUBSCRIBED, msg_id=62532
I (5171) mqtt_example: sent publish successful, msg_id=0
I (5201) mqtt_example: MQTT_EVENT_SUBSCRIBED, msg_id=61793
I (5201) mqtt_example: sent publish successful, msg_id=0
I (5201) mqtt_example: MQTT_EVENT_SUBSCRIBED, msg_id=18018
I (5201) mqtt_example: sent publish successful, msg_id=0


可以在巴法云服务器管理界面对应主题直接发送"on"
开灯 向对应的巴法云mqtt服务器的主题例如roomLight002打开灯时的日志


I (155151) mqtt_example: MQTT_EVENT_DATA
TOPIC=roomLight002
DATA=on
I (155151) mg90s: room_light_on

收到打开指令后,esp32c3开发板会控制舵机转动到45度或者135度后再回到90度。

可以在巴法云服务器管理界面对应主题直接发送"off"

关灯 向对应的巴法云mqtt服务器的主题关闭灯时的日志


TOPIC=roomLight002
DATA=off
I (194781) mg90s: room_light_off

收到关闭指令后,esp32c3开发板会控制舵机转动到135度或者45度后再回到90度。

当测试舵机能正确转动 再使用胶水或者其他固定方法让舵机贴合在开关侧边

对接天猫精灵

如果需要接入天猫精灵语音控制 请在天猫精灵app搜索巴法云

搜索巴法云

然后按巴法云物联平台的帮助教程操作 绑定自己的巴法云账号

绑定巴法云

绑定巴法云2

绑定巴法云3

绑定巴法云3

绑定完成后可以在天猫精灵app进行智能灯的控制

天猫精灵联动巴法云

如果有天猫精灵已经联网

可以直接对天猫精灵说“天猫精灵 开灯”或者“天猫精灵关灯”

天猫精灵会自动向绑定好的巴法云的主题推送“on”和“off"

最终实现语音控制舵机转动来按下灯的开关。

舵机无法转回90度

如果舵机开灯或关灯完成后转回90度后还在持续嗡嗡的转
就是舵机的塑料转子顶住了灯的开关没转回90度

需要适当的把塑料的转子刮去一些或者剪短一些。

需要按自己实际安装情况处理

塑料转子

GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.

简介

使用esp32C3对接巴法云MQTT通信,实现智能家居等联动。 展开 收起
C 等 3 种语言
LGPL-3.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/nikolan/esp32c3_bemfa_mqtt.git
git@gitee.com:nikolan/esp32c3_bemfa_mqtt.git
nikolan
esp32c3_bemfa_mqtt
esp32c3_bemfa_mqtt
master

搜索帮助