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

조회 수: 1 (최근 30일)
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
print("Connection failed")
def on_message(client, userdata, message):
print (message.payload)
Connected = False
broker_address= "" #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
channelID = "1304227"
topic = "channels/" + channelID + "/subscribe/field/field1/" + READapiKey
#led = LED(27)
client.subscribe(topic, qos=0)
while True:
except KeyboardInterrupt:
print ("exiting")

채택된 답변

Christopher Stapels
Christopher Stapels 2021년 2월 22일
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
Tejaswini Suriyanarayanan
Tejaswini Suriyanarayanan 2021년 2월 25일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Reeno Joseph
Reeno Joseph 2021년 2월 18일
Hi Tejaswini,
There is a Simulink basde solution for MQTT Publish/Subscrib as part of Raspberry Pi Simulink Support package.
  댓글 수: 1
Tejaswini Suriyanarayanan
Tejaswini Suriyanarayanan 2021년 2월 19일
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.

댓글을 달려면 로그인하십시오.

Christopher Stapels
Christopher Stapels 2021년 2월 19일
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
Tejaswini Suriyanarayanan
Tejaswini Suriyanarayanan 2021년 2월 22일
Thanks for the response. I double-checked the configuration parameters as you suggested. I am still unable to proceed.

댓글을 달려면 로그인하십시오.


더 많은 답변 보기:  ThingSpeak 커뮤니티


Help CenterFile Exchange에서 Write Data to Channel에 대해 자세히 알아보기


Community Treasure Hunt

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

Start Hunting!

Translated by