2022-05-15 15:34:05 +02:00
|
|
|
#include <WiFi.h>
|
|
|
|
|
2022-05-03 22:19:00 +02:00
|
|
|
#include <NeoPixelBus.h>
|
|
|
|
#include <NeoPixelSegmentBus.h>
|
|
|
|
#include <NeoPixelAnimator.h>
|
|
|
|
#include <NeoPixelBrightnessBus.h>
|
|
|
|
|
2022-05-15 15:34:05 +02:00
|
|
|
#include <MQTTPubSubClient.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2022-05-03 22:19:00 +02:00
|
|
|
// Only for ESP8266 D1 Mini
|
2022-05-15 15:23:24 +02:00
|
|
|
#define ECHO 5 // D1
|
|
|
|
#define TRIG 16 // D0
|
2022-05-03 22:19:00 +02:00
|
|
|
#define POTI_IN 17 // A0, analog In
|
|
|
|
|
2022-05-15 15:34:05 +02:00
|
|
|
WiFiClient client;
|
|
|
|
MQTTPubSubClient mqtt;
|
2022-05-15 15:23:24 +02:00
|
|
|
|
|
|
|
// Set Data pin for LED ring (we use GPIO3, which is also RX of the serial line)
|
2022-05-03 22:19:00 +02:00
|
|
|
#define DATA_PIN 3
|
|
|
|
const uint16_t PixelCount = 8;
|
|
|
|
NeoPixelBus<NeoGrbFeature, NeoEsp8266Dma800KbpsMethod> strip(PixelCount, DATA_PIN);
|
|
|
|
|
2022-05-15 15:23:24 +02:00
|
|
|
long duration; // here we measure the time needed by the ultrasonic pulse
|
|
|
|
float distance; // the calculated distance
|
2022-05-03 22:19:00 +02:00
|
|
|
|
2022-05-15 15:23:24 +02:00
|
|
|
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
|
2022-05-03 22:19:00 +02:00
|
|
|
|
2022-05-15 15:23:24 +02:00
|
|
|
int colorSaturation=255; // max color saturation for the colors (=brightness)
|
|
|
|
|
|
|
|
// define some colors we use later
|
2022-05-03 22:19:00 +02:00
|
|
|
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() {
|
|
|
|
|
2022-05-15 15:34:05 +02:00
|
|
|
// 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);
|
|
|
|
});
|
|
|
|
|
2022-05-15 15:23:24 +02:00
|
|
|
// Setup the ultrasonic sensor PIN
|
2022-05-03 22:19:00 +02:00
|
|
|
pinMode(TRIG, OUTPUT); // TRIG-Pin: Output
|
|
|
|
pinMode(ECHO, INPUT); // ECHO-Pin: Input
|
2022-05-15 15:23:24 +02:00
|
|
|
|
|
|
|
// Important to first initilizate Serial before the strip as
|
|
|
|
// we are useing the RX PIN to connect the strip
|
2022-05-03 22:19:00 +02:00
|
|
|
Serial.begin(9600); // Baudrate: 115200
|
2022-05-15 15:23:24 +02:00
|
|
|
|
2022-05-03 22:19:00 +02:00
|
|
|
// this resets all the neopixels to an off state
|
|
|
|
strip.Begin();
|
|
|
|
for(int i=0; i<PixelCount; i++) {strip.SetPixelColor(i, red);}
|
|
|
|
strip.Show();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void setColor(RgbColor c) {
|
|
|
|
for(int i=0; i<PixelCount; i++) {
|
|
|
|
strip.SetPixelColor(i, c);
|
|
|
|
}
|
|
|
|
strip.Show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setRed() {
|
|
|
|
setColor(red);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setYellow() {
|
|
|
|
setColor(yellow);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setGreen() {
|
|
|
|
setColor(green);
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
|
|
|
|
|
|
|
|
// Read poti (Value range 16 - 1024)
|
|
|
|
poti=analogRead(POTI_IN);
|
|
|
|
|
|
|
|
// Measure Distance
|
|
|
|
digitalWrite(TRIG, LOW); delayMicroseconds(2);
|
|
|
|
|
|
|
|
// TRIG-Pin ist HIGH für 10 Microsekunden
|
|
|
|
digitalWrite(TRIG, HIGH);
|
|
|
|
delayMicroseconds(10);
|
|
|
|
digitalWrite(TRIG, LOW);
|
|
|
|
|
|
|
|
// Liest das ECHO aus und speichert die Zeit von Senden / Empfangen in Microsekunden
|
|
|
|
duration = pulseIn(ECHO, HIGH);
|
|
|
|
|
|
|
|
// Berechnung der Entfernung
|
|
|
|
// Da der Weg doppel ist: Hinweg - Rückweg, muss der Wert durch 2 geteilt werden
|
|
|
|
distance = duration * 0.034 / 2;
|
|
|
|
|
|
|
|
// Set color depending on poti
|
|
|
|
// We assume poti value/20 (resulting in range <1,5 cm - 100cm)
|
|
|
|
// Green if we are more than 50% above goal, yellow below 50% of goal, red at or below goal
|
|
|
|
|
|
|
|
percent=distance/((float)poti/10)*100;
|
|
|
|
|
|
|
|
if (percent >150) { 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);
|
|
|
|
|
|
|
|
}
|