I have an Uno set up with some code that receives RF on 433MHz (weather station, energy meter), decodes it, and then either prints to serial or publishes to MQTT. I had an issue last week where using the SimpleTimer library to output every 30 seconds would result in the first print to work but the second to be ignored. I assumed this was an issue with the library, so I replaced the code with a basic delta Millis() check. This works for Serial.print, but for some reason when I try to publish to MQTT I get the issue again. I've been trying to narrow down the cause, but I'm at a loss. I didn't want to publish the whole code since it's a bit long, so I'm posting just the relevant segments (I hope). I don't think I'm doing anything exotic, but a second set of eyes would be very much appreciated.
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#define REPORT_TIME 30000 // 30 seconds
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
byte server[] = { 192, 168, 0, 200 };
byte ip[] = { 192, 168, 0, 70};
void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
}
EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);
long previousMillis = 0;
void setup () {
Serial.begin(38400);
Ethernet.begin(mac, ip);
if (client.connect("arduinoClient")) {
client.publish("ookDecoder", "Arduino ookDecoder online");
}
}
void loop () {
//will have issues with rollover
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > REPORT_TIME) {
// save the last time you blinked the LED
previousMillis = currentMillis;
blueline.MQTTreport(client);
acurite5n1.MQTTreport(client);
}
/* do all the RF receive and decoding here */
}
void MQTTreport (PubSubClient client) {
static char data[100];
String packet = "";
packet += "Windspeed=" + String(convKphMph(windspeedkph)) + ", ";
packet += "Winddir=" + String(winddir) + ", ";
packet += "Rainfall=" + String(rainfall) + ", ";
packet += "TempF=" + String(tempf) + ", ";
packet += "Humidity=" + String(humidity) + ", ";
packet += "battery=";
if (batteryok) {
packet += "OK";
} else {
packet += "LOW";
}
packet.toCharArray(data,100);
if (client.connect("arduinoClient"))
client.publish("acurite5n1",data);
}
void MQTTreport (PubSubClient client) {
static char data[100];
String packet = "";
packet += "TotalEnergy=" + String(g_RxWattHours) + ", ";
packet += "CurrentPower=" + String(g_RxWatts) + ", ";
packet += "TempF=" + String(g_RxTemperature) + ", ";
packet += "battery=";
if (g_battStatus == true) {
packet += "OK";
} else {
packet += "LOW";
}
packet.toCharArray(data,100);
if (client.connect("arduinoClient"))
client.publish("blueline",data);
}
And this is what I see when monitoring MQTT:
...
blueline TotalEnergy=19963, CurrentPower=1576, TempF=55, battery=OK
blueline TotalEnergy=19963, CurrentPower=1576, TempF=55, battery=OK
blueline TotalEnergy=19963, CurrentPower=1576, TempF=55, battery=OK
blueline TotalEnergy=19963, CurrentPower=1576, TempF=55, battery=OK
blueline TotalEnergy=19963, CurrentPower=1576, TempF=55, battery=OK
blueline TotalEnergy=19963, CurrentPower=1576, TempF=55, battery=OK
blueline TotalEnergy=19963, CurrentPower=1576, TempF=55, battery=OK
blueline TotalEnergy=19963, CurrentPower=1576, TempF=55, battery=OK
...
If I comment out "blueline.MQTTreport(client);", this is what I see:
acurite5n1 Windspeed=0.00, Winddir=135.00, Rainfall=0.00, TempF=51.80, Humidity=99, battery=OK
acurite5n1 Windspeed=1.14, Winddir=157.50, Rainfall=0.00, TempF=52.00, Humidity=99, battery=OK
This is exactly the same issue I saw when using the SimpleTimer library. I'm at a loss. I can post the full code if that would help.
Moderator edit: CODE TAGS. Why is it so difficult to use CODE TAGS?
ookDecoder-ben4.ino (3.42 KB)
DecodeOOK.h (2.62 KB)
Acurite5n1.h (9.71 KB)
Blueline.h (10.1 KB)
temp_lerp.h (963 Bytes)