Skip to main content

A library to interact with serial AT devices

Project description

ATlib

Python library for sending and receiving SMS texts using AT commands. Higher and lower level features supported. Tested with SIM800L.

The API is relatively straightforward. The API is not asynchronous meaning all methods return the result directly. Take a look at the source for the full library.

For an application of this library, see gsm-agent. Where SMS messages can used to run shell scripts on a server (with security checks & registration).

Installation

The package can be installed via PIP:

pip install atlib

API

The API features two classes for interfacing with the GSM modem.

The low level is the AT_Device class. This class exposes only a synchronous API for sending AT commands and reading responses. It abstracts the painful process of AT commands not directly responding due to latency. Responses are detected by a terminated OK or ERROR string. The read() commands returns a tokenized list of the reply for easy parsing.

  • Opening serial connection.
  • Synchronizing baudrate using sync_baudrate() (by sending "AT" and awaiting response).
  • Sending AT commands.
  • Reading AT commands reliably.
  • Detecting errors.

The high level is the GSM_Device class. This class inherits from AT_Device. This class provides higher level features such as

  • Unlocking the device sim using pin.
  • Sending text messages.
  • Reading text messages (by category unread, all, read, etc).
  • Deleting text messages
  • Checking device details, like manufacturer, model, serial number, ICCID, etc.
  • Checking operator details
  • Selecting operator
  • Calling
  • Checking signal strength

GSM_Device should only contain commands supported by all chips, if there are more special commands, they will be added to a custom class for that chip.

Currently the following Chips have additional functionality added:

  • AIR780EU
  • SIM7600GH

Supported Devices

Supported devices and functionality. Please keep in mind that the functionality depends on the breakout board you are using. For example SIM900 supports calls, but not all boards are equipped with audio jacks, so make sure that the actual hardware you need is required on the board.

Also keep in mind that not all generations of wireless communications are supported in all regions. In Europe some carriers have switched off 3G, although 2G still being widely available. 4G and 5G being the standard. So when deciding for a wireless communciation technology, make sure that your provider is still supporting it.

Chip Calls SMS 2G 3G 4G 5G GPS
SIM900 Yes Yes Yes -- -- -- --
SIM7070X Yes Yes Yes -- -- -- --
AIR780EU -- Yes -- -- Yes -- --
SIM7600GH Yes Yes Yes Yes Yes -- Yes

Contributing

If you have problems with your modem, you can open issues here. I have tested all commands with a properly hooked up SIM800L on a Raspberry Pi Bullseye. Know that not all devices support all AT commands, and therefore may fail when using this library. However most devices should support the basics.

Examples

Some examples can be found in the /examples directory. Refer to the library itself to see many AT commands in action already (e.g sending commands, checking responses). See below for a minimal texting application:

#!/bin/python
# Console SMS sender using ATlib.

from atlib import *

# Show debug logs
import logging
logging.basicConfig(level=logging.DEBUG)

gsm = GSM_Device("/dev/serial0")
if gsm.get_sim_status() != Status.OK:
    pin = input("SIM Pin: ")
    gsm.unlock_sim(pin)
else:
    print("SIM already unlocked.")

while True:
    print("")
    nr = input("Phone number: ")
    msg = input("Message: ")

    if gsm.send_sms(nr, msg) != OK:
        print("Error sending message.")

Development

Distribution

To build and upload to pypi, first update version in __init__.py and the pyproject.toml then run run:

python -m build
python -m twine upload --repository testpypi dist/*
python -m twine upload dist/*

Heritage

This code was initially developed by arceryz. He not longer maintains it and did not have interest to publish it to pypi but gave me permission to continue development and publish it to pypi myself.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

atlib-0.5.2.tar.gz (15.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

atlib-0.5.2-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file atlib-0.5.2.tar.gz.

File metadata

  • Download URL: atlib-0.5.2.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for atlib-0.5.2.tar.gz
Algorithm Hash digest
SHA256 1e496b7e72b09c50fe55b0301c7a374fff585325b0176b470ba5e446d20f059a
MD5 64511af25768e26073dc8675a349f975
BLAKE2b-256 d45eae132a14253dc233e4902b8a5720b0db9a997829d9cef28e8cf0b10ee900

See more details on using hashes here.

File details

Details for the file atlib-0.5.2-py3-none-any.whl.

File metadata

  • Download URL: atlib-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for atlib-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5bd480c3f0db9069d7299664427e6b7041954be40cb854339f83cbf463aaa4b6
MD5 c3049f504aa13843367cd6a3632d4043
BLAKE2b-256 eb42e596d646e99b95b1fe5aa00b88a1f9abeb54c424200e1bcdfc5af1097082

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page