I'm trying to make an automatic curtain closer mechanism.
I'm using alexa to open and close the curtains, but want a 2 momentary button to move the stepper motor either way until I release the button.
I have found to separate bits of code that work perfectly independently but when i add the button code to the MQTT code the button code doesn't work?
Help and advice appreciated.
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#define WIFI_SSID "BT-********"
#define WIFI_PASS "**********"
#define MQTT_SERV "io.adafruit.com"
#define MQTT_PORT 1883
#define MQTT_NAME ""
#define MQTT_PASS "**"
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, MQTT_SERV, MQTT_PORT, MQTT_NAME, MQTT_PASS);
// I named my adafruit feed "openclose"
Adafruit_MQTT_Subscribe openclose = Adafruit_MQTT_Subscribe(&mqtt, MQTT_NAME "/f/openclose");
boolean curtainOpen = false;
#define SLEEP 16 //D0(gpio16)
#define STEP 13 //D7(gpio13)
#define DIR 0 //D3(gpio0)
#define CW 9 //SD2(gpio9)
#define CCW 10 //SD3(gpio10)
#define STEPS_PER_ROTATION 1200
void setup()
{
Serial.begin(9600);
//Connect to WiFi
Serial.print("\n\nConnecting Wifi... ");
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
Serial.println("Connected");
//Subscribe to the openclose topic
mqtt.subscribe(&openclose);
pinMode(SLEEP, OUTPUT);
pinMode(STEP, OUTPUT);
pinMode(DIR, OUTPUT);
pinMode(CW, INPUT);
pinMode(CCW, INPUT);
}
void loop()
{
// Button ----------------
{
if(digitalRead(CCW))
digitalWrite(DIR, HIGH);
else if(digitalRead(CW))
digitalWrite(DIR, LOW);
else
// These four lines result in 1 step:
if (digitalRead(CCW) || digitalRead(CW))
{
digitalWrite(STEP, HIGH);
delayMicroseconds(500);
digitalWrite(STEP, LOW);
delayMicroseconds(500);
}
}
// End of Button ----------
//Connect/Reconnect to MQTT
MQTT_connect();
//Read from our subscription queue until we run out, or
//wait up to 5 seconds for subscription to update
Adafruit_MQTT_Subscribe * subscription;
while ((subscription = mqtt.readSubscription(5000)))
{
//If we're in here, a subscription updated...
if (subscription == &openclose)
{
//Print the new value to the serial monitor
Serial.print("openclose: ");
Serial.println((char*) openclose.lastread);
//If the switch is triggered and the curtain is closed, open it
//Otherwise, if the curtain is open, close the curtain.
if (!strcmp((char*) openclose.lastread, "trigger") && !curtainOpen)
{
openCurtain();
curtainOpen = true;
}
else if (!strcmp((char*) openclose.lastread, "trigger") && curtainOpen)
{
closeCurtain();
curtainOpen = false;
}
}
}
// ping the server to keep the mqtt connection alive
if (!mqtt.ping()) {
mqtt.disconnect();
}
}
void openCurtain() {
Serial.print("Opening the curtain...");
Serial.println(F("dir HIGH"));
digitalWrite(DIR, HIGH);
moveCurtain();
Serial.println(F("SLEEP LOW"));
digitalWrite(SLEEP, LOW);
Serial.print("Done opening curtain.");
//delay(1000); // waits for a second
}
void closeCurtain() {
Serial.print("Closing the curtain...");
Serial.println(F("dir LOW"));
digitalWrite(DIR, LOW);
moveCurtain();
Serial.println(F("SLEEP LOW"));
digitalWrite(SLEEP, LOW);
Serial.print("Done closing curtain");
//delay(1000); // waits for a second
}
// for the motor -------------
void stepNow(int totalSteps) {
Serial.print(totalSteps);
Serial.println(F(" steps."));
int i;
for (i = 0; i < totalSteps; ++i) {
digitalWrite(STEP, HIGH);
delay(1);
digitalWrite(STEP, LOW);
delay(1);
}
}
//----------------------------
void moveCurtain() {
delay(5);
stepNow(STEPS_PER_ROTATION);
}
void MQTT_connect()
{
int8_t ret;
// Stop if already connected
if (mqtt.connected())
{
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) // connect will return 0 for connected
{
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5); // wait 5 seconds
retries--;
if (retries == 0)
{
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}