Not receiving any data by subscribing(mqtt) from arduino client | ThingSpeak Projects | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
Not receiving any data by subscribing(mqtt) from arduino client
No permission to create posts
January 27, 2018
7:08 am
Avatar
Vignesh maker

Silver
Members
Forum Posts: 11
Member Since:
January 25, 2018
sp_UserOfflineSmall Offline

There is only documentation for publishing to thingspeak from Arduino client using mqtt. Can anyone help me out to subscribe a channel from an arduino client and receive data using mqtt. Is it even possible? 

BTW I was able to receive data using the MQTT.fx software.

Thanks in advance

January 31, 2018
9:57 am
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 258
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

It is most definitely possible. I have the recess lights in my kitchen subscribed to a channel using a NODE MCU programmed with Arduino.

In the documentation, there is an example for subscribing from a particle photon, which is very similar to Arduino.  You might want to use the pubsub client library for the arduino version.

Let me know if that works.  I can share my code if not.

February 1, 2018
12:07 am
Avatar
Vignesh maker

Silver
Members
Forum Posts: 11
Member Since:
January 25, 2018
sp_UserOfflineSmall Offline

Thank you for the reply

Please share your code. I tried everything. I used the pubsub client and only the publishing part was working. This is my code.

#include <PubSubClient.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
EthernetClient client;
unsigned long myChannelNumber = 409468;
char mqttUserName[] = "MQTTtrial";
char mqttPass[] = " MQTTAPIKEY";
const char* server = "mqtt.thingspeak.com";
char readAPIKey[] = "channelreadAPIkey";
unsigned long lastConnectionTime = 0;
const unsigned long postingInterval = 20L * 1000L;
static const char alphanum[] ="0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
PubSubClient mqttClient(client);

 

void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
}

 

void reconnect()
{
char clientID[10];
while (!mqttClient.connected())
{
Serial.print("Attempting MQTT connection...");
for (int i = 0; i < 8; i++) {
clientID[i] = alphanum[random(51)];
delay(50);
}

// Connect to the MQTT broker
if (mqttClient.connect(clientID,mqttUserName,mqttPass))
{
Serial.print("Connected with Client");
mqttClient.subscribe( "channels/409468/subscribe/fields/field1/READAPIKEY");

} else
{
Serial.print("failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}

 

void setup()
{
Ethernet.begin(mac);
Serial.begin(9600);
mqttClient.setServer(server, 1883);
mqttClient.setCallback(callback);
delay(1500);
}

 

void loop()
{
if (!mqttClient.connected())
{
reconnect();
}
mqttClient.loop();
}

 

 I am grateful. Thanks

February 1, 2018
4:48 pm
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 258
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

Here is my working subscribe function, running right now on a NODE MCU ESP8266-12 programmed in Arduino IDE.:

int mqttSubscribe(long subChannelID,int field,char* readKey){

String myTopic="channels/"+String(subChannelID)+"/subscribe/fields/field"+String(field)+"/"+String(readKey);
Serial.println("Subscribed to " +myTopic);
Serial.print("State= " + String(mqttClient.state()));
char charBuf[myTopic.length()+1];
myTopic.toCharArray(charBuf,myTopic.length()+1);
return mqttClient.subscribe(charBuf);
}

and here is the call to it from reconnect():

if (mqttClient.connect(clientID,mqttUserName,mqttPass))
{
Serial.println("Connected with Client ID: " + String(clientID) + " User "+ String(mqttUserName) + " Pwd "+String(mqttPass));
if(mqttSubscribe(channelID,1,readAPIKey)==1){
Serial.println("subscribed");
}

 

One difference is the conversion of the topic to a Char array.  Perhaps the string " ..." is null terminated and that is confusing the mqttClient.subscribe()?

February 2, 2018
12:23 am
Avatar
Vignesh maker

Silver
Members
Forum Posts: 11
Member Since:
January 25, 2018
sp_UserOfflineSmall Offline

Thank you csatples, but I am still not receiving any reply. The mqtt connection is getting established. The mqtt state is '0' which says the client is connected. 

'mqttClient.subscribe(charBuf)' is also returning true which states that my subscription is successful. 

The only problem is with the 'callback'. This function not even getting invoked. The program is not entering this function. Please help me out with this.

And after every subscription, the client is disconnecting and then connecting again using 'reconnect' . Is that normal?

 

#include
#include
#include
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
EthernetClient client;
unsigned long myChannelNumber = 409468;
char mqttUserName[] = "MQTTtrial";
char mqttPass[] = " mqttpass 
const char* server = "mqtt.thingspeak.com";
char readAPIKey[] = "read api key";
unsigned long lastConnectionTime = 0;
const unsigned long postingInterval = 20L * 1000L;
static const char alphanum[] ="0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
PubSubClient mqttClient(client);

 

void setup()
{
Ethernet.begin(mac);
Serial.begin(9600);
mqttClient.setServer(server, 1883);
mqttClient.setCallback(callback);
delay(1500);
}

 

void loop()
{
if (!mqttClient.connected())
{
reconnect();
}
mqttClient.loop();
}

 

void reconnect()
{
char clientID[10];
while (!mqttClient.connected())
{
Serial.print("Attempting MQTT connection...");
for (int i = 0; i < 8; i++) {
clientID[i] = alphanum[random(51)];
delay(50);
}

// Connect to the MQTT broker
if (mqttClient.connect(clientID,mqttUserName,mqttPass))
{
Serial.print("Connected with Client");
int a = mqttSubscribe(myChannelNumber,1,readAPIKey);
Serial.print(" value :");
Serial.println(a);

} else
{
Serial.print("failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}

void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
}

 

int mqttSubscribe(long subChannelID,int field,char* readKey){

String myTopic="channels/"+String(subChannelID)+"/subscribe/fields/field"+String(field)+"/"+String(readKey);
Serial.println("Subscribed to " +myTopic);
Serial.print("State= " + String(mqttClient.state()));
char charBuf[myTopic.length()+1];
myTopic.toCharArray(charBuf,myTopic.length()+1);
return mqttClient.subscribe(charBuf);
}

 

Thanks for your patience with me.

February 6, 2018
12:07 am
Avatar
Vignesh maker

Silver
Members
Forum Posts: 11
Member Since:
January 25, 2018
sp_UserOfflineSmall Offline

I dont know why but my post is in 'awaiting moderation' phase for a week now. 

February 6, 2018
10:54 am
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 258
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

Sometimes the spam checker flags posts for odd reasons.  We are looking into this.  Thanks for your patience.  

February 15, 2018
7:34 am
Avatar
Vignesh maker

Silver
Members
Forum Posts: 11
Member Since:
January 25, 2018
sp_UserOfflineSmall Offline

Hello cstaples,

The code that you suggested and I had been perfectly working. The problem was with my internet sharing from my laptop. I had used an arduino and an ethernet shield and using the laptop's LAN port I shared my wifi connection to the Arduino. 

the same process worked fine on a different laptop but with mine, it is not working. Still trying to figure out where the problem is with my laptop. Anyways the subscription is working well. Thanks a lot 

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
21 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 261

Vinod: 196

piajola: 85

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

Newest Members:

terranceqh2, DianeEmids, Alanawaype, uqovitay, ufijifode, manish01

Administrators: Hans: 387, lee: 457