What I have is simple sketch to allow me to controll continous servo in both direction aswell as stopping it. What I'd love to archive is to add collision sensor to stop it in open possition but while pressed command it to move other direction. I was able to stop it with button but my limited coding and understanding make it impossible to move it again when button was pressed. Could you please help me with coding as I'm a beginer.
That's what is working. the only things I need is to add collision sensor to stop it, and move blinds with this switch pressed. I couldn't make it work.
/***************************************************
Adafruit MQTT Library ESP8266 Example
Must use ESP8266 Arduino from:
https://github.com/esp8266/Arduino
Works great with Adafruit's Huzzah ESP board:
----> https://www.adafruit.com/product/2471
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Tony DiCola for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include <ESP8266WiFi.h>
#include <Servo.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
/************************* WiFi Access Point (CHANGE THIS) *********************************/
#define WLAN_SSID "x"
#define WLAN_PASS "x"
/************************* MQTT Setup (CHANGE THIS) *********************************/
#define AIO_SERVER "x"
#define AIO_SERVERPORT "x"
#define AIO_USERNAME "x"
#define AIO_KEY "x"
/************ Global State (Don't Change) ******************/
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// Store the MQTT server, username, and password in flash memory.
// This is required for using the Adafruit MQTT library.
const char MQTT_SERVER[] = AIO_SERVER;
const char MQTT_USERNAME[] = AIO_USERNAME;
const char MQTT_PASSWORD[] = AIO_KEY;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD);
/****************************** Feeds (CHANGE THESE)***************************************/
const char BLINDSTATUS[] = AIO_USERNAME "/blinds/state";
Adafruit_MQTT_Publish blindstatuspub = Adafruit_MQTT_Publish(&mqtt, BLINDSTATUS);
const char BLINDCOMMAND[] = AIO_USERNAME "/blinds/command";
Adafruit_MQTT_Subscribe blindcommandsub = Adafruit_MQTT_Subscribe(&mqtt, BLINDCOMMAND);
/*************************** 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 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());
mqtt.subscribe(&blindcommandsub);
}
void loop() {
MQTT_connect();
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(1000))) {
if (subscription == &blindcommandsub) {
Serial.print(F("Receieved command: "));
String blindcommand = (char *)blindcommandsub.lastread;
Serial.print(blindcommand);
if ( blindcommand == "off") {
myservo.attach(servoPin);
Serial.println("OPEN");
dirc = 180; // direction for servo to run
myservo.write(dirc);
blindstatuspub.publish("opening");
}
if ( blindcommand == "on" ) {
myservo.attach(servoPin);
Serial.println("CLOSE");
dirc = 0;
myservo.write(dirc);
blindstatuspub.publish("closing");
}
if ( blindcommand == "stop") {
myservo.attach(servoPin);
Serial.println("STOP");
myservo.detach();
}
}
if (! mqtt.ping()) {
mqtt.disconnect();
}
delay(500);
}
}
void MQTT_connect() {
int8_t ret;
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
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(5000); // wait 5 seconds
}
Serial.println("MQTT Connected!");
}