First off, I am very new to openhab. I have more experience with arduino and basic IOT. This community is amazing, I am learning so much by following other peoples topics and the openhab beginner instructions. Having a lot of fun until this :/…
I am trying to get openhab2 to operate a standard relay (on/off). I followed this other topic ([OH2] Control ESP8266 Relay using MQTT Eventbus) and was able to get everything working just fine. MQTT was setup and tested. The relay would come on and the turn off as configured. However, I don’t want the delay. I simply want the on/off function. I removed the delay commands from the arudino code but no luck. I have a feeling OpenHab needs to know the state of the relay so I am most likely missing some commands. Any help would be amazing! Listed below is Dim’s code that I edited.
#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 “MYSSID”
#define WLAN_PASS “MYPASS”
/************************* MQTT Broker Setup *********************************/
#define mqtt_server “10.0.0.XX”
#define mqtt_serverport 1883 // use 8883 for SSL
//#define mqtt_username “<mqtt_username>” not used for now…
//#define mqtt_password “<mqtt_password>” not used for now…
/************************* Constants, Variables, Integers, etc *********************************/
const int relayPin = D1;
//const long togDelay = 10; // pause for 10 milliseconds before toggling to Open
//const long postDelay = 20; // pause for 20 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);
// Setup subscription ‘Workbench light’ for monitoring topic for changes.
Adafruit_MQTT_Subscribe Desklight = Adafruit_MQTT_Subscribe(&mqtt, “openhab/out/Desklight/command”);
/*************************** Sketch Code ************************************/
void MQTT_connect();
void setup() {
Serial.begin(115200);
Serial.println(""); Serial.println(F(“Booting… v0.2”));
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 DeskLight feed.
mqtt.subscribe(&DeskLight);
// Begin OTA
ArduinoOTA.setPort(8266); // Port defaults to 8266
ArduinoOTA.setHostname(“DeskLight”); // Hostname defaults to esp8266-[ChipID]
ArduinoOTA.setPassword((const char *)""); // 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 == &DeskLight) {
Serial.print(F("Got: ")); Serial.println((char *)DeskLight.lastread);
// toggle the relay for 10 ms
Serial.println(“Toggle Relay On for 10 ms & then Off”);
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!”);
}
OpenHab2 config: