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

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





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

Register Lost password?
sp_Feed sp_TopicIcon
Not receiving any data by subscribing(mqtt) from arduino client
Avatar
Vignesh maker

Silver
Forum Posts: 10
sp_UserOfflineSmall Offline
1
January 27, 2018 - 7:08 am
sp_Permalink sp_Print sp_EditHistory

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

Avatar
cstapels
Moderator
Forum Posts: 158
sp_UserOfflineSmall Offline
2
January 31, 2018 - 9:57 am
sp_Permalink sp_Print

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.

Avatar
Vignesh maker

Silver
Forum Posts: 10
sp_UserOfflineSmall Offline
3
February 1, 2018 - 12:07 am
sp_Permalink sp_Print

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

Avatar
cstapels
Moderator
Forum Posts: 158
sp_UserOfflineSmall Offline
4
February 1, 2018 - 4:48 pm
sp_Permalink sp_Print

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()?

Avatar
Vignesh maker

Silver
Forum Posts: 10
sp_UserOfflineSmall Offline
5
February 2, 2018 - 12:23 am
sp_Permalink sp_EditHistory
Awaiting Moderation

Avatar
Vignesh maker

Silver
Forum Posts: 10
sp_UserOfflineSmall Offline
6
February 6, 2018 - 12:07 am
sp_Permalink sp_Print

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

Avatar
cstapels
Moderator
Forum Posts: 158
sp_UserOfflineSmall Offline
7
February 6, 2018 - 10:54 am
sp_Permalink sp_Print

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

Avatar
Vignesh maker

Silver
Forum Posts: 10
sp_UserOfflineSmall Offline
8
February 15, 2018 - 7:34 am
sp_Permalink sp_Print

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:
36 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 252

Vinod: 164

cstapels: 96

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

ibgedubo

aogomaeqaza

ureomug

itixuwidouzuv

eilimir

memozupi

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

 

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Administrators: Hans, lee