In Node-Red I have some Poll State nodes that send the API data every ten seconds:
In the NodeMC sketch, I subscribe to those topics. When those topics come into the callback function, I handle them each a little differently.
Here’s the code. It’s a bit long because I have some debug print statements, and I am sure someone with more C experience could write it tighter. [Which is weird, because x=x_1 compiles exactly the same as x=+1, but the former is more readable. The same for if() tests. Experienced programmers like to put if-then on one line, but spread over three or four lines, it compiles exactly the same.]
(By the way, my ten minute timeout isn’t working yet.)
// ********************************** mqtt callback *************************************
// This function is executed when some device publishes a message to a topic that this ESP8266 is subscribed to.
void callback(String topic, byte * message, unsigned int length) {
digitalWrite(ledPin, ledON); // Turn on LED and start timing it.
ledMillis = millis();
bgTimestamp = now(); // Used to flag if more than tenMinutes without a bg read has elapsed.
Serial.println();
Serial.println();
Serial.print(F("Message arrived on topic: "));
Serial.print(topic);
Serial.println(F("."));
Serial.print("messageString: ");
// Convert the character array to a string
String messageString;
for (int i = 0; i < length; i++) {
messageString += (char)message[i];
}
messageString.trim();
Serial.print(messageString);
Serial.println();
Serial.print(F("Length= "));
Serial.print(length);
Serial.println();
if (topic == dateTopic) {
Serial.print (F("Date= "));
Serial.println(messageString);
}
if (topic == bgTopic) {
//What we really want to do is display the msg on the HT16K33 I2C display.
//Format the array, right-justified, leading spaces, but leaving the rightmost display character empty.
//for trend up/down arrows.
//Send the data to the display
if (length < 2) {
//Should never see this
alpha4.writeDigitAscii(0, '?');
alpha4.writeDigitAscii(1, '?');
alpha4.writeDigitAscii(2, '?');
alpha4.writeDigitAscii(3, ' ');
alpha4.writeDisplay();
}
else if (length == 2) {
alpha4.writeDigitAscii(0, ' ');
alpha4.writeDigitAscii(1, message[0]);
alpha4.writeDigitAscii(2, message[1]);
alpha4.writeDigitAscii(3, ' ');
alpha4.writeDisplay();
}
else if (length == 3) {
alpha4.writeDigitAscii(0, message[0]);
alpha4.writeDigitAscii(1, message[1]);
alpha4.writeDigitAscii(2, message[2]);
alpha4.writeDigitAscii(3, ' ');
alpha4.writeDisplay();
}
else if (length > 3) {
//Should never see this
alpha4.writeDigitAscii(0, '?');
alpha4.writeDigitAscii(1, '?');
alpha4.writeDigitAscii(2, '?');
alpha4.writeDigitAscii(3, ' ');
alpha4.writeDisplay();
}
}
if (topic == trendTopic) {
switch (messageString.toInt())
{
case 1:
alpha4.writeDigitAscii(3, '1');
alpha4.writeDisplay();
break;
case 2:
alpha4.writeDigitRaw(3, 513); //Segments A+J
alpha4.writeDisplay();
break;
case 3:
alpha4.writeDigitRaw(3, 1024); //Segment K
alpha4.writeDisplay();
break;
case 4:
alpha4.writeDigitRaw(3, 128); //Segment G2
alpha4.writeDisplay();
break;
case 5:
alpha4.writeDigitRaw(3, 8192); //Segment N
alpha4.writeDisplay();
break;
default:
alpha4.writeDigitAscii(3, '?');
alpha4.writeDisplay();
Serial.print(F("Trend? "));
Serial.print(messageString);
}
}
}
Did this help?