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
Andrew Dumbill in MATLAB Answers
上次活动时间: 2022-5-25

I created a new MQTT device attached to my existing ThingSpeak channel. Then modified my application to use the mqtt3.thingspeak.com server, following the python example given. The application appears to publish data to the server successfully without any exception. However, the channel fields do not appear to update. The old app using the original MQTT server still appears to work for a while and then stops updating. Do I need to remove the old channel and create a completely new one and create a new device before linking them?In other words do I have to completely unlink the old channel before I can publish my data and use it invisualisations?
Graham Coulby in MATLAB Answers
上次活动时间: 2021-2-24

I followed this example to use MQTT from my ESP32: https://uk.mathworks.com/help/thingspeak/use-arduino-client-to-publish-to-a-channel.html Lets make some fake variables for discussion: long channelID = 1234567; char mqttPass[] = "ABCDEFGHIJKLM"; // MQTT API KEY (generated from user profile) char apiKey[] = "APIKEY"; //Channel Write API Key (generated from channel API keys tab) I can connect with the example code (sometimes it fails, but it reconnects and then holds the connection well). After the postingInterval has passed, the publish function is called: void mqttPublishFeed(float field1, float field2, float field3, float field4, float field5, float field6, float field7, float field8) { // Create data string to send to ThingSpeak. String data = "field1="; data += String(field1, DEC); data += "&field2="; data += String(field2, DEC); data += "&field3="; data += String(field3, DEC); data += "&field4="; data += String(field4, DEC); data += "&field5="; data += String(field5, DEC); data += "&field6="; data += String(field6, DEC); data += "&field7="; data += String(field7, DEC); data += "&field8="; data += String(field8, DEC); int length = data.length(); const char *msgBuffer; msgBuffer=data.c_str(); Serial.println(msgBuffer); // Create a topic string and publish data to ThingSpeak channel feed. String topicString = "channels/" + String( channelID ) + "/publish/"+String(apiKey); Serial.println(topicString); length = topicString.length(); const char *topicBuffer; topicBuffer = topicString.c_str(); mqttClient.publish( topicBuffer, msgBuffer ); lastConnectionTime = millis(); } Here is an example of the topicString and msgBuffer variables that are printed to the serial (using the fake variables state above) msgBuffer: field1=829.0000000000&field2=477.0000000000&field3=28.0499992371&field4=35.0566406250&field5=23.0000000000&field6=50.9949913025&field7=0.0000000000&field8=995.9716186523 topicString: channels/1234567/publish/APIKEY Problem: I have confirmed that (on my serial) the channelID and the channel API write key are correct and data seems to publish without error (though it is QOS 0 so its really hard to tell). Despite this, my channel does not update. I have previously used HTTPS and had no issues with API keys or channel updating so I know they aren't the issue.

关于 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.