/ dsp2017

Smogly - a dive into arduino

Smogly is an open air quality monitor

I've decided to set aside some time to work on my second data source - Smogly.

Smogly is Open Source and Open Hardware.

Sounds interesting?

I'm using Atom-based PlatformIO for coding. I normally prefer a full IDE, but since I'm an Arduino newbie I've decided to use whatever Smogly's team is using.

PlatformIO provides a registry of packages that it can install during a build.

We're using a Wemos D1 mini as well as several other parts. Since I'm only good enough with soldering iron to burn myself and get hurt I left hardware part to Justyna.

I've started with adding some fake data sending to web app so other team members with boards can already test Smogly's Django-based backend. WiFi connection is already established in setup() so sending data is very straightforward.

void loop() {
    String output = createPayload();
    HTTPClient http;
    http.begin(apiEndpoint);
    http.addHeader("Content-Type", "application/json");
    http.POST(output);
    http.end();
    delay(TIME_BETWEEN_METERINGS);
}

String createPayload()
{
  StaticJsonBuffer<300> jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  root["pm01"] = "1";
  root["pm25"] = "2";
  root["pm10"] = "3";
  root["temp_out1"] = "4";
  root["temp_out2"] = "5";
  root["temp_out3"] = "6";
  root["temp_int_air1"] = "7";
  root["hum_out1"] = "8";
  root["hum_out2"] = "9";
  root["hum_out3"] = "10";
  root["hum_int_air1"] = "11";
  root["rssi"] = "12";
  root["bpress_out1"] = "13";
  String output;
  root.printTo(output);
  return output;
}

But that's fake data.

PMS3003 (PM2.5 and PM10 sensor) unfortunately didn't arrive yet, so Justyna couldn't mount it into our setup, but we have DHT22 (temperature and humidity sensor).

It isn't much of a challenge. It has a separate power connection, and only uses one pin for communication with Arduino.

Install relevant lib in your platformio.ini:

[env:d1_mini]
platform=espressif8266
board=d1_mini
framework=arduino
# Last one in the following list:
lib_deps =
  WifiManager
  https://github.com/EnviroMonitor/ESP8266wifi.git#pgmspace_include
  ArduinoJson
  EspSoftwareSerial
  HTTPClient
  DHT sensor library

I've moved it to a separate class because I might want to process the data and hide the setup from the main loop code.

#include "SmoglyDHT.h"
#include <DHT.h>
#define DHTPIN 2        // what digital pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

DHT dht(DHTPIN, DHTTYPE);

void SmoglyDHT::setup() {
  dht.begin();
}

float SmoglyDHT::readHumidity() {
  return dht.readHumidity();
}

float SmoglyDHT::readTemperature() {
  return dht.readTemperature();
}

It's ugly code, but that's a starting point and I'm an Arduino newbie. :)