Sensor.Community is a community-driven sensor network that generates Open Environmental Data. This story describes how to sense air quality by detecting PM10 & PM2.5 values using the SDS011 sensor. The sensor is connected to a Blues Wireless Notecard containing an LTE-M modem with a global roaming SIM from Monogoto. From the Blues Wireless environment, the data is routed to Qubitro — a low-code IoT platform — for storing and visualizing data. In addition, a python script is created to get the latest data from Qubitro and post it to the open database of Sensor.Community every 5 minutes.
Register your Blues Wireless NotehubHave a look at the Blues Wireless Quickstart to register your device.
Connect a Nano SIM to the SIM card slot of the Notecard. After inserting the SIM, set the APN to: data.mono.
To do so, run the following configuration commands on the Notecard.
{
"req": "card.wireless",
"apn": "data.mono"
}
Connect your sensorConnect your SDS011 and, optionally additional sensors like the DHT22.
SDS011
- 5V to VUSB
- GND to GND
- TX to pin 9
- RX to pin 6
DHT22
- VCC to 3V3
- Data to pin 5
- GND to GND
Upload the following Sketch to the Swan. Get the productUID
from your Blues' Notehub environment.
#include <Notecard.h>
#define serialDebug Serial
#define productUID "{productUID}" // add your Blues Wireless productUID
Notecard notecard;
//DHT11 setup
#include <DHT.h>
#define DHTPIN 5 // connect sensor to pin 5
#define DHTTYPE DHT22 // DHT 22
DHT dht(DHTPIN, DHTTYPE);
//SDS011 setup
#include "SdsDustSensor.h"
int rxPin = 9;
int txPin = 6;
SdsDustSensor sds(rxPin, txPin);
float pm25 = 0.0;
float pm10 = 0.0;
float oldpm25 = 0.0;
float oldpm10 = 0.0;
void setup() {
delay(2500);
serialDebug.begin(115200);
notecard.begin();
notecard.setDebugOutputStream(serialDebug);
J *req = notecard.newRequest("hub.set");
JAddStringToObject(req, "product", productUID);
JAddStringToObject(req, "mode", "continuous");
notecard.sendRequest(req);
dht.begin();
sds.begin();
}
void loop() {
// get data from sds011
sds.wakeup();
delay(30000); // working 30 seconds
// take 3 readings and send the average
PmResult pm = sds.queryPm();
delay(3000);
PmResult pm2 = sds.queryPm();
delay(3000);
PmResult pm3 = sds.queryPm();
float pm25a = pm.pm25;
float pm10a = pm.pm10;
float pm25b = pm2.pm25;
float pm10b = pm2.pm10;
float pm25c = pm3.pm25;
float pm10c = pm3.pm10;
float pm25 = (pm25a+pm25b+pm25c)/3;
float pm10 = (pm10a+pm10b+pm10c)/3;
// get data from DHT22
float temperature = dht.readTemperature(false); // false == Celsius, true == Fahrenheit
float humidity = dht.readHumidity(false);
Serial.print("Temperature = ");
Serial.println(temperature);
Serial.print("Humidity = ");
Serial.println(humidity);
// to reduce data, only send data if pm25 or pm10 changed with >= 1
if (abs(oldpm25-pm25) >= 1 || abs(oldpm10-pm10) >= 1){
oldpm25 = pm25;
oldpm10 = pm10;
// send data
J *req = notecard.newRequest("note.add");
if (req != NULL) {
JAddBoolToObject(req, "sync", true);
J *body = JCreateObject();
if (body != NULL) {
JAddNumberToObject(body, "temperature", temperature);
JAddNumberToObject(body, "humidity", humidity);
JAddNumberToObject(body, "pm25", pm25);
JAddNumberToObject(body, "pm10", pm10);
JAddItemToObject(req, "body", body);
}
notecard.sendRequest(req);
}
}
WorkingStateResult state = sds.sleep();
delay(270000); // wait 4.5 minutes minutes
}
The source code can de found on my GitHub. Feel free to submit improvements.
Initiate route to QubitroCreate an account on Qubitro which is free up to 2 devices. For details on how to integrate data from Blues Notehub to Qubitro, see documentation.
- Create an account on https://devices.sensor.community/
- Go to My sensors and click Register a new sensor (there is no option to register a Blues Notecard, so I chose TTN instead).
- Update the following Python script with API details from Qubitro and Sensor.Community
- Execute the script every 5 minutes (which is the required frequency for Sensor.Community) using crontab.
import requests
import json
api_url_qubitro = "https://api.qubitro.com/v1/projects/{your ProductID}/devices/{add your deviceID}/data?keys=pm10,pm25&period=1&limit=1"
headers_qubitro = {
"Accept": "application/json",
"Authorization": "Bearer {add your API key}"
}
api_url_POST = "https://api.sensor.community/v1/push-sensor-data/"
headers_sensorcommunity = {
"Content-Type":"application/json",
"X-Pin": "1",
"X-Sensor": "{add your sensorID}"
}
def post_data():
response = requests.get(api_url_qubitro, headers=headers_qubitro).json()
data = response['response']
for pm25_value in data:
pm25 = pm25_value['pm25']
for pm10_value in data:
pm10 = pm10_value['pm10']
sensordata = {"sensordatavalues":[{"value_type":"P1","value":pm10},{"value_type":"P2","value":pm25}]}
response = requests.post(api_url_POST, headers=headers_sensorcommunity, data=json.dumps(sensordata))
if __name__ == '__main__':
post_data()
Final test 🥁If you run all the above steps successfully, you can see your sensor appear on the Sensor.Community map! To validate, look at your dashboard and see if it displays your latest sensor data.
Did you see your sensor appear? Congratulations!
Comments