Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 28.11.2020 installiert
- int MOT_RAUF = 5; // D1
- int MOT_RUNTER = 4; // D2
- int BUT_RAUF = 0; // D3
- int BUT_RUNTER = 2; // D4
- bool fahrbetrieb = false;
- bool testbetrieb_rauf = false;
- bool testbetrieb_runter = false;
- unsigned long previousMillis = 0; // speichert wie viele Sekunden seit derletzten Änderung vergangen sind
- unsigned long interval = 22000; // Fahrzeit in Milisekunden
- byte count {interval/1000}; // https://forum.arduino.cc/index.php?topic=652033.0
- //https://playground.arduino.cc/Learning/BlinkWithoutDelayDe/
- #include <FastLED.h>
- #define ENMOT_RUNTERLE_ARDUINOOTA
- #ifdef ARDUINO_ARCH_ESP32
- #include <WiFi.h>
- #else
- #include <ESP8266WiFi.h>
- #endif
- #include "fauxmoESP.h"
- fauxmoESP fauxmo;
- #define ID_DISCO "Discokugel"
- #include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal
- #include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
- #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
- #ifdef ENMOT_RUNTERLE_ARDUINOOTA
- #include <ArduinoOTA.h>
- #endif
- void setup() {
- pinMode(MOT_RAUF,OUTPUT);
- pinMode(MOT_RUNTER,OUTPUT);
- pinMode(LED_BUILTIN, OUTPUT); // LED als Output definieren
- pinMode(BUT_RAUF, INPUT_PULLUP);
- pinMode(BUT_RUNTER, INPUT_PULLUP);
- #ifdef ENMOT_RUNTERLE_ARDUINOOTA
- ArduinoOTA.setPassword("geheim");
- #endif
- Serial.begin(115200);
- Serial.println("\n\nconnect to Discokugel and configure wifi");
- WiFiManager wifiManager;
- if (!wifiManager.autoConnect("Discokugel")) {
- Serial.println("failed to connect and hit timeout");
- //reset and try again, or maybe put it to deep sleep
- ESP.reset();
- delay(1000);
- } else {
- // Erfolg
- Serial.println("connected to WIFI..");
- }
- digitalWrite(LED_BUILTIN, HIGH); // internal blue LED off
- #ifdef ENMOT_RUNTERLE_ARDUINOOTA
- ArduinoOTA.onStart([]() {
- String type;
- if (ArduinoOTA.getCommand() == U_FLASH) {
- type = "sketch";
- } else { // U_SPIFFS
- type = "filesystem";
- }
- // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
- Serial.println("Start updating " + type);
- });
- ArduinoOTA.onEnd([]() {
- Serial.println("\nEnd");
- });
- ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
- Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
- });
- ArduinoOTA.onError([](ota_error_t error) {
- Serial.printf("Error[%u]: ", error);
- if (error == OTA_AUTH_ERROR) {
- Serial.println("Auth Failed");
- } else if (error == OTA_BEGIN_ERROR) {
- Serial.println("Begin Failed");
- } else if (error == OTA_CONNECT_ERROR) {
- Serial.println("Connect Failed");
- } else if (error == OTA_RECEIVE_ERROR) {
- Serial.println("Receive Failed");
- } else if (error == OTA_END_ERROR) {
- Serial.println("End Failed");
- }
- });
- ArduinoOTA.begin();
- #endif
- // By default, fauxmoESP creates it's own webserver on the defined port
- // The TCP port must be 80 for gen3 devices (default is 1901)
- // This has to be done before the call to enable()
- fauxmo.createServer(true); // not needed, this is the default value
- // fauxmo.createServer(false); // not needed, this is the default value
- fauxmo.setPort(80); // This is required for gen3 devices
- // You have to call enable(true) once you have a WiFi connection
- // You can enable or disable the library at any moment
- // Disabling it will prevent the devices from being discovered and switched
- fauxmo.enable(true);
- // You can use different ways to invoke alexa to modify the devices state:
- // "Alexa, turn yellow lamp on"
- // "Alexa, turn on yellow lamp
- // "Alexa, set yellow lamp to fifty" (50 means 50% of brightness, note, this example does not use this functionality)
- // Add virtual devices
- fauxmo.addDevice(ID_DISCO);
- fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
- // Callback when a command from Alexa is received.
- // You can use device_id or device_name to choose the element to perform an action onto (relay, LED,...)
- // 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).
- // Just remember not to delay too much here, this is a callback, exit as soon as possible.
- // If you have to do something more involved here set a flag and process it in your main loop.
- Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);
- // Checking for device_id is simpler if you are certain about the order they are loaded and it does not change.
- // Otherwise comparing the device_name is safer.
- if (strcmp(device_name, ID_DISCO)==0) {
- switch (state) {
- case 0: // aus = rauf
- Kugel_rauf(true);
- break;
- default: // an = runter
- Kugel_runter(true);
- break;
- }
- }
- });
- Serial.println("Kugel initial rauf.");
- Kugel_rauf(true);
- }
- void loop() {
- #ifdef ENMOT_RUNTERLE_ARDUINOOTA
- ArduinoOTA.handle();
- #endif
- fauxmo.handle();
- if ( fahrbetrieb == true ) {
- EVERY_N_MILLISECONDS(500) digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
- EVERY_N_SECONDS(1){
- Serial.println(count);
- count--;
- }
- if (millis() - previousMillis > interval) {
- previousMillis = millis(); // aktuelle Zeit abspeichern
- Kugel_stop();
- }
- }
- if ( !digitalRead(BUT_RAUF) ) {
- Kugel_rauf(false);
- } else {
- if ( testbetrieb_rauf == true ) {
- Kugel_stop();
- }
- }
- if ( !digitalRead(BUT_RUNTER) ) {
- Kugel_runter(false);
- } else {
- if ( testbetrieb_runter == true ) {
- Kugel_stop();
- }
- }
- }
- void Kugel_rauf(bool fahrweiter) {
- Serial.print("Kugel rauf :");
- Serial.println(millis());
- digitalWrite(MOT_RAUF,HIGH);
- digitalWrite(MOT_RUNTER,LOW);
- if ( fahrweiter ) fahrbetrieb = true; else testbetrieb_rauf = true;
- previousMillis = millis(); // aktuelle Zeit abspeichern
- }
- void Kugel_runter(bool fahrweiter) {
- Serial.print("Kugel runter :");
- Serial.println(millis());
- digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
- digitalWrite(MOT_RAUF,LOW);
- digitalWrite(MOT_RUNTER,HIGH);
- if ( fahrweiter ) fahrbetrieb = true; else testbetrieb_runter = true;
- previousMillis = millis(); // aktuelle Zeit abspeichern
- }
- void Kugel_stop() {
- Serial.print("Kugel Stopp :");
- Serial.println(millis());
- digitalWrite(MOT_RAUF,LOW);
- digitalWrite(MOT_RUNTER,LOW);
- fahrbetrieb = false;
- testbetrieb_rauf = false;
- testbetrieb_runter = false;
- digitalWrite(LED_BUILTIN, HIGH); // internal blue LED off
- count = {interval/1000};
- }
- //
RAW Paste Data