我遇到了我的家庭自動化項目的一些問題。我從aliexpress買了一個節點MCUU v3,我想控制我的百葉窗。與節點MCU復現的代理消息
這是我正在使用它的代碼。我使用Arduino IDE將此代碼推送到節點MCUU。
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <SimpleTimer.h>
// MQTT Server
const char* ssid = "****";
const char* password = "****";
const char* mqtt_server = "****";
char message_buff[100];
int photoValue = 0;
int rainValue = 0;
int photo = A0;
int rain = D6;
int relayUp = D7;
int relayDown= D8;
long interval = 10000;
long previousMillis = 0;
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
delay(10);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
void setup() {
pinMode(photo, INPUT);
pinMode(rain, INPUT);
pinMode(relayUp, OUTPUT);
pinMode(relayDown, OUTPUT);
digitalWrite(relayUp ,LOW);
digitalWrite(relayDown, LOW);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
if (client.connect("ESP8266Client")) {
client.subscribe("home/relayBlinds");
} else {
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
// Connect (or reconnect) to mqtt broker on the openhab server
reconnect();
}
// Read Photo- and Rain-sensors
photoValue = analogRead(photo);
rainValue = analogRead(rain);
// publish Temperature reading every 10 seconds
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
// publish Photo
String pubStringPhoto = String(photoValue);
pubStringPhoto.toCharArray(message_buff, pubStringPhoto.length()+1);
client.publish("home/photo", message_buff);
// publish Rain
String pubStringRain = String(rainValue);
pubStringRain.toCharArray(message_buff, pubStringRain.length()+1);
client.publish("home/rain", message_buff);
}
client.loop();
}
void callback(char* topic, byte* payload, unsigned int length) {
// MQTT inbound Messaging
int i = 0;
// create character buffer with ending null terminator (string)
for(i=0; i<length; i++) {
message_buff[i] = payload[i];
}
message_buff[i] = '\0';
String msgString = String(message_buff);
if (msgString == "BLINDSUP") {
digitalWrite(relayUp ,HIGH);
delay(5000);
digitalWrite(relayUp ,LOW);
} else if (msgString == "BLINDSDOWN") {
digitalWrite(relayDown ,HIGH);
delay(5000);
digitalWrite(relayDown ,LOW);
}
}
該計劃是有一個樹莓派與openHAB作爲控制器。我已經使用了幾個指南來設置mosquitto和openHAB,並且我總是得到相同的結果。
所以這就是發生了什麼:nodeMCU連接到我的Wifi併發布雨和照片值。我可以在openHAB GUI中閱讀它們,沒有任何問題。
當我按下在openHAB激活按鈕來發布BLINDSUP或BLINDSDOWN消息到達沒有任何問題,我可以看到我mosquitto終端上的消息。現在是當意想不到的結果開始發生時。相同的消息被多次傳遞給我的節點MCU,而沒有它出現在蚊子終端中。
我一直在試圖找出爲什麼會這樣的行爲,我認爲這是因爲該行:
if (!client.connected()) {
是假的和nodeMCU重新連接並以某種方式得到了同樣的信息。但它始終是第一條信息。如果我發送BLINDSUP然後BLINDSDOWN它將只註冊BLINDSUP永遠。
我真的沒有想法如何解決這個問題,並會感謝任何幫助,謝謝。
URL到nodeMCU是否有幫助,無論如何:nodeMCU