Using Python for JSON commands
Posted: Thursday 26 May 2016 14:42
Dear Domoticz fans,
Currently I'm working on a project that reads 5 DS18B20 temperature sensors, connected to the GPIO ports. With the OneWire functionality, the sensors are read correctly and data is being updated. So far, so good
.
However, the OneWire sensors are only read once a minute, which is not enough for my project requirements (I need it almost continuously for a real-time scenario). So, I created (actually: copy-pasted) python code to read the values from the sensors, which is executed every 5 seconds. Works flawlessly.
Next step is to enhance this python program so that I can:
- send these readings to Domoticz via a JSON command
- send these readings to a local database
- send these reading to a webservice via an API
Currently, I'm stuck with sending the values to Domoticz via JSON. The JSON call works perfectly when I call the whole URL like this:
However, I would like to build the URL via python, so that I can loop through the 5 temperature sensors and update the respective IDX numbers in Domoticz with the correct svalues. I know this is also possible with calling the whole URL like in the example above, but I want the thing to just work with python (especially because the webservice I also want to send the values to works identical).
My python code:
When I execute this, I get a HTTP 200 response, so that is good. However: the value is not updated in Domoticz.
Again, it works with a direct URL call (with all params in the URL), but since my webservice also works with JSON, I would like to re-use the postdata that I send to Domoticz for the other webservice.
Does anybody have an idea why the values are not updated? What JSON version does Domoticz use? Do I have to give specific headers / postdata?
Currently I'm working on a project that reads 5 DS18B20 temperature sensors, connected to the GPIO ports. With the OneWire functionality, the sensors are read correctly and data is being updated. So far, so good
However, the OneWire sensors are only read once a minute, which is not enough for my project requirements (I need it almost continuously for a real-time scenario). So, I created (actually: copy-pasted) python code to read the values from the sensors, which is executed every 5 seconds. Works flawlessly.
Next step is to enhance this python program so that I can:
- send these readings to Domoticz via a JSON command
- send these readings to a local database
- send these reading to a webservice via an API
Currently, I'm stuck with sending the values to Domoticz via JSON. The JSON call works perfectly when I call the whole URL like this:
Code: Select all
curl 'http://IP:PORT/json.htm?type=command¶m=udevice&idx=123&svalue=99'
My python code:
Code: Select all
import requests
import json
url = 'http://192.168.1.2:9096/json.htm'
postdata = {'type':'command', 'param':'udevice','idx':'358','svalue':'66'}
print(postdata)
headers = {'content-type':'application/json'}
response = requests.post(url,data=json.dumps(postdata),headers =headers)
print(requests.post)
print(response)
Again, it works with a direct URL call (with all params in the URL), but since my webservice also works with JSON, I would like to re-use the postdata that I send to Domoticz for the other webservice.
Does anybody have an idea why the values are not updated? What JSON version does Domoticz use? Do I have to give specific headers / postdata?