Hello I've been working on combining 2 sketches for about a year on and off right now I have 2 different esp8266 nodemcu's running one code for each servo, I'm trying to free up a esp8266 and run the code on just one for both servos. I have combined both and get no errors the problem I'm having is no matter what Topic I send to open or close one servo, one will run and then the other one will. I want it to work if I send the twist topic I just want the twist servo to run, If I send the travel topic I just want the travel servo to run.
This is the code I am running on each esp8266 for now
#include <ESP8266WiFi.h>
#include <Servo.h>
#include <PubSubClient.h>
/***WiFi Access Point***/
#define WLAN_SSID "YOUR SSID HERE"
#define WLAN_PASS "YOUR PW HERE"
/*** MQTT Setup***/
const char* mqtt_server = "YOUR SERVER IP HERE";
const char* topic_state = "/blinds/state";
const char* topic_command = "/blinds/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);
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(" 433MHZ OPEN COMMAND RECEIVED");
dirc = 180; // direction for servo to run
spintime = 3700; // << CHANGE TO WHATEVER TIME YOU NEED TO OPEN YOUR BLINDS
state = 1; //sets current state
}
else if (strcmp(blindcommand,"close") == 0) {
Serial.println(" 433MHZ CLOSE COMMAND RECEIVED");
dirc = 0;
spintime = 3300; // << 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...");
if (client.connect("ESP8266Client")) {
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(5000); // wait 5 seconds
}
}
}
void loop() {
if (!client.connected()) {
MQTT_connect();
}
client.loop();
}
And here is the code I have been working on
#include <ESP8266WiFi.h>
#include <Servo.h>
#include <PubSubClient.h>
/***WiFi Access Point***/
#define WLAN_SSID "SSID"
#define WLAN_PASS "PASSWORD"
/*** MQTT Setup***/
const char* mqtt_server = "192.168.1.115";
// Topic to twist the blinds open or close
const char* twist_state = "Home/twist/state";
const char* twist_command = "Home/twist/command";
// Topic to move the blinds across the window
const char* travel_state = "Home/travel/state";
const char* travel_command = "Home/travel/command";
WiFiClient shades2;
PubSubClient client (shades2);
/***Sketch Code ***/
// TWIST SERVO
Servo twist;
int state_twist = 0;
int prevstate_twist = 0;
int dirc_twist = 0;
int spintime_twist = 0;
int servoPin_twist = D1; //CHANGE TO WHATEVETR PIN YOUR USING
// TRAVEL SERVO
Servo travel;
int state_travel = 0;
int prevstate_travel = 0;
int dirc_travel = 0;
int spintime_travel = 0;
int servoPin_travel = D2; //CHANGE TO WHATEVETR PIN YOUR USING
void MQTT_connect();
void callback(char* topic, byte* payload, unsigned int length);
void setup() {
Serial.begin(115200);
delay(10);
Serial.println("Shades2 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_twist == 180) {
client.publish(twist_state, "opened");
}
else if (dirc_twist == 0) {
client.publish(twist_state, "closed");
}
twist.attach(servoPin_twist);
twist.write(dirc_twist);
delay(spintime_twist);
twist.detach();
if ( dirc_travel == 180) {
client.publish(travel_state, "opened");
}
else if (dirc_travel == 0) {
client.publish(travel_state, "closed");
}
travel.attach(servoPin_travel);
travel.write(dirc_travel);
delay(spintime_travel);
travel.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 *twistcommand = (char *) payload; // convert mqtt byte array into char array
twistcommand[length] = '\0'; // Must delimit with 0
Serial.print(twistcommand);
if (strcmp(twistcommand, "open") == 0) {
Serial.println(" 433MHZ OPEN COMMAND RECEIVED");
dirc_twist = 180; // direction for servo to run
spintime_twist = 480; // << CHANGE TO WHATEVER TIME YOU NEED TO OPEN YOUR BLINDS
state_twist = 1; //sets current state
}
else if (strcmp(twistcommand, "close") == 0) {
Serial.println(" 433MHZ CLOSE COMMAND RECEIVED");
dirc_twist = 0;
spintime_twist = 480; // << CHANGE TO WHATEVER TIME YOU NEED TO CLOSE YOUR BLINDS
state_twist = 2; //sets current state
}
if (state_twist != prevstate_twist) {
Serial.println("State change!");
servo_move();
}
prevstate_twist = state_twist;
//second servo
char *travelcommand = (char *) payload; // convert mqtt byte array into char array
travelcommand[length] = '\0'; // Must delimit with 0
Serial.print(travelcommand);
if (strcmp(travelcommand, "open") == 0) {
Serial.println(" 433MHZ OPEN COMMAND RECEIVED");
dirc_travel = 180; // direction for servo to run
spintime_travel = 11480; // << CHANGE TO WHATEVER TIME YOU NEED TO OPEN YOUR BLINDS
state_travel = 1; //sets current state
}
else if (strcmp(travelcommand, "close") == 0) {
Serial.println(" 433MHZ CLOSE COMMAND RECEIVED");
dirc_travel = 0;
spintime_travel = 11480; // << CHANGE TO WHATEVER TIME YOU NEED TO CLOSE YOUR BLINDS
state_travel = 2; //sets current state
}
if (state_travel != prevstate_travel) {
Serial.println("State change!");
servo_move();
}
prevstate_travel = state_travel;
}
void MQTT_connect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("Shades2Client")) {
Serial.println("MQTT Connected!");
client.subscribe(twist_command);
client.subscribe(travel_command);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000); // wait 5 seconds
}
}
}
void loop() {
if (!client.connected()) {
MQTT_connect();
}
client.loop();
}
Today I tried changing the void servo_move to void twist and void travel, but got the same results
Thank you