MATLAB Answers

Controlling an LED with the Pi 4 using ThingSpeak MQTT subscribe.

18 views (last 30 days)
Hello,
I was working on a project to contol an LED connected to the Pi 4 using the subscribe feature in ThingSpeak MQTT. While I was able to publish data to ThingSpeak MQTT using the Pi 4 without any issues, I faced trouble while trying to subscribe to a channel in ThingSpeak MQTT to read data from a field to control an LED connected to the Pi.
Initially, I had interfaced both the LED and a BMP180 sensor to the same Pi and wanted to publish data from the BMP180 to a ThingSpeak channel and subscribe to the channel to control my LED based on the temperature values (I ran seperate scripts on the Pi 4 for the Publish and subscribe simuntaneously). Though the publish part worked, the subscribe part did not. I thought this was because I was using the same device (the Pi) to publish and subscribe to the channel.
So later, I installed the MQTT.fx app in my PC and used it to publish values to a new private ThingSpeak channel. I then used the Pi to subscribe to the same channel, receive values and control the LED that was interfaced to it. This did not work either. I am unable to receive messages from the ThingSpeak MQTT channel when I subscribe to it from the Pi 4.
I double checked the Channel ID and API keys were correct. I'm using the Paho MQTT library for publish and subscribe to ThingSpeak MQTT. I used port 1883.
I will share the python code I used to subscribe to the ThingSpeak MQTT channel. I have the following doubts:
  1. Please help me resolve the above issue. (highlighted)
  2. Is it possible to use the same device to publish and subscribe to ThingSpeak MQTT, as I did in the first case? If yes, how?
Code I used to subscribe to the ThingSpeak MQTT channel using the R Pi 4:
import paho.mqtt.client as mqttClient
import time
from gpiozero import LED
import random
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to broker")
global Connected
Connected = True
else:
print("Connection failed")
def on_message(client, userdata, message):
print (message.payload)
Connected = False
broker_address= "mqtt.thingspeak.com" #Broker address
port = 1883 #Broker port
user = "pi_led" #Connection username
password = "XXXXXXXXXXXXXXXXX"#Connection password - Used my MQTT API key from My Profile
client = mqttClient.Client("Python") #create new instance
client.username_pw_set(user, password=password) #set username and password
client.on_connect= on_connect #attach function to callback
client.on_message= on_message #attach function to callback
client.connect(broker_address, port=port) #connect to broker
client.loop_start() #start the loop
while Connected != True: #Wait for connection
time.sleep(0.1)
channelID = "1304227"
READapiKey = "YYYYYYYYYYYYYYYY"
topic = "channels/" + channelID + "/subscribe/field/field1/" + READapiKey
#led = LED(27)
client.subscribe(topic, qos=0)
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print ("exiting")
client.disconnect()
client.loop_stop()

Accepted Answer

Christopher Stapels
Christopher Stapels on 22 Feb 2021
I havent looked at the python doc for subscribe. Where do you set the client ID?
If this is it, you will want to use a different client ID.
client = mqttClient.Client("Python") #create new instance
This example is in C, but I show how to generate a new random client id each time. The subscription part of the broker requires a unique ID.
  1 Comment
Tejaswini Suriyanarayanan
Tejaswini Suriyanarayanan on 25 Feb 2021
Hello,
I tried running the code by generating a random unique client ID as you suggested. It worked! I was able to successfully receieve the message payload from the broker.
Thank you for the support!
Tejaswini

Sign in to comment.

More Answers (2)

Reeno Joseph
Reeno Joseph on 18 Feb 2021
Hi Tejaswini,
There is a Simulink basde solution for MQTT Publish/Subscrib as part of Raspberry Pi Simulink Support package.
-Reeno
  1 Comment
Tejaswini Suriyanarayanan
Tejaswini Suriyanarayanan on 19 Feb 2021
Hello,
Thanks for the response. But, I am looking for a solution that is not Simulink based.
I was able to publish data to the channel successfully using the MQTT API. Infact, I was also able to subscribe and receive data from the channel when I tried to do it using the MQTT.fx application on my PC. Then why am I not able to proceed with the same using python code in the Pi, as I did for publishing data to the channel?
Please help me solve this issue.
Tejaswini

Sign in to comment.


Christopher Stapels
Christopher Stapels on 19 Feb 2021
I would look at the MQTT troubleshooting page for subscribe. It is possible that you have settings in your client that the ThingSpeak broker does not agree with (such as QoS or LWT, etc.)
  1 Comment
Tejaswini Suriyanarayanan
Tejaswini Suriyanarayanan on 22 Feb 2021
Hello,
Thanks for the response. I double-checked the configuration parameters as you suggested. I am still unable to proceed.
Tejaswini

Sign in to comment.

Communities

More Answers in the  ThingSpeak Community

Community Treasure Hunt

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

Start Hunting!

Translated by