FUSION AUTOMATE
Published © GPL3+

Integrate Python with A9G Module to Send SMS

How to Integrate Python with A9G Module to Send Dynamic or Predefined SMS to Any Number

IntermediateProtip1 hour27
Integrate Python with A9G Module to Send SMS

Story

Read more

Schematics

connection diagram

Code

a9g_sms.py

Python
import serial
import time

def send_command(command):
    ser.write((command + '\r\n').encode())
    time.sleep(1)
    response = ser.read(ser.inWaiting()).decode()
    return response

def send_sms(phone_number, message):
    send_command('AT+CMGF=1')  # Set SMS text mode
    send_command('AT+CMGS="{}"'.format(phone_number))  # Set the phone number
    time.sleep(1)
    send_command(message + chr(26))  # Send the message and terminate with Ctrl+Z

if __name__ == "__main__":
    # Open serial connection to the A9G module
    ser = serial.Serial('COMX', 115200, timeout=1)

    # Wait for the module to initialize
    time.sleep(2)

    # Send SMS
    phone_number = input("Enter phone number (e.g. +91XXXXXXXXXX): ")
    message = input("Enter message: ")
    
    # phone_number = "+91XXXXXXXXXX"
    
    # message = """
    # Line 1
    # Line 2
    # Line 3
    # From Python
    # """

    send_sms(phone_number, message)

    # Close serial connection
    ser.close()

Credits

FUSION AUTOMATE

FUSION AUTOMATE

25 projects • 1 follower

Comments