I’ve already define it:
Switch InverterBlue "Testing..." { mqtt="<[brokerInverterBlue:mqtt/topic:state:ON], >[brokerInverterBlue:mqtt/back-topic:command:*:OFF]" }
I’ve created also a button in Habpanel with ON/OFF .
I can see the action of this button in OH2 log:
2018-07-16 13:42:42.496 [ome.event.ItemCommandEvent] - Item 'InverterBlue' received command ON
2018-07-16 13:42:42.503 [vent.ItemStateChangedEvent] - InverterBlue changed from OFF to ON
2018-07-16 13:42:45.082 [ome.event.ItemCommandEvent] - Item 'InverterBlue' received command OFF
2018-07-16 13:42:45.096 [vent.ItemStateChangedEvent] - InverterBlue changed from ON to OFF
I’ve update Arduino sketch to connect with this broker:
/*
* Dim - Relay Shield Toggle v1.0
* Closes the relay for 100 ms, then opens based on OH2 event
* Relay Shield transistor closes relay when D1 is HIGH
*/
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "xxx"
#define WLAN_PASS "x"
/************************* MQTT Broker Setup *********************************/
#define mqtt_server "192.168.1.2"
#define mqtt_serverport 1883 // use 8883 for SSL
#define mqtt_username ""
#define mqtt_password ""
/************************* Constants, Variables, Integers, etc *********************************/
const int relayPin = D1;
const long togDelay = 100; // pause for 100 milliseconds before toggling to Open
const long postDelay = 200; // pause for 200 milliseconds after toggling to Open
int value = 0;
int relayState = LOW;
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, mqtt_server, mqtt_serverport, mqtt_username, mqtt_password);
// Setup subscription 'Robo500' for monitoring topic for changes.
Adafruit_MQTT_Subscribe InverterBlue = Adafruit_MQTT_Subscribe(&mqtt, "openhab/out/brokerInverterBlue");
/*************************** Sketch Code ************************************/
void MQTT_connect();
void setup() {
Serial.begin(115200);
Serial.println(""); Serial.println(F("Booting... v1.0"));
pinMode(relayPin, OUTPUT);
// Connect to WiFi access point.
Serial.print("Connecting to "); Serial.println(WLAN_SSID);
WiFi.mode(WIFI_STA);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting in 5 secs...");
delay(5000);
ESP.restart();
}
// Setup MQTT subscription for Robo500 feed.
mqtt.subscribe(&InverterBlue);
// Begin OTA
ArduinoOTA.setPort(8266); // Port defaults to 8266
ArduinoOTA.setHostname("InverterBlueChipID"); // Hostname defaults to esp8266-[ChipID]
ArduinoOTA.setPassword((const char *)"<pass>"); // No authentication by default
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("");
Serial.println("Ready & WiFi connected");
Serial.print("IP address: "); Serial.println(WiFi.localIP());
}
void loop() {
ArduinoOTA.handle();
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
// this is our 'wait for incoming subscription packets' busy subloop
// try to spend your time here
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &InverterBlue) {
Serial.print(F("Got: ")); Serial.println((char *)InverterBlue.lastread);
// close the relay for 100 ms
Serial.println("Close Relay for 100 ms & then Open");
digitalWrite(relayPin, HIGH);
delay(togDelay);
digitalWrite(relayPin, LOW);
delay(postDelay);
}
}
// ping the server to keep the mqtt connection alive
// NOT required if you are publishing once every KEEPALIVE seconds
// if(! mqtt.ping()) {
// mqtt.disconnect();
// }
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}
The serial from Arduino tells me that MQTT connected.
Ready & WiFi connected
IP address: 192.168.1.102
Connecting to MQTT... MQTT Connected!
mqtt-eventbus file has been modified:
broker=brokerInverterBlue
statePublishTopic=openhab/out/${item}/state
commandPublishTopic=openhab/out/${item}/command
stateSubscribeTopic=openhab/in/${item}/state
commandSubscribeTopic=openhab/in/${item}/command
But I cannot see any action in Arduino when I clicked the button in OH2. What’s wrong?