daniel mancuso
Published © MIT

Indoor Air Quality Monitor

Use a USB air-quality sensor dongle to quickly mount an indoor air monitoring system and prevent headaches and further impact on health.

IntermediateFull instructions provided3 hours19,300
Indoor Air Quality Monitor

Things used in this project

Story

Read more

Code

Forward uThingVOC data to InfluxDB

Python
Python3 script to capture the data from the uThing::VOC and store it directly in a InfluxDB database
#!/usr/local/bin/python
import time, sys, json
from influxdb import InfluxDBClient
import serial

json_message = [{
      "measurement": "humidity",
      "fields": {
           "value": 12.34
       }
    }]   

influxClient = InfluxDBClient('localhost', 8086, 'user', 'password', 'database')

def serialToInflux():

  while True:
      try:
        message = ""
 
        #uart = serial.Serial('/dev/tty.usbmodem14101', 115200, timeout=11) #uThingVOC connected over USB-CDC (MacOS)
        uart = serial.Serial('/dev/ttyACM0', 115200, timeout=11) #uThingVOC connected over USB-CDC (Linux, RPi)
        uart.write(b'J\n')
        
      except serial.SerialException:
        print("Error opening uart")
        break

      while True:  
          try:   
            message = uart.readline()
            uart.flushInput()
          except:
            print("Error")  

          try:
            dataDict = json.loads(message.decode())

            for measurement, value in dataDict.items():
              # print("{}: {}".format(measurement, value))
              json_message[0]['measurement'] = measurement
              json_message[0]['fields']['value'] = float(value)
              print(json_message)
              try:
                influxClient.write_points(json_message)
              except OSError as e:
                print("Unable to write to InfluxDB: %s" % e)

          except ValueError:
            print("ValueErrorError on received or parsing data!")     
          except IndexError:           
            print("IndexErrorError on received or parsing data!")     

if __name__ == "__main__":
    serialToInflux()

airqualitymonitoringDashboard.json

JSON
JSON Dashboard file for Grafana
{
  "annotations": {
    "list": [
      {
        "builtIn": 1,
        "datasource": "-- Grafana --",
        "enable": true,
        "hide": true,
        "iconColor": "rgba(0, 211, 255, 1)",
        "name": "Annotations & Alerts",
        "type": "dashboard"
      }
    ]
  },
  "editable": true,
  "gnetId": null,
  "graphTooltip": 0,
  "id": 3,
  "links": [],
  "originalTemplating": [],
  "originalTime": {
    "from": "now-15m",
    "to": "now"
  },
  "panels": [
    {
      "cacheTimeout": null,
      "colorBackground": false,
      "colorValue": true,
      "colors": [
        "#82b5d8",
        "#9ac48a",
        "#ef843c"
      ],
      "datasource": "airquality",
      "format": "celsius",
      "gauge": {
        "maxValue": 50,
        "minValue": 0,
        "show": true,
        "thresholdLabels": false,
        "thresholdMarkers": true
      },
      "gridPos": {
        "h": 4,
        "w": 3,
        "x": 0,
        "y": 0
      },
      "hideTimeOverride": false,
      "id": 13,
      "interval": null,
      "links": [],
      "mappingType": 1,
      "mappingTypes": [
        {
          "name": "value to text",
          "value": 1
        },
        {
          "name": "range to text",
          "value": 2
        }
      ],
      "maxDataPoints": 100,
      "nullPointMode": "connected",
      "nullText": null,
      "postfix": "",
      "postfixFontSize": "50%",
      "prefix": "",
      "prefixFontSize": "50%",
      "rangeMaps": [
        {
          "from": "null",
          "text": "N/A",
          "to": "null"
        }
      ],
      "sparkline": {
        "fillColor": "rgba(234, 184, 57, 0.25)",
        "full": true,
        "lineColor": "#e5ac0e",
        "show": true
      },
      "tableColumn": "last",
      "targets": [
        {
          "groupBy": [
            {
              "params": [
                "$__interval"
              ],
              "type": "time"
            },
            {
              "params": [
                "null"
              ],
              "type": "fill"
            }
          ],
          "measurement": "temperature",
          "orderByTime": "ASC",
          "policy": "default",
          "refId": "A",
          "resultFormat": "time_series",
          "select": [
            [
              {
                "params": [
                  "value"
                ],
                "type": "field"
              },
              {
                "params": [],
                "type": "last"
              }
            ]
          ],
          "tags": []
        }
      ],
      "thresholds": "20,29,35",
      "title": "Temperature",
      "transparent": true,
      "type": "singlestat",
      "valueFontSize": "80%",
      "valueMaps": [
        {
          "op": "=",
          "text": "N/A",
          "value": "null"
        }
      ],
      "valueName": "current"
    },
    {
      "cacheTimeout": null,
      "colorBackground": false,
      "colorValue": true,
      "colors": [
        "#70dbed",
        "#e0f9d7",
        "#e24d42"
      ],
      "datasource": "airquality",
      "format": "humidity",
      "gauge": {
        "maxValue": 90,
        "minValue": 10,
        "show": true,
        "thresholdLabels": false,
        "thresholdMarkers": true
      },
      "gridPos": {
        "h": 4,
        "w": 3,
        "x": 3,
        "y": 0
      },
      "hideTimeOverride": false,
      "id": 12,
      "interval": null,
      "links": [],
      "mappingType": 1,
      "mappingTypes": [
        {
          "name": "value to text",
          "value": 1
        },
        {
          "name": "range to text",
          "value": 2
        }
      ],
      "maxDataPoints": 100,
      "nullPointMode": "connected",
      "nullText": null,
      "postfix": "",
      "postfixFontSize": "50%",
      "prefix": "",
      "prefixFontSize": "50%",
      "rangeMaps": [
        {
          "from": "null",
          "text": "N/A",
          "to": "null"
        }
      ],
      "sparkline": {
        "fillColor": "rgba(31, 118, 189, 0.18)",
        "full": true,
        "lineColor": "rgb(31, 120, 193)",
        "show": true
      },
      "tableColumn": "last",
      "targets": [
        {
          "groupBy": [
            {
              "params": [
                "$__interval"
              ],
              "type": "time"
            },
            {
              "params": [
                "null"
              ],
              "type": "fill"
            }
          ],
          "measurement": "humidity",
          "orderByTime": "ASC",
          "policy": "default",
          "refId": "A",
          "resultFormat": "time_series",
          "select": [
            [
              {
                "params": [
                  "value"
                ],
                "type": "field"
              },
              {
                "params": [],
                "type": "last"
              }
            ]
          ],
          "tags": []
        }
      ],
      "thresholds": "20,40,65",
      "title": "Relative Humidity",
      "transparent": true,
      "type": "singlestat",
      "valueFontSize": "80%",
      "valueMaps": [
        {
          "op": "=",
          "text": "N/A",
          "value": "null"
        }
      ],
      "valueName": "current"
    },
    {
      "cacheTimeout": null,
      "colorBackground": false,
      "colorValue": true,
      "colors": [
        "#6ed0e0",
        "#badff4",
        "#70dbed"
      ],
      "datasource": "airquality",
      "format": "pressurehpa",
      "gauge": {
        "maxValue": 1030,
        "minValue": 990,
        "show": true,
        "thresholdLabels": false,
        "thresholdMarkers": true
      },
      "gridPos": {
        "h": 4,
        "w": 3,
        "x": 6,
        "y": 0
      },
      "hideTimeOverride": false,
      "id": 11,
      "interval": null,
      "links": [],
      "mappingType": 1,
      "mappingTypes": [
        {
          "name": "value to text",
          "value": 1
        },
        {
          "name": "range to text",
          "value": 2
        }
      ],
      "maxDataPoints": 100,
      "nullPointMode": "connected",
      "nullText": null,
      "postfix": "",
      "postfixFontSize": "50%",
      "prefix": "",
      "prefixFontSize": "50%",
      "rangeMaps": [
        {
          "from": "null",
          "text": "N/A",
          "to": "null"
        }
      ],
      "sparkline": {
        "fillColor": "rgba(97, 77, 147, 0.29)",
        "full": true,
        "lineColor": "#aea2e0",
        "show": true
      },
      "tableColumn": "last",
      "targets": [
        {
          "groupBy": [
            {
              "params": [
                "$__interval"
              ],
              "type": "time"
            },
            {
              "params": [
                "null"
              ],
              "type": "fill"
            }
          ],
          "measurement": "pressure",
          "orderByTime": "ASC",
          "policy": "default",
          "refId": "A",
          "resultFormat": "time_series",
          "select": [
            [
              {
                "params": [
                  "value"
                ],
                "type": "field"
              },
              {
                "params": [],
                "type": "last"
              }
            ]
          ],
          "tags": []
        }
      ],
      "thresholds": "1005, 1014,1023",
      "title": "Barometric Pressure",
      "transparent": true,
      "type": "singlestat",
      "valueFontSize": "80%",
      "valueMaps": [
        {
          "op": "=",
          "text": "N/A",
          "value": "null"
        }
      ],
      "valueName": "current"
    },
    {
      "cacheTimeout": null,
      "colorBackground": false,
      "colorValue": true,
      "colors": [
        "#bf1b00",
        "rgba(237, 129, 40, 0.89)",
        "#508642"
      ],
      "datasource": "airquality",
      "format": "ohm",
      "gauge": {
        "maxValue": 90,
        "minValue": 0,
        "show": true,
        "thresholdLabels": false,
        "thresholdMarkers": true
      },
      "gridPos": {
        "h": 4,
        "w": 3,
        "x": 10,
        "y": 0
      },
      "hideTimeOverride": false,
      "id": 10,
      "interval": null,
      "links": [],
      "mappingType": 1,
      "mappingTypes": [
        {
          "name": "value to text",
          "value": 1
        },
        {
          "name": "range to text",
          "value": 2
        }
      ],
      "maxDataPoints": 100,
      "nullPointMode": "connected",
      "nullText": null,
      "postfix": "",
      "postfixFontSize": "50%",
      "prefix": "",
      "prefixFontSize": "50%",
      "rangeMaps": [
        {
          "from": "null",
          "text": "N/A",
          "to": "null"
        }
      ],
      "sparkline": {
        "fillColor": "rgba(221, 213, 0, 0.15)",
        "full": true,
        "lineColor": "#fce2de",
        "show": true
      },
      "tableColumn": "last",
      "targets": [
        {
          "groupBy": [
            {
              "params": [
                "$__interval"
              ],
              "type": "time"
            },
            {
              "params": [
                "null"
              ],
              "type": "fill"
            }
          ],
          "measurement": "gasResistance",
          "orderByTime": "ASC",
          "policy": "default",
          "refId": "A",
          "resultFormat": "time_series",
          "select": [
            [
              {
                "params": [
                  "value"
                ],
                "type": "field"
              },
              {
                "params": [],
                "type": "last"
              }
            ]
          ],
          "tags": []
        }
      ],
      "thresholds": "50, 65,90",
      "title": "Gas Resistance",
      "transparent": true,
      "type": "singlestat",
      "valueFontSize": "80%",
      "valueMaps": [
        {
          "op": "=",
          "text": "N/A",
          "value": "null"
        }
      ],
      "valueName": "current"
    },
    {
      "cacheTimeout": null,
      "colorBackground": false,
      "colorValue": true,
      "colors": [
        "#bf1b00",
        "rgba(237, 129, 40, 0.89)",
        "#508642"
      ],
      "datasource": "airquality",
      "format": "none",
      "gauge": {
        "maxValue": 90,
        "minValue": 0,
        "show": false,
        "thresholdLabels": false,
        "thresholdMarkers": true
      },
      "gridPos": {
        "h": 4,
        "w": 2,
        "x": 13,
        "y": 0
      },
      "hideTimeOverride": false,
      "id": 15,
      "interval": null,
      "links": [],
      "mappingType": 1,
      "mappingTypes": [
        {
          "name": "value to text",
          "value": 1
        },
        {
          "name": "range to text",
          "value": 2
        }
      ],
      "maxDataPoints": 100,
      "nullPointMode": "connected",
      "nullText": null,
      "postfix": "",
      "postfixFontSize": "50%",
      "prefix": "",
      "prefixFontSize": "50%",
      "rangeMaps": [
        {
          "from": "null",
          "text": "N/A",
          "to": "null"
        }
      ],
      "sparkline": {
        "fillColor": "rgba(221, 213, 0, 0.15)",
        "full": false,
        "lineColor": "#fce2de",
        "show": false
      },
      "tableColumn": "last",
      "targets": [
        {
          "groupBy": [
            {
              "params": [
                "$__interval"
              ],
              "type": "time"
            },
            {
              "params": [
                "null"
              ],
              "type": "fill"
            }
          ],
          "measurement": "iaqAccuracy",
          "orderByTime": "ASC",
          "policy": "default",
          "refId": "A",
          "resultFormat": "time_series",
          "select": [
            [
              {
                "params": [
                  "value"
                ],
                "type": "field"
              },
              {
                "params": [],
                "type": "last"
              }
            ]
          ],
          "tags": []
        }
      ],
      "thresholds": "0,1,2,3",
      "title": "IAQ Accuracy",
      "transparent": true,
      "type": "singlestat",
      "valueFontSize": "70%",
      "valueMaps": [
        {
          "op": "=",
          "text": "Calibrating",
          "value": "0"
        },
        {
          "op": "=",
          "text": "1 (stable)",
          "value": "1"
        },
        {
          "op": "=",
          "text": "2 (OK)",
          "value": "2"
        },
        {
          "op": "=",
          "text": "3 (Max)",
          "value": "3"
        }
      ],
      "valueName": "current"
    },
    {
      "cacheTimeout": null,
      "colorBackground": false,
      "colorValue": true,
      "colors": [
        "#629e51",
        "#e5ac0e",
        "#ef843c"
      ],
      "datasource": "airquality",
      "format": "none",
      "gauge": {
        "maxValue": 500,
        "minValue": 0,
        "show": false,
        "thresholdLabels": false,
        "thresholdMarkers": true
      },
      "gridPos": {
        "h": 4,
        "w": 4,
        "x": 15,
        "y": 0
      },
      "hideTimeOverride": false,
      "id": 16,
      "interval": null,
      "links": [],
      "mappingType": 1,
      "mappingTypes": [
        {
          "name": "value to text",
          "value": 1
        },
        {
          "name": "range to text",
          "value": 2
        }
      ],
      "maxDataPoints": 100,
      "nullPointMode": "connected",
      "nullText": null,
      "postfix": "",
      "postfixFontSize": "50%",
      "prefix": "",
      "prefixFontSize": "50%",
      "rangeMaps": [
        {
          "from": "0",
          "text": "good",
          "to": "50"
        },
        {
          "from": "51",
          "text": "average",
          "to": "100"
        },
        {
          "from": "101",
          "text": "little bad",
          "to": "150"
        },
        {
          "from": "151",
          "text": "bad",
          "to": "200"
        },
        {
          "from": "201",
          "text": "worse",
          "to": "300"
        },
        {
          "from": "301",
          "text": "very bad",
          "to": "500"
        }
      ],
      "sparkline": {
        "fillColor": "rgba(234, 184, 57, 0.25)",
        "full": true,
        "lineColor": "#e5ac0e",
        "show": true
      },
      "tableColumn": "last",
      "targets": [
        {
          "groupBy": [
            {
              "params": [
                "$__interval"
              ],
              "type": "time"
            },
            {
              "params": [
                "null"
              ],
              "type": "fill"
            }
          ],
          "measurement": "IAQ",
          "orderByTime": "ASC",
          "policy": "default",
          "refId": "A",
          "resultFormat": "time_series",
          "select": [
            [
              {
                "params": [
                  "value"
                ],
                "type": "field"
              },
              {
                "params": [],
                "type": "last"
              }
            ]
          ],
          "tags": []
        }
      ],
      "thresholds": "50,100,200,300",
      "title": "Air-Quality Idx",
      "transparent": true,
      "type": "singlestat",
      "valueFontSize": "150%",
      "valueMaps": [
        {
          "op": "=",
          "text": "N/A",
          "value": "null"
        }
      ],
      "valueName": "current"
    },
    {
      "cacheTimeout": null,
      "colorBackground": false,
      "colorValue": true,
      "colors": [
        "#629e51",
        "#e5ac0e",
        "#ef843c"
      ],
      "datasource": "airquality",
      "format": "none",
      "gauge": {
        "maxValue": 500,
        "minValue": 0,
        "show": false,
        "thresholdLabels": false,
        "thresholdMarkers": true
      },
      "gridPos": {
        "h": 4,
        "w": 3,
        "x": 19,
        "y": 0
      },
      "hideTimeOverride": false,
      "id": 14,
      "interval": null,
      "links": [],
      "mappingType": 2,
      "mappingTypes": [
        {
          "name": "value to text",
          "value": 1
        },
        {
          "name": "range to text",
          "value": 2
        }
      ],
      "maxDataPoints": 100,
      "nullPointMode": "connected",
      "nullText": null,
      "postfix": "",
      "postfixFontSize": "50%",
      "prefix": "",
      "prefixFontSize": "50%",
      "rangeMaps": [
        {
          "from": "0",
          "text": "good",
          "to": "50"
        },
        {
          "from": "51",
          "text": "average",
          "to": "100"
        },
        {
          "from": "101",
          "text": "little bad",
          "to": "150"
        },
        {
          "from": "151",
          "text": "bad",
          "to": "200"
        },
        {
          "from": "201",
          "text": "worse",
          "to": "300"
        },
        {
          "from": "301",
          "text": "very bad",
          "to": "500"
        }
      ],
      "sparkline": {
        "fillColor": "rgba(234, 184, 57, 0.25)",
        "full": true,
        "lineColor": "#e5ac0e",
        "show": false
      },
      "tableColumn": "last",
      "targets": [
        {
          "groupBy": [
            {
              "params": [
                "$__interval"
              ],
              "type": "time"
            },
            {
              "params": [
                "null"
              ],
              "type": "fill"
            }
          ],
          "measurement": "IAQ",
          "orderByTime": "ASC",
          "policy": "default",
          "refId": "A",
          "resultFormat": "time_series",
          "select": [
            [
              {
                "params": [
                  "value"
                ],
                "type": "field"
              },
              {
                "params": [],
                "type": "last"
              }
            ]
          ],
          "tags": []
        }
      ],
      "thresholds": "50,100,200,300",
      "title": "Air-Quality Idx",
      "transparent": true,
      "type": "singlestat",
      "valueFontSize": "100%",
      "valueMaps": [
        {
          "op": "=",
          "text": "N/A",
          "value": "null"
        }
      ],
      "valueName": "current"
    },
    {
      "aliasColors": {},
      "bars": true,
      "dashLength": 10,
      "dashes": false,
      "datasource": "airquality",
      "decimals": 2,
      "description": "airquality",
      "fill": 2,
      "gridPos": {
        "h": 5,
        "w": 11,
        "x": 0,
        "y": 4
      },
      "id": 2,
      "legend": {
        "avg": true,
        "current": false,
        "max": true,
        "min": true,
        "show": true,
        "total": false,
        "values": true
      },
      "lines": true,
      "linewidth": 1,
      "links": [],
      "nullPointMode": "null",
      "percentage": false,
      "pointradius": 1,
      "points": true,
      "renderer": "flot",
      "seriesOverrides": [],
      "spaceLength": 10,
      "stack": false,
      "steppedLine": false,
      "targets": [
        {
          "groupBy": [
            {
              "params": [
                "$__interval"
              ],
              "type": "time"
            },
            {
              "params": [
                "null"
              ],
              "type": "fill"
            }
          ],
          "measurement": "temperature",
          "orderByTime": "ASC",
          "policy": "default",
          "refId": "A",
          "resultFormat": "time_series",
          "select": [
            [
              {
                "params": [
                  "value"
                ],
                "type": "field"
              },
              {
                "params": [],
                "type": "last"
              }
            ]
          ],
          "tags": []
        }
      ],
      "thresholds": [],
      "timeFrom": null,
      "timeShift": null,
      "title": "BME680 Temperature",
      "tooltip": {
        "shared": true,
        "sort": 0,
        "value_type": "individual"
      },
      "transparent": true,
      "type": "graph",
      "xaxis": {
        "buckets": null,
        "mode": "time",
        "name": null,
        "show": true,
        "values": []
      },
      "yaxes": [
        {
          "decimals": 2,
          "format": "celsius",
          "label": "Temperature",
          "logBase": 1,
          "max": null,
          "min": null,
          "show": true
        },
        {
          "decimals": 2,
          "format": "celsius",
          "label": "Temperature",
          "logBase": 1,
          "max": "40",
          "min": "0",
          "show": false
        }
      ],
      "yaxis": {
        "align": false,
        "alignLevel": null
      }
    },
    {
      "aliasColors": {},
      "bars": true,
      "dashLength": 10,
      "dashes": false,
      "datasource": null,
      "fill": 1,
      "gridPos": {
...

This file has been truncated, please download it to see its full contents.

Credits

daniel mancuso

daniel mancuso

5 projects • 34 followers
Electronic Engineer. Embedded systems design, MCUs, IoT, RTOS, enjoy trying out new technologies. Founder - Lead Engineer @ OhmTech.io

Comments