6.8 KiB
Read GPS data - Virtual IoT Hardware
For dis part of di lesson, you go add GPS sensor to your virtual IoT device, and read di values wey e dey give.
Virtual Hardware
Di virtual IoT device go use one GPS sensor wey dem dey simulate, wey you fit access through UART via serial port.
Real GPS sensor dey get antenna wey dey collect radio wave from GPS satellites, and e go change di GPS signals to GPS data. Di virtual version dey simulate am by allowing you to set latitude and longitude, send raw NMEA sentences, or upload GPX file wey get plenty locations wey e fit return one by one.
🎓 NMEA sentences go dey explain later for dis lesson
Add di sensor to CounterFit
To use di virtual GPS sensor, you go need add am to di CounterFit app.
Task - add di sensor to CounterFit
Add di GPS sensor to di CounterFit app.
-
Create new Python app for your computer inside one folder wey you go call
gps-sensorwey get one file wey dem callapp.pyand Python virtual environment, then add di CounterFit pip packages.⚠️ You fit check di instructions for how to create and setup CounterFit Python project for lesson 1 if you need am.
-
Install one extra Pip package wey go install CounterFit shim wey fit work with UART sensors through serial connection. Make sure say you dey install am from terminal wey di virtual environment dey active.
pip install counterfit-shims-serial -
Make sure say di CounterFit web app dey run.
-
Create GPS sensor:
-
For di Create sensor box inside di Sensors pane, drop di Sensor type box and choose UART GPS.
-
Leave di Port as /dev/ttyAMA0
-
Click di Add button to create di GPS sensor for port
/dev/ttyAMA0
Di GPS sensor go dey create and e go show for di sensors list.
-
Program di GPS sensor
Di Virtual IoT device fit now dey program to use di virtual GPS sensor.
Task - program di GPS sensor
Program di GPS sensor app.
-
Make sure say di
gps-sensorapp dey open for VS Code. -
Open di
app.pyfile. -
Add dis code for di top of
app.pyto connect di app to CounterFit:from counterfit_connection import CounterFitConnection CounterFitConnection.init('127.0.0.1', 5000) -
Add dis code under di one wey dey above to import some libraries wey you need, including di library for di CounterFit serial port:
import time import counterfit_shims_serial serial = counterfit_shims_serial.Serial('/dev/ttyAMA0')Dis code dey import di
serialmodule from dicounterfit_shims_serialPip package. E then connect to di/dev/ttyAMA0serial port - na di address of di serial port wey di virtual GPS sensor dey use for e UART port. -
Add dis code under di one wey dey above to read from di serial port and print di values for console:
def print_gps_data(line): print(line.rstrip()) while True: line = serial.readline().decode('utf-8') while len(line) > 0: print_gps_data(line) line = serial.readline().decode('utf-8') time.sleep(1)One function wey dem call
print_gps_datadey define wey dey print di line wey dem pass give am for console.Next, di code go dey loop forever, dey read as many lines of text as e fit from di serial port for each loop. E go call di
print_gps_datafunction for each line.After e don read all di data, di loop go sleep for 1 second, then e go try again.
-
Run dis code, make sure say you dey use different terminal from di one wey di CounterFit app dey run, so dat di CounterFit app go still dey run.
-
From di CounterFit app, change di value of di GPS sensor. You fit do am like dis:
-
Set di Source to
Lat/Lon, then set di latitude, longitude and di number of satellites wey dem use to get di GPS fix. Dis value go send only once, so check di Repeat box to make di data dey repeat every second. -
Set di Source to
NMEAand add some NMEA sentences inside di text box. All di values go dey send, with delay of 1 second before each new GGA (position fix) sentence fit dey read.You fit use tool like nmeagen.org to generate dis sentences by drawing for map. Dis values go send only once, so check di Repeat box to make di data dey repeat one second after e don send all.
-
Set di Source to GPX file, then upload GPX file wey get track locations. You fit download GPX files from popular mapping and hiking sites like AllTrails. Dis files dey contain plenty GPS locations as trail, and di GPS sensor go dey return each new location every 1 second.
Dis values go send only once, so check di Repeat box to make di data dey repeat one second after e don send all.
Once you don configure di GPS settings, click di Set button to save di values for di sensor.
-
-
You go see di raw output from di GPS sensor, something like dis:
$GNGGA,020604.001,4738.538654,N,12208.341758,W,1,3,,164.7,M,-17.1,M,,*67 $GNGGA,020604.001,4738.538654,N,12208.341758,W,1,3,,164.7,M,-17.1,M,,*67
💁 You fit find dis code for di code-gps/virtual-device folder.
😀 Your GPS sensor program don work well!
Disclaimer:
Dis dokyument don use AI transleshion service Co-op Translator do di transleshion. Even as we dey try make am accurate, abeg make you sabi say automatik transleshion fit get mistake or no dey correct well. Di original dokyument wey dey di native language na di main source wey you go fit trust. For important informashon, e good make you use professional human transleshion. We no go fit take blame for any misunderstanding or wrong meaning wey fit happen because you use dis transleshion.




