This is my code:
// Based off 'End Motion' detection program by Nick Waterton (https://github.com/NickWaterton/mcScript/tree/master/libraries/accelerometer/lis2dh12/examples/EndMotionTest)
// This program detects Start and End of Motion for the MC DEMO 250 module
// After detecting End of Motion, it fires up the GPS and sends the location to Sigfox
// Needs Timing and LIS2DH12(accelerometer) libraries to run
// Libraries by N Waterton 25th January 2017 (https://github.com/NickWaterton/mcScript)
// default configuration is:
// Low Power mode, 50 Hz, 2G
// Sleep mode enabled only when interrupts are enabled (data rate is 10Hz in sleep mode) - wake time is 5 seconds (ie accelerometer wakes up for 5 sconds when threshold is exceeded).
// Motion detection on INT1, pin 1.
// For help go to (http://mcthings.createaforum.com/index.php)
// McScript user guide can be found here (https://www.mcthings.com/downloads/)
// Shaika.K February 2017
Class MotionLocation
' Libraries and accelerometer fields
Shared accel As LIS2DH12 //accelerometer library
Shared pin1data As Json //a Json object is a list and at each index of the list it stores a name and a value.
Shared pin2data As Json //to access value at a particular index go (.Item(0).Value().Get___()) eg pin2data.Item(0).Value().Get___()
//to access name at a particular index go (.Item(0).Name()) eg pin2data.Item(0).Name()
Shared pin1count As Integer
Shared pin2count As Integer
Shared Start_Motion_Detected As Boolean //used to detect start of motion
Shared End_Motion_Detected As Boolean //used to detect end of motion
Shared motionStartTime As String
Shared timer1 As Timing //Timer library
' Message Types
Const MSGTYPE_LOCATION As Byte = 1
' Configuration Constants
Const GPS_TIMEOUT_uS As Integer = 120000000 '120 second timeout
Const GPS_MIN_SAT_COUNT As Integer = 3 'minimum satellites to get a fix
' Battery Status
Const BAT_CRITICAL As Byte = 0
Const BAT_LOW As Byte = 1
Const BAT_NORMAL As Byte = 2
Const BAT_OPTIMAL As Byte = 3
Shared Event Boot()
//Lplan.SetMidPowerMode(5000)
Lplan.SigfoxRadioZone(SigfoxRadioZone.Australia)
Led2 = False
Led3 = False
pin1data = New Json
pin2data = New Json
Start_Motion_Detected = False
End_Motion_Detected = True
timer1 = New Timing()
accel = New LIS2DH12
If accel.online Then
If LIS2DH12.VERSION < 5 Then
'Both LED means error on Boot -> LIS2DH12 Library is out of Date, please upgrade!
Led2 = True
Led3 = True
Else
'Nicks's setup:
accel.Setup(LIS2DH12.LOW_POWER_MODE, LIS2DH12.DATA_RATE_50HZ, LIS2DH12.SCALE_2G)
accel.ConfigureMotionInterrupt(1.15, 20.0, 1, 1, True) 'INT1 pin 1, Latched. Pin 1 activates AccelerometerInt1()
'accel.ConfigureTransientInterrupt(0.15, 20.0) 'same as above
'accel.ConfigureTransientInterrupt(0.15, 20.0, 1, 1, True, LIS2DH12.INT_SRC_YH) ' as above, but only Y High axis interrupt enabled
accel.SetFilterBypass(False) 'disable HP filter bypass (values read will be with HP applied - usually this is enabled, or you just read 0's with interrupts enabled, but gets re-enabled in Publish() )
'To publish a paramterer value to Sigfox insert the corresponding number.
'0= general control registers
'1= Interrupt 1 settings
'2= Interrupt 2 settings
'3= Click Interrupt settings
'4= Misc others
Dim accelconfig As Json = accel.ReadConfiguration(0) //general control registers
'Lplan.Sigfox(accelconfig.Item(0).Value().GetString())
End If
Else
'Led3 means error on Boot -> accel didnt boot
Led3 = True
End If
//MQTT.use_delay = True
Led2 = True //indicates start of detection
End Event
Shared Event AccelerometerInt1() //this event is activated after pin1 is initialised and boot finsihes
//MOTION/TRANSIENT Detection INT1
'Read Int source register To clear interrupt And get source
Dim IntSource As Json = accel.GetInt1SourceAsJson(LIS2DH12.J_VALUES | LIS2DH12.J_SUMMARY)
If IntSource.Count> 0 Then 'if we have an interrupt (of any kind, x,y or z)
MotionLocation.updateTimer(True) //next motion has started
End_Motion_Detected = False
Led2 = False
End If
End Event
Shared Event MotionCheck() RaiseEvent Every 10 Seconds //change time to max time without motion to detect (actual time could be double this in theory)
If Start_Motion_Detected And End_Motion_Detected Then //no motion in last 10 seconds
MotionLocation.updateTimer(False) //motion ended
End If
End_Motion_Detected = True
End Event
Shared Function updateTimer(motion As Boolean) As Nothing
If motion Then
If Not Start_Motion_Detected Then //not old "start motion" but new one
motionStartTime = Timing.GetTimestamp(TIME_FORMAT.T_SECONDS)
timer1.GetTimeSpan() 'start timer
Start_Motion_Detected = True
Led2 = True
Thread.Sleep(50000)
End If
Else
Dim motionStopTime As String = Timing.GetTimestamp(TIME_FORMAT.T_SECONDS)
Dim duration As Integer = (timer1.GetTimeSpan() / 1000).ToInteger
Start_Motion_Detected = False
Thread.Sleep(50000)
Led2 = False
Lplan.Sigfox(getOrientation(0)) 'send message to sigfox
Thread.Delay(20000000)
Lplan.Sigfox(getOrientation(1)) 'send message to sigfox
Thread.Delay(20000000)
Lplan.Sigfox(getOrientation(2)) 'send message to sigfox
Thread.Delay(20000000)
Led3 = True
Device.StartGPS(GPS_TIMEOUT_uS, GPS_MIN_SAT_COUNT)
End If
End Function
'runs after GPS starts up, when GPS location has been aquired OR timeout has been reached
Shared Event LocationDelivery()
Dim msg As ListOfByte = New ListOfByte() 'message to send to sigfox
Dim Lat As Float = Device.GetLatitude()
Dim Lon As Float = Device.GetLongitude()
'if GPS timed out when retrieving location
If Lat.IsNaN() Then
Lat = 0.0 'lattitude is 0
End If
If Lon.IsNaN() Then
Lon = 0.0 'longitude is 0
End If
msg.Add(0x00) '00 is an indication to Losant that this payload contains location data
msg.AddFloat(Lat)
msg.AddFloat(Lon)
Lplan.Sigfox(msg) 'send message to sigfox
Led3 = False 'turn off Led to indicate transmission complete
End Event
Private Function getOrientation(type As Integer) As ListOfByte
accel.SetFilterBypass(True) 'enable HP filter bypass (so that values read below do not go through HP filter) - this is the normal configuration
accel.GetAccel() 'dummy read
Do
Thread.Sleep((accel.GetmsFromDataRate* 1000).ToInteger) 'wait 1 clock cycle for new unfiltered reading
While Not accel.NewDataAvailable() 'wait for new data
Dim accelValues As ListOfFloat = accel.GetAccel()
Dim PitchRoll As ListOfFloat = accel.GetPitchRollDegrees()
accel.SetFilterBypass(False) 'disable filter bypass again, this means the *values* given by interrupts (if you are displaying them) will be with HP filter enabled.
Dim msgData As ListOfByte = New ListOfByte()
msgData.Add(0x08) '08 is an indication to Losant that this payload contains location data
msgData.AddInteger(type)
'msgData.AddFloat(accelValues(0)) //x
msgData.AddFloat(accelValues(type)) //y
'msgData.AddFloat(accelValues(2)) //z
'msgData.AddFloat(PitchRoll(0))
'msgData.AddFloat(PitchRoll(1))
Return msgData
End Function
End Class
Am I doing things correctly?
I got the values x = 5.603067e-39, y = 5.557149e-39, z = 1.75883332e-38 when I shook the the device and laid it on its back like normal.