#include #include #include #include #include #include #include "config.h" // Only for ESP8266 D1 Mini #define ECHO 5 // D1 #define TRIG 16 // D0 #define POTI_IN 17 // A0, analog In WiFiClient client; MQTTPubSubClient mqtt; // Set Data pin for LED ring (we use GPIO3, which is also RX of the serial line) #define DATA_PIN 3 const uint16_t PixelCount = 8; NeoPixelBus strip(PixelCount, DATA_PIN); long duration; // here we measure the time needed by the ultrasonic pulse float distance; // the calculated distance int poti; // used to adjust the distance for yellow/red int percent=0; // will be used to calculate the current percentage of the distance reached int colorSaturation=255; // max color saturation for the colors (=brightness) // define some colors we use later RgbColor red(colorSaturation, 0, 0); RgbColor green(0, colorSaturation, 0); RgbColor yellow(colorSaturation, colorSaturation, 0); RgbColor blue(0, 0, colorSaturation); RgbColor white(colorSaturation); RgbColor black(0); void setup() { // start your network WiFi.begin(wifi_ssid, wifi_password); // connect to host client.connect(mqtt_server, 1883); // initialize mqtt client mqtt.begin(client); // connect to mqtt broker mqtt.connect("arduino", "public", "public"); // subscribe topic and callback which is called when /hello has come mqtt.subscribe("/hello", [](const String& payload, const size_t size) { Serial.print("/hello "); Serial.println(payload); }); // Setup the ultrasonic sensor PIN pinMode(TRIG, OUTPUT); // TRIG-Pin: Output pinMode(ECHO, INPUT); // ECHO-Pin: Input // Important to first initilizate Serial before the strip as // we are useing the RX PIN to connect the strip Serial.begin(9600); // Baudrate: 115200 // this resets all the neopixels to an off state strip.Begin(); for(int i=0; i150) { setGreen(); } else if (percent >100) { setYellow(); } else {setRed(); } // Anzeige der Entfernung im seriellen Monitor #ifdef DEBUG Serial.println("Poti: " + String(poti)); Serial.println("Entfernung: " + String(distance) + "cm"); Serial.println("Percent: " + String(percent) + " of goal value"); #endif delay(100); }