Extract values from payload of SENSOR DHT22 connected to Sonoff Tasmota

Internet of Things IoT Frameworks 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating
_x000D_ _x000D_ I'm using Sonoff basic flashed with Tasmora firmware. I connected a temp sensor DHT22. On this device is setup MQTT to send sensor values to Mosquitto, which is installed on a Linux pc. The data I am getting is in the format: mosquitto_sub -h my.dns.xxx -p 81111 -v -u user -P pass -t channels/sonoff/SENSOR/# channels/sonoff/SENSOR {"Time":"2018-07-07T03:55:13","AM2301":{"Temperature":25.0,"Humidity":62.4},"TempUnit":"C"} channels/sonoff/SENSOR {"Time":"2018-07-07T03:55:23","AM2301":{"Temperature":25.0,"Humidity":62.4},"TempUnit":"C"} channels/sonoff/SENSOR {"Time":"2018-07-07T03:55:33","AM2301":{"Temperature":25.0,"Humidity":62.5},"TempUnit":"C"} channels/sonoff/SENSOR {"Time":"2018-07-07T03:55:43","AM2301":{"Temperature":25.0,"Humidity":62.5},"TempUnit":"C"} channels/sonoff/SENSOR {"Time":"2018-07-07T03:55:53","AM2301":{"Temperature":25.0,"Humidity":62.6},"TempUnit":"C"} I need Python code which will publish the values of temperature and humidity, which will be published on thingspeak.com. I already have some Python code for publishing but it is for an OrangePI board where values are read in a different way: import dht22 import time import datetime import os PIN2 = port.PA6 gpio.init() n=75 #after every 150 (~10 minutes) load (connect to thingspeak) again! i=0 channelId = "xxxxxx" # Put your channel ID here,i.e.. the number from the URL, https://thingspeak.com/channels/285697 apiKey = "xxxxxxxxx" # Put the API key here (the Write API Key from the API Keys tab in ThingSpeak) client = mqtt.Client() client.connect("mqtt.thingspeak.com",1883,60) instance = dht22.DHT22(pin=PIN2) os.system('clear') while True: result = instance.read() if result.is_valid(): i=i+1 print 'I after increment is: ', i print 'VALID and i is:',i print("\033[37;1mLast valid input: \033[0m" + "\033[33;1m" + str(strftime("%d.%m.%Y %H:%M:%S", gmtime())) + "\033[0m") print("\033[37;1mTemperature: \033[1;31m%.2f C\033[0m" % result.temperature) print("\033[37;1mHumidity: \033[32;1m%.2f %%\033[0m\n" % result.humidity) if i == n-1: print 'I is: ', i print 'Now setting value i to zero\nLoading libary again' i=0 print 'I after setting to zero is: ', i client = mqtt.Client() client.connect("mqtt.thingspeak.com",1883,60) client.publish("channels/%s/publish/%s" % (channelId,apiKey), "field1=" + str(result.temperature) + "&field2=" + str(result.humidity)) else: client.publish("channels/%s/publish/%s" % (channelId,apiKey), "field1=" + str(result.temperature) + "&field2=" + str(result.humidity)) else: print 'WE HAVE INVALID and i is:',i time.sleep(4) I'm pretty sure that it is very similar to what I am searching for, but I'm not able to write the code. Can someone help me with this few lines of code?

Posted on 16 Aug 2022, this text provides information on IoT Frameworks related to Internet of Things. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
_x000D_ Sounds like you've misunderstood what MQTT is. There's a very useful reference on the ThingsSpeak site, and there's probably even better references in your native language. Note in particular that MQTT always requires a single broker and one or more clients. Clients may subscribe or publish to a topic. The broker will receive any publish messages from a client and send them to all the clients that have subscribed. In your case you have a Sonoff basic (which, incidentally, looks nothing like what you describe - you must have modified it extensively?) which is acting as a MQTT client. Your Linux PC is acting as a broker and you're also running a client on the PC (though the my.dns.xxx suggests there's some sort of virtual network involved too). You then want to connect to ThingsSpeak, which is also a broker. Brokers don't talk to brokers so what you're trying to do wont work. If you want to stick with what you've got (and not simply change the Sonoff basic to be client of ThingsSpeak, rather than the PC, for example) then what you need is a MQTT Bridge. A bridge allows you to connect two brokers. A bridge is essentially a client that subscribes to one broker and publishes everything to another broker.

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Important Internet of Things Links