Re: Import RIVM sensor data
Posted: Tuesday 27 February 2018 17:24
kick....
Please
Please
Code: Select all
#! /usr/bin/python
from lxml import html
import requests
import subprocess
page = requests.get('https://www.luchtmeetnet.nl/tabel')
tree = html.fromstring(page.content)
value = tree.xpath('//a/text()')
tel =1
while tel < 500:
if "Hoogvliet" in value[tel]:
command = ('curl -i "http://user:password@ip:8080/json.htm?type=command¶m=udevice&idx=1&nvalue=0&svalue='+str(float(value[tel+1]))+'"')
output = subprocess.check_output(['bash','-c', command])
command = ('curl -i "http://user:password@ip:8080/json.htm?type=command¶m=udevice&idx=2&nvalue=0&svalue='+str(float(value[tel+2]))+'"')
output = subprocess.check_output(['bash','-c', command])
command = ('curl -i "http://user:password@ip:8080/json.htm?type=command¶m=udevice&idx=3&nvalue=0&svalue='+str(float(value[tel+3]))+'"')
output = subprocess.check_output(['bash','-c', command])
command = ('curl -i "http://user:password@ip:8080/json.htm?type=command¶m=udevice&idx=4&nvalue=0&svalue='+str(float(value[tel+4]))+'"')
output = subprocess.check_output(['bash','-c', command])
command = ('curl -i "http://user:password@ip:8080/json.htm?type=command¶m=udevice&idx=5&nvalue=0&svalue='+str(float(value[tel+5]))+'"')
output = subprocess.check_output(['bash','-c', command])
command = ('curl -i "http://user:password@ip:8080/json.htm?type=command¶m=udevice&idx=6&nvalue=0&svalue='+str(float(value[tel+6]))+'"')
output = subprocess.check_output(['bash','-c', command])
command = ('curl -i "http://user:password@ip:8080/json.htm?type=command¶m=udevice&idx=7&nvalue=0&svalue='+str(float(value[tel+7]))+'"')
output = subprocess.check_output(['bash','-c', command])
tel = tel +1
ErikAtSpijk wrote: ↑Monday 12 March 2018 21:29 It might be a bit pure amateurism but the small piece of code works for me to get luchtmeetnet.nl data. Before using it you need to install lxml. (sudo apt-get install python3-lxml ) Save the pyhon script under /home/pi/domoticz/scripts and create a crontab job.
The scipt works by looking for all links (a herf) in the tabel from luchtmeetnet. It then loops though the data until it finds a location containing "Hoogvliet". You can change this for your own location. The value of the polution is then contained in the next value, just in the order as they are displayed on the website. If there is no data on the website for a component than it is just skipped. For every component following the script then uploads the data into the domoticz api, you need to fill in the domoticz username, password and ip adress plus the idx value of the virtual device that need to contain the data in the string "command ". Depeding on how many components are measured for your location you might need to remove or add a few uploads to the api.
It's pretty basic but does works for me.
Code: Select all
#! /usr/bin/python from lxml import html import requests import subprocess page = requests.get('https://www.luchtmeetnet.nl/tabel') tree = html.fromstring(page.content) value = tree.xpath('//a/text()') tel =1 while tel < 500: if "Hoogvliet" in value[tel]: command = ('curl -i "http://user:password@ip:8080/json.htm?type=command¶m=udevice&idx=1&nvalue=0&svalue='+str(float(value[tel+1]))+'"') output = subprocess.check_output(['bash','-c', command]) command = ('curl -i "http://user:password@ip:8080/json.htm?type=command¶m=udevice&idx=2&nvalue=0&svalue='+str(float(value[tel+2]))+'"') output = subprocess.check_output(['bash','-c', command]) command = ('curl -i "http://user:password@ip:8080/json.htm?type=command¶m=udevice&idx=3&nvalue=0&svalue='+str(float(value[tel+3]))+'"') output = subprocess.check_output(['bash','-c', command]) command = ('curl -i "http://user:password@ip:8080/json.htm?type=command¶m=udevice&idx=4&nvalue=0&svalue='+str(float(value[tel+4]))+'"') output = subprocess.check_output(['bash','-c', command]) command = ('curl -i "http://user:password@ip:8080/json.htm?type=command¶m=udevice&idx=5&nvalue=0&svalue='+str(float(value[tel+5]))+'"') output = subprocess.check_output(['bash','-c', command]) command = ('curl -i "http://user:password@ip:8080/json.htm?type=command¶m=udevice&idx=6&nvalue=0&svalue='+str(float(value[tel+6]))+'"') output = subprocess.check_output(['bash','-c', command]) command = ('curl -i "http://user:password@ip:8080/json.htm?type=command¶m=udevice&idx=7&nvalue=0&svalue='+str(float(value[tel+7]))+'"') output = subprocess.check_output(['bash','-c', command]) tel = tel +1
Code: Select all
sudo apt-get install python3-lxml
Code: Select all
*/2 * * * * python /home/odroid/domoticz/scripts/python/rivm.py
Code: Select all
@192.168.5.70:8080/json.htm?type=command¶m=udevice&9277=1&nvalue=0&svalue='+str(float(value[tel+1]))+'"')
Code: Select all
Traceback (most recent call last):
File "./rivm.py", line 3, in <module>
from lxml import html
ImportError: No module named lxml
odroid@odroid:~/domoticz/scripts/python$
Code: Select all
odroid@odroid:~$ sudo apt-get install python3-lxml
[sudo] password for odroid:
Reading package lists... Done
Building dependency tree
Reading state information... Done
python3-lxml is already the newest version (3.5.0-1build1).
The following packages were automatically installed and are no longer required:
libllvm3.8 libllvm4.0 libmircommon5 libqmi-glib1
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 15 not upgraded.
odroid@odroid:~$
Code: Select all
** make sure the development packages of libxml2 and libxslt are installed **
Code: Select all
Could not find function xmlCheckVersion in library libxml2. Is libxml2 insta lled?
Code: Select all
odroid@odroid:~$ sudo apt-get install libxslt
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package libxslt
odroid@odroid:~$
Code: Select all
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24 100 24 0 0 8000 0 --:--:-- --:--:-- --:--:-- 8000
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24 100 24 0 0 8000 0 --:--:-- --:--:-- --:--:-- 12000
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24 100 24 0 0 12000 0 --:--:-- --:--:-- --:--:-- 12000
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24 100 24 0 0 6000 0 --:--:-- --:--:-- --:--:-- 6000
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24 100 24 0 0 4000 0 --:--:-- --:--:-- --:--:-- 4000
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24 100 24 0 0 8000 0 --:--:-- --:--:-- --:--:-- 12000
domoticz@odroid:~/domoticz/scripts/python$
ErikAtSpijk wrote: ↑Monday 02 April 2018 21:30 Edwin, the RIVM website seems to stop, the website luchtmeetnet.nl seems to be the new site.