Guest User

Untitled

a guest
Oct 18th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. def send_email(recipient, subject, body):
  2. import smtplib
  3. user='XXXX@gmail.com'
  4. pwd='XXXX'
  5. FROM = user
  6. TO = recipient if isinstance(recipient, list) else [recipient]
  7. SUBJECT = subject
  8. TEXT = body
  9.  
  10. # Prepare actual message
  11. message = """From: %s\nTo: %s\nSubject: %s\n\n%s
  12. """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
  13. try:
  14. server = smtplib.SMTP("smtp.gmail.com", 587)
  15. server.ehlo()
  16. server.starttls()
  17. server.login(user, pwd)
  18. server.sendmail(FROM, TO, message)
  19. server.close()
  20. print ('successfully sent the mail')
  21. except:
  22. print ("failed to send mail")
  23.  
  24. import datetime
  25. def dateS():
  26. return datetime.datetime.now().strftime('%Y-%m-%d')
  27. def timeS():
  28. return datetime.datetime.now().strftime('%H:%M:%S')
  29.  
  30. import serial, time,os
  31. #arduino = os.popen("sudo aimsadmin /dev/ttyAMA0", "r") # send terminal commande with root permission and save the resulte in "arduino"
  32. arduino = serial.Serial('/dev/ttyACM0', 9600, timeout=.1)
  33. print("Port is open")
  34. time.sleep(1) #give the connection a second to settle
  35.  
  36.  
  37. while True:
  38. data = arduino.readline()
  39. if data:
  40. if data==b'Alert!!!\r\n':
  41. print("Alert!!!")
  42. else:
  43. if data==b'Used\r\n':
  44. print('Generator on')
  45. else:
  46. print (float(data)) #strip out the new lines for now
  47. send_email('XXXXXXX@gmail.com','Fueling Operation','Prototype'+'\n'+float(data)+'\n'+dateS()+'\n'+timeS())
  48. # (better to do .read() in the long run for this reason
  49. time.sleep(2)
  50. else:
  51. print("Empty")
  52. time.sleep(2)
Add Comment
Please, Sign In to add comment