Page 1 of 1

Using Python for JSON commands

Posted: Thursday 26 May 2016 14:42
by D'rMorris
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:

Code: Select all

curl 'http://IP:PORT/json.htm?type=command&param=udevice&idx=123&svalue=99'
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:

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)
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?

Re: Using Python for JSON commands

Posted: Thursday 26 May 2016 18:36
by MikeF
You have to do something like this:

Code: Select all

import urllib
import urllib2

url = 'http://<DOMO_IP>:<DOMO_PORT>/json.htm?type=command&param=udevice&nvalue=0&idx=<IDX>&svalue=<VALUE>'
request = urllib2.Request(url)
response = urllib2.urlopen(request)

Re: Using Python for JSON commands

Posted: Friday 27 May 2016 8:06
by D'rMorris
Thank you for your reply, but that is exactly what I do not want :).
I want the base URL to be: "url = 'http://192.168.1.2:9096/json.htm'". I want to list the parameters in postdata parameters separately. The other webservice also receives the data like this (variables in the JSON parameters, not encapsulated in the URL itself.

Re: Using Python for JSON commands

Posted: Thursday 15 September 2016 14:09
by D'rMorris
No Python experts that can help me with this :-)?

Re: Using Python for JSON commands

Posted: Thursday 15 September 2016 15:20
by Egregius
What's wrong woth the provided solution? That works, so why make it in another way?

Re: Using Python for JSON commands

Posted: Thursday 15 September 2016 15:25
by Sneezydevil
I am no python expert but what about:

Code: Select all

postdata = {'type':'command', 'param':'udevice','idx':'358','svalue':'66'}
resp = requests.get(url=url, params=postdata)