Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #include <ezButton.h>
- #include <ArduinoOTA.h>
- #define DEBUG 1
- #if DEBUG == 1
- #define debug(x) Serial.print(x)
- #define debugln(x) Serial.println(x)
- #else
- #define debug(x)
- #define debugln(x)
- #endif
- const char* ssid = "xxxxxxxxxxx";
- const char* password = "xxxxxxxx";
- const char* mqttServer = "xxxxxxxx";
- const int mqttPort = xxxxxxxx;
- const char* mqttUser = "xxxxxxxx";
- const char* mqttPassword = "xxxxxxxx";
- const char* mqttTopic = "esp/curtain-loggia/set";
- const char* mqttTopicDebug = "esp/curtain-loggia/debug";
- const char* mqttTopicState = "esp/curtain-loggia";
- const char* mqttClient = "curtain-loggia";
- const char* currentState = "unknown";
- const int stepPin = D2;
- const int dirPin = D1;
- //const int enablePin = D0;
- const int enablePin = D3;
- const int M3Pin = D6;
- const int M2Pin = D7;
- //const int M1Pin = D8;
- const int M1Pin = D4;
- ezButton limitSwitch(D5);
- WiFiClient espClient;
- PubSubClient client(espClient);
- void setup() {
- Serial.begin(115200);
- pinMode(stepPin,OUTPUT);
- pinMode(dirPin,OUTPUT);
- pinMode(enablePin,OUTPUT);
- pinMode(M3Pin,OUTPUT);
- pinMode(M2Pin,OUTPUT);
- pinMode(M1Pin,OUTPUT);
- digitalWrite(enablePin,HIGH);
- limitSwitch.setDebounceTime(50);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- debugln("Connecting to WiFi..");
- }
- debugln("Connected to the WiFi network");
- ArduinoOTA.setHostname(mqttClient);
- ArduinoOTA.onStart([]() {
- debugln("Start");
- });
- ArduinoOTA.onEnd([]() {
- debugln("\nEnd");
- });
- ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
- debug("Progress: %u%%\r");
- debugln(progress / (total / 100));
- });
- ArduinoOTA.onError([](ota_error_t error) {
- Serial.printf("Error[%u]: ", error);
- if (error == OTA_AUTH_ERROR) debugln("Auth Failed");
- else if (error == OTA_BEGIN_ERROR) debugln("Begin Failed");
- else if (error == OTA_CONNECT_ERROR) debugln("Connect Failed");
- else if (error == OTA_RECEIVE_ERROR) debugln("Receive Failed");
- else if (error == OTA_END_ERROR) debugln("End Failed");
- });
- ArduinoOTA.begin();
- client.setServer(mqttServer, mqttPort);
- client.setCallback(callback);
- client.setKeepAlive(300);
- while (!client.connected()) {
- debugln("Connecting to MQTT...");
- if (client.connect(mqttClient, mqttUser, mqttPassword )) {
- debugln("connected");
- } else {
- debug("failed with state ");
- debug(client.state());
- delay(2000);
- }
- }
- client.publish(mqttTopic, "Ready!");
- client.subscribe(mqttTopic);
- }
- void callback(char* topic, byte* payload, unsigned int length) {
- debug("Message arrived in topic: ");
- debugln(topic);
- String messageChar;
- debug("Message:");
- for (int i = 0; i < length; i++) {
- messageChar += (char)payload[i];
- }
- debugln(messageChar);
- if(messageChar == "ON"){
- client.publish(mqttTopicDebug, "Order received!");
- motorMove(true);
- const char* currentState = "ON";
- }
- if(messageChar == "OFF"){
- client.publish(mqttTopicDebug, "Order received!");
- motorMove(false);
- const char* currentState = "OFF";
- }
- debugln();
- debugln("-----------------------");
- }
- void motorMove(boolean motorDirection){
- limitSwitch.loop();
- digitalWrite(enablePin,LOW);
- delay(100);
- digitalWrite(M1Pin,HIGH);
- digitalWrite(M2Pin,HIGH);
- digitalWrite(M3Pin,LOW);
- digitalWrite(dirPin,motorDirection);
- while(1) {
- limitSwitch.loop();
- yield();
- digitalWrite(stepPin,HIGH);
- delayMicroseconds(100);
- digitalWrite(stepPin,LOW);
- delayMicroseconds(100);
- if(limitSwitch.getState() == LOW){
- digitalWrite(dirPin, (!motorDirection));
- delay(500);
- // Quarter turn back
- for(int x = 0; x < 1000; x++) {
- digitalWrite(stepPin,HIGH);
- delayMicroseconds(500);
- digitalWrite(stepPin,LOW);
- delayMicroseconds(500);
- }
- if(motorDirection){
- client.publish(mqttTopicState, "ON");
- }else{
- client.publish(mqttTopicState, "OFF");
- }
- client.publish(mqttTopicDebug, "Order executed!");
- digitalWrite(enablePin,HIGH);
- return;
- }
- }
- delay(100);
- return;
- }
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- debug("Attempting MQTT connection...");
- // Create a random client ID
- String clientId = "ESP8266Client-";
- clientId += String(random(0xffff), HEX);
- // Attempt to connect
- if (client.connect(mqttClient, mqttUser, mqttPassword )) {
- debugln("connected");
- // Once connected, publish an announcement...
- client.publish(mqttTopicDebug, "hello world");
- // ... and resubscribe
- client.subscribe(mqttTopic);
- } else {
- debug("failed, rc=");
- debug(client.state());
- debugln(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- void loop() {
- ArduinoOTA.handle();
- client.loop();
- }
Advertisement
Add Comment
Please, Sign In to add comment