7:08 am


Silver
January 25, 2018

9:57 am


Moderators
March 7, 2017

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.
12:07 am


Silver
January 25, 2018

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
4:48 pm


Moderators
March 7, 2017

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()?
12:23 am


Silver
January 25, 2018

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.
12:07 am


Silver
January 25, 2018

10:54 am


Moderators
March 7, 2017

7:34 am


Silver
January 25, 2018

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
Most Users Ever Online: 166
Currently Online:
29 Guest(s)
Currently Browsing this Page:
1 Guest(s)
Top Posters:
rw950431: 272
Vinod: 240
piajola: 95
turgo: 70
vespapierre: 63
Adarsh_Murthy: 62
Member Stats:
Guest Posters: 1
Members: 8665
Moderators: 1
Admins: 2
Forum Stats:
Groups: 3
Forums: 14
Topics: 1600
Posts: 5760
Newest Members:
Advantagetreeexperts, laundrydaddyuk, techhhelp5, ken, tran, huldacormierModerators: cstapels: 460
Administrators: Hans: 405, lee: 457