Sodial Dust Sensor on Android
by AndresR145 in Circuits > Sensors
1127 Views, 1 Favorites, 0 Comments
Sodial Dust Sensor on Android
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.
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
- wire
- A piece of wood to mount everything on
Then you'll need some simple tools:
- A bucksaw for cutting the wood
- tweezers
- 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
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"
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.
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.
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.
WakeLock Partial
TextColor 100,75,10
TextColorA 50,50,50
TextAlign 0
TextSize 24
CLS
Popup "Dust Sensor Meter (c) ARJ 2017"
Global dustData(), dustDataF(), timeStamp() Global index, choice, maxData, fileName$
Dim timeStamp(59)
Dim dustData(59)
Dim dustDataF(59)
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
timeStamp(i)=i
Next i
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
Select choice
Case 0 maxData = 100
Case 1 maxDate = 1000
Case 2 maxData = 5000
Case 3 maxData = 10000
Case 4 maxData = 0
End Select
''Connect sensor
dim pair$(0)
pair$() = BTGetPaired$()
If pair$(0) = "none" Then
Print "No paired devices found. Is BT turned on?" Print "Program terminated"
End
Endif
List pair$(), device$
name$=ItemExtract$(device$,0)
address$=ItemExtract$(device$,1)
BTConnect 1, address$
'Wait for connection
Progress ON
Print "Trying to connect to ";address$
For i = 1 to 20
Progress i/2
If BTGetstate(1)=4 Then Exit For Wait 1000
Next i
Progress OFF
'On success connect to the BT device
If BTGetState(1) = 4 Then Print "Connected" Else Print "Could not connect to ";name$
Print "Program terminated"
End
Endif
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
Graphics On
'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
data$="" packet$=""
index=0
Do While maxData > 0
BTRead 1, packet$,10
data$=data$+packet$
If Len(data$) >= 10 Then
If (ASCII(Left$(data$,1))=170) & (ASCII(Right$(data$,1)) = 171) Then
dustDataF(index)=ASCII(Mid$(data$,2,1))
dustDataF(index)=(dustDataF(index)+256*ASCII(Mid$(data$,3,1)))/10
dustData(index)=ASCII(Mid$(data$,4,1))
dustData(index)=(dustData(index)+256*ASCII(Mid$(data$,5,1)))/10
Writeln 1, FormatTime$(Time(), "kk:mm:ss") + " " + Str$(dustDataF(index))+ " " + Str$(dustData(index))
data$=""
maxData = maxData-1
index=index+1
If index>59 Then index=0
dustData(index)=0
dustDataF(index)=0
Endif
Endif
DrawGraph()
Wait 100
Loop
Close 1
Graphics Off
CLS Print "Program terminated"
End
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
Sub DrawGraph()
' In Graphics mode the screen clears to the current color
Color 0,0,0
CLS
Color 0,0,100
' Set the graphics color to be used to draw the grid lines
TextColor 100,100,100,50
'TextColor is the color of the grid main title
TextColorA 100,100,100
' 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
TextSize 20
FixDecimal 0
' Set to display 2 decimal places
PadDigits 2
' 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
Color 100,0,0
GraphXY timeStamp(), dustDataF()
Color 0,100,0
GraphXY timeStamp(), dustData()
TextColor 100,0,0
DrawText "PM2.5", 30, Int(ScreenY()-60),90,1
TextColor 0,100,0
DrawText "PM10", 30, Int(ScreenY()-150),90,1
TextColor 100,100,100,50
Return
Download the source code here
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!