How to log sensor data to a file?

Timmay

Member
Licensed User
Longtime User
Hi, I'm new to most of this stuff, I'm an engineer, not a programmer so excuse me if I'm lacking programming knowledge here ha!

Anyways, I'm trying to make an app that logs various sensor data, including GPS to a .csv on the SD card. I have done the GPS tutorial, and have tired to change the code abit so as I can track the phones location and speed every second. For now, what I have done is setup a timer that runs every second, and calls a textwriter, and the current time, GPS lat/long and speed are put into the file, alongside a new line (using CRLF). However, in the output txt file, it will only log one line!

Does anyone know where I am going wrong? Or am I barking up the wrong tree totally using the timer to log at a set timestep. I have included the code below:

Sub Process_Globals
Dim GPS1 As GPS
Dim timer1 As Timer
End Sub

Sub Globals
Dim lblLon As Label
Dim lblLat As Label
Dim lblSpeed As Label
Dim lblSatellites As Label
Dim freq As Int 'sampling rate, 1000=1sec
Dim Button1 As Button
Dim EditText1 As EditText
Dim lat As String
Dim lon As String
Dim speed As String
Dim time1 As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
GPS1.Initialize("GPS")
End If
Activity.LoadLayout("1")
freq = 1000
timer1.Initialize("Timer1", freq)
timer1.Enabled = True

If File.ExternalWritable = False Then
Msgbox("Cannot write on storage card.", "")
Return
End If



End Sub

Sub Activity_Resume
If GPS1.GPSEnabled = False Then
ToastMessageShow("Please enable the GPS device.", True)
StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
Else
GPS1.Start(0, 0) 'Listen to GPS with no filters.
End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
GPS1.Stop
End Sub

Sub GPS_LocationChanged (Location1 As Location)
lblLat.Text = "Lat = " & Location1.Latitude
lblLon.Text = "Lon = " & Location1.Longitude
lblSpeed.Text = "Speed = " & Location1.Speed
lat = Location1.Latitude
lon = Location1.Longitude
speed = Location1.Speed

End Sub


Sub GPS_UserEnabled (Enabled As Boolean)
ToastMessageShow("GPS device enabled = " & Enabled, True)
End Sub

Sub GPS_GpsStatus (Satellites As List)
lblSatellites.Text = "Satellites:" & CRLF
For i = 0 To Satellites.Size - 1
Dim Satellite As GPSSatellite
Satellite = Satellites.Get(i)
lblSatellites.Text = lblSatellites.Text & CRLF & Satellite.Prn & _
" " & Satellite.Snr & " " & Satellite.UsedInFix & " " & Satellite.Azimuth _
& " " & Satellite.Elevation
Next
End Sub

Sub timer1_Tick

time1 = DateTime.Now

WriteTextWriter

End Sub


Sub WriteTextWriter
Dim TextWriter1 As TextWriter
TextWriter1.Initialize(File.OpenOutput(File.DirRootExternal, "GPS_log.txt", False))
TextWriter1.WriteLine(time1 & "," & lat & "," & lon & "," & speed)
TextWriter1.Close
End Sub
 

Brad

Active Member
Licensed User
Longtime User
Someone correct me if I'm wrong but I believe every time you open the file it sees it as a new file. I would suggest you save the data to a list then when you are finished collecting data use:
B4X:
TextWriter1.Initialize(File.OpenOutput(File.DirRoo tExternal, "GPS_log.txt", False))
TextWriter1.WriteList(List As List)
TextWriter.Close
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Nearly right Brad, the File.OpenOutput has the append flag set to false, so it will always create a new file.

If the OP changes the flag as below his original code should work fine.

B4X:
Sub WriteTextWriter
Dim TextWriter1 As TextWriter
TextWriter1.Initialize(File.OpenOutput(File.DirRootExternal, "GPS_log.txt", True))
TextWriter1.WriteLine(time1 & "," & lat & "," & lon & "," & speed) 
TextWriter1.Close
End Sub
 
Upvote 0
Top