Using Python for JSON commands

Moderator: leecollings

Post Reply
D'rMorris
Posts: 138
Joined: Thursday 01 May 2014 9:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands - Sittard
Contact:

Using Python for JSON commands

Post 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?
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: Using Python for JSON commands

Post 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)
D'rMorris
Posts: 138
Joined: Thursday 01 May 2014 9:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands - Sittard
Contact:

Re: Using Python for JSON commands

Post 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.
D'rMorris
Posts: 138
Joined: Thursday 01 May 2014 9:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands - Sittard
Contact:

Re: Using Python for JSON commands

Post by D'rMorris »

No Python experts that can help me with this :-)?
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Using Python for JSON commands

Post by Egregius »

What's wrong woth the provided solution? That works, so why make it in another way?
Sneezydevil
Posts: 111
Joined: Monday 18 January 2016 9:56
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Using Python for JSON commands

Post 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)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest