pierrot10
Posts: 106
Joined: Mon Dec 15, 2014 3:17 pm

How can I use GPS with Serial while another sensor use it

Sun Apr 19, 2020 2:57 pm

Dear All,

I am working on a project to measure the Air Quality Index with an Raspberry Zero W with Rabian Buster
My sensor is connected to the USB port /dev/ttyUSB0).

I would like to add the GP-735 GPS https://www.sparkfun.com/products/13670

I think I can connect it to the pin


GPS ––––––––––––––––––––––– Pi
Vin (Pin 1) –––––––––––––––––– 5v (Pin 2 or 4)
GND (Pin 2) –––––––––––––––– GND (Pin 6, 9, or 14)
TX (Pin 3) –Voltage Converter– RX (Pin 10)
RX (Pin 4) –––––––––––––––––– TX (Pin 8)

That correct, it will be using /dev/ttyAMA0

Then I can try to decode the GPS message sent to RX?

Do you have any experience to share let me do it soon?
What would be your advise and tutorils?

Many thank
Pierre

User avatar
bensimmo
Posts: 4577
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: How can I use GPS with Serial while another sensor use it

Sun Apr 19, 2020 4:46 pm

What other sensors are you attaching?

That GPS is very expensive (it's a Ublox7) you can find Ublox 8 GNSS modules on Aliexpress in USB/UART setup under the 8030 chipset, I've used a lot of them. They are a bit bigger and probably similar to the 'mouse' on that page, but you take it out of it box and it's easier to use.
Even better is you can easily (bit of soldering) switch between gps and uart.

You shouldn't need the voltage convertor, the GNSS modules usually take a wide input and drop them all to 3V3 on output iirc, but do check. Most also will run from the 3V3 line just to be safe.

But what you say is pretty much how you would connect it.


links for reference
https://www.aliexpress.com/item/33014536910.html
which can also be found in
https://www.aliexpress.com/item/32873049702.html


as one who has set up air-monitoring PiZero's with SDS011(UART), GNSS(USB), BME680/280(I2C/BSEC), DS18B20(1-wire) Could add TTN LoraWAN as well, but I've not ran them all together..

(and also NodeMCU /ESP8266 versions too).

pierrot10
Posts: 106
Joined: Mon Dec 15, 2014 3:17 pm

Re: How can I use GPS with Serial while another sensor use it

Sun Apr 19, 2020 10:08 pm

Hello

Thank a lot for your reply.

It's exactely what I am doing.
I attached a SDS011 to my Raspberry Zero and I send the result over LoRaWAN (TTN)
the SDS011 is attached at /dev/ttyUSB0
It's works since a couple of hour.
https://demo.smart-idea.io
Log as demo, Click Air Quality Index and then Station 1.

Actually it take a measure each 60sec

I have a couple GP-735 and I would like to use one of them, but it's true, it s expensive
https://www.sparkfun.com/products/13670

I also have a GPS antenna with a SMA connector

Image

The best would be to use the Antenna or the GP-735 with wires connected as bellow.

As the SDS011 is connected to the USB, can I connect the GPS to another USB port? And what about Rx/Tx. I heard only one devise can use Rx/Tx. I read some where, I could connect m GPS to

GPS ––––––––––––––––––––––– Pi
Vin (Pin 1) –––––––––––––––––– 5v (Pin 2 or 4)
GND (Pin 2) –––––––––––––––– GND (Pin 6, 9, or 14)
TX (Pin 3) –Voltage Converter– RX (Pin 10)
RX (Pin 4) –––––––––––––––––– TX (Pin 8)

to use That correct, it will be using /dev/ttyAMA0

That was my first idea, but not sure about the best way to process.
What would you suggest me ?

Thank

User avatar
bensimmo
Posts: 4577
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: How can I use GPS with Serial while another sensor use it

Mon Apr 20, 2020 8:57 am

if you have a usb *hub*, then you can use usb GPS, if not then you will need to use the UART.

OR switch it around and use the SDS on UART and avoid the adaptor.

Though you'll not be able to power down the SDS on UART as it resets the Pi when the fan kicks in.
Though if you are recording every 60 seconds, you'll not be powering the fan (I assume)

get it set up, just use 3V3 for the gps and wire everything else up direct.

Then setup the UART...

pierrot10
Posts: 106
Joined: Mon Dec 15, 2014 3:17 pm

Re: How can I use GPS with Serial while another sensor use it

Mon Apr 20, 2020 11:35 am

Hello

Then when you said
Then setup the UART...
you mean connecting as the following

GPS ––––––––––––––––––––––– Pi
Vin (Pin 1) –––––––––––––––––– 5v (Pin 2 or 4)
GND (Pin 2) –––––––––––––––– GND (Pin 6, 9, or 14)
TX (Pin 3) –Voltage Converter– RX (Pin 10)
RX (Pin 4) –––––––––––––––––– TX (Pin 8)

That was my idea and preference.

Do you know python library can read GPS thourth UART?

As you used sds011, I have another question for you.

My Pathon scripts is mesuring 10 times the PMs and wait 10sec after and it wake up to measure the first measure

Code: Select all

[INFO] Waking up SDS011
[INFO] Measuring 10 times
0. pm2.5: 18.8 µg/m3  pm10:20.7 µg/m3
1. pm2.5: 37.8 µg/m3  pm10:41.599999999999994 µg/m3
2. pm2.5: 57.3 µg/m3  pm10:62.99999999999999 µg/m3
3. pm2.5: 76.9 µg/m3  pm10:84.5 µg/m3
4. pm2.5: 96.9 µg/m3  pm10:106.4 µg/m3
5. pm2.5: 117.10000000000001 µg/m3  pm10:128.6 µg/m3
6. pm2.5: 137.3 µg/m3  pm10:150.79999999999998 µg/m3
7. pm2.5: 157.60000000000002 µg/m3  pm10:173.1 µg/m3
8. pm2.5: 177.90000000000003 µg/m3  pm10:195.4 µg/m3
9. pm2.5: 198.10000000000002 µg/m3  pm10:217.6 µg/m3
[INFO] SDS011 go to sleep
---------------------------------------
2020-04-20 (13:31:45)    PM2.5: 19.8 µg/m3    PM10: 21.8 µg/m3
2020-04-20 (13:31:45)    AQI (PMT2.5): 67    AQI(PMT10): 19
 
[INFO] Sending data
[INFO] Data sent to TTN
[INFO] Sleep for 60 sec
But I am a bit surprised the the value only increase. I guess if I measure 20 time, it will again increase?

During the 10 measures, I though to see different value, inceasing but not all the time. After the measures, it make a average and I can print the final value.

Something look wrong for you?

User avatar
bensimmo
Posts: 4577
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: How can I use GPS with Serial while another sensor use it

Mon Apr 20, 2020 11:45 am

the way the SDS works, you wake it up (or it does it itself if in that mode).
wait 30 seconds for it to stabilise and get a good flow going
then you take measurements.

if you don't turn it off, you don't need to wait.

Personaly I take 30 measurements (1/second) then average.
BUT over the 30 seconds it doesn't really change.
so I would get 5.0, 5.0,5.0,5.0,4.9,5.0, 5.0 ...

I measure every 6 minutes.


GNSS/GPS
-----------

Vin -> 3V3
GND - GND
TX - RX
RX - TX

sudo raspi-config
..Interface settings.
...Console OFF
...Serial port ON

close
reboot.

check with
cat /dev/serial0

wait till you have a lock and see NMEA streams.

once that works, then start programming.

you can either use
pynmea2 Python3 module
or use gpsd methods.
up to you.

pierrot10
Posts: 106
Joined: Mon Dec 15, 2014 3:17 pm

Re: How can I use GPS with Serial while another sensor use it

Mon Apr 20, 2020 3:12 pm

Hello,
Thanks a lot for those useful information.
I am going to work on it
Pierre

pierrot10
Posts: 106
Joined: Mon Dec 15, 2014 3:17 pm

Re: How can I use GPS with Serial while another sensor use it

Mon Apr 20, 2020 7:17 pm

I can see Console in
5 Interfacing Options
and cat /dev/serial0
does not exist :(
but cat /dev/ttyS0 works better
$GPRMC,194235.00,A,4627.20872,N,00613.15490,E,0.335,,200420,,,A*7C

$GPVTG,,T,,M,0.335,N,0.621,K,A*23

$GPGGA,194235.00,4627.20872,N,00613.15490,E,1,04,4.22,716.8,M,47.2,M,,*58

$GPGSA,A,3,10,27,08,32,,,,,,,,,7.98,4.22,6.78*04

$GPGSV,3,1,11,01,47,287,,03,17,222,,08,70,173,18,10,30,051,37*72
Interface setting is not listed

mwrich4
Posts: 99
Joined: Wed Jul 25, 2012 1:24 pm
Location: Stuart, Florida

Re: How can I use GPS with Serial while another sensor use it

Tue Apr 21, 2020 12:15 am

In other projects, I have used a buffer chip to toggle between 2 or more serial paths. It's an extra layer for your code, but aside from the toggle everything else works the same.

User avatar
bensimmo
Posts: 4577
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: How can I use GPS with Serial while another sensor use it

Tue Apr 21, 2020 6:58 am

that's the stream, you must have it set up right :-).

pierrot10
Posts: 106
Joined: Mon Dec 15, 2014 3:17 pm

Re: How can I use GPS with Serial while another sensor use it

Tue Apr 21, 2020 12:34 pm

Dear All

I make a good work (I hope). I will post my code later, I have to leave

devendraraju
Posts: 1
Joined: Tue Apr 21, 2020 2:35 pm

Re: How can I use GPS with Serial while another sensor use it

Tue Apr 21, 2020 2:39 pm

bensimmo wrote:
Tue Apr 21, 2020 6:58 am
that's the stream, you must have it set up right :-).
Indeed, someone is saying that he will share his code. lets see.

pierrot10
Posts: 106
Joined: Mon Dec 15, 2014 3:17 pm

Re: How can I use GPS with Serial while another sensor use it

Tue Apr 21, 2020 10:30 pm

I could make it woorking but it s not optimal yet

Code: Select all

def get_gps():
    print('[INFO] Getting GPS')
   try:
        with serial.Serial('/dev/ttyAMA0', baudrate=9600, timeout=1) as ser:
            gpsOk = False

            #print('[INFO] Warmup GPS')
            for i in range(100):
                warm = ser.readline()
                print(warm)

            # read 20 lines from the serial output
            for i in range(20):
                #print('Read ' + str(i))
                try:
                    data = ser.readline()
                    sdata=data[0:6].decode('UTF-8')
                    if sdata == "$GPRMC":
                        nmea  = pynmea2.parse(data.decode('UTF-8'),check=True)
                        gpsOk = True
                        break
                except serial.SerialException as e:
                    print('Device error: {}'.format(e))
                    break
                except pynmea2.ParseError as e:
                    print('Parse error: {}'.format(e))
                    continue

            # NOT USED
            # parseGPS(data)

    except Exception as e:
        print('Error reading serial port:' , e)

   # Power off GPS
    print('[INFO] Turn OFF GPS')
    gps_power.value = False

    if gpsOk:
        print('[INFO] OK')
        return nmea.latitude, nmea.longitude
    else:
        print('[ERROR] Cannot find GPRMC sentences')
        return 0.0,0.0
The things I missed is to check the ENMA sentence, if the checksum is valid or not.

Some time, I read this
b'$GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99*30\r\n'
b'$GPGSV,6,1,21,01,,,24,02,,,26,03,,,28,04,,,24*77\r\n'
b'$GPGSV,6,2,21,06,,,25,07,,,26,09,,,25,10,,,25*74\r\n'
b'$GPGSV,6,3,21,11,,,25,14,,,36,22,,,25,23,,,24*78\r\n'
b'$GPGSV,6,4,21,24,,,25,25,,,24,26,,,24,27,,,23*7E\r\n'
b'$GPGSV,6,5,21,28,,,24,30,,,25,31,,,37,32,,,40*72\r\n'
b'$GPGSV,6,6,21,50,,,26*7B\r\n'
b'$GPGLL,,,,,214908.00,V,N*4C\r\n'
b'$GPRMC,214909.00,V,,,,,,,210420,,,N*7F\r\n'
b'$GPVTG,,,,,,,,,N*30\r\n'
b'$GPGGA,214909.00,,,,,0,00,99.99,,,,,,*61\r\n'
b'$GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99*30\r\n'
b'$GPGSV,6,1,21,01,,,23,02,,,24,03,,,26,04,,,24*7C\r\n'
b'$GPGSV,6,2,21,06,,,24,07,,,25,09,,,24,10,,,24*76\r\n'
b'$GPGSV,6,3,21,11,,,24,14,,,36,22,,,24,23,,,23*7F\r\n'
b'$GPGSV,6,4,21,24,,,24,25,,,23,26,,,23,27,,,23*7F\r\n'
b'$GPGSV,6,5,21,28,,,23,30,,,24,31,,,37,32,,,40*74\r\n'
b'$GPGSV,6,6,21,50,,,27*7A\r\n'
b'$GPGLL,,,,,214909.00,V,N*4D\r\n'
b'$GPRMC,214910.00,V,,,,,,,210420,,,N*77\r\n'
b'$GPVTG,,,,,,,,,N*30\r\n'
b'$GPGGA,214910.00,,,,,0,00,99.99,,,,,,*69\r\n'
b'$GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99*30\r\n'
b'$GPGSV,5,1,20,01,,,23,02,,,24,03,,,24,04,,,23*7B\r\n'
b'$GPGSV,5,2,20,06,,,24,07,,,24,09,,,24,10,,,23*72\r\n'
b'$GPGSV,5,3,20,11,,,24,14,,,35,22,,,23,23,,,22*78\r\n'
b'$GPGSV,5,4,20,24,,,23,25,,,28,26,,,24,27,,,22*77\r\n'
b'$GPGSV,5,5,20,28,,,22,30,,,23,31,,,36,32,,,39*7F\r\n'
b'$GPGLL,,,,,214910.00,V,N*45\r\n'
b'$GPRMC,214911.00,V,,,,,,,210420,,,N*76\r\n'
And I read it, I would like to loop a bit longer, or to return a warning.

My GPS (GP-735) has a PWR_CTRL pin. If I leave it disconnected, I read valid sentence, but when it pull HIGH (poweron), I go thos bad sentence. This I do not undertand why, but it any case, if I can find a solution to flag when the sentence is like this
b'$GPRMC,214910.00,V,,,,,,,210420,,,N*77\r\n'
or
b'$GPRMC,220440.00,A,4627.20597,N,00613.15767,E,0.184,,210420,,,A*70\r\n'

User avatar
mwilliams03
Posts: 125
Joined: Mon Oct 29, 2012 2:56 pm
Contact: Website

Re: How can I use GPS with Serial while another sensor use it

Mon May 25, 2020 4:50 am

Here are a few examples of how to read the NMEA sentences using Python
https://ozzmaker.com/using-python-with- ... pberry-pi/
Mark -- OzzMaker.com -- Creator of;
+ BerryIMU
+ Berry GPS-IMU
+BerryGPS-GSM
+ PiScreen - A 3.5" TFT for the Rasspberry Pi

User avatar
DougieLawson
Posts: 38882
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How can I use GPS with Serial while another sensor use it

Mon May 25, 2020 8:00 am

Run gpsd to read the sensor. gpsd supports multiple concurrent TCP/IP clients.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Return to “General discussion”