Esp8266 Window Blinds MQTT

Hey @jjhtpc thank you for sharing your project!

I’m trying to use it with a FS90R continuous servo on a nodemcu but when I upload the sketch my servo just starts rotating nonstop(?)

Also on the sketch do we need to change every , , to the topic we want?

@Sthope I don’t think his sketch works with a continuous rotating servo.

@quadmasta sorry I didn’t get back with you I have family in town. I’m not sure the cover platform will help anyways as my servo is a continuous rotating and rotates about 20 times to close and open the blinds.

Not sure why you’d say that. It’s definitely possible.

@quadmasta OK I will get back with you in a few days Thanks

Hey as a quick update it appears Amazon fixed the open commands. So Alexa open the blinds works again.

The code posted does not want to connect to the internet. I have tried other codes and they work just fine.

I’m not too sure what you mean by the internet, if you mean to wifi, then did you enter your ssid and password properly to connect to your router? The only other items beyond that will be you topics. If all of those are filled in and you have mosquitto or another Mqtt service properly running, you should be good.

The ssid and password are correct. The Esp8266 does not connect to the wifi, as seen from the serial monitor. Other codes have worked fine in connecting to wifi and the mqtt server.

The pictures of the mechanism dont load for me, can you like post them also somewhere else so I can see the principle?

You can send me your code sans password and I can take a look. Beyond that I reloaded one of my esp8266s last night and all works well.

I was able to get it to work by mixing it with another code.

Thank you for your help and the awesome write up!

Let me know any feedback. If you want to setup a branch on GitHub that may help others too.

Either way congrats… the windows are by far my favorite automation.

If you get a chance post a video. I like seeing code in action.

I agree, my parrots room has 4 windows with blinds that we need to open and close everyday to give them sunlight and to help protect my neighbors ears when they go bananas at the end of the evenings and since we work in shifts we could ensure they always be open/close as needed.

I have been trying to use your project but I’m probably not understanding something related with the topics, I have been trying to set it up as you show on your configuration.yaml example.

my sketch atm is:

#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 "***.***.*.***" // Enter your MQTT server adderss or IP. I use my DuckDNS adddress (yourname.duckdns.org) in this field
#define mqtt_user "******" //enter your MQTT username
#define mqtt_password "************" //enter your password








WiFiClient espClient;
PubSubClient client(espClient);

Servo myservo;

int val;
int itsatrap = 0;


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 (itsatrap == 0 && mytopic == "blind/bc/command" && message.equals("ON")){  
      myservo.attach(D3);
      delay(500);
      myservo.write(90); 
      client.publish("blind/bc/state", "ON");
      delay(1000);
      myservo.detach();
      }
    else if (mytopic == "blind/br/command" && message.equalsIgnoreCase("OFF")){
      myservo.attach(D3);
      delay(500);
      myservo.write(0);  
      client.publish("blind/br/state", "OFF");
      delay(1000);
      myservo.detach();
    }
    else if (mytopic == "blind/bc/level"){
      myservo.attach(D3);
      delay(500);
      val = message.toInt(); //converts command to integer to be used for positional arrangement
      val = map (val, 0, 99, 0, 180);
      myservo.write(val);
      client.publish("blind/bc/state", "ON");
      delay(3000);
      myservo.detach();
      itsatrap = 1;
    }
    else{
        itsatrap = 0;
    }

}



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("ESPBlindstl666", mqtt_user, mqtt_password)) {
      Serial.println("connected");

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

any help is very appreciated :slight_smile:

Ok, I think I can help. It looks like in your void reconnect your topics don’t match what you have above in your void callback(). If you are using my .yaml syntax then the first subscribe should be /blind/bc/command, and the second subscribe should be /blind/bc/level.

Also in the first else if, in the void callback, you need to replace /blind/br/command with /blind/bc/command. As well as /blind/br/state with /blind/bc/state.

That is all I can see for now.

1 Like

Ok I got it working!! My mistake was thinking that BR and BC were meant to be the open and close :stuck_out_tongue: instead of 2 windows

Thank you jjhtpc!

Think my servo needs to be adjusted, ON and OFF rotates small portions the same direction.

One last question, do I need to adjust/set how long to rotate to fully open or close?

I’m not too sure, if you are using a standard analog rc servo looks the futaba s3003 it should rotate 90 degrees for on and then the opposite 90 degrees for off.

My current servo is digital FS90R

And ordered yesterday a better one (I hope) for my big curtains https://www.aliexpress.com/item/Official-smarian-360-Degree-Metal-Gear-High-Torque-Servo-MG995-for-Robot-Tank-Car-Arm-Clamp/32793920449.html?spm=a2g0s.9042311.0.0.qVF1Tz

It was late yesterday when I gave up but its only rotating 90 degrees the same way for ON and OFF and the brightness isn’t working as it should as well but hass already communicating so its a start :smiley:

I forked your project for better viewing online

Yes, you may have to adjust the code to work with digital servos. I would think there would be some examples on the Arduino forum.

I already bought the NodeMCU module : https://www.gearbest.com/transmitters-receivers-module/pp_366523.html?wid=21 and a pair of MG995 servos : https://www.gearbest.com/development-boards/pp_175428.html?wid=21

Are those servos working good with the code? I don’t know but I’ve read some bad reviews about them.