Hi Gary,
All that makes sense, thank you.  My service is coded directly from the 
documentation linked above, with the addition of a function to "somehow 
downloads the data", and I have included it below.  One thing to note, I am 
not a programmer, I dabble as a hobby, so if there is something done poorly 
or improperly, I am open to suggestions, recomendations and things that can 
be done better.  I have also included the relevant bit of code from 
weewx.config about how I am referencing my service, and the configuration 
to help explain things.  Currently my service is adding data to the archive 
record, and in order to augment the loop packets, I would need to change 
the structure to speed up the process of getting data.  Currently, with 4 
separate sensors, it takes a measurable amount of time to get the data from 
all of them, and I would want to convert to a queue structure, maybe a 
local MQTT instance.  (I have already toyed with this idea, and this would 
give me a good excuse.) Let me know if there is anything else that I can 
answer or provide that would help.  2 second resolution would be great, but 
I would be happy with 30 or 60 seconds.  Currently, my archive interval is 
set to 5 minutes.

THank you,

Custom Service: (esp8266.py)

#!/usr/bin/env python                                                           
                                                                       

import weewx
import syslog
from weewx.engine import StdService
import schemas.wview
import requests

class add_esp_records(StdService):

    def __init__(self, engine, config_dict):

# Initialize my superclass first:   

       super(add_esp_records, self).__init__(engine, config_dict)

        # Bind to any new archive record events:                           
                                                                            
        self.bind(weewx.NEW_ARCHIVE_RECORD, self.new_archive_packet)

        try:
            # Dig the needed options out of the configuration dictionary.   
                                                                           
            # If a critical option is missing, an exception will be raised 
and                                                                         
            # the alarm will not be set.                                   
                                                                            
            self.devices    = config_dict['ESP8266']

            #print "Devices: "                                             
                                                                            
            #print self.devices                                             
                                                                           


            syslog.syslog(syslog.LOG_INFO, "ESP8266: Setup for devices")

        except KeyError as e:
            syslog.syslog(syslog.LOG_INFO, "ESP8266: Not configured. 
 Missing parameter: %s" % e)


    def new_archive_packet(self, event):

        for device, value in self.devices.iteritems():
            self.url = device
            #print "URL: "                                                 
                                                                            
            #print self.url                                                 
                                                                           
            self.sensors = value
            #print "Sensors: "                                             
                                                                            
            #print self.sensors                                             
                                                                           

            #Pass the URL and the expected values to the read function.     
                                                                           
            esp_records = read_esp_json(self.url);

            if esp_records != 0:


                #print self.sensors['mac']                                      
                                                                       
                #print esp_records['UID']                                       
                                                                       

                if self.sensors['mac'] == esp_records['UID']:
                    #print "Sensor ID matches, continue"                        
                                                                       

                    for sensor in self.sensors:
                        if sensor != 'mac':
                            print "DB Entry: " + self.sensors[sensor]
                            try:
                                value = esp_records[sensor]

                            except KeyError:
                                 value = None

                            print "Value: " + str(value)
                            event.record[self.sensors[sensor]] = value

#Reads the data from the ESP and pass it back to be added to the database.
def read_esp_json(url):

    #Fetch the data, read, and do some basic sanitization on the output.        
                                                                       
    try:
        response = requests.get(url, timeout=2)
    except (requests.exceptions.RequestException) as err:
        syslog.syslog(syslog.LOG_INFO, "ESP8266: Unable to access " + url + ": 
%s" %err)
        return 0

    data = response.json()

    print data

#    print data['KWh']                                                          
                                                                       

    result = {}

   for key in data:
#        print key                                                         
                                                                            
        if key == 'UID':
            result[key] = data[key]
        else:
            if data[key] == 'nan':
                result[key] = None
            else:
                result[key] = float(data[key])

    return result

#Add extra fields to the schema.
schema_with_esp = schemas.wview.schema + [('DoorOpen1', 'REAL'), 
('DoorClosed1', 'REAL'), ('DoorOpen2', 'REAL'), ('DoorClosed2', 'REAL'), 
('IrrigationFlow', 'REAL'), ('IrrigationPressure', 'REAL'),('esp1Signal', 
'REAL'), ('esp2Signal', 'REAL'),  ('esp3Signal', 'REAL'), ('esp3LIPOVoltage', 
'REAL'), ('esp3LIPOCurrent', 'REAL'), ('esp3SolarVoltage', 'REAL'), 
('esp3SolarCurrent', 'REAL'), ('esp3OutputVoltage', 'REAL'), 
('esp3OutputCurrent', 'REAL'), ('extraHumid3', 'REAL'), ('esp4Signal', 'REAL'), 
('esp4LIPOVoltage', 'REAL'), ('l1_watt', 'REAl'), ('l1_volt', 'REAl'), 
('l1_amp', 'REAl'), ('l2_watt', 'REAl'), ('l2_volt', 'REAl'), ('l2_amp', 
'REAl'), ('kwh', 'REAL')]



Weewx.conf

##############################################################################  
                                                                       

#   This section configures the internal weewx engine.                          
                                                                       

[Engine]

    [[Services]]
        # This section specifies the services that should be run. They are      
                                                                       
        # grouped by type, and the order of services within each group          
                                                                       
        # determines the order in which the services will be run.               
                                                                       
        prep_services = weewx.engine.StdTimeSynch
        data_services = user.esp8266.add_esp_records,
        process_services = weewx.engine.StdConvert, weewx.engine.StdCalibrate, 
weewx.engine.StdQC, weewx.wxservices.StdWXCalculate, user.cmon.Computer\
Monitor
        archive_services = weewx.engine.StdArchive
        restful_services = weewx.restx.StdStationRegistry, 
weewx.restx.StdWunderground, weewx.restx.StdPWSweather, weewx.restx.StdCWOP, 
weewx.restx.St\
dWOW, weewx.restx.StdAWEKAS
        report_services = weewx.engine.StdPrint, weewx.engine.StdReport

##############################################################################  
                                                                       

#   This section is for information about the additional ESP8266 Sensors that 
are in use.                                                              

[ESP8266]
    [[http://192.168.2.104]]
        mac = 5C:CF:7F:02:0C:17
        Temp = extraTemp1
        Humid = extraHumid1

        doorOpen = DoorOpen1
        doorClosed = DoorClosed1
        Signal = esp1Signal

    [[http://192.168.2.107]]
        mac = 5C:CF:7F:07:5B:90
        Temp = extraTemp2
        Humid = extraHumid2
        doorOpen = DoorOpen2
        doorClosed = DoorClosed2
        Signal = esp2Signal

    [[http://192.168.2.105]]
        mac = F8:F0:05:E7:A2:7B
        Flow = IrrigationFlow
        Pressure = IrrigationPressure
        Signal = esp3Signal
        LIPO_Voltage = esp3LIPOVoltage
        LIPO_Current = esp3LIPOCurrent
        Solar_Voltage = esp3SolarVoltage
        Solar_Current = esp3SolarCurrent
        Output_Voltage = esp3OutputVoltage
        Output_Current = esp3OutputCurrent

    [[http://192.168.2.112]]
        mac = F8:F0:05:E7:A2:89
        Signal = esp4Signal
        L1_apparentPower = l1_watt

        L1_volt = l1_volt
        L1_amp = l1_amp
        L2_apparentPower = l2_watt
        L2_volt = l2_volt
        L2_amp = l2_amp
        KWh = kwh

##############################################################################  
                                                                       

#  Options for extension 'cmon'                                                 
                                                                       
[ComputerMonitor]
    data_binding = cmon_binding



-- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to