Vehicle traffic statistics

Introduction

This module provides information about the number of vehicles detected by the sensor located at Laureà Miró Street in Sant Feliu de Llobregat. This sensor is published on the Sentilo platform. The following picture shows the system architecture:

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.

This module has three dashboards to show the traffic information. The first one, called Statistics, has a text box with the following information:

In addition, it has a graphic widget showing the number of vehicles per hour in a week period.

The second and third are called Current Data and Times. They have a graphic widget and a text box. The graphic 'Number of Vehicles' shows how the number of vehicles develops over time. The graphic widget called 'Time between vehicles' shows two different parameters: the time between two consecutive vehicles and the average of these times. Text box is the same in both dashboards and shows these data:

Programming the module

The sensor publishes its data in Sentilo, so the information is available in the platform. The first code executed, once the module is initiated, is that contained in the start variable:

(do
  (### "Starts the main loop")
  (spawn (eval loop))
  (### "Creates the subscription to the sensor")
  (sentilo/suscription
    setup/instances/santfeliu
    setup/sensor
  )
)

The sentilo/subscription function launches a subscription to the sensor data. It means that every data generated by the sensor will be delivered to the module too:

(function (sentilo_instance sensor)
  (http
    "PUT"
    (concat
      sentilo_instance/url
      "/subscribe/data/"
      sensor/provider
      "/"
      sensor/ID
    )
    "body" =>
    "{\"endpoint\" : \"http://smartcity.santfeliu.cat/brain4it-server-web/modules/VehicleStatistics/@callback\"}"
    "properties" =>
    (list "IDENTITY_KEY" => sensor/key)
  )
)

Since the module is subscribed to the sensor, whenever a new value is generated, the function @callback is called:

(function (context data)
  (###
    "This function is called every time a vehicle is detected"
  )
  (set variables/data (parse data "json"))
  (add-node-to-history)
  (calculate-time-between-vehicles)
  (calculate-average-time-between-vehicles)
  (module-notify
    "@get-history"
    "@get-value"
    "@get-history-time"
    "@get-value-time"
    "@get-display-current-values"
  )
)

This data is added to a list and showed in Current_Data dashboard.

In order to complete the module functionality, loop is started before the subscription and the sensor current value will be obtained every variables/access_interval_sec seconds:

(while true
  (try
    (do
      (###
        "Every access_interval_sec a new value is generated"
      )
      (generate-data)
      (sleep (* 1000 setup/access_interval_sec))
    )
    (ex
      "*" =>
      (do
        (###
          "When an error takes place, it is saved in a variable named 'error'"
        )
        (set error (list (format-date (date)) ex))
        (sleep (* 1000 setup/access_interval_sec))
      )
    )
  )
)

The function generate-data is responsible for getting the value and adding it to the list that will be represented in the Statistics dashboard.

The user views are the following:

(* images * )

The stop code is executed when module is stopped. It launches the unsubscribe command to Sentilo in order to delete the subscription.

(do
  (### "Delete sensor suscription")
  (sentilo/unsuscription
    setup/instances/santfeliu
    setup/sensor
  )
)

This is the function that performs the unsuscription action:

(function (sentilo_instance sensor)
  (http
    "DELETE"
    (concat
      sentilo_instance/url
      "/subscribe/data/"
      sensor/provider
      "/"
      sensor/ID
    )
    "properties" =>
    (list "IDENTITY_KEY" => sensor/key)
  )
)
Download module
Top