进阶必做任务内容补充:
2.3 进阶任务(必做):通过Wi-Fi,利用MQTT协议接入到开源的智能家居平台HA(HomeAssistant)
2.3.1 物料准备&设计思路
装有 HA 的树莓派/Win10 主机/Arduino IDE /Arduino UR4 WIFI 开发板/ TYPE-C 连接线
2.3.2 软件流程
2.3.3 代码片段
#include <WiFiS3.h>
#include <ArduinoHA.h>
#include "arduino_secrets.h"
#include "analogWave.h"
//RGB PIN 定义
#define LED_RGB_G D5
#define LED_RGB_R D6
#define LED_RGB_B D9
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS; // the WiFi radio's status
WiFiClient client;
HADevice device(MQTT_CLIENT_ID);
HAMqtt mqtt(client, device);
HALight light("MyRgbLight", HALight::RGBFeature);
byte u8RgbSwitch =0;
void setup()
{
//RGB 初始化
pinMode(LED_RGB_R, OUTPUT);
pinMode(LED_RGB_G, OUTPUT);
pinMode(LED_RGB_B, OUTPUT);
analogWrite(LED_RGB_R, 260);
analogWrite(LED_RGB_G, 260);
analogWrite(LED_RGB_B, 260);
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE)
{
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the data:
Serial.print("You're connected to the network");
device.setName("Arduino Ur4");
light.setName("Bathroom");
// handle light states
light.onStateCommand(onStateCommand);
light.onRGBColorCommand(onRGBColorCommand); // optional
Serial.println("\nStart connecting to MQTT server");
if (!mqtt.begin(MQTT_SERVER, MQTT_PORT, MQTT_USERNAME, MQTT_PASSWORD))
{
Serial.print("Connection falied");
Serial.print(mqtt.getState());
Serial.println("Try again in 5 seconds");
delay(5000);
}
}
void loop() {
// check the network connection once every 10 seconds:
mqtt.loop();
}
//RGB 相关函数
void onStateCommand(bool state, HALight* sender)
{
Serial.print("State: ");
Serial.println(state);
if(1 ==state) //RGB 开启
{
u8RgbSwitch =1;
}
else //RGB 关闭
{
u8RgbSwitch =0;
}
sender->setState(state); // report state back to the Home Assistant
}
void onBrightnessCommand(uint8_t brightness, HALight* sender)
{
Serial.print("Brightness: ");
Serial.println(brightness);
sender->setBrightness(brightness); // report brightness back to the Home Assistant
}
void onRGBColorCommand(HALight::RGBColor color, HALight* sender)
{
Serial.print("Red: ");
Serial.println(color.red);
Serial.print("Green: ");
Serial.println(color.green);
Serial.print("Blue: ");
Serial.println(color.blue);
if(1 ==u8RgbSwitch)
{
analogWrite(LED_RGB_R, (color.red*2));
analogWrite(LED_RGB_G, (color.green*2));
analogWrite(LED_RGB_B, (color.blue*2));
}
else
{
analogWrite(LED_RGB_R, 260);
analogWrite(LED_RGB_G, 260);
analogWrite(LED_RGB_B, 260);
}
sender->setRGBColor(color); // report color back to the Home Assistant
}
任务帖详解:【Follow me第二季第2期】5.进阶任务之 树莓派 HA+Windows+UR4 控制 RGB