I’m trying to read UDP messages coming from an old Owl Intuition device which reports electricity usage and heating details, and I want to then send those details to Home Assistant via an MQTT broker.
I’ve been able to write sketches to send messages through MQTT. And different sketches that sniff out the UDP messages and serial print the data.
What I can’t do is mix the 2. As soon as I introduce the following lines
before Setup() and nothing else into the working UDP sniffer sketch, the UDP (?) packetBuffer seems to get corrupted/not cleared and nothing looks sensible anymore.
Clearly I’m no expert on this but can anyone help send me in the right direction, or is it impossible to have the 2 network thing going on at the same time?
I’m doing this on an Arduino Uno with the 5100 Ethernet shield. (I originally wanted to do it on a 8266 but the wifi was not picking up the UDP packets!)
I thought you were on to something there but there is no client object in the UDP flows.
More investigation is telling me that it is more likely to do with lack of memory and corruption going in the Strings. I’m changing tack now to try and do it with char arrays and trying to make the code as small as possible.
The following code works - it receives and Serial prints the UDP messages (they arrive about every 10 seconds) AND it publishes the topic ok, although it does need to reconnect about 1 in 4 cycles. The problems all come when i bloat out the code with String manipulation code to extract the values and build the payload I actually want to publish.
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <PubSubClient.h>
#define buffer_size 360
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, xxx); // Give the Arduino an IP address
unsigned int localPort = 22600; // local port to listen on
char packetBuffer[buffer_size]; //buffer to hold incoming packet,
EthernetUDP Udp;
EthernetClient ardClient;
PubSubClient client(ardClient);
void setup() {
// start the Ethernet and UDP
Serial.begin(115200);
Ethernet.begin(mac,ip);
Udp.begin(localPort);
client.setServer("192.168.0.xxx", 1883);
}
void loop() {
int packetSize = Udp.parsePacket();
if(packetSize){ // The packet has some size
Udp.read(packetBuffer,packetSize); //buffer_size);
for(int i=0; i < packetSize; i++) Serial.print(char(packetBuffer[i]));
Serial.println();
// Code for extracting the data and building the publish payload goes here
for(int i=0;i<buffer_size;i++) packetBuffer[i] = 0; // and clear the buffer
if (!client.connected()) {
reconnect();
}
Serial.println("Still connected");
client.publish("owl/message", "true"); // test topic and payload
client.loop();
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ArduinoClient", "user", "password")) {
Serial.println("connected");
client.setBufferSize(256);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000); // Wait 5 seconds before retrying
}
}
}