本帖最后由 kiddfu 于 2020-2-11 22:58 编辑
背景: 家中装修时,考虑会用到智能家居,但又对市面上流行的无线设备不太满意,所以最终敲定以有线智能为主、辅以少量无线设备。 布线时未采用传统布线方案,而是采用了一种中央辐射式的点到点方案。
使用nodered做服务端数据处理平台
前段时间入手了几个小爱音响就想着把非米家设备接入小爱。此方案使用8266承载blinker做桥,通过互通数据达到语音控制的效果。 优点:不需要去破解、拦截小爱音响,只要刷入固件就无脑接入小爱。 缺点:需要添加额外的设备(8266)、接入数量有限(5个) bug:8266发热巨大、有明显延迟 库自带例子做稍微修改,加入mqtt向服务器上报设备状态。 [mw_shl_code=arduino,true]#define BLINKER_WIFI
#define BLINKER_MIOT_OUTLET
#define MYAIO_SERVER "192.168.0.200"
#define MYAIO_SERVERPORT 1883 // use 8883 for SSL
#define MYAIO_USERNAME ""
#define MYAIO_KEY ""
#define MYAIO_BDNAME "ESPAIKTZD"
#include <Blinker.h>
//#include "Adafruit_MQTT.h"
//#include "Adafruit_MQTT_Client.h"
char auth[] = "xxxxxxx"; //密钥
char ssid[] = "xxxxxx"; //wifiSSID
char pswd[] = "xxxxxxxx"; //wifi密码
bool oState = false; //开关状态
void miotPowerState(const String & state) //反馈电源状态
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(13, HIGH);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
oState = true;
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(13, LOW);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
oState = false;
}
}
void miotQuery(int32_t queryCode) //设备查询接口
{
BLINKER_LOG("MIOT Query codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_ALL_NUMBER :
BLINKER_LOG("MIOT Query All");
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("MIOT Query Power State");
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
break;
default :
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
break;
}
}
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print("millis", BlinkerTime);
}
WiFiClient MyMqttClient;
Adafruit_MQTT_Client MyMqtt(&MyMqttClient, MYAIO_SERVER, MYAIO_SERVERPORT, MYAIO_USERNAME, MYAIO_KEY);
Adafruit_MQTT_Publish potValue = Adafruit_MQTT_Publish(&MyMqtt, MYAIO_BDNAME "/feeds/potValue");
Adafruit_MQTT_Subscribe ledBrightness = Adafruit_MQTT_Subscribe(&MyMqtt, MYAIO_BDNAME "/feeds/ledBrightness");
//uint32_t x=0;
void MQTT_connect()
{
int8_t ret;
if (MyMqtt.connected()) return;
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = MyMqtt.connect()) != 0)
{
Serial.println(MyMqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
MyMqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) while (1);
}
Serial.println("MQTT Connected!");
}
uint8_t ledPin = 13;
uint16_t potAdcValue = 0;
uint16_t ledBrightValue = 0;
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachQuery(miotQuery);
MyMqtt.subscribe(&ledBrightness);
}
void loop()
{
Blinker.run();
MQTT_connect();
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = MyMqtt.readSubscription(5000)))
{
if (subscription == &ledBrightness)
{
Serial.print(F("Got LED Brightness : "));
ledBrightValue = atoi((char *)ledBrightness.lastread);
Serial.println(ledBrightValue);
//analogWrite(ledPin, ledBrightValue);
if(ledBrightValue == 0) {digitalWrite(13, LOW);oState = false;}
else {digitalWrite(13, HIGH);oState = true;}
}
}
if(oState == false)
{
potAdcValue = 0;
}
else potAdcValue = 1;
if (! potValue.publish(potAdcValue)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
}[/mw_shl_code] 在米家app里面第三方账号里绑定账号同步设备,然后在nodered里面用订阅消息就ok了 视频还在审核就不展示了
|