KnxEsp/src/main.cpp
2024-10-08 18:09:12 +02:00

71 lines
1.2 KiB
C++

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <KnxTpUart.h>
#include <PubSubClient.h>
KnxTpUart knx(&Serial1, "15.15.20");
PubSubClient mqtt;
void knxSetup();
void knxLoop();
void mqttLoop();
void mqttSetup();
void wifiSetup();
void setup() {
Serial.begin(112500);
delay(500);
Serial.print("\n\n\nStartup\n");
knxSetup();
wifiSetup();
configTime("Europe/Berlin", "de.pool.ntp.org");
mqttSetup();
}
void wifiSetup() {
WiFi.hostname("KnxEsp");
WiFi.begin("HappyNet", "1Grausame!Sackratte7");
while (WiFi.localIP() == 0UL) {
delay(100);
}
}
void loop() {
knxLoop();
mqttLoop();
}
void mqttSetup() {
mqtt.setServer("10.0.0.50", 1883);
mqtt.setBufferSize(512);
}
void mqttLoop() {
if (!mqtt.loop()) {
if (!mqtt.connect(WiFi.hostname().c_str())) {
mqtt.disconnect();
}
}
}
void knxSetup() {
Serial1.begin(19200, SERIAL_8E1);
knx.uartReset();
knx.setListenToBroadcasts(true);
}
void knxLoop() {
auto type = knx.serialEvent();
if (type != KNX_TELEGRAM) {
return;
}
KnxTelegram *t = knx.getReceivedTelegram();
Serial.printf("%3d / %3d / %3d len=%d, bool=%d\n", t->getTargetMainGroup(), t->getTargetMiddleGroup(), t->getTargetSubGroup(), t->getPayloadLength(), t->getBool());
}