Hi everyone, i really need some help here.
I am running hass v0.54.0
1 x nodemcu v0.9 blue standard one.
1 x MG995 Servo that can run continuously - http://www.electronicoscaldas.com/datasheet/MG995_Tower-Pro.pdf
Here is my code for HASS:
# T-Room - troomblinds1 - NodeMCU device. - platform: mqtt name: "T-Room blinds1" state_topic: "/troomblinds1/state" command_topic: "/troomblinds1/command" payload_on: "open" payload_off: "close" optimistic: true qos: 1 retain: false
My nodemcu .ino code that i uploaded to the nodemcu via Arduino IDE:
#include <ESP8266WiFi.h>
#include <Servo.h>
#include <PubSubClient.h>
/************************* WiFi Access Point (CHANGE THIS) **********************************/
#define WLAN_SSID "wifi_ssid"
#define WLAN_PASS "wifi_password"
#define mqtt_user "mqtt_username"
#define mqtt_password "mqtt_password"
#define SENSORNAME "blinds1"
/************************* MQTT Setup (CHANGE THIS****************************************/
const char* mqtt_server = "mqtt_server_ip";
const char* username = "username";
const char* password = "password";
const char* topic_state = "/troomblinds1/state";
const char* topic_command = "/troomblinds1/command";
WiFiClient espClient;
PubSubClient client (espClient);
/*********************** Sketch Code ****************************************************/
Servo myservo;
int state = 0;
int prevstate = 0;
int dirc = 0;
int spintime = 0;
int servoPin = D4; //CHANGE TO WHATEVETR PIN YOUR USING
void MQTT_connect();
void callback(char* topic, byte* payload, unsigned int length);
void setup() {
Serial.begin(115200);
myservo.detach();
delay(10);
Serial.println("Blind Startup Sequence");
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void servo_move() {
Serial.println("State Change. Rotating Servo");
if ( dirc == 180) {
client.publish(topic_state,"opened");
}
else if (dirc == 0) {
client.publish(topic_state,"closed");
}
myservo.attach(servoPin);
myservo.write(dirc);
delay(spintime);
myservo.detach();
Serial.println("Returning to main loop");
return;
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived: [");
Serial.print(topic);
Serial.print(" ]");
char *blindcommand = (char *) payload; // convert mqtt byte array into char array
blindcommand[length] ='\0'; // Must delimit with 0
Serial.print(blindcommand);
if (strcmp(blindcommand,"open") == 0) {
Serial.println("OPEN COMMAND RECEIVED");
dirc = 180; // direction for servo to run
spintime = 20000; // << CHANGE TO WHATEVER TIME YOU NEED TO OPEN YOUR BLINDS
state = 1; //sets current state
}
else if (strcmp(blindcommand,"close") == 0) {
Serial.println("CLOSE COMMAND RECEIVED");
dirc = 0;
spintime = 18000; // << CHANGE TO WHATEVER TIME YOU NEED TO CLOSE YOUR BLINDS
state = 2; //sets current state
}
if (state != prevstate) {
Serial.println("State change!");
servo_move();
}
prevstate = state;
}
void MQTT_connect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(SENSORNAME, mqtt_user, mqtt_password)) {
Serial.println("MQTT connected");
client.subscribe(topic_command);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(1000); // wait 5 seconds
}
}
}
void loop() {
if (!client.connected()) {
MQTT_connect();
}
client.loop();
}
Everything is working great, the ONLY problem i have is this:
When i close my blinds, and if the nodemcu loose power, and starts again, its going to close state again, so the blinds is running the same way as the last time i run it.
I have been changing the nodemcu and the hass code alot of time, but i cant get this to work properly.
Can anyone help me in the right direction for my project?
Greetings DK.