Git Product home page Git Product logo

gsmhat's Introduction

Update on Thu Apr 4th, 2024

Unfortunately, there is no time (and equipment anymore) to maintain this repository. I hope that someone else will find the time to continue with that.

gsmHat - Waveshare GSM/GPRS/GNSS HAT for Raspberry Pi with Python

With gsmHat, you can easily use the functionality of the Waveshare GSM/GPRS/GNSS HAT for Raspberry Pi (Link to HAT). On this module a SIM868 Controller is doing the job to connect your Raspberry Pi with the world just by using a sim card.

Update on Wed Oct 21st, 2020

๐Ÿ‘‰ Internet functionality added!

Overview

gsmHat was written for Python 3. It provides the following features

  • Non-blocking receiving and sending SMS in background
  • Non-blocking calling
  • Non-blocking refreshing of actual gps position
  • Non-blocking URL Call and receiving of response

Usage

In the following paragraphs, I am going to describe how you can get and use gsmHat for your own projects.

Getting it

To download gsmHat, either fork this github repo or simply use Pypi via pip.

$ python3 -m pip install -U gsmHat

Prepare

  • Install your sim card in your module, connect the GSM and the GPS antennas and mount the module on the pin headers of your Raspberry Pi. Make sure, that you do not need to enter Pin Code to use your card. Pin Codes are not supported yet.

  • Enable the Uart Interface in your Raspberry Pi

    1. Start raspi-config: sudo raspi-config.
    2. Select option 5 - interfacing options.
    3. Select option P6 - serial.
    4. At the prompt Would you like a login shell to be accessible over serial? answer 'No'
    5. At the prompt Would you like the serial port hardware to be enabled? answer 'Yes'
    6. Exit raspi-config and reboot the Pi for changes to take effect.

Using it

  1. Import gsmHat to your project
from gsmHat import GSMHat, SMS, GPS
  1. Init gsmHat
gsm = GSMHat('/dev/ttyS0', 115200)
  1. Check, if new SMS are available in your main loop
# Check, if new SMS is available
if gsm.SMS_available() > 0:
    # Get new SMS
    newSMS = gsm.SMS_read()
    # Do something with it
  1. Do something with your newly received SMS
# Get new SMS
newSMS = gsm.SMS_read()

print('Got new SMS from number %s' % newSMS.Sender)
print('It was received at %s' % newSMS.Date)
print('The message is: %s' % newSMS.Message)
  1. You can also write SMS
Number = '+491601234567'
Message = 'Hello mobile world'

# Send SMS
gsm.SMS_write(Number, Message)
  1. Or you can call a number
Number = '+491601234567'
gsm.Call(Number)        # This call hangs up automatically after 15 seconds
time.sleep(10)          # Wait 10 seconds ...
gsm.HangUp()            # Or you can HangUp by yourself earlier
gsm.Call(Number, 60)    # Or lets change the timeout to 60 seconds. This call hangs up automatically after 60 seconds
  1. Lets see, where your Raspberry Pi (in a car or on a motocycle or on a cat?) is positioned on earth
# Get actual GPS position
GPSObj = gsm.GetActualGPS()

# Lets print some values
print('GNSS_status: %s' % str(GPSObj.GNSS_status))
print('Fix_status: %s' % str(GPSObj.Fix_status))
print('UTC: %s' % str(GPSObj.UTC))
print('Latitude: %s' % str(GPSObj.Latitude))
print('Longitude: %s' % str(GPSObj.Longitude))
print('Altitude: %s' % str(GPSObj.Altitude))
print('Speed: %s' % str(GPSObj.Speed))
print('Course: %s' % str(GPSObj.Course))
print('HDOP: %s' % str(GPSObj.HDOP))
print('PDOP: %s' % str(GPSObj.PDOP))
print('VDOP: %s' % str(GPSObj.VDOP))
print('GPS_satellites: %s' % str(GPSObj.GPS_satellites))
print('GNSS_satellites: %s' % str(GPSObj.GNSS_satellites))
print('Signal: %s' % str(GPSObj.Signal))
  1. Calculate the distance between two Points on earth
GPSObj1 = GPS()                 # You can also use gsm.GetActualGPS() to get an GPS object
GPSObj1.Latitude = 52.266949    # Location of Braunschweig, Germany
GPSObj1.Longitude = 10.524822

GPSObj2 = GPS()
GPSObj2.Latitude = 36.720005    # Location of Manavgat, Turkey
GPSObj2.Longitude = 31.546094

print('Distance from Braunschweig to Manavgat in metres:')
print(GPS.CalculateDeltaP(GPSObj1, GPSObj2))        # this will print 2384660.7 metres
  1. Call URL to send some data
# Init gsmHat
gsm = GSMHat('/dev/ttyS0', 115200)

# Set the APN Connection data. You will get this from your provider
# e.g. German Provider 'Congstar'
gsm.SetGPRSconnection('internet.telekom', 'congstar', 'cs')

# Get actual GPS position
GPSObj = gsm.GetActualGPS()

# Build url string with data
url = 'www.someserver.de/myscript.php'
url += '?time='+str(int(GPSObj.UTC.timestamp()))
url += '&lat='+str(GPSObj.Latitude)
url += '&lon='+str(GPSObj.Longitude)
url += '&alt='+str(GPSObj.Altitude)

gsm.CallUrl(url)    # Send actual position to a webserver
  1. Get the Response from a previous URL call
# Check, if new Response Data is available
if gsm.UrlResponse_available() > 0:
    # Read the Response
    newResponse = gsm.UrlResponse_read()
    # Do something with it

What will come in the future?

  • More options to configure the module (e.g. using sim cards with pin code)

On which platform was gsmHat developed and tested?

Hardware:

Software:

  • Raspbian (Codename: buster, Release: 10)
  • Kernel: Linux 5.4.51-v7l+
  • Python: 3.7.3

License

MIT License

Copyright (c) 2020 Tarek Tounsi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

contact me: [email protected]

gsmhat's People

Contributors

civlo85 avatar thinktransit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

gsmhat's Issues

can I use it with pi-zero ? I have SIM7600G-H 4G HAT (B)

Hello,
wanted to know if I can use it on pi-zero ?
because I'm trying to get GPS data only

from gsmHat import GSMHat, SMS, GPS

try:
    gsm = GSMHat('/dev/ttyS0', 115200)
except Exception as e :
    print (e)
else:
    try:
         GPSObj = gsm.GetActualGPS()
    except Exception as e :
         print (e)
    else:
         print('GNSS_status: %s' % str(GPSObj.GNSS_status))
         print('Fix_status: %s' % str(GPSObj.Fix_status))
         print('UTC: %s' % str(GPSObj.UTC))
         print('Latitude: %s' % str(GPSObj.Latitude))
         print('Longitude: %s' % str(GPSObj.Longitude))
         print('Altitude: %s' % str(GPSObj.Altitude))
         print('Speed: %s' % str(GPSObj.Speed))

and I get error

    raise Exception('Could not determine Jetson model')
Exception: Could not determine Jetson model

the modem is connect to the internet without any problem (so the modem is working)

I'm able to "talk" to the modem using :

minicom -D /dev/ttyUSB2


Thanks,

Thread crash on receiving SMS

I just had the library crash the thread when it received an SMS. here is the log:

2021-10-17 09:03:56,265 - gsmHat.gsmHat - DEBUG - Received Data: +SAPBR: 1,3,"0.0.0.0"
2021-10-17 09:03:56,295 - gsmHat.gsmHat - DEBUG - Received Data: OK
2021-10-17 09:03:56,307 - gsmHat.gsmHat - DEBUG - Lock Off
2021-10-17 09:03:56,313 - gsmHat.gsmHat - ERROR - Timeout during data reception
2021-10-17 09:03:56,318 - gsmHat.gsmHat - INFO - Command sent: AT+SAPBR=2,1
2021-10-17 09:03:56,324 - gsmHat.gsmHat - INFO - Actual state of programme: 61
2021-10-17 09:03:56,326 - gsmHat.gsmHat - CRITICAL - Exception: Unhandled timeout during data reception

looking at the script after sending the critical warning it basically causes the script to end using the raise command. Why does it do that? As soon as that happens the library stops working within my script and I don't get any sms messages being sent out or received. Is there a more graceful way to handle the error? Maybe making it wait a few seconds and then try again later?

How to stop logging?

I am not using the GPS but I am getting a huge log because it isn't getting valid data.

Is there anyway to turn off the logging? Either that or to turn off the GPS?

RTC Time on board

Hello,

The datasheet of the board tells that we can get RTC Time by the Command AT (AT+CCLK?) and set by (AT+CCLK="18/12/12,17:53:00+01").
Do you plan to develop this feature in your library?
Thank 's you.

Cant use any function

I have installed your module correctly. My sim card is in HAT and it works (LED is on every 3 seconds).I turned on ttyS0 and I know it works.

when i execute your sample script:

from gsmHat import *
gsm = GSMHat('/dev/ttyS0', 115200)
Number = '+some_number'
gsm.Call(Number)

but nothing happens after that.

i did some testing. and I can see that while executing this script AT+CMGF=1 is written to /dev/ttyS0 and then get the answer OK
but nothing else. each time it executes the script (SMS or call) there is only
AT+CMGF=1
OK

so i installed minicom and when it executes the call command directly call/ Hang up works :
minicom -D /dev/ttyS0
ATD+some_number;
OK
ATH
OK

log:

2021-05-17 22:29:43,581 - gsmHat.gsmHat - INFO - Serial connection to /dev/ttyS0 established
2021-05-17 22:29:43,582 - gsmHat.gsmHat - INFO - Worker started

Not getting any gps data in GPSobj

when I got the hat working with the serial communication I have a problem. it seems that the GPSObj not stores the values
when I running the test codes for the gps data ex:

`GPSObj = gsm.GetActualGPS()

print('Latitude: %s' % str(GPSObj.Latitude))
print('Longitude: %s' % str(GPSObj.Longitude))
print('Altitude: %s' % str(GPSObj.Altitude))`

it just returning:
Latitude: 0.0
Longitude: 0.0
Altitude: 0.0

i have checked my gpsHat logfile and I can see that i get the gps cordinate from the gps reciver:

2022-05-09 19:57:28,604 - gsmHat.gsmHat - DEBUG - Sent to hat: AT+CGNSINF

2022-05-09 19:57:28,705 - gsmHat.gsmHat - DEBUG - Received Data: AT+CGNSINF
2022-05-09 19:57:28,715 - gsmHat.gsmHat - DEBUG - Received Data: +CGNSINF: 1,1,20220509175728.000,60.502652,15.313006,101.952,0.00,0.0,1,,0.7,1.0,0.7,,13,16,,,46,,
2022-05-09 19:57:28,715 - gsmHat.gsmHat - DEBUG - New GPS Data:
2022-05-09 19:57:28,717 - gsmHat.gsmHat - DEBUG - Received Data: OK
2022-05-09 19:57:28,717 - gsmHat.gsmHat - DEBUG - Lock Off
2022-05-09 19:57:29,419 - gsmHat.gsmHat - DEBUG - Sent to hat: AT+CPMS="SM"

2022-05-09 19:57:29,521 - gsmHat.gsmHat - DEBUG - Received Data: AT+CPMS="SM"
2022-05-09 19:57:29,523 - gsmHat.gsmHat - DEBUG - Received Data: +CPMS: 0,30,0,30,0,30
2022-05-09 19:57:29,524 - gsmHat.gsmHat - DEBUG - Received Data: OK

Someone that has this problem to or know how to do to fix it?

running on a raspberry pie zero 2 w with latest raspian lite

pip3 install gsmHat does not work

Hi, Just for info: I could install the library from Github but installing the library from pip3 throws SyntaxError.

I am using python 3.7.3 and also have the latest version of pip3.

Below is the detail of the error I am getting:

Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting gsmHat
Using cached https://www.piwheels.org/simple/gsmhat/gsmHat-0.2-py3-none-any.whl
Collecting datetime (from gsmHat)
Using cached https://files.pythonhosted.org/packages/73/22/a5297f3a1f92468cc737f8ce7ba6e5f245fcfafeae810ba37bd1039ea01c/DateTime-4.3-py2.py3-none-any.whl
Collecting logging (from gsmHat)
Using cached https://files.pythonhosted.org/packages/93/4b/979db9e44be09f71e85c9c8cfc42f258adfb7d93ce01deed2788b2948919/logging-0.4.9.6.tar.gz
Complete output from command python setup.py egg_info:
running egg_info
creating pip-egg-info/logging.egg-info
writing pip-egg-info/logging.egg-info/PKG-INFO
writing dependency_links to pip-egg-info/logging.egg-info/dependency_links.txt
writing top-level names to pip-egg-info/logging.egg-info/top_level.txt
writing manifest file 'pip-egg-info/logging.egg-info/SOURCES.txt'
Traceback (most recent call last):
File "", line 1, in
File "/tmp/pip-install-1jx6x948/logging/setup.py", line 13, in
packages = ["logging"],
File "/usr/lib/python3.7/distutils/core.py", line 148, in setup
dist.run_commands()
File "/usr/lib/python3.7/distutils/dist.py", line 966, in run_commands
self.run_command(cmd)
File "/usr/lib/python3.7/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/usr/lib/python3/dist-packages/setuptools/command/egg_info.py", line 296, in run
self.find_sources()
File "/usr/lib/python3/dist-packages/setuptools/command/egg_info.py", line 303, in find_sources
mm.run()
File "/usr/lib/python3/dist-packages/setuptools/command/egg_info.py", line 534, in run
self.add_defaults()
File "/usr/lib/python3/dist-packages/setuptools/command/egg_info.py", line 570, in add_defaults
sdist.add_defaults(self)
File "/usr/lib/python3.7/distutils/command/sdist.py", line 226, in add_defaults
self._add_defaults_python()
File "/usr/lib/python3/dist-packages/setuptools/command/sdist.py", line 127, in _add_defaults_python
build_py = self.get_finalized_command('build_py')
File "/usr/lib/python3.7/distutils/cmd.py", line 298, in get_finalized_command
cmd_obj = self.distribution.get_command_obj(command, create)
File "/usr/lib/python3.7/distutils/dist.py", line 857, in get_command_obj
klass = self.get_command_class(command)
File "/usr/lib/python3/dist-packages/setuptools/dist.py", line 845, in get_command_class
self.cmdclass[command] = cmdclass = ep.load()
File "/usr/lib/python3/dist-packages/pkg_resources/init.py", line 2411, in load
return self.resolve()
File "/usr/lib/python3/dist-packages/pkg_resources/init.py", line 2417, in resolve
module = import(self.module_name, fromlist=['name'], level=0)
File "/usr/lib/python3/dist-packages/setuptools/command/build_py.py", line 15, in
from setuptools.lib2to3_ex import Mixin2to3
File "/usr/lib/python3/dist-packages/setuptools/lib2to3_ex.py", line 12, in
from lib2to3.refactor import RefactoringTool, get_fixers_from_package
File "/usr/lib/python3.7/lib2to3/refactor.py", line 18, in
import logging
File "/tmp/pip-install-1jx6x948/logging/logging/init.py", line 618
raise NotImplementedError, 'emit must be implemented '
^
SyntaxError: invalid syntax

Will this work on SIM7000?

I cannot get past the first call of AT+CMGF=1
I do not get any valid reply.

Does anybody have a fix for a SIM7000 module? Is there an alternative?
I would love to have this functionality in my Application.

Cheers

Cant get any result?

GNSS_status: 0
Fix_status: 0
UTC:
Latitude: 0.0
Longitude: 0.0
Altitude: 0.0
Speed: 0.0
Course: 0.0
HDOP: 0.0
PDOP: 0.0
VDOP: 0.0
GPS_satellites: 0
GNSS_satellites: 0
Signal: 0.0
pi@raspberrypi:~ $

this is what the original gps.py do:

AT+CGNSTST=1
OK
$GNGGA,192954.811,,,,,0,0,,,M,,M,,*5C
$GPGSA,A,1,,,,,,,,,,,,,,,*1E
$GLGSA,A,1,,,,,,,,,,,,,,,02
$GPGSV,3,1,12,21,70,288,,08,60,193,41,01,45,284,,32,43,100,39
7D
$GPGSV,3,2,12,22,40,230,,10,32,058,43,27,29,161,33,14,22,308,*72
$GPGSV,3,3,12,28,21,316,,03,12,229,,24,05,023,,23,01,055,*7C
$GLGSV,3,1,10,65,64,067,29,88,60,112,33,81,59,319,,66,38,186,*63
$GLGSV,3,2,10,72,23,034,,87,20,121,,80,16,320,,82,11,309,67
$GLGSV,3,3,10,73,08,008,,79,05,269,66
$GNRMC,192954.811,V,,,,,0.00,0.00,120421,,,N
5D
$GNVTG,0.00,T,,M,0.00,N,0.00,K,N
2C

this is my skript :

from gsmHat import GSMHat, SMS, GPS
import time
gsm = GSMHat('/dev/ttyS0', 115200)

GPSObj = gsm.GetActualGPS()

Lets print some values

print('GNSS_status: %s' % str(GPSObj.GNSS_status))
print('Fix_status: %s' % str(GPSObj.Fix_status))
print('UTC: %s' % str(GPSObj.UTC))
print('Latitude: %s' % str(GPSObj.Latitude))
print('Longitude: %s' % str(GPSObj.Longitude))
print('Altitude: %s' % str(GPSObj.Altitude))
print('Speed: %s' % str(GPSObj.Speed))
print('Course: %s' % str(GPSObj.Course))
print('HDOP: %s' % str(GPSObj.HDOP))
print('PDOP: %s' % str(GPSObj.PDOP))
print('VDOP: %s' % str(GPSObj.VDOP))
print('GPS_satellites: %s' % str(GPSObj.GPS_satellites))
print('GNSS_satellites: %s' % str(GPSObj.GNSS_satellites))
print('Signal: %s' % str(GPSObj.Signal))

pi 4 all new also pi 3 tested an an old 2018 img also tested same problem

by Robby

Program errors out if SIM can't support 20 messages.

gsmHat/gsmHat/gsmHat.py

Lines 540 to 543 in f8e4bf7

if(self.__smsToRead == 20):
self.__smsToRead = 0
else:
self.__smsToRead = self.__smsToRead + 1

This codeblock assumes that a SIM can always store a total of 20 SMS. However, this isn't always the case. The output of AT+CPMS="SM" should be used to determine the maximum storage, since the HAT will raise an error if a slot that's too high is access ed.

Cannot receive any data from hat

Hi!
I'm trying out the code but I can't receive any data from my hat.

I'm using the repo example code for fetching the board GPS position.

from gsmHat import GSMHat, SMS, GPS
gsm = GSMHat('/dev/ttyS0', 115200)
GPSObj = gsm.GetActualGPS()
print('GNSS_status: %s' % str(GPSObj.GNSS_status))

Python result:

GNSS_status: 0

And here are the logs:

2021-08-31 07:23:19,639 - gsmHat.gsmHat - INFO - Serial connection to /dev/ttyS0 established
2021-08-31 07:23:19,640 - gsmHat.gsmHat - INFO - Worker started
2021-08-31 07:23:19,641 - gsmHat.gsmHat - DEBUG - Sent to hat: AT+CMGF=1

Just some notes:

  • The hat is connected to my raspberry pi 3B
  • The jumpers are on position B
  • If I connect the board to my PC directly (and put the jumpers on position A) I can send commands and receive data
  • Tried waiting for a GPS fix but result doesn't change
  • I've also tried manually powering ON the hat (with the power key button) but it makes not difference at all

It looks like the raspberry may not be communicating the hat.

AttributeError: module 'serial' has no attribute 'Serial'

Hello,

Just loaded on RPI3 with fresh raspios-buster image

python3 test.py
Traceback (most recent call last):
File "test.py", line 2, in
gsm = GSMHat('/dev/ttyS0', 115200)
File "/home/pi/.local/lib/python3.7/site-packages/gsmHat/gsmHat.py", line 74, in init
self.__connect()
File "/home/pi/.local/lib/python3.7/site-packages/gsmHat/gsmHat.py", line 78, in __connect
self.__ser = serial.Serial(self.__port, self.__baudrate)
AttributeError: module 'serial' has no attribute 'Serial'

tried
sudo apt-get remove python3-serial
pip3 install pyserial --upgrade

same error

OK resolved ==== pip3 uninstall serial

Error while GSM hat is connected via USB

I am using the USB to connect the GSM hat to the RPi 4.
I set the gsm as:
''
gsm = GSMHat('/dev/ttyUSB0', 115200)
''
However, I can not communicate with the HAt. The serial comm is already enabled.

if no SIM card is entered - the __startWorking will timeout

Have you noticed, that if you start the Waveshare module without a SIM card (you want to test just the GPS part), the gsmHat will timeout and never get to the GPS code?
I am getting stuck here:

gsmHat.gsmHat - ERROR - Timeout during data reception
gsmHat.gsmHat - INFO - Command sent: AT+CMGF=1

I was going through the code and the problem is, that if the SIM card is missing, it starts with this command:
if self.__sendToHat('AT+CMGF=1'):
however the CMGF will return error:

AT
OK
AT+CMGF=1
ERROR

The reason is, that initial state is 1. But if SIM card is missing, initial state should probably be 97?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.