Python Forum

Full Version: Insert a variable in a Python > Cellular AT command
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am an ElectronicsNut that is venturing into foreign coding territory. I have a Raspberry Pi that I have connected to a FONA 3G cellular module via port ttyUSB0. The attached code is up and running, but I would like to modify the line of code in Python script with a variable that would be used to specify a specific ALERT. The current test message that is sent to my cell phone in the code is "ALERT MESSAGE" (for testing purposes), which I would like to make a variable called "Alert Message". Based on the coding that I write, the Alert Messages could be:
Fridge Low Temperature Alert
Freezer Low Temperature Alert
Low Water Pressure Alert
No AC voltage
.....
I just need to figure out how to insert the variable in that line of code (see below).
I left the rcv and print lines in for testing purposes.
Thanks in advance for any assistance

import serial
import RPi.GPIO as GPIO
import os, time


port = serial.Serial("/dev/ttyUSB0", baudrate=115200, timeout=1)
rcv = port.read(10)
print rcv

port.write('AT'+'\r\n')
rcv = port.read(10)
print rcv

port.write('ATE0'+'\r\n')
rcv = port.read(10)
print rcv 

port.write('AT+CMGF=1'+'\r\n')
rcv = port.read(10)
print rcv

port.write('AT+CMGS="xxxxxxxxxx" '+'\r\n')
rcv = port.read(10)
print rcv

[b]port.write('ALERT MESSAGE'+'\r\n')[/b]
rcv = port.read(10)
print rcv

port.write("\x1A")
rcv = port.read(10)
print rcv

port.close()
hello your question isn't clear to me. Does your code output all the write messages to your phone? How are you checking temperature or water pressure?
In this example POWER is set to True:
if POWER == False:
    format_text= 'Alert Message %s'%('No A/C')
    port.write(format_text+'\r\n')
    rcv = port.read(10)
    print rcv