ChristianKnorr

Untitled

Nov 28th, 2020
846
122 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 28.11.2020 installiert
  2.  
  3. int MOT_RAUF = 5; // D1
  4. int MOT_RUNTER = 4; // D2
  5. int BUT_RAUF = 0; // D3
  6. int BUT_RUNTER = 2; // D4
  7. bool fahrbetrieb = false;
  8. bool testbetrieb_rauf = false;
  9. bool testbetrieb_runter = false;
  10. unsigned long previousMillis = 0; // speichert wie viele Sekunden seit derletzten Änderung vergangen sind
  11. unsigned long interval = 22000;   // Fahrzeit in Milisekunden
  12. byte count {interval/1000}; // https://forum.arduino.cc/index.php?topic=652033.0
  13. //https://playground.arduino.cc/Learning/BlinkWithoutDelayDe/
  14. #include <FastLED.h>
  15. #define ENMOT_RUNTERLE_ARDUINOOTA
  16.  
  17. #ifdef ARDUINO_ARCH_ESP32
  18. #include <WiFi.h>
  19. #else
  20. #include <ESP8266WiFi.h>
  21. #endif
  22.  
  23. #include "fauxmoESP.h"
  24. fauxmoESP fauxmo;
  25. #define ID_DISCO "Discokugel"
  26.  
  27. #include <DNSServer.h>            //Local DNS Server used for redirecting all requests to the configuration portal
  28. #include <ESP8266WebServer.h>     //Local WebServer used to serve the configuration portal
  29. #include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
  30. #ifdef ENMOT_RUNTERLE_ARDUINOOTA
  31. #include <ArduinoOTA.h>
  32. #endif
  33.  
  34. void setup() {
  35.   pinMode(MOT_RAUF,OUTPUT);
  36.   pinMode(MOT_RUNTER,OUTPUT);
  37.   pinMode(LED_BUILTIN, OUTPUT); // LED als Output definieren
  38.   pinMode(BUT_RAUF, INPUT_PULLUP);
  39.   pinMode(BUT_RUNTER, INPUT_PULLUP);
  40.  
  41. #ifdef ENMOT_RUNTERLE_ARDUINOOTA
  42.   ArduinoOTA.setPassword("geheim");
  43. #endif
  44.  
  45.   Serial.begin(115200);
  46.   Serial.println("\n\nconnect to Discokugel and configure wifi");
  47.   WiFiManager wifiManager;
  48.   if (!wifiManager.autoConnect("Discokugel")) {
  49.     Serial.println("failed to connect and hit timeout");
  50.     //reset and try again, or maybe put it to deep sleep
  51.     ESP.reset();
  52.     delay(1000);
  53.   } else {
  54.   // Erfolg
  55.     Serial.println("connected to WIFI..");
  56.   }
  57.   digitalWrite(LED_BUILTIN, HIGH); // internal blue LED off
  58.  
  59. #ifdef ENMOT_RUNTERLE_ARDUINOOTA
  60.   ArduinoOTA.onStart([]() {
  61.     String type;
  62.     if (ArduinoOTA.getCommand() == U_FLASH) {
  63.       type = "sketch";
  64.     } else { // U_SPIFFS
  65.       type = "filesystem";
  66.     }
  67.  
  68.     // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
  69.     Serial.println("Start updating " + type);
  70.   });
  71.   ArduinoOTA.onEnd([]() {
  72.     Serial.println("\nEnd");
  73.   });
  74.   ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  75.     Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  76.   });
  77.   ArduinoOTA.onError([](ota_error_t error) {
  78.     Serial.printf("Error[%u]: ", error);
  79.     if (error == OTA_AUTH_ERROR) {
  80.       Serial.println("Auth Failed");
  81.     } else if (error == OTA_BEGIN_ERROR) {
  82.       Serial.println("Begin Failed");
  83.     } else if (error == OTA_CONNECT_ERROR) {
  84.       Serial.println("Connect Failed");
  85.     } else if (error == OTA_RECEIVE_ERROR) {
  86.       Serial.println("Receive Failed");
  87.     } else if (error == OTA_END_ERROR) {
  88.       Serial.println("End Failed");
  89.     }
  90.   });
  91.   ArduinoOTA.begin();
  92. #endif
  93.     // By default, fauxmoESP creates it's own webserver on the defined port
  94.     // The TCP port must be 80 for gen3 devices (default is 1901)
  95.     // This has to be done before the call to enable()
  96.     fauxmo.createServer(true); // not needed, this is the default value
  97. //    fauxmo.createServer(false); // not needed, this is the default value
  98.     fauxmo.setPort(80); // This is required for gen3 devices
  99.  
  100.     // You have to call enable(true) once you have a WiFi connection
  101.     // You can enable or disable the library at any moment
  102.     // Disabling it will prevent the devices from being discovered and switched
  103.     fauxmo.enable(true);
  104.  
  105.     // You can use different ways to invoke alexa to modify the devices state:
  106.     // "Alexa, turn yellow lamp on"
  107.     // "Alexa, turn on yellow lamp
  108.     // "Alexa, set yellow lamp to fifty" (50 means 50% of brightness, note, this example does not use this functionality)
  109.  
  110.     // Add virtual devices
  111.     fauxmo.addDevice(ID_DISCO);
  112.     fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
  113.        
  114.         // Callback when a command from Alexa is received.
  115.         // You can use device_id or device_name to choose the element to perform an action onto (relay, LED,...)
  116.         // State is a boolean (ON/OFF) and value a number from 0 to 255 (if you say "set kitchen light to 50%" you will receive a 128 here).
  117.         // Just remember not to delay too much here, this is a callback, exit as soon as possible.
  118.         // If you have to do something more involved here set a flag and process it in your main loop.
  119.        
  120.         Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);
  121.  
  122.         // Checking for device_id is simpler if you are certain about the order they are loaded and it does not change.
  123.         // Otherwise comparing the device_name is safer.
  124.  
  125.         if (strcmp(device_name, ID_DISCO)==0) {
  126.             switch (state) {
  127.               case 0: // aus = rauf
  128.                 Kugel_rauf(true);
  129.                 break;
  130.               default: // an = runter
  131.                 Kugel_runter(true);
  132.                 break;
  133.             }
  134.         }
  135.  
  136.     });
  137.   Serial.println("Kugel initial rauf.");
  138.   Kugel_rauf(true);
  139. }
  140.  
  141. void loop() {
  142.   #ifdef ENMOT_RUNTERLE_ARDUINOOTA
  143.     ArduinoOTA.handle();
  144.   #endif
  145.   fauxmo.handle();
  146.   if ( fahrbetrieb == true ) {
  147.     EVERY_N_MILLISECONDS(500) digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  148.     EVERY_N_SECONDS(1){
  149.       Serial.println(count);
  150.       count--;
  151.     }
  152.     if (millis() - previousMillis > interval) {
  153.       previousMillis = millis();   // aktuelle Zeit abspeichern
  154.       Kugel_stop();
  155.     }
  156.   }
  157.  
  158.   if ( !digitalRead(BUT_RAUF) ) {
  159.     Kugel_rauf(false);
  160.   } else {
  161.     if (  testbetrieb_rauf == true ) {
  162.       Kugel_stop();
  163.     }
  164.   }
  165.  
  166.   if ( !digitalRead(BUT_RUNTER) ) {
  167.     Kugel_runter(false);
  168.   } else {
  169.     if (  testbetrieb_runter == true ) {
  170.       Kugel_stop();
  171.     }
  172.   }
  173.  
  174. }
  175.  
  176. void Kugel_rauf(bool fahrweiter) {
  177.   Serial.print("Kugel rauf :");
  178.   Serial.println(millis());
  179.   digitalWrite(MOT_RAUF,HIGH);
  180.   digitalWrite(MOT_RUNTER,LOW);
  181.   if ( fahrweiter ) fahrbetrieb = true; else testbetrieb_rauf = true;
  182.   previousMillis = millis();   // aktuelle Zeit abspeichern
  183. }
  184.  
  185. void Kugel_runter(bool fahrweiter) {
  186.   Serial.print("Kugel runter :");
  187.   Serial.println(millis());
  188.   digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  189.   digitalWrite(MOT_RAUF,LOW);
  190.   digitalWrite(MOT_RUNTER,HIGH);
  191.   if ( fahrweiter ) fahrbetrieb = true; else testbetrieb_runter = true;
  192.   previousMillis = millis();   // aktuelle Zeit abspeichern
  193. }
  194.  
  195. void Kugel_stop() {
  196.   Serial.print("Kugel Stopp :");
  197.   Serial.println(millis());
  198.   digitalWrite(MOT_RAUF,LOW);
  199.   digitalWrite(MOT_RUNTER,LOW);
  200.   fahrbetrieb = false;
  201.   testbetrieb_rauf = false;
  202.   testbetrieb_runter = false;
  203.   digitalWrite(LED_BUILTIN, HIGH); // internal blue LED off
  204.   count = {interval/1000};
  205. }
  206.  
  207. //
RAW Paste Data