Read xml data from url

108 views
Skip to first unread message

Tryphon Cosinus

unread,
Jan 15, 2018, 9:10:48 AM1/15/18
to weewx-user
Hello,

I try to adapt the code of the Pond service found at https://github.com/weewx/weewx/wiki/add-sensor to access xml data from a known url and record these data in my sqlite weewx database. Sure I am not a Python guy and I did my best to write the following code :

import syslog
import weewx
from weewx.wxengine import StdService
import urllib2
import xml.etree.ElementTree as ET

class ElectronService(StdService):
        def __init__(self, engine, config_dict):
                try:
                        super(ElectronService, self).__init__(engine, config_dict)
                        self.dashdata_url = 'http://192.168.1.111/api/DashData.xml?T=0&D=0&M=1'
                        xmlurl = urllib2.urlopen(self.dashdata_url)
                        xmldata = xmlurl.read()
                        tree = ET.parse(xmldata)
                        root = tree.getroot()
                        syslog.syslog(syslog.LOG_INFO, "TED Pro: probing %s" % self.dashdata_url)
                        self.bind(weewx.NEW_ARCHIVE_RECORD, self.read_xml)
                except Exception as e:
                       syslog.syslog(syslog.LOG_ERR, "TED Pro: cannot reach TED url: %s" % e)

        def read_xml(self, event):
                try:
                       event.record['networkVoltage'] = float(root.find('Voltage').text)
                       event.record['powerConsumption'] = float(root.find('TDY').text)
                except Exception as e:
                       syslog.syslog(syslog.LOG_ERR, "TED Pro: cannot read value: %s" % e)


Could you give some advises on this code ?

For example I do not understand the line : self.bind(weewx.NEW_ARCHIVE_RECORD, self.read_xml)

read_xml is defined with two parameters but in this line this function is not even called with parameters ...

Thank you.

Thomas Keffer

unread,
Jan 15, 2018, 9:50:22 AM1/15/18
to weewx-user
The line

self.bind(weewx.NEW_ARCHIVE_RECORD, self.read_xml)

means when a weeWX NEW_ARCHIVE_RECORD event comes in, call my member function read_xml. It binds the event to the function. The expression self.read_xml refers to the member function. Note that it does not actually invoke the function, which would require a set of parenthesis afterwards, which, yes, would have to have the right number of parameters. But, that's not what's happening here.

See the section Creating a new service in the Customizing Guide.

-tk

--
You received this message because you are subscribed to the Google Groups "weewx-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to weewx-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tryphon Cosinus

unread,
Jan 15, 2018, 11:11:54 PM1/15/18
to weewx-user
Thank you Tom,

I corrected my code (ted.py) thanks to your link :

import syslog
import weewx
from weewx.wxengine import StdService
import urllib2
import xml.etree.ElementTree as ET

class electricityService(StdService):

       
def __init__(self, engine, config_dict):
               
try:

                       
super(electricityService, self).__init__(engine, config_dict)
                        d
= config_dict.get('electricityService', {})
                       
self.dashdata_url = d.get('dashdata_url','http://192.168.1.77:8089/api/DashData.xml?T=0&D=0&M=1')

                        syslog
.syslog(syslog.LOG_INFO, "TED Pro: probing %s" % self.dashdata_url)
                       
self.bind(weewx.NEW_ARCHIVE_RECORD, self.read_xml)

                       
self.previousEnergy = None

               
except Exception as e:
                       syslog
.syslog(syslog.LOG_ERR, "TED Pro: cannot reach TED url: %s" % e)

       
def read_xml(self, event):
               
try:

                       xmlurl
= urllib2.urlopen(self.dashdata_url)
                       xmldata
= xmlurl.read()

                       tree
= ET.fromstring(xmldata)
# gives Wh since a zero (since midnight)
                       currentEnergy
= float(tree.find('TDY').text) / 1000
# gives voltage of my installation in V
                       
event.record['networkVoltage'] = float(tree.find('Voltage').text) / 10
                       
if self.previousEnergy:
# record energy consumption over a period (5 min in my weewx.conf)
                             
event.record['powerConsumption'] = currentEnergy - self.previousEnergy
                       
self.previousEnergy = currentEnergy
               
except Exception as e:

                       syslog
.syslog(syslog.LOG_ERR, "TED Pro: cannot read value: %s" % e)



This allows to read directly xml data from a URL.
Now the database is populated with voltage and energy consumption by my house. I give the code in case somebody is interested.

d k

unread,
Oct 16, 2020, 10:06:29 PM10/16/20
to weewx-user
Thanks for getting this started.

I updated the code for python3. Anyone want it?
Reply all
Reply to author
Forward
0 new messages