├── LICENSE ├── README.md └── fonagps.py /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Initial State 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Cellular & GPS Enabled Pi Zero: Fona + Pi Zero 2 | 3 | 4 | 5 | What if your Raspberry Pi Zero could access internet services from anywhere? What if your Fona Cellular GSM and GPS breakout had the brains of a full Linux computer behind it? What if you could access the Fona's cellular capabilities and GPS data from a Raspberry Pi at the same time? 6 | 7 | Read from the Fona's GPS and then stream that data using it's cellular connection. [Read more...](https://github.com/InitialState/fona-pi-zero/wiki) 8 | 9 | _Note: If you're looking to use a Pi Zero W or Pi 3, see [here](https://github.com/initialstate/fona-raspberry-pi-3/wiki)._ 10 | -------------------------------------------------------------------------------- /fonagps.py: -------------------------------------------------------------------------------- 1 | from os import system 2 | import serial 3 | import subprocess 4 | from time import sleep 5 | from ISStreamer.Streamer import Streamer 6 | 7 | BUCKET_NAME = "Fona GPS" 8 | BUCKET_KEY = "fona" 9 | ACCESS_KEY = "Your_Access_Key" 10 | SECONDS_BETWEEN_READS = 60 11 | 12 | # Start PPPD 13 | def openPPPD(): 14 | # Check if PPPD is already running by looking at syslog output 15 | output1 = subprocess.check_output("cat /var/log/syslog | grep pppd | tail -1", shell=True) 16 | if "secondary DNS address" not in output1 and "locked" not in output1: 17 | while True: 18 | # Start the "fona" process 19 | subprocess.call("sudo pon fona", shell=True) 20 | sleep(2) 21 | output2 = subprocess.check_output("cat /var/log/syslog | grep pppd | tail -1", shell=True) 22 | if "script failed" not in output2: 23 | break 24 | # Make sure the connection is working 25 | while True: 26 | output2 = subprocess.check_output("cat /var/log/syslog | grep pppd | tail -1", shell=True) 27 | output3 = subprocess.check_output("cat /var/log/syslog | grep pppd | tail -3", shell=True) 28 | if "secondary DNS address" in output2 or "secondary DNS address" in output3: 29 | return True 30 | 31 | # Stop PPPD 32 | def closePPPD(): 33 | print "turning off cell connection" 34 | # Stop the "fona" process 35 | subprocess.call("sudo poff fona", shell=True) 36 | # Make sure connection was actually terminated 37 | while True: 38 | output = subprocess.check_output("cat /var/log/syslog | grep pppd | tail -1", shell=True) 39 | if "Exit" in output: 40 | return True 41 | 42 | # Check for a GPS fix 43 | def checkForFix(): 44 | print "checking for fix" 45 | # Start the serial connection 46 | ser=serial.Serial('/dev/ttyAMA0', 115200, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=1) 47 | # Turn on the GPS 48 | ser.write("AT+CGNSPWR=1\r") 49 | ser.write("AT+CGNSPWR?\r") 50 | while True: 51 | response = ser.readline() 52 | if " 1" in response: 53 | break 54 | # Ask for the navigation info parsed from NMEA sentences 55 | ser.write("AT+CGNSINF\r") 56 | while True: 57 | response = ser.readline() 58 | # Check if a fix was found 59 | if "+CGNSINF: 1,1," in response: 60 | print "fix found" 61 | print response 62 | return True 63 | # If a fix wasn't found, wait and try again 64 | if "+CGNSINF: 1,0," in response: 65 | sleep(5) 66 | ser.write("AT+CGNSINF\r") 67 | print "still looking for fix" 68 | else: 69 | ser.write("AT+CGNSINF\r") 70 | 71 | # Read the GPS data for Latitude and Longitude 72 | def getCoord(): 73 | # Start the serial connection 74 | ser=serial.Serial('/dev/ttyAMA0', 115200, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=1) 75 | ser.write("AT+CGNSINF\r") 76 | while True: 77 | response = ser.readline() 78 | if "+CGNSINF: 1," in response: 79 | # Split the reading by commas and return the parts referencing lat and long 80 | array = response.split(",") 81 | lat = array[3] 82 | print lat 83 | lon = array[4] 84 | print lon 85 | return (lat,lon) 86 | 87 | 88 | # Start the program by opening the cellular connection and creating a bucket for our data 89 | if openPPPD(): 90 | # Initialize the Initial State streamer 91 | streamer = Streamer(bucket_name=BUCKET_NAME, bucket_key=BUCKET_KEY, access_key=ACCESS_KEY, buffer_size=20) 92 | # Wait long enough for the request to complete 93 | sleep(10) 94 | 95 | while True: 96 | # Close the cellular connection 97 | if closePPPD(): 98 | print "closing connection" 99 | sleep(1) 100 | # The range is how many data points we'll collect before streaming 101 | for i in range(10): 102 | # Make sure there's a GPS fix 103 | if checkForFix(): 104 | # Get lat and long 105 | if getCoord(): 106 | latitude, longitude = getCoord() 107 | coord = str(latitude) + "," + str(longitude) 108 | print coord 109 | # Buffer the coordinates to be streamed 110 | streamer.log("Coordinates",coord) 111 | sleep(SECONDS_BETWEEN_READS) 112 | # Turn the cellular connection on every 10 reads 113 | if i == 9: 114 | print "opening connection" 115 | 116 | if openPPPD(): 117 | print "streaming" 118 | # Flush the streaming queue and send the data 119 | streamer.flush() 120 | print "streaming complete" 121 | 122 | 123 | 124 | --------------------------------------------------------------------------------