Difference between revisions of "StofAnanas"

From RevSpace
Jump to navigation Jump to search
(Colour mapping)
(47 intermediate revisions by the same user not shown)
Line 8: Line 8:
  
 
== The concept ==
 
== The concept ==
The stofananas/stoflama/pmlamp shows airborne particulate matter concentration from the nearest luftdaten citizen science measurement station as a colour on a LED lamp, inspired by http://stoflamp.nl
+
The stofananas/stoflama visualizes the current airborne particulate matter concentration from the nearest sensor.community measurement station, inspired by http://stoflamp.nl
The data that the LED colour is based on is retrieved over WiFi from the luftdaten website every 5 minutes.
+
The data that the LED colour is based on is retrieved over WiFi from the sensor.community website every 5 minutes.
  
This lamp is a soft plastic translucent pine apple ("ananas"), bought at the "Action" store.
+
This lamp is a soft plastic translucent pine apple ("ananas") or lama, bought at the "Action" store.
  
 
Features:
 
Features:
 
* The lamp shows PM10 particulate matter concentration as a colour, from dark blue for low concentration to bright pink for high concentration
 
* The lamp shows PM10 particulate matter concentration as a colour, from dark blue for low concentration to bright pink for high concentration
* The lamp retrieves the PM value over wifi from the the luftdaten website, there is a one-time setup to configure your WiFi network so the device can access the internet
+
* The lamp retrieves the PM value over wifi from the the sensor.community website, there is a one-time setup to configure your WiFi network so the device can access the internet
* The lamp automatically determines the nearest luftdaten particulate measurement station, by doing WiFi based geolocation.
+
* The lamp automatically determines the nearest sensor.community particulate measurement station, by doing WiFi-based geolocation.
* The number/id of the luftdaten station can also be entered manually during the one-time setup.
+
* The number/id of the sensor.community station can also be entered manually during the one-time setup.
 
* The hardware consists of a Wemos D1 mini board (about E3,-), a LED shield and a USB power supply + cable.
 
* The hardware consists of a Wemos D1 mini board (about E3,-), a LED shield and a USB power supply + cable.
 
* The software supports two different kinds of Wemos D1 mini LED "shield", both the shield with a single LED and the shield with 7 LEDs arranged in a star.  
 
* The software supports two different kinds of Wemos D1 mini LED "shield", both the shield with a single LED and the shield with 7 LEDs arranged in a star.  
  
 
Ideas/nice to have:
 
Ideas/nice to have:
* Use streaming mode of ESP8266HttpClient to parse JSON directly from a stream, avoiding memory problems / stack overflows
 
 
* Built-in web server, with links to historic measurement data of the sensor it's monitoring, link to google maps, etc.
 
* Built-in web server, with links to historic measurement data of the sensor it's monitoring, link to google maps, etc.
 
** complication: the [https://www.madavi.de/sensor/graph.php?sensor=esp8266-9137604-pms nice graphs on madavi.de] are based on the *hardware id* (not database id), we can't get that automatically
 
** complication: the [https://www.madavi.de/sensor/graph.php?sensor=esp8266-9137604-pms nice graphs on madavi.de] are based on the *hardware id* (not database id), we can't get that automatically
 
** just show a table with all properties in the left column (PM values, WGS84 location) and their values in the right column?
 
** just show a table with all properties in the left column (PM values, WGS84 location) and their values in the right column?
* The lamp "breathes" by varying the intensity over time, e.g. a cycle of 10 seconds where the light dims a bit and brightens a bit
 
** perhaps increase the breathing rate for higher PM?
 
** perhaps modulate the colour saturation instead of intensity?
 
** flash to indicate problems with the particulate matter module, so you can know something is wrong with your Luftdaten sensor and when to power-down/up to reset it again
 
 
* some kind of compatibility mode with the stoflamp from stoflamp.nl
 
* some kind of compatibility mode with the stoflamp from stoflamp.nl
 
Next steps:
 
 
* Run the software directly on a WiFi-enabled E14 RGB lamp from the Action store. This lamp has the microcontroller already built in. No separate USB power adapter is needed, it runs directly off 230V. See the [[WifiLamp]] project.
 
* Run the software directly on a WiFi-enabled E14 RGB lamp from the Action store. This lamp has the microcontroller already built in. No separate USB power adapter is needed, it runs directly off 230V. See the [[WifiLamp]] project.
  
Line 40: Line 33:
 
It consists of a few parts:
 
It consists of a few parts:
 
* a Wemos D1 mini board
 
* a Wemos D1 mini board
* a LED shield, either the one with 1 LED, or the one with 7 LEDs
+
* a LED shield, either the one with 1 LED, or the one with 7 LEDs (preferred, brighter)
* a 5V USB charger + USB cable
+
* a 5V USB charger with stable voltage output + USB cable
 
* a plastic lamp enclosure
 
* a plastic lamp enclosure
 +
 +
Steps:
 +
* solder the female headers onto the Wemos D1 mini, start with soldering just one pin so you can re-heat it and adjust it if the fit with the male pins is not quite right.
 +
* push the male headers into the female headers and solder the male headers onto the LED shield
 +
* connect the USB micro cable and flash the firmware into the Wemos d1 mini board
 +
* verify that it works as expected, on startup it does a quick colour animation, starting and ending with the red colour
 +
* remove the USB micro cable and push the Wemos d1 mini board + LED shield into the plastic enclosure
 +
* reattach the USB micro cable and verify it still works
 +
 +
It helps to make a few little knife cuts in the plastic of the enclosure to make the wemos d1 mini board fit more easily.
  
 
== Operation Manual ==
 
== Operation Manual ==
 
[[File:stoflama.jpg|right|thumb|stoflama]]
 
[[File:stoflama.jpg|right|thumb|stoflama]]
  
=== Programming ===
+
=== Flashing the firmware ===
* connect everything up as indicated under 'hardware' and connect the Wemos D1 board to the USB port of your development PC / laptop
+
* Connect everything up as indicated under 'hardware' and connect the Wemos D1 board to the USB port of your development PC / laptop
* flash the software into the Wemos D1 mini, typically from the command line using platformio:
+
* Flash the software into the Wemos D1 mini. Open a command line and enter the 'stofananas' source code directory, then compile and upload it using the following command:
 
   pio run -t upload
 
   pio run -t upload
  
 
This assumes you have platformio installed, this is done on Debian Linux for example, as follows:
 
This assumes you have platformio installed, this is done on Debian Linux for example, as follows:
   sudo apt-get install python-pip
+
   sudo apt install python-pip
 
   sudo pip install platformio
 
   sudo pip install platformio
  
Line 62: Line 65:
 
* power up the lamp
 
* power up the lamp
 
* on a mobile phone, connect to the "ESP-PMLAMP" network, and select login
 
* on a mobile phone, connect to the "ESP-PMLAMP" network, and select login
* when presented with the captive portal, select a WiFi network, fill in the WiFi password, leave the luftdaten id field empty, press SAVE
+
* when presented with the captive portal, select a WiFi network, fill in the WiFi password, leave the luftdaten id field EMPTY, press SAVE
* the closest luftdaten particulate matter sensor will automatically be determined
+
* the closest sensor.community particulate matter sensor will automatically be determined
  
 
Manual configuration:
 
Manual configuration:
 
* on https://netherlands.maps.luftdaten.info find the particulate matter station you want to follow and note the number of the station, for example "12246"
 
* on https://netherlands.maps.luftdaten.info find the particulate matter station you want to follow and note the number of the station, for example "12246"
 +
* power up the lamp
 
* on a mobile phone, connect to the "ESP-PMLAMP" network, and select login
 
* on a mobile phone, connect to the "ESP-PMLAMP" network, and select login
 
* when presented with the captive portal, select a WiFi network, fill in the WiFi password, AND ALSO fill in the number you looked up earlier, press SAVE
 
* when presented with the captive portal, select a WiFi network, fill in the WiFi password, AND ALSO fill in the number you looked up earlier, press SAVE
Line 74: Line 78:
 
* open a console to the serial port of the wemos D1 mini (115200 bps) and enter the following command:
 
* open a console to the serial port of the wemos D1 mini (115200 bps) and enter the following command:
 
   config auto
 
   config auto
* within a few seconds, you should see it prints the current approximate geographic location and the id of the luftdaten station, the id should be non-zero. It is saved in internal memory.
+
* within a few seconds, you should see it prints the current approximate geographic location and the id of the sensor.community station, the id should be non-zero. It is saved in internal memory.
  
 
=== Usage ===
 
=== Usage ===
Line 80: Line 84:
 
* The LED turns white for a short time, then changes to a colour representing a PM concentration
 
* The LED turns white for a short time, then changes to a colour representing a PM concentration
 
* A new PM measurement is retrieved automatically every 5 minutes.
 
* A new PM measurement is retrieved automatically every 5 minutes.
 +
* A flashing colour (1x per second) means that something went wrong, either communication with the sensor.community server or decoding the sensor.community server response
  
 
== Hardware ==
 
== Hardware ==
Line 89: Line 94:
  
 
The 7-LED shield is a lot brighter than the 1-LED shield.
 
The 7-LED shield is a lot brighter than the 1-LED shield.
 +
 +
Unfortunately not all LED shields are equal, some contain an RGB LED, others have the RGB colours switched.
 +
During startup, the colour animation should start and end with the RED colour.
  
 
== Software ==
 
== Software ==
Line 98: Line 106:
  
 
You can also compile it with the Arduino IDE, libraries used:
 
You can also compile it with the Arduino IDE, libraries used:
* WiFiManager (v 0.14) is used to manage WiFi connection to the local AP
+
* ArduinoJSON (v 6.17.2) for parsing the sensor.community response
* ArduinoJSON (v 6.11) for the parsing the luftdaten response
+
* FastLED (v 3.3.3) is used to control the RGB LED
* FastLED (v 3.2.9) is used to control the RGB LED
+
* WiFiManager (v 0.15.0) is used to manage WiFi connection to the local WiFi access point
  
=== Luftdaten interface ===
+
=== Sensor.community interface ===
 
The current measurement value of a particular sensor can be determined by parsing the JSON messages retrieved over a REST interface, by performing an HTTP GET on URL
 
The current measurement value of a particular sensor can be determined by parsing the JSON messages retrieved over a REST interface, by performing an HTTP GET on URL
 
   http://api.luftdaten.info/v1/sensor/<sensor-id>/
 
   http://api.luftdaten.info/v1/sensor/<sensor-id>/
where <sensor-id> is the database id used by luftdaten (and visible on their map).
+
  http://data.sensor.community/airrohr/v1/sensor/<sensor-id>/
 +
where <sensor-id> is the database id used by sensor.community (and visible on their map).
 
The trailing slash in the URL is significant.
 
The trailing slash in the URL is significant.
  
 
For example:
 
For example:
 
   http://api.luftdaten.info/v1/sensor/12246/
 
   http://api.luftdaten.info/v1/sensor/12246/
 +
  http://data.sensor.community/airrohr/v1/sensor/12246/
  
 
The software looks for the "P1" (PM10) sensor value in the JSON message and averages all measurements it finds (usually the current one and a previous one).
 
The software looks for the "P1" (PM10) sensor value in the JSON message and averages all measurements it finds (usually the current one and a previous one).
Line 119: Line 129:
 
* 30 ug/m3: <span style="background:#FFFF00>yellow</span>
 
* 30 ug/m3: <span style="background:#FFFF00>yellow</span>
 
* 60 ug/m3: <span style="background:#FF0000>red</span>
 
* 60 ug/m3: <span style="background:#FF0000>red</span>
* 120 ug/m3: (and higher)<span style="background:#FF0080>pink</span>
+
* 120+ ug/m3: <span style="background:#FF0080>pink</span>
 +
 
 +
== Next generation ==
 +
[[File:pmlamp_sequence.png|right|thumb|lamp-sensor pairing sequence diagram]]
 +
 
 +
The next generation will work as follows:
 +
* The protocol is ESP-NOW between roles 'lamp' and 'sensor', both can be ESP8266. ESP-NOW is a connection-less WiFi protocol running at a reduced rate of 1 Mbps, it does not require any credentials (like WEP/WPA) to be entered. '''The lamp is *not* connected to regular WiFi, this is different from (say) the mechanism used by the sensor.community flashing tool to find a sensor in the local network.'''
 +
* On startup, the lamp sends broadcast discovery messages at a regular interval, say every second, it flashes blue while doing so.
 +
* The sensor receives the discovery message and completes a pairing / association sequence with the lamp.
 +
* When a new measurement is done, the lamp sends a data message to the lamp. The lamp shows the associated color.
 +
 
 +
Pairing works as follows:
 +
* The lamp sends broadcast discovery messages (possibly on different channels) to announce its presence and discover the sensor, it flashes blue while doing so.
 +
* The dust sensor receives the broadcast discovery message and sends an associate message back.
 +
* The lamp receives the associate message, it stops flashing blue.
 +
* The (implicit) ACK from the lamp back to the sensor signals that the association is complete, the sensor remembers the lamp MAC and id
 +
 
 +
The data format is JSON, human readable, extensible. Not so friendly on data size, but we can send up to 250 bytes, should be enough.
 +
The dust sensor can show in its internal web page that a lamp was found and can display the lamp id.
 +
 
 +
Example broadcast discovery packet (lamp to dust sensor):
 +
  {"msg":"discover","id":"PMLAMP-000D1DD8"}
 +
 
 +
Example unicast discovery response packet (dust sensor to lamp):
 +
  {"msg":"associate","id":"SENSOR-00EE18EC"}
 +
 
 +
Example unicast measurement/color packet (dust sensor to lamp):
 +
  {"msg":"data","PM10":12.3,"PM2.5":8.4,"PM1.0":4.5,"humidity":42.5,"temperature":14.3,"pressure":102511,"color":"#11FF00"}
 +
  {"msg":"data","color":"#ffff00"}
 +
The lamp software can be implemented to either just display the color, or calculate it from the measurement.
 +
 
 +
==== Hardware ====
 +
You need two wemos d1 mini boards, one acting as the 'sensor', the other one as the 'lamp'.
 +
The wemos d1 mini acting as 'lamp' should be fitted with an RGB shield.
 +
 
 +
==== Software ====
 +
Source code is available here: https://github.com/bertrik/pmlamp-espnow-proto
 +
 
 +
Power on the lamp, the LED starts flashing blue, then power on the sensor, the LED stops flashing. You can connect over serial to the sensor node and type a command like 'led FF0000' to set the color of the lamp to red.
 +
 
 +
==== Other resources ====
 +
* http://smallbits.marshall-tribe.net/blog/2018/05/20/esp8266-now-talk

Revision as of 00:46, 1 January 2021

Project StofAnanas
StofAnanas.jpg
Showing particulate matter concentration as a coloured light
Status In progress
Contact bertrik
Last Update 2021-01-01

The concept

The stofananas/stoflama visualizes the current airborne particulate matter concentration from the nearest sensor.community measurement station, inspired by http://stoflamp.nl The data that the LED colour is based on is retrieved over WiFi from the sensor.community website every 5 minutes.

This lamp is a soft plastic translucent pine apple ("ananas") or lama, bought at the "Action" store.

Features:

  • The lamp shows PM10 particulate matter concentration as a colour, from dark blue for low concentration to bright pink for high concentration
  • The lamp retrieves the PM value over wifi from the the sensor.community website, there is a one-time setup to configure your WiFi network so the device can access the internet
  • The lamp automatically determines the nearest sensor.community particulate measurement station, by doing WiFi-based geolocation.
  • The number/id of the sensor.community station can also be entered manually during the one-time setup.
  • The hardware consists of a Wemos D1 mini board (about E3,-), a LED shield and a USB power supply + cable.
  • The software supports two different kinds of Wemos D1 mini LED "shield", both the shield with a single LED and the shield with 7 LEDs arranged in a star.

Ideas/nice to have:

  • Built-in web server, with links to historic measurement data of the sensor it's monitoring, link to google maps, etc.
    • complication: the nice graphs on madavi.de are based on the *hardware id* (not database id), we can't get that automatically
    • just show a table with all properties in the left column (PM values, WGS84 location) and their values in the right column?
  • some kind of compatibility mode with the stoflamp from stoflamp.nl
  • Run the software directly on a WiFi-enabled E14 RGB lamp from the Action store. This lamp has the microcontroller already built in. No separate USB power adapter is needed, it runs directly off 230V. See the WifiLamp project.

Building it

stoflama

It consists of a few parts:

  • a Wemos D1 mini board
  • a LED shield, either the one with 1 LED, or the one with 7 LEDs (preferred, brighter)
  • a 5V USB charger with stable voltage output + USB cable
  • a plastic lamp enclosure

Steps:

  • solder the female headers onto the Wemos D1 mini, start with soldering just one pin so you can re-heat it and adjust it if the fit with the male pins is not quite right.
  • push the male headers into the female headers and solder the male headers onto the LED shield
  • connect the USB micro cable and flash the firmware into the Wemos d1 mini board
  • verify that it works as expected, on startup it does a quick colour animation, starting and ending with the red colour
  • remove the USB micro cable and push the Wemos d1 mini board + LED shield into the plastic enclosure
  • reattach the USB micro cable and verify it still works

It helps to make a few little knife cuts in the plastic of the enclosure to make the wemos d1 mini board fit more easily.

Operation Manual

stoflama

Flashing the firmware

  • Connect everything up as indicated under 'hardware' and connect the Wemos D1 board to the USB port of your development PC / laptop
  • Flash the software into the Wemos D1 mini. Open a command line and enter the 'stofananas' source code directory, then compile and upload it using the following command:
 pio run -t upload

This assumes you have platformio installed, this is done on Debian Linux for example, as follows:

 sudo apt install python-pip
 sudo pip install platformio

Configuration

configuration

Automatic configuration:

  • power up the lamp
  • on a mobile phone, connect to the "ESP-PMLAMP" network, and select login
  • when presented with the captive portal, select a WiFi network, fill in the WiFi password, leave the luftdaten id field EMPTY, press SAVE
  • the closest sensor.community particulate matter sensor will automatically be determined

Manual configuration:

  • on https://netherlands.maps.luftdaten.info find the particulate matter station you want to follow and note the number of the station, for example "12246"
  • power up the lamp
  • on a mobile phone, connect to the "ESP-PMLAMP" network, and select login
  • when presented with the captive portal, select a WiFi network, fill in the WiFi password, AND ALSO fill in the number you looked up earlier, press SAVE

Alternatively:

  • make sure at least the WiFi credentials have been entered and saved
  • open a console to the serial port of the wemos D1 mini (115200 bps) and enter the following command:
 config auto
  • within a few seconds, you should see it prints the current approximate geographic location and the id of the sensor.community station, the id should be non-zero. It is saved in internal memory.

Usage

  • Power up the lamp while in range of the WiFi network you configured. The LED shows a colour animation when starting up (starting with red and ending with red)
  • The LED turns white for a short time, then changes to a colour representing a PM concentration
  • A new PM measurement is retrieved automatically every 5 minutes.
  • A flashing colour (1x per second) means that something went wrong, either communication with the sensor.community server or decoding the sensor.community server response

Hardware

The electronic part consists of a Wemos D1 mini board and a LED shield.

The LED can be one of these LED shields:

The 7-LED shield is a lot brighter than the 1-LED shield.

Unfortunately not all LED shields are equal, some contain an RGB LED, others have the RGB colours switched. During startup, the colour animation should start and end with the RED colour.

Software

Getting and building the source code

Source code is available on github.

To compile and upload it, I use platform io:

 pio run -t upload

You can also compile it with the Arduino IDE, libraries used:

  • ArduinoJSON (v 6.17.2) for parsing the sensor.community response
  • FastLED (v 3.3.3) is used to control the RGB LED
  • WiFiManager (v 0.15.0) is used to manage WiFi connection to the local WiFi access point

Sensor.community interface

The current measurement value of a particular sensor can be determined by parsing the JSON messages retrieved over a REST interface, by performing an HTTP GET on URL

 http://api.luftdaten.info/v1/sensor/<sensor-id>/
 http://data.sensor.community/airrohr/v1/sensor/<sensor-id>/

where <sensor-id> is the database id used by sensor.community (and visible on their map). The trailing slash in the URL is significant.

For example:

 http://api.luftdaten.info/v1/sensor/12246/
 http://data.sensor.community/airrohr/v1/sensor/12246/

The software looks for the "P1" (PM10) sensor value in the JSON message and averages all measurements it finds (usually the current one and a previous one).

Colour mapping

Mapping is as follows, with piecewise linear interpolation:

  • 0 ug/m3: deep blue
  • 15 ug/m3: green
  • 30 ug/m3: yellow
  • 60 ug/m3: red
  • 120+ ug/m3: pink

Next generation

lamp-sensor pairing sequence diagram

The next generation will work as follows:

  • The protocol is ESP-NOW between roles 'lamp' and 'sensor', both can be ESP8266. ESP-NOW is a connection-less WiFi protocol running at a reduced rate of 1 Mbps, it does not require any credentials (like WEP/WPA) to be entered. The lamp is *not* connected to regular WiFi, this is different from (say) the mechanism used by the sensor.community flashing tool to find a sensor in the local network.
  • On startup, the lamp sends broadcast discovery messages at a regular interval, say every second, it flashes blue while doing so.
  • The sensor receives the discovery message and completes a pairing / association sequence with the lamp.
  • When a new measurement is done, the lamp sends a data message to the lamp. The lamp shows the associated color.

Pairing works as follows:

  • The lamp sends broadcast discovery messages (possibly on different channels) to announce its presence and discover the sensor, it flashes blue while doing so.
  • The dust sensor receives the broadcast discovery message and sends an associate message back.
  • The lamp receives the associate message, it stops flashing blue.
  • The (implicit) ACK from the lamp back to the sensor signals that the association is complete, the sensor remembers the lamp MAC and id

The data format is JSON, human readable, extensible. Not so friendly on data size, but we can send up to 250 bytes, should be enough. The dust sensor can show in its internal web page that a lamp was found and can display the lamp id.

Example broadcast discovery packet (lamp to dust sensor):

 {"msg":"discover","id":"PMLAMP-000D1DD8"}

Example unicast discovery response packet (dust sensor to lamp):

 {"msg":"associate","id":"SENSOR-00EE18EC"}

Example unicast measurement/color packet (dust sensor to lamp):

 {"msg":"data","PM10":12.3,"PM2.5":8.4,"PM1.0":4.5,"humidity":42.5,"temperature":14.3,"pressure":102511,"color":"#11FF00"}
 {"msg":"data","color":"#ffff00"}

The lamp software can be implemented to either just display the color, or calculate it from the measurement.

Hardware

You need two wemos d1 mini boards, one acting as the 'sensor', the other one as the 'lamp'. The wemos d1 mini acting as 'lamp' should be fitted with an RGB shield.

Software

Source code is available here: https://github.com/bertrik/pmlamp-espnow-proto

Power on the lamp, the LED starts flashing blue, then power on the sensor, the LED stops flashing. You can connect over serial to the sensor node and type a command like 'led FF0000' to set the color of the lamp to red.

Other resources