From ecef529a5847076e4b3a7fdba8e052060cb273c0 Mon Sep 17 00:00:00 2001 From: Stefan Date: Sun, 15 May 2022 15:34:05 +0200 Subject: [PATCH] Added first code for Wifi and MQTT --- Abstandssensor.ino | 23 +++++++++++++++++++++++ config.h | 4 ++++ 2 files changed, 27 insertions(+) create mode 100644 config.h diff --git a/Abstandssensor.ino b/Abstandssensor.ino index 867a700..b2a41d7 100644 --- a/Abstandssensor.ino +++ b/Abstandssensor.ino @@ -1,13 +1,21 @@ +#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 @@ -32,6 +40,21 @@ 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 diff --git a/config.h b/config.h new file mode 100644 index 0000000..2fb9d54 --- /dev/null +++ b/config.h @@ -0,0 +1,4 @@ + char* wifi_ssid = "your-ssid"; + char* wifi_password = "your-passwd"; +const char* mqtt_server = "your.mqtt.broker.org"; +const char* mqtt_topic = "/your/topic/to/listen/to";