The script below will send an SMS message the to designated recipient. Be sure to change the phone number in the script. To communicate with the GSM over serial be sure to install the serial port module.
sudo pip install pyserial
# at.py
# A basic script to send SMS with Waveshare GSM Hat
import serial
conn = serial.Serial('/dev/serial0', baudrate=9600, timeout=1.0)
conn.write('AT+CMGF=1\r\n')
result = conn.read(100)
print(result)
# Change the line below to your phone number
conn.write('AT+CMGS="+11234567890"\r\n')
result = conn.read(100)
print(result)
conn.write('Hello from Pi Zero')
result = conn.read(100)
print(result)
conn.write('\x1A\r\n')
result = conn.read(100)
print(result)
DroneKit Scripts
The following script is for testing basic communication with your autopilot hardware running PX4. In my case I'm using Pixfalcon with Pi Zero connected to the micro USB port.
# dronekit-gps.py
from __future__ import print_function
from dronekit import connect, VehicleMode
import time
# Connect to Pixfalcon's serial port
vehicle = connect('/dev/ttyACM0', wait_ready=True, baud=9600)
print(" Autopilot Firmware version: %s" % vehicle.version)
# Loop continuously and print the GPS location every second
while True:
print("Location: ", vehicle.location.global_frame)
print("Attitude: ", vehicle.attitude)
time.sleep(1)
The following script combines the AT SMS commands with GPS from DroneKit to send a Google Maps link to the designated phone number.
# dronekit_gps_to_sms.py
from dronekit import connect, VehicleMode
import time
import serial
def send(lat, lon):
conn = serial.Serial('/dev/serial0', baudrate=9600, timeout=1.0)
conn.write('AT+CMGF=1\r\n')
result = conn.read(100)
print(result)
conn.write('AT+CMGS="+11234567890"\r\n')
result = conn.read(100)
print(result)
conn.write('https://www.google.com/maps/place/' + str(lat) + ',' + str(lon))
result = conn.read(100)
print(result)
conn.write('\x1A\r\n')
result = conn.read(100)
print(result)
vehicle = connect('/dev/ttyACM0', wait_ready=True, baud=9600)
# Send an SMS message with GPS location every 10 seconds
while True:
print("Location: ", vehicle.location.global_frame)
send(vehicle.location.global_frame.lat, vehicle.location.global_frame.lon)
time.sleep(10)
Work in progress below to stream UDP to QGC....
qgc.py
from __future__ import print_function
from dronekit import connect, VehicleMode
from dronekit.mavlink import MAVConnection
vehicle = connect('/dev/ttyACM0', wait_ready=True, baud=9600)
udp_conn = MAVConnection('udpin:0.0.0.0:15667', source_system=1)
vehicle._handler.pipe(udp_conn)
udp_conn.master.mav.srcComponent = 1 # needed to make QGroundControl work!
udp_conn.start()