尝试编译OpenMQTTGateway

仓库地址 https://github.com/1technophile/OpenMQTTGateway

尝试使用ESP8266做射频遥控器,同时接入Homeassistant通过手机进行远程控制。

参考文档:
https://docs.openmqttgateway.com/upload/builds.html#configure-upload-with-platformio
https://post.smzdm.com/p/a6l8m3dn/
https://blog.csdn.net/qq_35456540/article/details/115535483

配置环境

环境使用之前docker部署的codeserver进行编译。
首先安装PlatformIO IDE插件,插件需要C/C++和Python3.6及以上版本支持。
C/C++直接安装拓展即可,Python可以通过源或者编译安装.
使用debian官方源进行安装python3.9

1
sudo apt isntall python3.9 python3.9-distutils python3.9-dev python3.9-pip python3.9-venv

在PlatformIO IDE安装时会要求提供python路径。

1
2
3
4
5
$ whereis python3.9

python3: /usr/bin/python3.9-config /usr/bin/python3 /usr/bin/python3.9 /usr/lib/python3 /usr/lib/python3.9 /etc/python3 /etc/python3.9 /usr/local/lib/python3.9 /usr/include/python3.9 /usr/share/python3 /usr/share/man/man1/python3.1.gz
#输入/usr/bin/python3.9
#注意上面apt安装时如果没有安装完全可能会导致反复弹出要求输入python路径

环境配置完成
跟着提示重新加载页面即可

新建PlatformIO IDE项目

首先从github仓库拉取源码
codeserver左侧一栏会多出一个PlatformIO IDE的LOGO
点击Pick a folder选择源码路径,选择完页面会自动重新加载,等待加载完成后重新进入左侧一栏的PlatformIO IDE
等待下载相关依赖仓库

修改配置文件

回到左侧一栏第一个的资源管理器中
修改配置文件的同时PlatformIO IDE也在根据配置文件更新依赖

修改platformio.ini

OpenMQTTGateway/platformio.ini
找到自己对应板子的型号取消注释

1
default_envs = nodemcuv2-rf

修改User_cofnig.h

OpenMQTTGateway/main/User_config.h

  1. 根据自己的需求设置WifiManager,即开机后发射Wi-Fi提供配置联网。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    //#define WM_PWD_FROM_MAC true // enable to set the password from the last 8 digits of the ESP MAC address for enhanced security, enabling this option requires to have access to the MAC address, either through a sticker or with serial monitoring
    #ifndef WifiManager_password
    # define WifiManager_password "your_password" //this is going to be the WPA2-PSK password for the initial setup access point
    #endif
    #ifndef WifiManager_ssid
    # define WifiManager_ssid Gateway_Name //this is the network name of the initial setup access point
    #endif
    #ifndef WifiManager_ConfigPortalTimeOut
    # define WifiManager_ConfigPortalTimeOut 120
    #endif
    #ifndef WifiManager_TimeOut
    # define WifiManager_TimeOut 5
    #endif
    #ifndef WM_DEBUG_LEVEL
    # define WM_DEBUG_LEVEL 1 // valid values are: DEBUG_ERROR = 0, DEBUG_NOTIFY = 1, DEBUG_VERBOSE = 2, DEBUG_DEV = 3, DEBUG_MAX = 4
    #endif

    这里的Gwateway_Name是一个变量
    修改SSID需要在OpenMQTTGateway/enviroments.ini配置文件中修改对应环境的Gateway_Name

    1
    2
    3
    4
    build_flags =
    ${com-esp.build_flags}
    '-DZgatewayRF="RF"'
    '-DGateway_Name="OpenMQTTGateway_ESP8266_RF_IR"'
    1
    '-DGateway_Name="OpenMQTTGateway_ESP8266_RF_IR"'

    Gateway_Name是网关名称,发出的SSID名称也会相同

  2. 配置MQTT服务器的配置文件
    上面WifiManager配置了的话,刷完固件启动后配网可以设置mqtt服务器,这里可以不用设置。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    #ifndef MQTT_USER
    # define MQTT_USER "your_username"
    #endif
    #ifndef MQTT_PASS
    # define MQTT_PASS "your_password"
    #endif
    #ifndef MQTT_SERVER
    # define MQTT_SERVER "192.168.1.17"
    #endif
    #ifndef MQTT_PORT
    # define MQTT_PORT "1883"
    #endif
  3. 配置功能
    选择自己需要的功能取消注释

    1
    2
    3
    4
    5
    6
    7
    /*-------------DEFINE THE MODULES YOU WANT BELOW----------------*/
    //Addons and module management, uncomment the Z line corresponding to the module you want to use

    #define ZgatewayRF "RF" //ESP8266, Arduino, ESP32
    //#define ZgatewayIR "IR" //ESP8266, Arduino, Sonoff RF Bridge
    //#define ZgatewayLORA "LORA" //ESP8266, Arduino, ESP32
    ···

    如果需要自己修改传感器的接线阵脚,而不是用项目默认的阵脚可以在对应的配置文件中进行修改
    比如修改RF射频文件则在同目录下的config_RF.h文件中修改

修改引脚(GPIO输入功能)

在配置功能里选择了GPIO输入后可以修改引脚

1
//#define ZsensorGPIOInput "GPIOInput" //ESP8266, Arduino, ESP32

修改引脚配置文件
OpenMQTTGateway/main/User_config.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*-------------------PIN DEFINITIONS----------------------*/
#ifndef INPUT_GPIO
# if defined(ESP8266) || defined(ESP32)
# define INPUT_GPIO 13
# else
# define INPUT_GPIO 7
# endif
#endif

#ifndef GPIO_INPUT_TYPE
# define GPIO_INPUT_TYPE INPUT_PULLUP
#endif

#define INPUT_GPIO_ON_VALUE "HIGH"
#define INPUT_GPIO_OFF_VALUE "LOW"

#endif

编译

完成所有配置后
在codeserver底栏选择 √ (PlatformIO:Build) 进行编译

1
2
3
4
5
6
7
8
Creating BIN file ".pio/build/nodemcuv2-rf/firmware.bin" using "/home/coder/.platformio/packages/framework-arduinoespressif8266/bootloaders/eboot/eboot.elf" and ".pio/build/nodemcuv2-rf/firmware.elf"
===================================================================================== [SUCCESS] Took 72.99 seconds =====================================================================================

Environment Status Duration
------------- -------- ------------
nodemcuv2-rf SUCCESS 00:01:12.993
====================================================================================== 1 succeeded in 00:01:12.993 ======================================================================================
* 终端将被任务重用,按任意键关闭。