Hoang Bach in Discussions
上次活动时间: 2024-4-16

Hey, so I have a simple air quality measurement IoT system but the values are not arriving in my ThingSpeak channel. I use basic tutorials for it and tried some kind of variation but the code is so simple, I don't see any problem code-wise. Maybe someone here can help me? I am working with a Raspberry Pi 3 and the following code for a SDS011 sensor: import time from datetime import datetime import paho.mqtt.publish as publish import paho.mqtt.client as mqtt import psutil from sds011 import * import aqi sensor = SDS011("/dev/ttyUSB0", use_query_mode=True) def get_data(n=3): sensor.sleep(sleep=False) pmt_2_5 = 0 pmt_10 = 0 time.sleep(10) for i in range (n): x = sensor.query() pmt_2_5 = pmt_2_5 + x[0] pmt_10 = pmt_10 + x[1] time.sleep(2) pmt_2_5 = round(pmt_2_5/n, 1) pmt_10 = round(pmt_10/n, 1) sensor.sleep(sleep=True) time.sleep(2) return pmt_2_5, pmt_10 def conv_aqi(pmt_2_5, pmt_10): aqi_2_5 = aqi.to_iaqi(aqi.POLLUTANT_PM25, str(pmt_2_5)) aqi_10 = aqi.to_iaqi(aqi.POLLUTANT_PM10, str(pmt_10)) return aqi_2_5, aqi_10 def save_log(): with open("/YOUR PATH/air_quality.csv", "a") as log: dt = datetime.now() log.write("{},{},{},{},{}\n".format(dt, pmt_2_5, aqi_2_5, pmt_10, aqi_10)) log.close() channelID = "YOUR CHANNEL ID" apiKey = "YOUR WRITE KEY" clientID = "YOUR CLIENT ID" tUsername = "YOUR USERNAME" tPassword = "YOUR PASSWORD" topic = "channels/" + channelID + "/publish/" + apiKey mqttHost = "mqtt3.thingspeak.com" tTransport = "tcp" tPort = 1883 tTLS = None tProtocol = mqtt.MQTTv311 while True: pmt_2_5, pmt_10 = get_data() aqi_2_5, aqi_10 = conv_aqi(pmt_2_5, pmt_10) print ("AQI2.5 =", aqi_2_5," AQI10 =", aqi_10) tPayload = "field1=" + str(pmt_2_5)+ "&field2=" + str(aqi_2_5)+ "&field3=" + str(pmt_10)+ "&field4=" + str(aqi_10) try: publish.single(topic, payload=tPayload, hostname=mqttHost, port=tPort, client_id=clientID, auth={'username':tUsername, 'password':tPassword}, tls=tTLS, transport=tTransport, protocol=tProtocol) print ("[INFO] Published data") save_log() time.sleep(60) except Exception as e: print ("[INFO] Failure in sending data") print (e) time.sleep(60) Publish via Paho-MQTT publish.single but data is not arriving? Have a look at the documentation for MQTT and the provided examples. You are using syntax from the old broker, which was replaced in July 2021 and deprecated in April. Here is the new publish syntax for publish. The topic you are publishing to is incorrect. Take a look at the examples Christopher has linked to. So is this page in your documentation outdated then: https://se.mathworks.com/help/thingspeak/use-raspberry-pi-board-that-runs-python-websockets-to-publish-to-a-channel.html? Is there any way to use paho_mqtt to publish data in ThingSpeak or is the only option to use MQTT X? The code you reference should work. You can definitely still use paho, im pretty sure ive done it already on a pi. Just set up am MQTT device on the thingSpeak web UI, enable publishing from that device to your channel of interest, and copy the credentials to the pi. Can you tell us where it is failing for you? Now I found it: one extra /. Had f"channels/{channel_ID}/publish/ instead of f"channels/{channel_ID}/publish Damn, it was hard to find it. :P Thanks for help anyway! Ive had that one before, sorry I didnt look close enough to see that. We appreciate you sharing the solution! If its working, can you share an interesting plot from your monitor to inspire others who see this discussion? publish mqtt paho-mqtt air-quality sds011 raspberry pi 3
Pankaj Thakur in MATLAB Answers
上次活动时间: 2020-7-25

Using a Nod-Red flow on my raspberry pi 3b+ model running on Rasbian, I setup a DHT11 sensor to send Temperatrure and Humidity values to first and second field of my channel on Thingspeak after every 10 minutes. But it is only uploading Temperature value or Humidity value at a time. Both are not updated.
Claire McV in MATLAB Answers
上次活动时间: 2019-12-24

Hello I am hoping someone can help please. I have connected my raspberry pi 3 to a Thingspeak channel and set up two fields to be read and displayed, 'Temperature' and 'Accelerometer'. The temperator information is displayed as a field chart but the accelerometer is not. I know the raspberry pi is reading it as the values are printing and when I use 'export recent data' I can see the information there. Am I missing something in getting accelerometer information to display as a field? I am importing with 2 fields but I checked with another field (pressure) and it worked so I know the code for two fields is right. Any help greatly appreciated.

关于 ThingSpeak

The community for students, researchers, and engineers looking to use MATLAB, Simulink, and ThingSpeak for Internet of Things applications. You can find the latest ThingSpeak news, tutorials to jump-start your next IoT project, and a forum to engage in a discussion on your latest cloud-based project. You can see answers to problems other users have solved and share how you solved a problem.