Python Forum
Please help with code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please help with code
#1
My python code is meant to send me an email with the string send from Arduino through the serial. Everything is working fine (Emails are sent when string is present in the serial port) except that, emails are sent continuously, instead of once. I receiving about 100s of emails with the same information. I need only one. I don't know how to fix it. Please help.

import time
import serial
import smtplib
 
TO = '[email protected]'
GMAIL_USER = '[email protected]'
GMAIL_PASS = 'password'
SUBJECT = 'ALERT!!!'  
ser = serial.Serial('COM4', 9600)
message = ser.readline()
TEXT = message
def send_email():
    print("Sending Email")
    server = smtplib.SMTP("smtp.gmail.com",587)
    server.ehlo()
    server.starttls()
    server.ehlo
    server.login(GMAIL_USER, GMAIL_PASS)
    header = 'To:' + TO + '\n' + 'From: ' + GMAIL_USER
    header = header + '\n' + 'Subject:' + SUBJECT + '\n'
    print header
    msg = header + '\n' + TEXT + ' \n\n'
    server.sendmail(GMAIL_USER, TO, msg)
    server.close()
    
while True:
    print(message)
    send_email()
    time.sleep(0.5)
Reply
#2
What else do you expect from such an infinite loop?

while True:
   print(message)
   send_email()
   time.sleep(0.5)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
How can I fix it? Im real begginer. Just started learning python about few days ago. Any help will be appreciated.
Reply
#4
You can't learn much by running code that you don't understand how it works / what it does. Fort starters, don't use a loop for something you don't want repeated.
Reply
#5
Thanks for yours suggestions guys. Really appreciate. I managed to fix it by adding "if" inside the loop and now Im getting as required only one email. However, I've got a different problem now. Data presented in serial monitor every time is different but my email message content is always with the first value which was recorded in the serial monitor. How I can reset the message content after email was sent?
Reply
#6
Getting the message should be in the loop body....
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#7
Emails are not being sent at all when I put "message = ser.readline()" in the loop body like that:
while True:
message = ser.readline()
print(message)
if message[0] == 'C' :
send_email()
message.reset
time.sleep(0.5)
Reply
#8
Stop using global variables.  If you want to send an email with a specific message, then pass that message as a parameter to send_message().  Because right now, you're changing message, but your send_message() never uses that variable at all.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020