Extract values from payload of SENSOR DHT22 connected to Sonoff Tasmota
Internet of Things
IoT Frameworks
2 years ago
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.