MQTT subscriber can’t retrieve channel data from ThingSpeak broker | 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
MQTT subscriber can’t retrieve channel data from ThingSpeak broker
No permission to create posts
May 21, 2018
10:31 am
Avatar
Squifer
New Member
Members
Forum Posts: 2
Member Since:
May 21, 2018
sp_UserOfflineSmall Offline

I am trying to retrieve the channels from the ThingSpeak MQTT broker but sometimes the program doesn’t retrieve anything or it will only retrieve only one channel and would not get the updated channel. On rare occasions we could retrieve multiple channels initially and then get the updated channels. We could retrieve all the channels without problems until the start of this month. Does anybody else have this problem?
Here is the code:
# ---------------------------------------------------------------------------
def on_connect(client, userdata, flags, rc):

if rc == 0:

print("Connected to broker")
global Connection
Connection = "Connected"

else:
print("Connection failed")

# ---------------------------------------------------------------------------

def on_message(client, userdata, msg):
print "Message Received"
payload = json.loads(msg.payload, object_hook=lambda d: namedtuple('X', d.keys())(*d.values()))
print "Channel ID:", payload.channel_id
print "Created at:", payload.created_at
print "Entry ID:", payload.entry_id
print "Active Power x10:", payload.field1
print "Avg Voltage:", payload.field2
print "KWhr:", payload.field3
print "Max voltage:", payload.field4
print "Min voltage:", payload.field5
print "KVA x10:", payload.field6
print "RSSI:", payload.field7, "
"

mqttc = MQTT.Client("Py5") # Create new instance
mqttc.on_connect = on_connect # Attach a callback for connect
mqttc.on_message = on_message # Attach a callback for message

Connection = "Disconnected"
user = "yourUser" # Connection username
password = "SAMPLEPASSWORD" # Connection password
mqttc.username_pw_set(user, password=password) # Set username and password
mqttc.connect("mqtt.thingspeak.com", 1883) # Connect to broker

mqttc.loop_start()

while Connection != "Connected": # Wait for connection status to change
time.sleep(1)
print "waiting"

print "Subscribing to channels..."
mqttc.subscribe('channels/486606/subscribe/json/ SAMPLEPASSWORD ',0) # Subscribe to Test Channel 1
mqttc.subscribe('channels/487883/subscribe/json/ SAMPLEPASSWORD ',0) # Subscribe to Test Channel 2
mqttc.subscribe('channels/487888/subscribe/json/ SAMPLEPASSWORD ',0) # Subscribe to Test Channel 3
mqttc.subscribe('channels/487889/subscribe/json/ SAMPLEPASSWORD ',0) # Subscribe to Test Channel 4
mqttc.subscribe('channels/487891/subscribe/json/ SAMPLEPASSWORD,0) # Subscribe to Test Channel 5

print "Subscribed
"

try:
while True:
time.sleep(1)
print "Test"

except KeyboardInterrupt:
mqttc.disconnect()
mqttc.loop_stop()
print "Program Ended"

#if __name__ == "__main__":

May 22, 2018
8:35 am
Avatar
JasonW

MathWorks
Members
Forum Posts: 54
Member Since:
October 21, 2016
sp_UserOfflineSmall Offline

Which account type do you have?
There is a limit of 3 subscriptions for a free user account.

It is possible to temporarily make more than 3 subscriptions with a free account. The server logic takes a few minutes to catch up when there has been a lot of connecting/disconnecting by a client.

May 22, 2018
1:50 pm
Avatar
Squifer
New Member
Members
Forum Posts: 2
Member Since:
May 21, 2018
sp_UserOfflineSmall Offline

Thanks for the reply. However, the problem isn't the subscriptions. I have a free account with 3 devices subscribing to it although I usually use one of them at a time.

May 23, 2018
9:23 am
Avatar
cstapels
Moderator
Members


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

In your code you seem to have at least 5 subscriptions:
print "Subscribing to channels..."
mqttc.subscribe('channels/486606/subscribe/json/ SAMPLEPASSWORD ',0) # Subscribe to Test Channel 1
mqttc.subscribe('channels/487883/subscribe/json/ SAMPLEPASSWORD ',0) # Subscribe to Test Channel 2
mqttc.subscribe('channels/487888/subscribe/json/ SAMPLEPASSWORD ',0) # Subscribe to Test Channel 3
mqttc.subscribe('channels/487889/subscribe/json/ SAMPLEPASSWORD ',0) # Subscribe to Test Channel 4
mqttc.subscribe('channels/487891/subscribe/json/ SAMPLEPASSWORD,0) # Subscribe to Test Channel 5

Also, you may want to check the credentials you are using. For connection, the password is your MQTT API key. When subscribing to a private channel, use the read API Key. You seem to have " SAMPLEPASSWORD" for both, but that could just have replaced them both with the same thing. Have a look at the doc to be sure.
https://www.mathworks.com/help/thingspeak/subscribetoachannelfeed.html

Forum Timezone: America/New_York

Most Users Ever Online: 166

Currently Online:
13 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:

khanhNguyen_CT, susyburr, quijano__, meosuckhoe, briskpanda, Dejw0089

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457