Guest User

curtainopener

a guest
Mar 23rd, 2022
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3. #include <ezButton.h>
  4. #include <ArduinoOTA.h>
  5.  
  6. #define DEBUG 1
  7.  
  8. #if DEBUG == 1
  9. #define debug(x) Serial.print(x)
  10. #define debugln(x) Serial.println(x)
  11. #else
  12. #define debug(x)
  13. #define debugln(x)
  14. #endif
  15.  
  16. const char* ssid = "xxxxxxxxxxx";
  17. const char* password =  "xxxxxxxx";
  18. const char* mqttServer = "xxxxxxxx";
  19. const int mqttPort = xxxxxxxx;
  20. const char* mqttUser = "xxxxxxxx";
  21. const char* mqttPassword = "xxxxxxxx";
  22. const char* mqttTopic = "esp/curtain-loggia/set";
  23. const char* mqttTopicDebug = "esp/curtain-loggia/debug";
  24. const char* mqttTopicState = "esp/curtain-loggia";
  25. const char* mqttClient = "curtain-loggia";
  26. const char* currentState = "unknown";
  27.  
  28. const int stepPin = D2;
  29. const int dirPin = D1;
  30. //const int enablePin = D0;
  31. const int enablePin = D3;
  32. const int M3Pin = D6;
  33. const int M2Pin = D7;
  34. //const int M1Pin = D8;
  35. const int M1Pin = D4;
  36.  
  37.  
  38. ezButton limitSwitch(D5);
  39. WiFiClient espClient;
  40. PubSubClient client(espClient);
  41.  
  42. void setup() {
  43.  
  44.   Serial.begin(115200);
  45.  
  46.   pinMode(stepPin,OUTPUT);
  47.   pinMode(dirPin,OUTPUT);
  48.   pinMode(enablePin,OUTPUT);
  49.   pinMode(M3Pin,OUTPUT);
  50.   pinMode(M2Pin,OUTPUT);
  51.   pinMode(M1Pin,OUTPUT);
  52.   digitalWrite(enablePin,HIGH);
  53.   limitSwitch.setDebounceTime(50);
  54.  
  55.   WiFi.begin(ssid, password);
  56.  
  57.   while (WiFi.status() != WL_CONNECTED) {
  58.     delay(1000);
  59.     debugln("Connecting to WiFi..");
  60.   }
  61.   debugln("Connected to the WiFi network");
  62.  
  63.   ArduinoOTA.setHostname(mqttClient);
  64.  
  65.   ArduinoOTA.onStart([]() {
  66.     debugln("Start");
  67.   });
  68.   ArduinoOTA.onEnd([]() {
  69.     debugln("\nEnd");
  70.   });
  71.   ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  72.     debug("Progress: %u%%\r");
  73.     debugln(progress / (total / 100));
  74.   });
  75.   ArduinoOTA.onError([](ota_error_t error) {
  76.     Serial.printf("Error[%u]: ", error);
  77.     if (error == OTA_AUTH_ERROR) debugln("Auth Failed");
  78.     else if (error == OTA_BEGIN_ERROR) debugln("Begin Failed");
  79.     else if (error == OTA_CONNECT_ERROR) debugln("Connect Failed");
  80.     else if (error == OTA_RECEIVE_ERROR) debugln("Receive Failed");
  81.     else if (error == OTA_END_ERROR) debugln("End Failed");
  82.   });
  83.   ArduinoOTA.begin();
  84.  
  85.   client.setServer(mqttServer, mqttPort);
  86.   client.setCallback(callback);
  87.   client.setKeepAlive(300);
  88.   while (!client.connected()) {
  89.     debugln("Connecting to MQTT...");
  90.  
  91.     if (client.connect(mqttClient, mqttUser, mqttPassword )) {
  92.  
  93.       debugln("connected");  
  94.  
  95.     } else {
  96.  
  97.       debug("failed with state ");
  98.       debug(client.state());
  99.       delay(2000);
  100.  
  101.     }
  102.   }
  103.  
  104.   client.publish(mqttTopic, "Ready!");
  105.   client.subscribe(mqttTopic);
  106.  
  107. }
  108.  
  109. void callback(char* topic, byte* payload, unsigned int length) {
  110.   debug("Message arrived in topic: ");
  111.   debugln(topic);
  112.   String messageChar;
  113.  
  114.   debug("Message:");
  115.   for (int i = 0; i < length; i++) {
  116.     messageChar += (char)payload[i];
  117.   }
  118.  
  119.   debugln(messageChar);
  120.  
  121.   if(messageChar == "ON"){
  122.     client.publish(mqttTopicDebug, "Order received!");
  123.     motorMove(true);
  124.     const char* currentState = "ON";
  125.   }
  126.   if(messageChar == "OFF"){
  127.     client.publish(mqttTopicDebug, "Order received!");
  128.     motorMove(false);
  129.     const char* currentState = "OFF";
  130.   }
  131.  
  132.   debugln();
  133.   debugln("-----------------------");
  134.  
  135. }
  136.  
  137. void motorMove(boolean motorDirection){
  138.   limitSwitch.loop();
  139.  
  140.   digitalWrite(enablePin,LOW);
  141.   delay(100);
  142.   digitalWrite(M1Pin,HIGH);
  143.   digitalWrite(M2Pin,HIGH);
  144.   digitalWrite(M3Pin,LOW);
  145.   digitalWrite(dirPin,motorDirection);
  146.  
  147.   while(1) {
  148.     limitSwitch.loop();
  149.     yield();
  150.     digitalWrite(stepPin,HIGH);
  151.     delayMicroseconds(100);
  152.     digitalWrite(stepPin,LOW);
  153.     delayMicroseconds(100);
  154.    
  155.     if(limitSwitch.getState() == LOW){
  156.      
  157.       digitalWrite(dirPin, (!motorDirection));
  158.       delay(500);
  159.       // Quarter turn back
  160.       for(int x = 0; x < 1000; x++) {
  161.         digitalWrite(stepPin,HIGH);
  162.         delayMicroseconds(500);
  163.         digitalWrite(stepPin,LOW);
  164.         delayMicroseconds(500);
  165.       }
  166.      
  167.       if(motorDirection){
  168.         client.publish(mqttTopicState, "ON");          
  169.       }else{
  170.         client.publish(mqttTopicState, "OFF");          
  171.       }
  172.      
  173.       client.publish(mqttTopicDebug, "Order executed!");
  174.       digitalWrite(enablePin,HIGH);  
  175.       return;
  176.      
  177.     }
  178.    
  179.   }
  180.  
  181.   delay(100);
  182.   return;
  183. }
  184.  
  185. void reconnect() {
  186.   // Loop until we're reconnected
  187.   while (!client.connected()) {
  188.     debug("Attempting MQTT connection...");
  189.     // Create a random client ID
  190.     String clientId = "ESP8266Client-";
  191.     clientId += String(random(0xffff), HEX);
  192.     // Attempt to connect
  193.     if (client.connect(mqttClient, mqttUser, mqttPassword )) {
  194.       debugln("connected");
  195.       // Once connected, publish an announcement...
  196.       client.publish(mqttTopicDebug, "hello world");
  197.       // ... and resubscribe
  198.       client.subscribe(mqttTopic);
  199.     } else {
  200.       debug("failed, rc=");
  201.       debug(client.state());
  202.       debugln(" try again in 5 seconds");
  203.       // Wait 5 seconds before retrying
  204.       delay(5000);
  205.     }
  206.   }
  207. }
  208.  
  209. void loop() {
  210.   ArduinoOTA.handle();
  211.   client.loop();
  212. }
Advertisement
Add Comment
Please, Sign In to add comment