🌍 Connect to Wi-Fi
1. Compile the example in Ai-Thinker-WB2/applications/protocols/mqtt/tcp/. After success, burn it to the board.
2. Post power-on, check the board's #WiFi connection in the router's backend. Then, verify its connection to the MQTT server in the server's backend. If not connected, it's likely due to the unchanged MQTT server IP in the code (omit the "mqtt://" prefix, just the IP).
3. Recompile and upload. Using the open-source EMQX server, notice a subscribed topic. Try sending a message and check WB2's output with a debugger. Note the default baud rates (115,200 for WB2 serial and 2,000,000 for the burner, which may need adjustment).
4. From the serial output, find the received #MQTT message is processed in a complex function that prints the topic and message. Also, identify the subscription and sending functions.
🪐 Remotely Control the #LED
1. Modify the message handling to check if the received message is "ON" or "OFF". If so, turn the LED on or off. (This judgment had issues; with help, an Arduino code was adapted for success.)
2. Incorporate the blinking code from Ai-Thinker-WB2/applications/get-started/blink/ to light up the LED.
· Adjust Brightness with PWM
1. Mere lighting isn't enough; add a PWM program for brightness control. Replace the blink code with that from Ai-Thinker-WB2/applications/peripherals/demo_pwm/ to manage the onboard LED's RGB.
2. The received MQTT message is a string, but RGB requires numerical values. After searching, a function was used to split the received message (initially a clumsy method was attempted). Now, the LED can be wirelessly controlled in color, brightness, and switch via MQTT.
3. However, after power loss and reboot, the LED's settings reset. So, data storage is needed.
· Store Data in Flash
1. When the WB2 powers on but isn't connected to Wi-Fi or the MQTT server, it doesn't receive parameters and remains unlit.
2. Locate the demo in Ai-Thinker-WB2/applications/storage/easyflash/, modify it, and integrate it into the code.
3. Configure the program to read data from the WB2 flash at startup, then connect to Wi-Fi and MQTT. Also, save data to the flash upon each command reception.
🧾 Summary
The production process:
Power-on -> Read flash -> Light up LED -> Connect Wi-Fi and MQTT -> Subscribe and set topics -> Process received messages -> Update LED state -> Upload LED state -> Save LED state to flash"
P1: Witness the demonstration. Devices in HA are highly versatile, even a simple LED.
P2: Observe the brightness adjustment.
P3: See the color adjustment.
P4: Input parameters directly.
Welcome more interesting ideas!