Reputation: 11
Hello all I'm trying to send a very basic email to myself using my raspberry pi and a sim800l module... however, it cannot send a basic message to myself. I've used it with an arduino so I know that the module is okay but cannot seem to get the rest working. Please help
import RPi.GPIO as GPIO
import smtplib, datetime, os, time, serial
# EMAIL - - - - -
USERNAME = '[email protected]' # Username for authentication
PASSWORD = 'pass' # Password for authentication
SMTP_SERVER = "smtp.gmail.com" # URL of SMTP server
SSL_PORT = 465 #message sumbmission over TLS protocal
FROM = "[email protected]" # Name shown as sender
TO = '[email protected]' # Mail address of the recipient
NAME = 'patrick bateman'
# SIM800L - - - - -
cmd=''
ser = serial.Serial('/dev/serial0', 9600, timeout=2)
ser.reset_input_buffer()
def setup():
GPIO.setmode(GPIO.BOARD)
# GPIO.setup(P_BUTTON, GPIO.IN, GPIO.PUD_UP)
def sendCommand(command):
smd= command
smd=smd+'\r\n'
smds=smd.encode('ascii')
ser.write(smds)
time.sleep(1)
line = ser.read(80).decode('ascii').rstrip()
print(line)
def sendMail(subject, text, img = None):
print("initilizing")
def checkSystemIsFullyOpperational():
sendCommand('AT') #check that you can get a response form SIM module
sendCommand('AT+CMEE=2')
sendCommand('AT+CPIN?') # checks
sendCommand('AT+CSQ') # checks signal strength. col 1: 31 is bet, 0 worst.
# 99 means you don't have a connection.
sendCommand('AT+CGREG?') # Checks if device is regestered to a network. Want "0,1"
checkSystemIsFullyOpperational()
sendCommand('AT')
sendCommand('AT+SAPBR=3,1,"Contype","GPRS"') # Configure bearer profile 1
sendCommand('AT+EMAILCID=1') # Set paramaters of Email
#sendCommand('AT+EMAILTO=30') # Timeout for server response (defult 30 secs)
sendCommand('AT+SMTPSRV="{}","{}"'.format(SMTP_SERVER, SSL_PORT)) # Set SMTP server address and port
sendCommand('AT+SMTPAUTH=1,"{}",{}'.format(USERNAME,PASSWORD)) # Set username and password.
sendCommand('AT+SMTPFROM="{}",{}'.format(USERNAME,FROM)) # Set sender address and name
sendCommand('AT+SMTPRCPT=0,0,"{}",{}'.format(TO,NAME)) # Set the recipients name
sendCommand('AT+SMTPSUB="{}"'.format("This is sent from the rapsberry pi")) # Body of text
sendCommand('AT+SMTPSEND')
Upvotes: 1
Views: 672
Reputation: 11
So I've edited my code and tried using /22 instead of double double quotes. I've read though chapter 5 of V.250 however I wonder, why can't you use single quotes for declaring that an item is a string to be decoded (with objects inside the command being double quoted). Currently neither method works, however my code states that I am successfully sending now?? (it's not, aditionally and this is a minor detail, I cannot see the text I am including in the body of the email.) Thank you for your support - Forester
#- - - SHELL OUTPUT - - -
>>> %Run sendEmailsPiATcommands.py
AT
OK
OKAY
AT+CMEE=2
OK
OKAY
AT+CPIN?
+CPIN: READY
OK
OKAY
AT+CSQ
+CSQ: 28,0
OK
OKAY
AT+CGREG?
+CGREG: 0,1
OK
OKAY
AT+EMAILCID=1
OK
OKAY
AT+EMAILTO=30
OK
OKAY
AT+SMTPSRV="smtp.gmail.com",465
OK
OKAY
AT+SMTPAUTH=1,"[email protected]","pass"
OK
OKAY
AT+SMTPFROM="[email protected]","me"
OK
OKAY
AT+SMTPRCPT=0,0,"[email protected]","greenEggsAndHam"
OK
OKAY
AT+SMTPSUB="TEST"
OK
OKAY
AT+SMTPBODY=5
DOWNLOAD
I've been reached
OK
OKAY
AT+SMTPFILE=1,"yesIAm.txt",0
OK
OKAY
AT+SMTPSEND
OK
OKAY
import RPi.GPIO as GPIO
import smtplib, datetime, os, time, serial
# EMAIL - - - - -
USERNAME = "[email protected]" # Username for authentication
PASSWORD = "pass" # Password for authentication
SMTP_SERVER = "smtp.gmail.com" # URL of SMTP server
SSL_PORT = 465 #message sumbmission over TLS protocal
FROM = "me" # Name shown as sender
TO = "[email protected]" # Mail address of the recipient
NAME = "greenEggsAndHam"
FILENAME = "yesIAm.txt"
message = "hello"
# SIM800L - - - - -
cmd=""
ser = serial.Serial("/dev/serial0", 9600, timeout=5)
ser.reset_input_buffer()
def setup():
GPIO.setmode(GPIO.BOARD)
# GPIO.setup(P_BUTTON, GPIO.IN, GPIO.PUD_UP)
def sendCommand(command):
smd= command
smd=smd+"\r"
smds=smd.encode("ascii")
ser.write(smds)
escape = "wait"
line = ser.readline(500).decode("ascii").rstrip()
print(line)
while escape != "nextCommand":
if line.find("OK") >= 0:
print("OKAY")
escape = "nextCommand"
elif line.find("ERROR") >= 0:
print("error has occoured")
escape = "nextCommand"
elif line.find("DOWNLOAD") >= 0:
print("I've been reached")
time.sleep(0.200)
sendCommand('{}'.format(message))
escape = "nextCommand"
elif line.find("61") >= 0:
print("network error")
escape = "nextCommand"
else:
time.sleep(0.4) # 0.125 seconds can be how long it takes to reset memory buffers so we are waiting longer than that
line = ser.readline(500).decode("ascii").rstrip()
print(line)
ser.reset_input_buffer()
def checkSystemIsFullyOpperational():
sendCommand("AT") #check that you can get a response form SIM module
sendCommand("AT+CMEE=2") #Enable result code and use verbose values (will expand upon errors)
sendCommand("AT+CPIN?") # checks which pin is reqiured
sendCommand("AT+CSQ") # checks signal strength. col 1: 31 is bet, 0 worst.
# 99 means you don"t have a connection.
sendCommand("AT+CGREG?") # Checks if device is regestered to a network. Want "0,1"
checkSystemIsFullyOpperational()
#sendCommand("AT+SAPBR=3,1,"Contype","GPRS"") # Configure bearer profile 1
sendCommand("AT+EMAILCID=1") # Set paramaters of Email
sendCommand("AT+EMAILTO=30") # Timeout for server response (defult 30 secs)
sendCommand('AT+SMTPSRV="{}",{}'.format(SMTP_SERVER, SSL_PORT)) # Set SMTP server address and port
sendCommand('AT+SMTPAUTH=1,"{}","{}"'.format(USERNAME,PASSWORD)) # Set username and password.
sendCommand('AT+SMTPFROM="{}","{}"'.format(USERNAME,FROM)) # Set sender address and name
sendCommand('AT+SMTPRCPT=0,0,"{}","{}"'.format(TO,NAME)) # Set the recipients name
sendCommand('AT+SMTPSUB="TEST"') # Email title
lengthOfMessage = len(message)
sendCommand('AT+SMTPBODY={}'.format(str(lengthOfMessage))) #shouldn't be quoted
sendCommand('AT+SMTPFILE=1,"{}",0'.format(FILENAME))
sendCommand('AT+SMTPSEND')
Upvotes: 0
Reputation: 28188
Download the V.250 specification and read all of chapter 5. That will teach you various fundamental, basic AT command related things like for instance:
\r
only, not \r\n
.\"
which is common in other contexts, but as \22
, e.g. AT+SOMECMD="message=\22Hello world\22"
.and many other very important tings related to AT commands. Don't give up if you don't grasp everything at the beginning, but the specification is an absolute must-read for anyone that handles AT commands.
Just to emphasis how important that document is: Even after working with implementing AT commands in mobile phones in Ericsson for over a decade I and my colleagues still consulted that document regularly!
The second point above is not done correctly in the code a couple of places, for instance sendCommand('AT+SMTPRCPT=0,0,"{}",{}'.format(TO,NAME))
which should be sendCommand('AT+SMTPRCPT=0,0,"{}","{}"'.format(TO,NAME))
. All string parameters that lack double quotes must be fixed.
Then find a large A3 sheet of paper, a red pen and write 1000 times
I will never use
time.sleep(1)
as a substitute for reading and parsing responses from a modem.I will never use
time.sleep(1)
as a substitute for reading and parsing responses from a modem.I will never use
time.sleep(1)
as a substitute for reading and parsing responses from a modem.I will never use
time.sleep(1)
as a substitute for reading and parsing responses from a modem....
When sending AT commands to a modem, you MUST read and parse everything it sends back to you.
First of all, for the commands that does not encapsulate string parameters with double quotes the modem will return ERROR
(or +CME ERROR
). Since the code completely ignores the response it gets back from the modem this goes undetected.
And secondly you never know how long is a "sufficient long" time to wait before sending the next command. Some AT commands might take several seconds to execute! And if you send data before the modem has sent the Final Result Code, the command line is aborted (see chapter "5.6.1 Aborting commands" for details).
Attempting to use sleep is as useful as kicking dogs that stand in your way in order to get them to move. Yes it might actually work occasionally, but at some point you will guarantied be sorry for taking that approach...
Upvotes: 0