A year ago a friend of mine had a weekend workshop about environmental monitoring. The goal of the workshop was to built up a dust sensor connected to an raspberry pi board to put measurement data on some server that provided frequently updated dust concentration maps. My friend asked if there is a way to get the sensor data directly on his smartphone for monitoring and logging. So i digged the internet for a datasheet and saw that the sensor had a simple UART interface with 9600Baud 8N1 protocol. But how to connect a UART to a smartphone? Well, that's easy. I just had to use one of those ubiquitous little Bluetooth modules that provide an emulated comport on android. Now see how i made it.
Step 1: What You Need
You need the following parts
- A mating connector JST XH 7-pin for the Sodial interface with wires. I bought mine on Ebay.
- A Bluetooth module HC05 or 06 compatible with UART connector
- A USB-serial converter with TTL level interface. We use this to give the BT-module a unique name
- Sodial SDS011 dust sensor. I got mine from Ebay
- a piece of veroboard
- USB-B connector
- A piece of wood to mount everything on
Then you'll need some simple tools:
- A bucksaw for cutting the wood
- solder iron and solder
- wire cutter
- Hot glue gun
- A piece of 8mm silicon sleeve(not on picture)
You can download the Sodial SDS011 datasheet here Sodial SDS011 datasheet
Step 2: Preparing the Bluetooth Module
The BT-Module has a UART interface with TTL-level. It can be reconfigured wit "AT" commands like we did with internet modems back in ancient times. To connect it to a terminal program on your machine you need to adapt the UART to your computer. I used a USB-RS232 converter i bought at amazon. I applied a connector for the BT-module and routed the 3,3V power supply and GND from the converter to the BT-module. Then i connected the respective TxD and RxD lines in crossover. TxD from USB-converter to RxD from BT-module and vice versa.
I have a linux machine and used cutecom. After connecting the USB-converter the comport was "ttyUSB0". You can find the comport names in the "/dev" directory on your linux machine. For windows users i would recommend "hterm". It's easy to operate. Type "AT" and you should get "AT" as response. Then type "AT+NameSensor" to give the BT-module the name "Sensor"
Step 3: Mounting the Parts
Cut a piece of wood in a size suitable for taking all the parts. Connect all signals as indicated in the schematic. It's a good idea to put an silicon sleeve around the wires to protect them. Solder the USB-B plug on the perfboard. It is just used for the power supply. Fix all parts with screws on the wooden base. Finally hot glue the cables to fix them on the wood.
Step 4: Pairing
Power the sensor application by plugging-in a USB power supply. A red LED on the BT-module will start blinking. No try to pair it with your android smartphone. You have to enter a pin code. This is "1234". After entering the code your smartphone should be paired with the BT-module.
Step 5: The Software
I like to write Android apps on the target platform itself. it saves you from all that emulation stuff you have to care about if you're working with Android Studio. I found out three suitable development tools on Android itself
- Mintoris Basic. A Basic interpreter with a rich set of commands to tinker around with almost everything on android. You can create shortcuts for your apps. Mintoris basic does not contain a compiler. So you must have installed Mintoris on every device you're using. But you only have to pay for it once (about 7€)
- Basic! Extreme good Basic interpreter and compiler (add-on for some €). Almost hooks on everything in android and you can compile real apps for distribute them without having Basic! on the target device. Sadly Basic! lacks the excellent diagram chart functions of Mintoris
- AIDE is a semi professional IDE for doing android development in java on android. With AIDE you have the utmost flexibility but you need to learns java. AIDE has annual costs of about 50€
I chose Mintoris. In this section i will not give you a tutorial of programming in Mintoris but a short description of the function blocks.
In the following part three arrays are declared for the two sensor data lines and the respective timestamps. The timestamp data is used for labeling the x-axis of the diagramm. The Sodial outputs two data streams each specified for a special particle size. The two dustdata-arrays take these values.
Popup "Dust Sensor Meter (c) ARJ 2017"
Global dustData(), dustDataF(), timeStamp() Global index, choice, maxData, fileName$
Dim Menu$(4 ) = "max. 100 datasets","max. 1000 data sets", "max. 5000 data sets", "max. 10000 data sets", "Exit"
'Init the arrays
For i = 0 to 59
dustData(i) = 0
dustDataF(i) = 0
Next a List menue is configured. This gives the user a choice to select the maximum size of data to collect. This is just a safety switch to prevent the smartphone from sucking in endless data. The Functions BTgetPaired$() gives back a list with all paired devices on the android device, their names and BT-adress.
List Menu$(), choice
'Select max amount if data to be stored
runLevel = 1
Case 0 maxData = 100
Case 1 maxDate = 1000
Case 2 maxData = 5000
Case 3 maxData = 10000
Case 4 maxData = 0
pair$() = BTGetPaired$()
If pair$(0) = "none" Then
Print "No paired devices found. Is BT turned on?" Print "Program terminated"
List pair$(), device$
BTConnect 1, address$
'Wait for connection
Print "Trying to connect to ";address$
For i = 1 to 20
If BTGetstate(1)=4 Then Exit For Wait 1000
'On success connect to the BT device
If BTGetState(1) = 4 Then Print "Connected" Else Print "Could not connect to ";name$
Print "Program terminated"
The next block shows the data aquirement. For each data session a file is automatically opened and named after time and date. Then the loop is reading the sensor data. The data is packed in several bytes. A set of bytes is identified by two ASCII characters 170 and 171. The following data is reorganized and filled into the dust-arrays
'Open datafile to write
fileName$ = FormatTime$(t, "yyyy-MM-dd-kk-mm-ss") + ".dat"
Open 1, fileName$, "w+" Print "Opened datafile ";fileName$ Writeln 1, FormatTime$(Time(), "yy-MM-dd")
Writeln 1, "Time Dust2.5 Dust10"
'Fill array with the measured data
Do While maxData > 0
BTRead 1, packet$,10
If Len(data$) >= 10 Then
If (ASCII(Left$(data$,1))=170) & (ASCII(Right$(data$,1)) = 171) Then
Writeln 1, FormatTime$(Time(), "kk:mm:ss") + " " + Str$(dustDataF(index))+ " " + Str$(dustData(index))
maxData = maxData-1
If index>59 Then index=0
CLS Print "Program terminated"
The last part is a subroutine that is called after every data reception. It clears the screen, redraws the diagram with the actual data stored in the dust- and timestamp arrays.
' Draw the coordinates, the labels, ticks and also the data curves
' In Graphics mode the screen clears to the current color
' Set the graphics color to be used to draw the grid lines
'TextColor is the color of the grid main title
' TextColorA is used for Axis titles and grid annotations.
' Set the size of the axis title text
' The grid main title is 2x this size
' Set to display 2 decimal places
' Draw a grid for the graph ' Set the range and title of the X & Y
Axis AxisX 0, 59, "Time/s"
AxisY 0, 10000, "ug/m3"
Grid 3, "Dust concentration"
' Draw Dust Graphs
GraphXY timeStamp(), dustDataF()
GraphXY timeStamp(), dustData()
DrawText "PM2.5", 30, Int(ScreenY()-60),90,1
DrawText "PM10", 30, Int(ScreenY()-150),90,1
Download the source codehere
Step 6: Test
Power up the sensor and start the app. From the list of paired devices choose the one named "Sensor". After connecting the sensor the screen will start to display the data. Simultaneously the data file is allocated. After finishing the fashion you can use GnuPlot to display the data. Use the file "Test.gp" in GnuPlot to configure GnuPlot for displaying a datafile named "Test.dat". You cans also find it here
See the video for more details and testing. Have a lot of fun and more ideas!