So tomorrow afternoon I have my flat for a few hours and my plan is to try to setup the mqtt as it seems this is the way to go…
So I have a few question in that mather to @Dim , I tried to modify you code:
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &RollerCurtainMasterBedRoom) {
Serial.print(F("Got: ")); Serial.println((char *)RollerCurtainMasterBedRoom.lastread);
// how to obtain the value of steps sent from OpenHab??
motor.step(stepsFromOH);
//How to make sure that this never happens??
if maxUp = HIGH {
//Report back to OH that the curtain is max up
}
//Do this on every reboot of OH..
//Startup rule of OH: sendMqtt recalibrate, set curtain switch to up, set curtain slider to 0%
//if received command recalibrate , run motor up untill end switch kicks in...
motor.step(-9999);
while maxUp = LOW {}
}
}
The stepper motor library which I am using is located here:
In Openhabian I can activate mqtt, what do I need to alter in my arduino sketch @ThomDietrich to make it work?
/************************* MQTT Broker Setup *********************************/
#define mqtt_server "<mqtt_broker>" //What does OPenhabian use??
#define mqtt_serverport 1883 //What does OPenhabian use?? // use 8883 for SSL
#define mqtt_username "<mqtt_username>" //What does OPenhabian use??
#define mqtt_password "<mqtt_password>" //What does OPenhabian use??
Openhabian enable the MQTT binding and add the mqtt.cfg file correct?
How should my simple item file look like?
Switch RollerCurtainMasterBedRoom {mqtt="<direction>[<broker>:<topic>:<type>:<transformer>], <direction>[<broker>:<topic>:<type>:<transformation>], ..."}
Here is my complete arduino code:
/*
* 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>
#include <StepperMotor.h>
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "<ssid>" //I change this of course
#define WLAN_PASS "<pass>" // I change this of course
/************************* MQTT Broker Setup *********************************/
#define mqtt_server "<mqtt_broker>" //What does OPenhabian use??
#define mqtt_serverport 1883 //What does OPenhabian use?? // use 8883 for SSL
#define mqtt_username "<mqtt_username>" //What does OPenhabian use??
#define mqtt_password "<mqtt_password>" //What does OPenhabian use??
/************************* Constants, Variables, Integers, etc *********************************/
const int maxUp = D1; //My endswitch
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;
// Create the stepper object
StepperMotor motor(8,9,10,11);
// 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 'RollerCurtainMasterBedRoom' for monitoring topic for changes.
Adafruit_MQTT_Subscribe RollerCurtainMasterBedRoom = Adafruit_MQTT_Subscribe(&mqtt, "openhab/out/RollerCurtainMasterBedRoom/command");
/*************************** Sketch Code ************************************/
void MQTT_connect();
void setup() {
Serial.begin(115200);
Serial.println(""); Serial.println(F("Booting... v1.0"));
// Set up the stepper motor
pinMode(maxUp, INPUT);
motor.setStepDuration(1);
// 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(&RollerCurtainMasterBedRoom);
// Begin OTA
ArduinoOTA.setPort(8266); // Port defaults to 8266
ArduinoOTA.setHostname("RollerCurtainMasterBedRoom"); // 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 == &RollerCurtainMasterBedRoom) {
Serial.print(F("Got: ")); Serial.println((char *)RollerCurtainMasterBedRoom.lastread);
// how to obtain the value of steps sent from OpenHab??
motor.step(stepsFromOH);
//How to make sure that this never happens??
if maxUp = HIGH {
//Report back to OH that the curtain is max up
}
//Do this on every reboot of OH..
//Startup rule of OH: sendMqtt recalibrate, set curtain switch to up, set curtain slider to 0%
//if received command recalibrate , run motor up untill end switch kicks in...
motor.step(-9999);
while maxUp = LOW {}
}
}
// 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!");
I will update this post with some photos of the IKEA hack when I get it to work:)