Esp8266 Window Blinds MQTT

Could you tell me what happens to your blinds when you have a short power outage? Do your blinds remain the same, and what is the value in Home Assistant?
I notice that when I power on the arduino, the servo always starts turning for a moment.

It happens to me tooā€¦ I would like to see how to change that.

I have the same issue. BUT what i do is: i Always define a start position. So OR they go to the start position with power on, or they are in start position with power on, in that case the servo cannot go further and stays where he starts. I my case startposition is ā€˜doorā€™ closed.

in answer to badstraw, yes the modified MG995 are working fineā€¦https://youtu.be/YSDZHeLJjLM

i want to give this a go but stuck on which servo to go for, am i correct im thinking a digital servo will not work ?

The only servo I have ever used is the futaba s3003. It is analog and has worked great.

Thanks for the response i shall grab one

This sketch can work with a continuing rotation servo, you just need to be careful about how long you let it spin.

I use this sketch on roller blinds: hereā€™s the code I use

#include <Servo.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiAP.h>
#include <ESP8266WiFiGeneric.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WiFiScan.h>
#include <ESP8266WiFiSTA.h>
#include <ESP8266WiFiType.h>
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>

#include <PubSubClient.h>

/************ WIFI and MQTT INFORMATION (CHANGE THESE FOR YOUR SETUP) ******************/
#define wifi_ssid "" //enter your WIFI SSID
#define wifi_password "" //enter your WIFI Password

#define mqtt_server "" // 
#define mqtt_user "" //enter your MQTT username
#define mqtt_password "" //enter your password

WiFiClient espClient;
PubSubClient client(espClient);

Servo myservo;

void setup() {
  Serial.begin(115200);
  setup_wifi();

  client.setServer(mqtt_server, 1883); //CHANGE PORT HERE IF NEEDED
  client.setCallback(callback);

}


void setup_wifi() {

  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(wifi_ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(wifi_ssid, wifi_password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  char p[length + 1];
  memcpy(p, payload, length);
  p[length] = NULL;
  String message(p);
  String mytopic(topic);
  if (mytopic == "blind/payload" && message == "OFF") {
    myservo.attach(D4);
    Serial.println("turning blinds off");
    delay(500);
    myservo.write(0);
    client.publish("blind/state", "OFF");
    delay(33000); //spinTime
    myservo.detach();
  }
  
  if (mytopic == "blind/payload" && message == "ON") {
    myservo.attach(D4);
    Serial.println("Connecting servo");
    delay(500);
    myservo.write(180);
    Serial.println("turning blinds on");
    client.publish("blind/state", "ON");
    delay(33000); //spinTime
    myservo.detach();
  }
}



void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();
}


void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESPBlindstl", mqtt_user, mqtt_password)) {
      Serial.println("connected");

      client.subscribe("blind/payload");
      } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

Does have the annoying habit of whenever it loses power, it defaults to the open position - but thatā€™s not such a huge issue, because if it keeps rotating, it just flops around and goes in circles till itā€™s done, then it works again (annoying if this happens in the middle of the night cos the servos arenā€™t exactly quietā€¦)

Iā€™m using MG995 servos.

If anyone can tell me how to get this to stop defaulting to ON everytime it loses power, that would be awesome

EDIT: Iā€™m using it for roller blinds (hence the long spin time)

3 Likes

Did you find a solution on this, because i have the same issue that you have.
Every time the nodemcu loss power, its turn the motor back again ):
Please help

No, I havenā€™t found a way to stop this. Iā€™d imagine it would be possible to have it generate some kind of start position, so when the network connects, it moves to a specific angle, then waits for the command to be received on the subscribed topic, but I have no idea how to implement this.

Iā€™m assuming it has something to do with the way my callback function is set up, but I have NO idea what Iā€™m doing there anyway - it was a copy/paste job! Iā€™m a TOTAL novice when it comes to programming - I can generally read and understand whatā€™s going on, but when it comes to writing the code myself, hopeless!

Let me know if you figure it out.

1 Like

Solution here: Window Blinds - MQTT / Nodemcu problems

1 Like

Has anyone ever thought of taking apart the tilting mechanism of the blinds and somehow attaching a servo to it. This way the servo would tiilt the mechanism, which is great because the mechanism, has a locking feature in which the shaft wont move. Many people donā€™t need this but, my blinds go all the way to the ground and this would be really helpful.

Thought about it, but there is quite a bit of pressure on that set of strings. I donā€™t know if there is a servo that could be nicely hidden with enough torque. It would definitely require an additional power source beyond the voltage of vin.

How do you define a start position? No matter what I do as soon as I connect the nodemcu the servo keeps running for a few seconds and then after a little while It lets me control it again normally.

What are you guys using for power options to power the board? Iā€™m new to the whole NodeMCU chip. Is there a reason why this is the suggested chip?

Cheap. $5 per board with built in wireless. Canā€™t beat it.
I guess you can match it with a Pi Zero W but then you still have to buy an sd card.

You can power with a 5v dc source or just power via usb

Makes sense. I just bought the three suggested components. Going to give this a shot on one blind and if it works out well do several more.

I bought these, do they look okay to do this?
https://www.amazon.com/gp/product/B074FYMV53/ref=oh_aui_detailpage_o01_s00?ie=UTF8&psc=1
https://www.amazon.com/gp/product/B0015H2V72/ref=oh_aui_detailpage_o01_s00?ie=UTF8&psc=1
https://www.amazon.com/gp/product/B00P2NQONM/ref=oh_aui_detailpage_o00_s00?ie=UTF8&psc=1

They look like what I purchased. I did this project months ago and got it working pretty easily, but couldnā€™t get it reliable enough for continuous use. My blinds seemed to need too much effort and as some other have mentioned it was kind of tough to get the ā€˜timingā€™ tuned correctly.

What was the issue with reliability? Wifi disconnects? MQTT disconnects? I see a few people mention issues with the servo turning if the board power cycles.

I think on the MQTT broker you need to turn off the persist command, beyond that if they do get out of sorts a quick shout to Alexa cures the problem. Beyond that I thought that the alignment was pretty easy. Just set the servo to itā€™s 0 position (unplugged if course) and then set the blinds to closed up. You will need to trim the opposite end of the blind control rod to fit the servo and remove the manual pulley strings (you will no longer need them). That should get you about perfect. I have six large windows and they all look like they are positioned identical.

No, MQTT works quite well.

I think it was the physical connection of my blinds to the servo that was lacking, which I take responsibility for.

Forge ahead!