GithubHelp home page GithubHelp logo

Comments (15)

matthewwall avatar matthewwall commented on May 29, 2024

create a weewx service that gets the pressure (from a sensor or from a file or from a web page or whatever) and adds that value to the loop or record (depending on the service binding).

https://github.com/weewx/weewx/wiki/add-sensor

from weewx-sdr.

n7qnm avatar n7qnm commented on May 29, 2024

from weewx-sdr.

matthewwall avatar matthewwall commented on May 29, 2024

The data I want shows up about once every 10 minutes, so the service code would just be “spinning” waiting for that to happen. Is that going to cause issues with the rest of weewx? And, when I find the data and post it, should I just keep reading, or stop and restart.

a data collection service can be bound to each NEW_LOOP_PACKET event or to each NEW_ARCHIVE_RECORD event. in your case, go with the latter. so on each archive record your service would be invoked. no 'spinning' involved. just make sure that whatever you do to get the barometric data does not block for a very long time.

from weewx-sdr.

n7qnm avatar n7qnm commented on May 29, 2024

from weewx-sdr.

matthewwall avatar matthewwall commented on May 29, 2024

OK – it’ll basically block for 10 minutes or more.

you don't want to block in a service - it can screw up the rest of the weewx data processing.

put the socket read in a thread. the thread just opens a socket and waits for data. when it times out, it checks to see if the thread should still run, then tries to connect again. when it receives data, it puts data into a common variable.

then the NEW_ARCHIVE_RECORD callback (running in the main thread) just reads whatever is in the variable. (if you want to distinguish the age of the data, then use a queue instead of a dict)

from weewx-sdr.

n7qnm avatar n7qnm commented on May 29, 2024

from weewx-sdr.

matthewwall avatar matthewwall commented on May 29, 2024

threading is really easy in python. take a look at this driver for an example:

https://github.com/matthewwall/weewx-ws3000

from weewx-sdr.

n7qnm avatar n7qnm commented on May 29, 2024

from weewx-sdr.

matthewwall avatar matthewwall commented on May 29, 2024

something like this (the socket logic could be streamlined, but this should convey the idea):

import socket
import time
import weewx
from weewx.engine import StdService

class PressureService(StdService):
    def __init__(self, engine, config_dict):
        super(PressureService, self).__init__(engine, config_dict)
        d = config_dict.get('PressureService', {})
        self.server_name = d.get('server', 'localhost')
        # the thread will set this each time it reads a new value.
        # the service will read this each archive interval.
        self.pressure = None
        # start the thread that captures the pressure value
        self._thread = PressureThread(self, self.server_name)
        self._thread.start()
        # bind this service to happen with each new archive record
        self.bind(weewx.NEW_ARCHIVE_RECORD, self.new_archive_record)

    def shutDown(self):
        # when the service shuts down, tell the thread to die and wait for it
        if self._thread:
            self._thread.running = False
            self._thread.join()
            self._thread = None

    def new_archive_record(self, event):
        # add the pressure value to the record.  this assumes that
        # the pressure is in units appropriate to the record's unit system.
        event.record['barometer'] = self.pressure

class PressureThread(threading.Thread):
    def __init__(self, service, server_name):
        threading.Thread.__init__(self)
        self.service = service
        self.server_name = server_name
        self.running = False

    def run(self):
        self.running = True
        while self.running:
            try:
                sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                server = (self.server_name, 2023)
                sock.connect(server)
                while self.running:
                    data = sock.recv(1024)
                    if data and data.find("N7QNM-10") != -1:
                        idx = data.find("b")
                        if idx >= 0:
                            p = float(data[idx+1:idx+6]) * 0.002953
                            service.pressure = p
            except socket.error:
                pass
            time.sleep(1)

from weewx-sdr.

n7qnm avatar n7qnm commented on May 29, 2024

from weewx-sdr.

n7qnm avatar n7qnm commented on May 29, 2024

from weewx-sdr.

matthewwall avatar matthewwall commented on May 29, 2024

Is that correct? So, given that, I could put the callsign (N7QNM-10) and the port (2023) in parameters as well, correct?

right-o!

    self.server_name = d.get('server', 'localhost')
    self.port = int(d.get('port', 2023))
    self.callsign = d.get('callsign', 'N7QNM-10')

d is a python dict, extracted from config_dict, which is a dict created from weewx.conf.

the keys of d are case-sensitive.

from weewx-sdr.

n7qnm avatar n7qnm commented on May 29, 2024

from weewx-sdr.

matthewwall avatar matthewwall commented on May 29, 2024

what does the log say?

that code won't run - you'll get an exception in the pressure thread as soon as you hit the service.pressure = p line. try self.service.pressure = p instead.

please continue this discussion by posting at the weewx dev forum:

http://groups.google.com/group/weewx-development

from weewx-sdr.

n7qnm avatar n7qnm commented on May 29, 2024

from weewx-sdr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.