Data isn't showing up in the channel
3 views (last 30 days)
Show older comments
I have used the following code in Raspi 4. When I run the code it displays field1 and field2 on the screen. However my channel is not receiving any data.
Writing Payload = field1=6.6&field2=17.2 to host: mqtt3.thingspeak.com clientID= xxxxxxxxxxxxxxxxxxxx User xxxxxxxxx PWD xxxxxxxxxxxxxxxx
import paho.mqtt.publish as publish
import psutil
import string
# The ThingSpeak Channel ID.
# Replace <YOUR-CHANNEL-ID> with your channel ID.
channel_ID = "xxxxxxxx"
# The hostname of the ThingSpeak MQTT broker.
mqtt_host = "mqtt3.thingspeak.com"
# Your MQTT credentials for the device
mqtt_client_ID = "xxxxxxxxxxxxxxxx"
mqtt_username = "xxxxxxxxxxxxx"
mqtt_password = "xxxxxxxxxxxxx"
t_transport = "websockets"
t_port = 80
# Create the topic string.
topic = "channels/" + channel_ID + "/publish"
while (True):
# get the system performance data over 20 seconds.
cpu_percent = psutil.cpu_percent(interval=20)
ram_percent = psutil.virtual_memory().percent
# build the payload string.
payload = "field1=" + str(cpu_percent) + "&field2=" + str(ram_percent)
# attempt to publish this data to the topic.
try:
print ("Writing Payload = ", payload," to host: ", mqtt_host, " clientID= ", mqtt_client_ID, " User ", mqtt_username, " PWD ", mqtt_password)
publish.single(topic, payload, hostname=mqtt_host, transport=t_transport, port=t_port, client_id=mqtt_client_ID, auth={'username':mqtt_username,'password':mqtt_password})
except (keyboardInterrupt):
break
except Exception as e:
print (e)
4 Comments
Christopher Stapels
on 31 Jan 2023
Edited: Christopher Stapels
on 31 Jan 2023
Your code looks fine to me. Things I would try:
- Publish a fixed number to see if your sensors are failing.
payload = "field1=" + str(cpu_percent) + "&field2=" + str(ram_percent)
# change to
payload = "field1=10"
2. See if you can publish using the REST API, to test the wifi connection for your device.
3. Write everything out to the command line in your python program to make sure it is executng as expected.
4. final thing to try is to make sure the space for the MQTT command buffer is large enough, This used to be an arduino problem, I dont think its ever been an Rpi problem.
Accepted Answer
More Answers (1)
Christopher Stapels
on 1 Feb 2023
Can you try this code on you pi for the REST API? Do you get a value in your channel?
import requests
import threading
event=threading.Event()
writeKey='YOUR WRITE API KEY'
while True:
url='https://api.thingspeak.com/update?api_key=%s&field1=10&status=%s'% (writeKey,'success!')
try:
y=requests.get(url)
print("wrote" + url)
y.raise_for_status()
except requests.exceptions.HTTPError as errh:
print ("Http Error:",errh)
except requests.exceptions.ConnectionError as errc:
print ("Error Connecting:",errc)
except requests.exceptions.Timeout as errt:
print ("Timeout Error:",errt)
except requests.exceptions.RequestException as err:
print ("OOps: Something Else",err)
#pause for a while
event.wait(16)
7 Comments
Christopher Stapels
on 2 Feb 2023
Edited: Christopher Stapels
on 2 Feb 2023
Great! Use the Read API or thingSpeakRead to se the data in you channel. If it doesnt show up on the plot, ThingSpeak is probably interpreting your post as string data. You can see the read API format on the API keys tab of your channel view. Also you can click export recent data on the private view of your channel. I suspect you will see special characters or other things that confuse thingSpeak and we can repair the issue.
Communities
More Answers in the ThingSpeak Community
See Also
Categories
Find more on MQTT API in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!