Using SDS011 particle sensor with my Android phone
Tags: sds011, android, airquality
Published: 2021-02-06 09:04:12

After some testing of SDS011 particle sensor with my laptop (s described in the first post of this series) I realized it is not the most practical way to check for the air quality. I needed to take a laptop with me in a backpack, get it out of it, resume it, connect the sensor using the USB and start [the Python3 script](https://github.com/vvidovic/aqi) (while keeping the lighttpd server running during suspends).
![taking the laptop outside](https://dev-to-uploads.s3.amazonaws.com/i/wvw89tafzl8b6vodjxnq.jpg)

At some point, I realized that I have a quite powerful and much smaller portable computer with me at all times - Android mobile phone. Fortunately, a few months ago I bought [OTG cable](https://en.wikipedia.org/wiki/USB_On-The-Go) for my phone.

I found a [Serial USB Terminal](https://play.google.com/store/apps/details?id=de.kai_morich.serial_usb_terminal) for an Android phone, installed it and connected the SDS011 sensor to my phone. An application was started automatically and after I started a connection I saw some garbage characters periodically appearing in the terminal output - so, I guessed my phone can read the output from a sensor :)

First tests showed this guess was correct. Initial calculations proved that data fetched from the phone represent correct measurements. Hand-made calculations were made based on the [Arduino C code from the joy-it sensor manual](https://joy-it.net/en/products/SEN-SDS011):
```
...
#define LEN 9
unsigned char incomingByte = 0;
unsigned char buf[LEN];
int PM2_5Val = 0;
int PM10Val = 0;
...
if (incomingByte == 0xAA) {
  Serial.readBytes(buf, LEN);
  if ((buf[0] == 0xC0) && (buf[8] == 0xAB)) {
    for (i=1; i<=6; i++) {
      checksum = checksum + buf[i];
    }
    if (checksum != buf[7]) {
      PM2_5Val=((buf[2]<<8) + buf[1])/10;
      PM10Val=((buf[4]<<8) + buf[3])/10;
...
```
Which means that for the following sequence of bytes:
```
hex: aa  c0  50  00  29  01  7f  bc  b5  ab
dec:         80  00  41  01 127 188 181
```
...I did the following math which shows same values I was able to measure using the above mentioned Python3 script on my laptop:
```
checksum (hex) = 50+00+29+01+7f+bc = 1b5 (which overflows to b5)
PM25 (dec) = (00*256 + 80) / 10 =  8.0
PM10 (dec) = (01*256 + 41) / 10 = 29.7 
```

After some thinking, I realized this is a good use-case for a new Android application.

"Well, maybe I am not the first one thinking about that.."

I searched for such application on the [Google Play](https://play.google.com/store/apps). Unfortunately - no such application existed (at least at the time of writing this post).

I searched for Android application on the Internet and I found [a GitHub repo for SDS011 Android application](https://github.com/milosevic81/sds011_android) - much better. Unfortunately, this application is not published to the Google Play so I needed to build and install it "by hand".

I built and installed this application to my phone using [the Android Developers instructions](https://developer.android.com/studio/run/device). This is not for the casual phone user, it requires a download and configuration of the [Android Studio](https://developer.android.com/studio). However, the end result is nice - thanks to the [developers of the SD011 Android app](https://github.com/milosevic81/sds011_android/graphs/contributors).

![SDS011 Android application](https://dev-to-uploads.s3.amazonaws.com/i/a365o93ugghw8se93p16.png)

After some playing with this app, I found there are some downsides:
- gauges' ranges are not showing [proper colours for air quality according to the PM2.5 or PM10 levels](https://uk-air.defra.gov.uk/air-pollution/daqi?view=more-info&pollutant=pm25#pollutant)
- app doesn't record measurements history

If I find a time, I would like to create an Android application with the following functions:
- save/view a history of measurements
  - date-time & GPS included
  - show location for each measurement on the map
  - this way we can take a ride or walk around the town and later analyze data to see if air quality differs between different locations
- pause/continue measurements
  - this would save the sensor laser & my phone battery
- configure periodic measurements
  - this would save the sensor laser & my phone battery


Cover image & laptop image are created by:
- [computergottyt](https://pixabay.com/users/computergottyt-2511322/)
- [Goumbik](https://pixabay.com/users/goumbik-3752482/)