Wifi working, mqtt still to do
This commit is contained in:
parent
1c41e514f7
commit
bbec6e6f49
1 changed files with 51 additions and 22 deletions
|
@ -1,4 +1,4 @@
|
||||||
#include <WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
|
|
||||||
#include <NeoPixelBus.h>
|
#include <NeoPixelBus.h>
|
||||||
#include <NeoPixelSegmentBus.h>
|
#include <NeoPixelSegmentBus.h>
|
||||||
|
@ -20,7 +20,7 @@ MQTTPubSubClient mqtt;
|
||||||
// Set Data pin for LED ring (we use GPIO3, which is also RX of the serial line)
|
// Set Data pin for LED ring (we use GPIO3, which is also RX of the serial line)
|
||||||
#define DATA_PIN 3
|
#define DATA_PIN 3
|
||||||
const uint16_t PixelCount = 8;
|
const uint16_t PixelCount = 8;
|
||||||
NeoPixelBus<NeoGrbFeature, NeoEsp8266Dma800KbpsMethod> strip(PixelCount, DATA_PIN);
|
NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod > strip(PixelCount, DATA_PIN);
|
||||||
|
|
||||||
long duration; // here we measure the time needed by the ultrasonic pulse
|
long duration; // here we measure the time needed by the ultrasonic pulse
|
||||||
float distance; // the calculated distance
|
float distance; // the calculated distance
|
||||||
|
@ -38,30 +38,61 @@ RgbColor blue(0, 0, colorSaturation);
|
||||||
RgbColor white(colorSaturation);
|
RgbColor white(colorSaturation);
|
||||||
RgbColor black(0);
|
RgbColor black(0);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void callback(char* topic, byte* payload, unsigned int length) {
|
||||||
|
Serial.print("Message arrived [");
|
||||||
|
Serial.print(topic);
|
||||||
|
Serial.print("] ");
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
Serial.print((char)payload[i]);
|
||||||
|
}
|
||||||
|
Serial.println();
|
||||||
|
|
||||||
|
// Switch on the LED if an 1 was received as first character
|
||||||
|
if ((char)payload[0] == '1') {
|
||||||
|
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
|
||||||
|
// but actually the LED is on; this is because
|
||||||
|
// it is acive low on the ESP-01)
|
||||||
|
} else {
|
||||||
|
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
// Important to first initilizate Serial before the strip as
|
||||||
|
// we are useing the RX PIN to connect the strip
|
||||||
|
Serial.begin(115200); // Baudrate: 115200
|
||||||
|
|
||||||
// start your network
|
// start your network
|
||||||
WiFi.begin(wifi_ssid, wifi_password);
|
WiFi.mode(WIFI_STA);
|
||||||
// connect to host
|
WiFi.begin(wifi_ssid, wifi_password);
|
||||||
client.connect(mqtt_server, 1883);
|
|
||||||
// initialize mqtt client
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
mqtt.begin(client);
|
delay(500);
|
||||||
// connect to mqtt broker
|
Serial.print(".");
|
||||||
mqtt.connect("arduino", "public", "public");
|
}
|
||||||
|
|
||||||
// subscribe topic and callback which is called when /hello has come
|
randomSeed(micros());
|
||||||
mqtt.subscribe("/hello", [](const String& payload, const size_t size) {
|
|
||||||
Serial.print("/hello ");
|
Serial.println(""); Serial.println("WiFi connected");
|
||||||
Serial.println(payload);
|
Serial.println("IP address: "); Serial.println(WiFi.localIP());
|
||||||
});
|
|
||||||
|
// 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(mqtt_topic, [](const String& payload, const size_t size) {
|
||||||
|
Serial.print(mqtt_topic); Serial.println(payload);
|
||||||
|
});
|
||||||
|
|
||||||
// Setup the ultrasonic sensor PIN
|
// Setup the ultrasonic sensor PIN
|
||||||
pinMode(TRIG, OUTPUT); // TRIG-Pin: Output
|
pinMode(TRIG, OUTPUT); // TRIG-Pin: Output
|
||||||
pinMode(ECHO, INPUT); // ECHO-Pin: Input
|
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
|
// this resets all the neopixels to an off state
|
||||||
strip.Begin();
|
strip.Begin();
|
||||||
|
@ -121,11 +152,9 @@ void loop() {
|
||||||
else {setRed(); }
|
else {setRed(); }
|
||||||
|
|
||||||
// Anzeige der Entfernung im seriellen Monitor
|
// Anzeige der Entfernung im seriellen Monitor
|
||||||
#ifdef DEBUG
|
|
||||||
Serial.println("Poti: " + String(poti));
|
Serial.println("Poti: " + String(poti));
|
||||||
Serial.println("Entfernung: " + String(distance) + "cm");
|
Serial.println("Entfernung: " + String(distance) + "cm");
|
||||||
Serial.println("Percent: " + String(percent) + " of goal value");
|
Serial.println("Percent: " + String(percent) + " of goal value");
|
||||||
#endif
|
delay(1000);
|
||||||
delay(100);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue