While trying to mess with some mqtt issues like not being able to decode the payload. I started to use serial print for testing and it was giving me troubles so I switched to the log system. I see plenty of info from the system but nothing from the app.
Here is the code for a test argon. This is a extremely stripped code from a working system that is not using mqtt.
#include <MQTT.h>
#include <JsonParserGeneratorRK.h>
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
char gDeviceInfo[120]; //adjust size as required
SYSTEM_THREAD(ENABLED);
SerialLogHandler LogHandler;
String cloudname = "";
int led = D7;
int count = 0;
unsigned long lastTime = 0;
MQTT client("10.0.198.254", 1883, callback, true);
void callback(char* topic, byte* payload, unsigned int length) {
Log.info("Callback");
client.publish("go","a");
delay(30);
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
//if (!strcmp(topic,"LESS")){
if (!strcmp(p,"ds")){
client.publish("MORE","ds"); //not working. not reading payload
}
//}
int i = 0;
char pay[length+1];
for ( i; i < length; i++){
pay[i] = ((char)payload[i]);
Log.info("Count =%i", i);
}
client.publish("helpme",pay);
Log.trace("Here: %d", pay);
}
// setup() runs once, when the device is first turned on.
void setup() {
waitFor(Serial.isConnected, 30000);
delay(1000);
Particle.function("command",commandToggle);
Particle.variable("deviceInfo",gDeviceInfo);
snprintf(gDeviceInfo, sizeof(gDeviceInfo)
,"App: %s, Date: %s, Time: %s, Sysver: %s"
,__FILENAME__
,__DATE__
,__TIME__
,(const char*)System.version() // cast required for String
);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
client.connect("it");
delay(400);
if (client.isConnected()){
client.publish("out/here","startup");
client.subscribe("abc123");
client.subscribe("LESS");
}
}
// loop() runs over and over again, as quickly as it can execute.
void loop() {
// The core of your code will likely live here.
/*
if (client.isConnected()){
Serial.print("mqtt loop");
client.loop();
delay(3000);
}
else {
client.disconnect();
Serial.print("Reconnect");
client.connect("it");
client.subscribe("abc123");
client.subscribe("LESS");
delay(3000);
}
*/
}
void rrhalarm () {
JsonWriterStatic<256> jw;
{
JsonWriterAutoObject obj(&jw);
jw.insertKeyValue("id", 8);
jw.insertKeyValue("alarm", 0);
jw.insertKeyValue("temp", 22.34);
}
//if (!client.isConnected()){
//client.connect(System.deviceID());
//}
//if (client.isConnected()){
//client.loop();
client.publish("/topic_2", jw.getBuffer());
//client.disconnect();
//}
}
int commandToggle(String command) {
/* Particle.functions always take a string as an argument and return an integer.
Since we can pass a string, it means that we can give the program commands on how the function should be used.
In this case, telling the function "on" will turn the LED on and telling it "off" will turn the LED off.
Then, the function returns a value to us to let us know what happened.
In this case, it will return 1 for the LEDs turning on, 0 for the LEDs turning off,
and -1 if we received a totally bogus command that didn't do anything to the LEDs.
*/
if (command=="restart"){
System.reset();
return 1;
}
else if (command == "mqtt"){
int rr = rand() % (0-30+1) + 0;
if (!client.isConnected()){
client.connect(System.deviceID());
}
if (client.isConnected()){
client.loop();
client.publish("/topic_1", String(rr));
// client.disconnect();
}
return rr;
}
else if (command == "mq2"){
rrhalarm();
return 22;
}
else {
return -1;
}
}