Davis stations

Introduction

The municipality of Sant Feliu de Llobregat owns two Davis Instruments Vantage Pro stations that have multiple sensors to measure the weather conditions. External applications can read the data from these sensors through the Vantage Serial Protocol, a propietary protocol of Davis Instruments. Brain4it has a library which is able to communicate with Davis stations using the mentioned serial protocol.

This Brain4it module connects to these weather stations at a certain frequency to get the sensor data and publish it on the Sentilo platform.

Sentilo is an open source sensor and actuator platform designed to fit in the Smart City architecture of any city looking for openness and easy interoperability. This platform provides a simple HTTP REST API to manage sensory data.

The following diagram illustrates the functioning of the system:

Programming the module

Instead of having a loop that connects to the stations and then sleeps for a while, this module use the timer-schedule function to schedule a task that will read the weather data at a certain frequency.

This method is more efficient that the loop because it does not consumes an executor thread permanently.

When the module is stopped, the timer-cancel function is called to cancel future executions of the task.

While the module is running, all the scheduled tasks within the module can be listed using timer-tasks function.

Once the module is initiated, the code contained in the start variable (spawn (eval main)) is executed and a function is scheduled for execution at intervals of (* 1000 setup/access_interval_sec) milliseconds.

The main variable contains this code:

(timer-schedule
  (function ()
    (try
      (do
        (### "New values are generated")
        (generate-data)
      )
      (ex
        "*" =>
        (do
          (###
            "When an error takes place, it is saved in a variable named 'error'"
          )
          (set error
            (list (format-date (date)) ex)
          )
        )
      )
    )
  )
  0
  (* 1000 setup/access_interval_sec)
)

Every time this code is executed, weather data is obtained and published on Sentilo.

Once the data is acquired from the station, it is parsed and transformed to publish it on Sentilo.

Before the information is published, the module assures that all the data is correct verifying that the sensor values are in a valid range. At this point, the weather information is ready for being uploaded to the platform.

(function (ctx info)
  (local data)
  (###
    "data format: "
    (
      "AJU"
      "tem" => 27.4
      "hum" => 78.4
      ...
      "winddir" => 45
    )
  )
  (for-each setup/stations station
    (do
      (set data
        (weather-station
          component-name
          (concat station/ip ":" station/port)
        )
      )
      (local node now sensor-value sensor-type)
      (set node station/id)
      (### "add to current data")
      (if (not (has node-data node))
        (put node-data node (list))
      )
      (###
        "delete from data not relevant information"
      )
      (delete-info data)
      (###
        "set data names as required by this module"
      )
      (parse-data data)
      (### "check data")
      (for-each (names data) sensor
        (set sensor-value (get data sensor))
        (set sensor-type (get sensor-types sensor))
        (if
          (<=
            sensor-type/min
            sensor-value
            sensor-type/max
          )
          null
          (remove data sensor)
        )
      )
      (append (get node-data node) data)
      (set now (date))
      (put (get node-data node) "update" now)
      (### "add to history data")
      (add-history-data node data now)
      (### "publish data")
      (sentilo/publish node data)
      (module-notify "@display")
      (### "return node config")
      (get-node-config node)
    )
  )
)

The stop variable contains the code block that will be executed the module is stopped. This code will call the timer-cancel function to cancel the scheduled task.

(do
  (### "Stops the module")
  (timer-cancel)
)

In addition to publishing data on Sentilo, this module also shows the information on 10 different dashboards:

The module configuration parameters (access_interval_sec, Davis stations, Sentilo instances, etc.) are hold in the setup list.

Download module
Top