├── LICENSE ├── README.md └── sim800 ├── __init__.py ├── core.py ├── gprs.py ├── sms.py ├── tcpip.py └── utils.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Flynn McLean 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 | # SIM800 MicroPython Library 2 | 3 | This library provides an interface for the SIM800 module to perform GSM, GPRS, SMS, and TCP/IP communications using MicroPython on microcontrollers. It includes functions to handle voice calls, SMS, HTTP requests, FTP, and more. 4 | 5 | ## Installation 6 | 7 | Copy the `sim800` folder to your MicroPython device filesystem. I advise a repo structure main.py entrypoint and importing as below. 8 | 9 | ## Usage 10 | 11 | ### Initialize the SIM800 Module 12 | 13 | ```python 14 | from sim800 import SIM800 15 | sim800 = SIM800(uart_pin=1, baud=115200) 16 | ``` 17 | 18 | ### Send an SMS 19 | 20 | 21 | ```python 22 | 23 | from sim800 import SIM800SMS 24 | sim800 = SIM800SMS(uart_pin=1) 25 | sim800.send_sms('+1234567890', 'Hello World') 26 | ``` 27 | 28 | ### Make a Voice Call 29 | ```python 30 | from sim800 import SIM800 31 | sim800 = SIM800(uart_pin=1) 32 | sim800.dial_number('+1234567890') 33 | 34 | # To hang up the call 35 | sim800.hang_up() 36 | ``` 37 | ### Get Network Time 38 | ```python 39 | from sim800 import SIM800 40 | sim800 = SIM800(uart_pin=1) 41 | time = sim800.get_network_time() 42 | print(time) 43 | ``` 44 | ### HTTP GET Request 45 | ```python 46 | from sim800 import SIM800TCPIP 47 | sim800 = SIM800TCPIP(uart_pin=1) 48 | sim800.http_init() 49 | sim800.http_set_param("URL", "http://example.com") 50 | response = sim800.http_get() 51 | print(response) 52 | sim800.http_terminate() 53 | ``` 54 | 55 | ### FTP Download File 56 | ```python 57 | from sim800 import SIM800TCPIP 58 | sim800 = SIM800TCPIP(uart_pin=1) 59 | sim800.ftp_init(server='ftp.example.com', username='user', password='pass') 60 | file_content = sim800.ftp_get_file('path/to/file.txt', '/remote/path') 61 | print(file_content) 62 | ``` 63 | ### Get GSM Location 64 | ```python 65 | 66 | from sim800 import SIM800GPRS 67 | sim800 = SIM800GPRS(uart_pin=1) 68 | location_info = sim800.get_gsm_location() 69 | print("GSM Location Info:", location_info) 70 | ``` 71 | -------------------------------------------------------------------------------- /sim800/__init__.py: -------------------------------------------------------------------------------- 1 | from .core import SIM800 2 | from .sms import SIM800SMS 3 | from .gprs import SIM800GPRS 4 | from .tcpip import SIM800TCPIP 5 | from .utils import SIM800Utils 6 | 7 | __all__ = ['SIM800', 'SIM800SMS', 'SIM800GPRS', 'SIM800TCPIP', 'SIM800Utils'] 8 | -------------------------------------------------------------------------------- /sim800/core.py: -------------------------------------------------------------------------------- 1 | import machine 2 | import utime 3 | 4 | class SIM800: 5 | """ 6 | Core class for handling communication with the SIM800 module. 7 | Includes basic functionalities to initialize, send commands, and read responses. 8 | """ 9 | def __init__(self, uart_pin, baud=115200): 10 | """ 11 | Initializes the UART connection with the SIM800 module. 12 | """ 13 | self.uart = machine.UART(uart_pin, baudrate=baud) 14 | self.initialize() 15 | 16 | def send_command(self, command, timeout=1000): 17 | """ 18 | Sends an AT command to the SIM800 module. 19 | """ 20 | self.uart.write(command + '\r') 21 | utime.sleep_ms(100) # Delay to allow command processing 22 | return self.read_response(timeout) 23 | 24 | def read_response(self, timeout=1000): 25 | """ 26 | Reads the response from the SIM800 module over UART. 27 | """ 28 | start_time = utime.ticks_ms() 29 | response = b'' 30 | while (utime.ticks_diff(utime.ticks_ms(), start_time) < timeout): 31 | if self.uart.any(): 32 | response += self.uart.read(self.uart.any()) 33 | return response 34 | 35 | def initialize(self): 36 | """ 37 | Basic initialization commands to set up the SIM800 module. 38 | """ 39 | self.send_command('AT') # Check module presence 40 | self.send_command('ATE0') # Turn off command echoing 41 | self.send_command('AT+CFUN=1') # Set full functionality mode 42 | 43 | def reset(self): 44 | """ 45 | Resets the SIM800 module. 46 | """ 47 | self.send_command('AT+CFUN=1,1') # Reset the module 48 | -------------------------------------------------------------------------------- /sim800/gprs.py: -------------------------------------------------------------------------------- 1 | from .core import SIM800 2 | 3 | class SIM800GPRS(SIM800): 4 | def attach_gprs(self): 5 | """ 6 | Attach to the GPRS service. 7 | """ 8 | return self.send_command('AT+CGATT=1') 9 | 10 | def detach_gprs(self): 11 | """ 12 | Detach from the GPRS service. 13 | """ 14 | return self.send_command('AT+CGATT=0') 15 | 16 | def set_apn(self, apn, user='', pwd=''): 17 | """ 18 | Set the APN, username, and password for the GPRS connection. 19 | """ 20 | self.send_command(f'AT+CSTT="{apn}","{user}","{pwd}"') 21 | return self.send_command('AT+CIICR') 22 | 23 | def get_ip_address(self): 24 | """ 25 | Get the local IP address assigned to the module. 26 | """ 27 | return self.send_command('AT+CIFSR') 28 | 29 | def start_tcp_connection(self, mode, ip, port): 30 | """ 31 | Start a TCP or UDP connection. 32 | """ 33 | return self.send_command(f'AT+CIPSTART="{mode}","{ip}","{port}"') 34 | 35 | def send_data_tcp(self, data): 36 | """ 37 | Send data through the TCP or UDP connection. 38 | """ 39 | self.send_command(f'AT+CIPSEND={len(data)}') 40 | self.uart.write(data + chr(26)) 41 | return self.read_response() 42 | 43 | def close_tcp_connection(self): 44 | """ 45 | Close the TCP or UDP connection. 46 | """ 47 | return self.send_command('AT+CIPCLOSE=1') 48 | 49 | def shutdown_gprs(self): 50 | """ 51 | Deactivate GPRS PDP context, effectively shutting down the GPRS service. 52 | """ 53 | return self.send_command('AT+CIPSHUT') 54 | 55 | def get_gsm_location(self): 56 | """ 57 | Get the GSM location based on the nearest cell tower. 58 | Returns the longitude and latitude along with a timestamp. 59 | """ 60 | response = self.send_command('AT+CIPGSMLOC=1,1') 61 | return response 62 | 63 | -------------------------------------------------------------------------------- /sim800/sms.py: -------------------------------------------------------------------------------- 1 | from .core import SIM800 2 | 3 | class SIM800SMS(SIM800): 4 | def set_sms_format(self, format="1"): 5 | """ 6 | Set SMS format (0 for PDU mode, 1 for text mode). 7 | """ 8 | return self.send_command(f'AT+CMGF={format}') 9 | 10 | def send_sms(self, number, message): 11 | """ 12 | Send an SMS message. 13 | """ 14 | self.send_command(f'AT+CMGS="{number}"') 15 | self.uart.write(message + chr(26)) # End with Ctrl-Z to send 16 | return self.read_response() 17 | 18 | def read_sms(self, index=1): 19 | """ 20 | Read an SMS at a given index. 21 | """ 22 | return self.send_command(f'AT+CMGR={index}') 23 | 24 | def delete_sms(self, index): 25 | """ 26 | Delete an SMS at a given index. 27 | """ 28 | return self.send_command(f'AT+CMGD={index}') 29 | 30 | def read_all_sms(self): 31 | """ 32 | Read all SMS messages stored in the memory. 33 | """ 34 | return self.send_command('AT+CMGL="ALL"') 35 | 36 | def delete_all_sms(self): 37 | """ 38 | Delete all SMS messages. 39 | """ 40 | return self.send_command('AT+CMGDA="DEL ALL"') 41 | -------------------------------------------------------------------------------- /sim800/tcpip.py: -------------------------------------------------------------------------------- 1 | from .core import SIM800 2 | 3 | class SIM800TCPIP(SIM800): 4 | def start_tcp_connection(self, mode, ip, port): 5 | """ 6 | Start a TCP or UDP connection. 7 | Mode can be 'TCP' or 'UDP'. IP address and port number are required. 8 | """ 9 | return self.send_command(f'AT+CIPSTART="{mode}","{ip}","{port}"') 10 | 11 | def send_data_tcp(self, data): 12 | """ 13 | Send data through the TCP or UDP connection. 14 | """ 15 | self.send_command(f'AT+CIPSEND={len(data)}') 16 | self.uart.write(data + chr(26)) # End with Ctrl-Z 17 | return self.read_response() 18 | 19 | def receive_data_tcp(self): 20 | """ 21 | Receive data from TCP or UDP connection. 22 | """ 23 | return self.send_command('AT+CIPRXGET=2') 24 | 25 | def close_tcp_connection(self): 26 | """ 27 | Close the TCP or UDP connection. 28 | """ 29 | return self.send_command('AT+CIPCLOSE=1') 30 | 31 | def shutdown_gprs(self): 32 | """ 33 | Deactivate GPRS PDP context, effectively shutting down the GPRS service. 34 | """ 35 | return self.send_command('AT+CIPSHUT') 36 | 37 | def get_ip_address(self): 38 | """ 39 | Get local IP address. 40 | """ 41 | return self.send_command('AT+CIFSR') 42 | 43 | def http_init(self): 44 | """ 45 | Initialize HTTP service. 46 | """ 47 | return self.send_command('AT+HTTPINIT') 48 | 49 | def http_set_param(self, param, value): 50 | """ 51 | Set HTTP parameter. 52 | """ 53 | return self.send_command(f'AT+HTTPPARA="{param}","{value}"') 54 | 55 | def http_get(self, url): 56 | """ 57 | Perform HTTP GET method. 58 | """ 59 | self.http_set_param("URL", url) 60 | self.send_command('AT+HTTPACTION=0') 61 | return self.read_response() 62 | 63 | def http_post(self, url, data): 64 | """ 65 | Perform HTTP POST method. 66 | """ 67 | self.http_set_param("URL", url) 68 | self.send_command(f'AT+HTTPDATA={len(data)},10000') 69 | self.uart.write(data) 70 | self.send_command('AT+HTTPACTION=1') 71 | return self.read_response() 72 | -------------------------------------------------------------------------------- /sim800/utils.py: -------------------------------------------------------------------------------- 1 | import utime 2 | 3 | class SIM800Utils: 4 | @staticmethod 5 | def wait_for_response(uart, expected_response, timeout=5000): 6 | """ 7 | Waits for a specific response from the SIM800 module. 8 | 9 | :param uart: The UART object used for communication. 10 | :param expected_response: The response string to wait for. 11 | :param timeout: The maximum time to wait in milliseconds. 12 | :return: The response if received, or None if timeout occurs. 13 | """ 14 | start_time = utime.ticks_ms() 15 | response = b'' 16 | 17 | while utime.ticks_diff(utime.ticks_ms(), start_time) < timeout: 18 | if uart.any(): 19 | response += uart.read(uart.any()) 20 | if expected_response.encode() in response: 21 | return response.decode('utf-8') 22 | utime.sleep_ms(100) 23 | 24 | return None 25 | 26 | @staticmethod 27 | def clear_uart_buffer(uart): 28 | """ 29 | Clears the UART buffer to avoid leftover data affecting new commands. 30 | 31 | :param uart: The UART object whose buffer is to be cleared. 32 | """ 33 | while uart.any(): 34 | uart.read() 35 | 36 | @staticmethod 37 | def send_command(uart, command, wait_for="OK", timeout=2000): 38 | """ 39 | Sends an AT command to the SIM800 module and waits for a response. 40 | 41 | :param uart: The UART object used for communication. 42 | :param command: The AT command to send. 43 | :param wait_for: The expected response to wait for. 44 | :param timeout: The maximum time to wait for the response in milliseconds. 45 | :return: The response from the SIM800 module, or None if timeout occurs. 46 | """ 47 | SIM800Utils.clear_uart_buffer(uart) 48 | uart.write(command + '\r') 49 | return SIM800Utils.wait_for_response(uart, wait_for, timeout) 50 | 51 | --------------------------------------------------------------------------------