The accelerometer(LSM6DSL) on the BerryIMUv3 has built in double tap detection, which makes it very easy to detect double taps without the need for any fancy code.
When the LSM6DSL detects a double tap, it can fire an interrupt pin on the BerryIMUv3. We will use a Raspberry Pi to monitor the interrupt pin and turn a LED off and on when a double-tap is detected.
Double-Tap event recognition has special registers which control tap recognition functionality, these are the tap threshold and the Shock, Quiet and Duration time windows
The Raspberry Pi will configure the BerryIMUv3 for double tap recognition. It will also monitor for double taps, which will be used to turn a LED on and off.
INT1 On the BerryIMUv3 will go high when a double tap is detected.
GPIO18 (physical pin 12) on the Raspberry Pi will be used to monitor INT1 , using an interrupt.
GPIO20 (physical pin 28) will be used to drive the LED.
The resister below is 330 Ohms
Here is the hock up diagrams
from LSM6DSL import *
import RPi.GPIO as GPIO
bus = smbus.SMBus(1)
LED_ON = 0 #Used to track of the current state of the LED
INTERRUPT_PIN = 12 #The interrupt pin which will be connected to the IMU
LED_PIN = 38 #The pin which will be driving the LED
#Used to clean up when Ctrl-c is pressed
def signal_handler(sig, frame):
#Used to write to the IMU
bus.write_byte_data(device_address, register, value)
LED_ON = 0
LED_ON = 1
writeByte(LSM6DSL_ADDRESS,LSM6DSL_CTRL1_XL,0b01100000) #ODR_XL = 416 Hz, FS_XL = +/- 2 g
writeByte(LSM6DSL_ADDRESS,LSM6DSL_TAP_CFG,0b10001110) #Enable interrupts and tap detection on X, Y, Z-axis
writeByte(LSM6DSL_ADDRESS,LSM6DSL_TAP_THS_6D,0b10001100) #Set tap threshold
writeByte(LSM6DSL_ADDRESS,LSM6DSL_INT_DUR2,0b01111111) #Set Duration, Quiet and Shock time windows
writeByte(LSM6DSL_ADDRESS,LSM6DSL_WAKE_UP_THS,0b10000000) #Double-tap enabled
writeByte(LSM6DSL_ADDRESS,LSM6DSL_MD1_CFG,0b00001000) #Double-tap interrupt driven to INT1 pin
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(INTERRUPT_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(INTERRUPT_PIN, GPIO.RISING, callback=LEDnotification, bouncetime=300)
We will cover specific code which relates to double-tap recognition.
Line 37, LSM6DSL_TAP_CFG is used to enable tap recognition on the X, Y, Z directions. It is also used to enable the interrupt function for double-tap recognition.
Line 38, LSM6DSL_TAP_THS_6D is used to set the tap thresholds. a lower value will result in softer taps being detected.
Line 39, LSM6DS_INT_DUR2 is used to set the duration, quiet and shock time window. A larger duration will result in a longer time between 1st and 2nd tap.
Line 40, LSM6DSL_WAKE_UP_THS. Set the left most bit to enable double tap recognition.
Line 41, LSM6DSL_MD1_CFG is used to set which interrupt pin ont he BerryIMUv3 is used. In this instance, it is set to INT1.
One of the main contributing factors to GPS position accuracy is the geometric configuration (the position in the sky) of the satellites used to obtain a position. The best position fix is given when a satellite is directly overhead and another three are equally spaced around the horizon.
This aspect is called the 'geometry' of the system and is measured as DOP (Dilution of Precision).
The influence of satellite geometry on imprecision is demonstrated in the image below. When both satellites are widely separated (figure left) the position error (area in red) is smaller. If the satellites are close to one another (right figure), then the area of error is more spread out. This is valid when the uncertainty for determining the position, known as the Range Error (R-E: yellow and blue areas), is the same for both satellites. R (R1 and R2) refers to the measured distance of the satellites to the user (pseudorange).
There are a number of different DOP elements which can be used, we will focus on HDOP (Horizontal-DOP).
The HDOP can be seen when using gpsmon. The image below has HDOP highlighted;
The HDOP can also be found in the GSA sentence. Below it is shown as 1.3;
A HDOP value of 1 or below would give you an accuracy of about 2.5M.
When in mountainous areas, forests and urban canyons, you can experience high HDOP values as some of the available satellites will be obstructed. The satellites used will be closer together creating a large area of error as the signal from each satellite have a larger intersect.
Out on the open see, you should be able to see a low HDOP value as the satellites used would be spread out and has less area of a intersect.
Once installed, open u-Center. You will get the default view as shown below. No data will be shown as we are not connected to a GPS.
The next step, is to create a new network connection and connect to the GPS which is connected to our Raspberry Pi. You can create a new connection under the Receiver and thenNetwork connection menus.
In the new window, enter the IP address of the Raspberry Pi and specify port 6000. This is the port we configured in ser2net on the Raspberry Pi.
This is what the default view looks like when connected and the GPS has a fix.
Below I will list of the more useful windows/tools within u-Center. You can also click on the images below for a larger version.
Data View This window will show you the longitude, latitude, altitude and fix mode. It will also show the HDOP, which is the Horizontal Dilution of Precision. Lower is better, anything below 1.0 means you have a good signal.
Ground Track This window will show you where the satellites are as well as what time.
Skye View Sky view is an excellent tool for analyzing the performance of antennas as well as the conditions of the satellite observation environment.
Deviation Map This map shows the average of all previously measured positions.
In this guide we will show you how to control the GPIO pins of a Raspberry pi by send a SMS to the Raspberry Pi from a mobile phone.
For this guide, the GSM modem we are using to receive the SMS is the BerryGPS-GSM.
On the software side, we will be using Gammu, which is specifically designed to control phones and GSM modules. It also has a daemon which will monitor the GSM modem for incoming SMSs.
We will configure Gammu to trigger a python script when a new SMS is received. We will use the contents of the SMS to control what happens in Python
LEDs are used here as an example, but you can do anything you like Eg. Open a garage door, turn on some lights, etc..
We will be using the three bottom right GPIO pins on the Raspberry Pi header. These are GPIO 16, 20 and 21. Each is connected to a different color LED as shown above. The The resistors used are 330 Ohm and the GND pin (shorter pin) of the LEDs is connected to the GND power rail.
Send a test SMS to a mobile number. The mobile number below is an example, you will need to update this;
pi@raspberrypi ~ $ echo "This is a test from a Raspberry Pi" | /usr/bin/gammu --sendsms TEXT +614123456789
This python script will run every time a new SMS is received.
pi@raspberrypi ~ $ nano ~/smsReceived.py
Copy in the below code
import RPi.GPIO as GPIO
RED_LED = 21
GREEN_LED = 20
BLUE_LED = 16
filename=str(sys.argv) #Gammu will pass the filename of the new SMS as an argument
complete_filename="/var/spool/gammu/inbox/"+filename #we create the full path to the file here
GPIO.setup(RED_LED , GPIO.OUT)
GPIO.setup(GREEN_LED , GPIO.OUT)
GPIO.setup(BLUE_LED , GPIO.OUT)
#read the contents of the SMS file
message=sms_file.read(160) #note that a not-parted SMS can be maximum 160 characters
#search the contents and perform an action. Rather than use 'find',
# we will use regular expression (re) so we can ignore case.
#Most smartphones will have the first letter capitalised
if re.search('red', message, re.IGNORECASE):
GPIO.output(RED_LED , GPIO.HIGH)
GPIO.output(RED_LED , GPIO.LOW)
elif re.search('green', message, re.IGNORECASE):
GPIO.output(GREEN_LED , GPIO.HIGH)
GPIO.output(GREEN_LED , GPIO.LOW)
elif re.search('blue', message, re.IGNORECASE):
GPIO.output(BLUE_LED , GPIO.HIGH)
GPIO.output(BLUE_LED , GPIO.LOW)
In this guide we will show you how to send a SMS using a button connected to the GPIO pins of a Rasberry Pi Zero.
For this guide, the GSM modem we are using to send the SMS is the BerryGPS-GSM.
On the software side, we will be using Gammu, which is specifically designed to control phones and GSM modules.
Python will be used to monitor some buttons connected to GPIO pins and Gammu python bindings will be used to send a SMS.
Buttons are used here as an example, but you can use anything to trigger the SMS, E.g. Temperature sensor, water level sensor, light sensor, etc..
We have includes some LEDs so we can see when the buttons are pressed.
We will be using the three bottom right GPIO pins on the Raspberry Pi header. These are GPIO 16, 20 and 21. Each is connected to a button and different color LED as shown above. The internal pull-down resisters will be used on these GPIO. 3.3v and GND are connect to the power rails on the breadboard. The resistors used are 330 Ohm and the GND pin (shorter pin) of the LEDs is connected to the GND power rail.
Initialstate has some great tools to easily stream data from a Raspberry Pi to Initialstate.com and show this data within a dashboard using tiles. We will send longitude, latitude and speed. And use a BerryGPS-GSM to get these values and upload them via 3G.
You will need to create an account on Initialstate.com and then grab your access key which can be found under "My Settings"
If you are using a BerryGPS-GSM, you can follow this guide to get the GPS working and get your Pi to connect to via 3G using PPP.
The above guide also shows how to make your Pi connect to the carrier network automatically when booted. You will need this if you plan to perform remote tracking(E.g. Asset tracking).
Here we will create the main script which will stream the GPS data to Initialstate.com. The code below creates a separate thread which is used to monitor the serial port. This is needed because we have a pause in the main loop. The pause is there to limit how much data we upload over 3G. If we did everything in the same thread during the pause, the serial buffer would fill up (it is FIFO) and when we get the next value from the buffer, it will be old by a few seconds. This happens every loop and eventually the data will be minutes or hours behind.
The access key below is not a valid key, it is just an example. You will need to replace it with your own key.
pi@raspberrypi ~ $ nano ~/GPStracker.py
from gps import *
from time import *
from ISStreamer.Streamer import Streamer
gpsd = None #Setup global variable
#Setup the Initialstate stream, give it a bucket name and the access key
streamer = Streamer(bucket_name="GPS_Tracker20190713", bucket_key="GPS_Tracker20190713", access_key="ist_W4aHj0eCkMjCD8JVpp3AMsKomys8NaD")
def __init__(self, threadID):
self.threadID = threadID
global gpsd #bring it in scope
gpsd = gps(mode=WATCH_ENABLE) #Start GPSD
self.running = True #Start running this thread
if __name__ == '__main__':
gpsdThread = GPSDcollector(1) # create a thread to collect data
gpsdThread.start() # start it up
print 'GPS ' , gpsd.utc,'--> CPU time->',datetime.datetime.now().time() ,
if (gpsd.fix.longitude<>0) and (gpsd.fix.longitude<>'nan'): #Only upload data if it is valid
print ' lat ' , gpsd.fix.latitude,
print ' lon ' , gpsd.fix.longitude,
print ' speed ', gpsd.fix.speed
except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
print "\nKilling Thread..."
gpsdThread.running = False
gpsdThread.join() # wait for the thread to finish what it's doing
Start the script automatically on boot
If you are doing remote monitoring, you would want the script to run on boot. To do this, we will create a small script which will start the main python program.
Typically, a GPS module can take a few minutes to get Time To First Fix(TTFF), or even longer if you are in built up areas(+20mins). This is because the Almanac needs to be downloaded from satellites before a GPS fix can be acquired and only a small portion of the Almanac is sent in each GPS update.
Assisted GPS speeds this up significantly by downloading ephemeris, almanac, accurate time and satellite status over the network, resulting in faster TTTF, in a few seconds. This is very similar how to GPS works on a smartphone.
The BerryGPS-GSM supports assisted GPS. The below video shows a comparison between assisted and normal GPS.
The SARA-U201 can be configured to download GPS assist data and then pass this over the the GPS module. These two components speak to each other via i2c.
This assist data is downloaded by the SARA-U201 modem (not the Pi), therefore the modem needs to create an internal PDP (Packet Data Protocol) connection.
Once the PDP connection is made, the SARA-U201 will reach out to uBlox AssitNow servers and download the latest assist data. A valid token is needed to perform this, all BerryGPS-GSM have had this token pre-configured.