標籤:

HOME Assistant:使用nodemcu控制繼電器MQTT初步試水

關於智能家居這個問題,我們國內有許多的平台,但是他們都互相排斥,像米家和京東都有獨立的app控制。鑒於這個問題,我們可以通過樹莓派或者liux開發板或者舊手機來搭建HOME Assistant平台。

具體搭建方法可參照下列網站:通用 - Home Assistant 中文文檔

我用的是HASS.IO安裝的。

出現這種界面後。不要相信預計20分鐘。其實要半天時間。反正耐心等。

安裝完成後出現這個界面就算安裝成功了

安裝成功後:點擊hass.io>add-on

添加https://github.com/hassio-addons/repository源地址安裝

Mosquitto broker

Samba share

Homebridge

這幾個插件。並啟用它們。

接下來配置Homebridge

在我的電腦網路里發現HASSIO

打開\HASSIOconfighomebridge目錄找到config.json文件用Notepad++打開。在文件內添加

"default_visibility": "visible",

回到config文件夾找到configuration.yaml文件Notepad++打開 配置MQTT和開關

mqtt: broker: 填入設備ip地址 username: 設置用戶名 password: 設置密碼switch: platform: mqtt name: "Sonoff_WoShi_3" state_topic: "home/Bed_room/3/stat" command_topic: "home/Bed_room/3" qos: 1 payload_on: "on" payload_off: "off" retain: true

然後點擊配置>通用>重啟服務

等待。如果無法重啟請留意home-assistant.log文件

拿出nodemcu

下載好Arduino-ide配置好開發環境

http://wechat.doit.am/package_esp8266com_index.json 或者

http://arduino.esp8266.com/stable/package_esp8266com_index.json

下載成功後在項目里添加zip庫文件

文件下載地址pan.baidu.com/s/1HV6kT9

這是引腳圖

代碼是sonoff開關代碼。根據io口更改RELAY 代碼

#include <ESP8266WiFi.h>#include <PubSubClient.h>#include <Ticker.h>#define BUTTON 0 // (Dont Change for Sonoff)#define RELAY 5 // (Dont Change for Sonoff)#define LED 4 // (Dont Change for Sonoff)#define MQTT_CLIENT "Sonoff_WoShi_3" // mqtt client_id (Sonoff開關的ClinetID,每個開關必需單獨設置)#define MQTT_SERVER "192.168.2.127" // mqtt server(樹莓派ip地址)#define MQTT_PORT 1883 // mqtt port(MQTT伺服器埠)#define MQTT_TOPIC "home/Bed_room/3" // mqtt topic (Sonoff開關的Topic,每個開關必需單獨設置)#define MQTT_USER "jiang" // mqtt user(MQTT伺服器用戶名)#define MQTT_PASS "jianghao" // mqtt password(MQTT伺服器的密碼)#define WIFI_SSID "jiang" // wifi ssid(你家的wifi名字,注意不能是5G頻段)#define WIFI_PASS "jianghao" // wifi password(你家的wifi密碼)#define VERSION "

------------------ Sonoff Powerpoint v1.0p -------------------"extern "C" { #include "user_interface.h" }bool sendStatus = false;bool requestRestart = false;int kUpdFreq = 1;int kRetries = 10;unsigned long TTasks;unsigned long count = 0;WiFiClient wifiClient;PubSubClient mqttClient(wifiClient, MQTT_SERVER, MQTT_PORT);Ticker btn_timer;void callback(const MQTT::Publish& pub) { if (pub.payload_string() == "stat") { } else if (pub.payload_string() == "on") { digitalWrite(LED, LOW); digitalWrite(RELAY, HIGH); } else if (pub.payload_string() == "off") { digitalWrite(LED, HIGH); digitalWrite(RELAY, LOW); } else if (pub.payload_string() == "reset") { requestRestart = true; } sendStatus = true;}void setup() { pinMode(LED, OUTPUT); pinMode(RELAY, OUTPUT); pinMode(BUTTON, INPUT); digitalWrite(LED, HIGH); digitalWrite(RELAY, LOW); btn_timer.attach(0.05, button); mqttClient.set_callback(callback); WiFi.begin(WIFI_SSID, WIFI_PASS); Serial.begin(115200); Serial.println(VERSION); Serial.print("
ESP ChipID: "); Serial.print(ESP.getChipId(), HEX); Serial.print("
Connecting to "); Serial.print(WIFI_SSID); Serial.print(" Wifi"); while ((WiFi.status() != WL_CONNECTED) && kRetries --) { delay(500); Serial.print(" ."); } if (WiFi.status() == WL_CONNECTED) { Serial.println(" DONE"); Serial.print("IP Address is: "); Serial.println(WiFi.localIP()); Serial.print("Connecting to ");Serial.print(MQTT_SERVER);Serial.print(" Broker . ."); delay(500); while (!mqttClient.connect(MQTT::Connect(MQTT_CLIENT).set_keepalive(90).set_auth(MQTT_USER, MQTT_PASS)) && kRetries --) { Serial.print(" ."); delay(1000); } if(mqttClient.connected()) { Serial.println(" DONE"); Serial.println("
---------------------------- Logs ----------------------------"); Serial.println(); mqttClient.subscribe(MQTT_TOPIC); blinkLED(LED, 40, 8); digitalWrite(LED, HIGH); } else { Serial.println(" FAILED!"); Serial.println("
----------------------------------------------------------------"); Serial.println(); } } else { Serial.println(" WiFi FAILED!"); Serial.println("
----------------------------------------------------------------"); Serial.println(); }}void loop() { mqttClient.loop(); timedTasks(); checkStatus();}void blinkLED(int pin, int duration, int n) { for(int i=0; i<n; i++) { digitalWrite(pin, HIGH); delay(duration); digitalWrite(pin, LOW); delay(duration); }}void button() { if (!digitalRead(BUTTON)) { count++; } else { if (count > 1 && count <= 40) { digitalWrite(LED, !digitalRead(LED)); digitalWrite(RELAY, !digitalRead(RELAY)); sendStatus = true; } else if (count >40){ Serial.println("

Sonoff Rebooting . . . . . . . . Please Wait"); requestRestart = true; } count=0; }}void checkConnection() { if (WiFi.status() == WL_CONNECTED) { if (mqttClient.connected()) { Serial.println("mqtt broker connection . . . . . . . . . . OK"); } else { Serial.println("mqtt broker connection . . . . . . . . . . LOST"); requestRestart = true; } } else { Serial.println("WiFi connection . . . . . . . . . . LOST"); requestRestart = true; }}void checkStatus() { if (sendStatus) { if(digitalRead(LED) == LOW) { mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "on").set_retain().set_qos(1)); Serial.println("Relay . . . . . . . . . . . . . . . . . . ON"); } else { mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "off").set_retain().set_qos(1)); Serial.println("Relay . . . . . . . . . . . . . . . . . . OFF"); } sendStatus = false; } if (requestRestart) { blinkLED(LED, 400, 4); ESP.restart(); }}void timedTasks() { if ((millis() > TTasks + (kUpdFreq*60000)) || (millis() < TTasks)) { TTasks = millis(); checkConnection(); }}

推薦閱讀:

ZigBee 無線通訊技術到底安不安全?
智能除濕機成智能家居爭奪新焦點
智能家居的八大功能是什麼?
沒準你和智能家居只有一個開關的距離
這扇來自未來的窗戶,能讓你在家中環遊世界

TAG:智能家居 |