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')
Step 1
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:
\ronly, 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!
Step 2
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 besendCommand('AT+SMTPRCPT=0,0,"{}","{}"'.format(TO,NAME)). All string parameters that lack double quotes must be fixed.Step 3
Then find a large A3 sheet of paper, a red pen and write 1000 times
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...