MATLAB Answers

MQTT subscribe gets no data back

9 views (last 30 days)
Mar Ron
Mar Ron on 2 May 2020
I have a channel that has several feeds with live data. I am trying to use a particle Argon and thingspeak MQTT broker to subscribe to this data. I used the example provided and got nothing back from the subscription. I am pasting my code below. If anyone has a working example, that would be great!
#include "MQTT.h"
const long channelID = 10X4XXX;
String ReadAPIKey = "XXXXXXXXX";
String subscribeTopic = "channels/" + String( channelID ) + "/subscribe/fields/field1/"+String(ReadAPIKey);
String MQTTAPIKey = "XXXXXXXXXX";
void callback( char* topic, byte* payload, unsigned int length );
MQTT client( "mqtt.thingspeak.com" , 1883 , callback );
// This function processes the messages relayed by the MQTT broker.
void callback( char* topic, byte* payload, unsigned int length ) {
Serial.println("in callback");
char p[ length + 1 ]; // Leave an extra space to null terminate the string.
memcpy( p, payload, length );
p[ length ] = NULL; // Terminate the string.
}
void setup() {
Serial.println("in void setup");
// Connect to the server.
subscribeMQTT();
}
void loop() {
Serial.println("in void loop");
int timeHour = Time.hour();
if (client.isConnected()){
client.loop();
}
else{
subscribeMQTT();
}
if ( ( timeHour > 23 ) or ( timeHour < 4 ) ){
Particle.publish( "Sleep" );
System.sleep( SLEEP_MODE_DEEP , 7200 );
}
delay(20000);
}
void subscribeMQTT(){
Serial.println("in subscribe MQTT");
if (!client.isConnected()) {
client.connect( " MGSubscribeArgon" , "MGUsername" , MQTTAPIKey , NULL , MQTT::QOS0 , 0 , NULL , true );
Particle.publish( " Connect " );
Serial.println("Connected to broker");
delay( 1000 );
if ( client.isConnected()) {
Serial.println("MQTT connected");
client.subscribe( subscribeTopic );
Particle.publish( "subs" );
}
}
}

  0 Comments

Sign in to comment.

Answers (2)

Vinod
Vinod on 4 May 2020
Have you taken a look at this example? It should be possible to modify it for a Particle Argon quite easily.

  1 Comment

Mar Ron
Mar Ron on 5 May 2020
That IS the example I am using. In the code I posted above, the only part that I deleted was the "strncmp" portion. I deleted it in the above example by mistake, but I eventually just use serial.println() in the callback function to see what is being received. That callback function that is supposed to be called by the receipt of a packet on MQTT subscribe, is never triggered.

Sign in to comment.


Christopher Stapels
Christopher Stapels on 6 May 2020
Edited: Christopher Stapels on 6 May 2020
Can you try connecting with another desktop based broker (MATLAB has one) or MQTTFx to make sure you have all the api keys correct? If you verify that, I have an Argon here at home that I can re verify the code with. Its previously been verified with an photon.

  2 Comments

Mar Ron
Mar Ron on 8 May 2020
Chris,
I have publish working just fine. So I know I have my keys set up correctly.
Christopher Stapels
Christopher Stapels on 8 May 2020
Publish and subscribe have slightly different requirements, I have found using a different client can be a really good check to make sure everything is correct. I've failed to match the appropriate channel ID for example and the subscription broker just disconnects me without a messge. Unofrtunately its the nature of the protocol.
Ive done pub and sub MQTT on a photon previously so im pretty confident it will work. Have a look a the serial ourput, especially during the subscription method. Do you see all the test messages?
Ill see If I can get a test going on my argon soon, but my first step will be to check the connection with MQTTfx.

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!