Hello,
I’m trying to build automated curtains with a stepper motor. I want to make this controllable over MQTT so I tried to implement this in my Arduino sketch. I get a MQTT connection and I see that something got pulished in the topic but apparentely the ESP8266 doesn’t get the message and doesn’t controls the stepper. And as soon as the stepper made for example 10 turns and the curtains are closed it should publish the closed state in the /state topic.
Here’s my Arduino sketch (I use a Wemos D1 and DRV882 driver):
#include ESP8266WiFi.h #include PubSubClient.h // Update these with values suitable for your network. const char* ssid = "SSID"; const char* password = "PW"; const char* mqtt_server = "IP"; WiFiClient espClient; PubSubClient client(espClient); int Index; int payLoad; void setup_wifi() { delay(100); // We start by connecting to a WiFi network Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } randomSeed(micros()); Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Command from MQTT broker is : ["); Serial.print(topic); int p =(char)payload[0]; if(p==1) { digitalWrite(D4,HIGH); for(Index = 0; Index < 2000; Index++) { digitalWrite(D5,HIGH); delayMicroseconds(500); digitalWrite(D5,LOW); delayMicroseconds(500); } client.publish("blindbr/state", "0"); Serial.print("on"); } else if(p==0) { digitalWrite(D4,LOW); for(Index = 0; Index < 2000; Index++) { digitalWrite(D5,HIGH); delayMicroseconds(500); digitalWrite(D5,LOW); delayMicroseconds(500); } client.publish("blindbr/state", "1"); Serial.print("off"); } Serial.println(); } // Serial.println(); //end callback void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Create a random client ID String clientId = "ESP8266Client-"; clientId += String(random(0xffff), HEX); // Attempt to connect //if you MQTT broker has clientID,username and password //please change following line to if (client.connect(clientId,userName,passWord)) if (client.connect(clientId.c_str()), "homeassistant", "lucy") { Serial.println("connected"); //once connected to MQTT broker, subscribe command if any client.subscribe("blindbr/move"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 6 seconds before retrying delay(6000); } } } //end reconnect() void setup() { Serial.begin(115200); setup_wifi(); client.setServer(mqtt_server, 1883); client.setCallback(callback); // set the speed at 80 rpm: pinMode(D6, OUTPUT); //Enable pinMode(D5, OUTPUT); //Step pinMode(D4, OUTPUT); //Direction digitalWrite(D6,LOW); } void loop() { if (!client.connected()) { reconnect(); } client.loop(); }
And here’s my HA config part:
switch: - platform: mqtt name: Curtains state_topic: "blindbr/state" command_topic: "blindbr/move" payload_open: "1" payload_close: "0" state_open: "1" state_closed: "0" optimistic: false
Thank you!
Kidn regards