smtplib disconnects after being idle for some time.

I am new to the beaglebone but not to python I have worked a lot with it on the raspberry pi but I am having trouble with this script.It is supposed, at startup to make an lcd Display Initializing… connect to the gmail smtp server and print “Ready” on the lcd and when it detects that I have pressed a button it will send an email to my address saying “test” the script works fine but after being idle for a couple of hours it crashes and returns an error.To control the GPIO I am using adafruit’s BBIO library for the lcd i am using adafruit’s Char LCD library and for emails i am using smtplib.Here is the code:

import time
import Adafruit_CharLCD as LCD
import smtplib
import Adafruit_BBIO.GPIO as GPIO
lcd_rs = ‘P9_11’
lcd_en = ‘P9_12’
lcd_d4 = ‘P9_13’
lcd_d5 = ‘P9_14’
lcd_d6 = ‘P9_15’
lcd_d7 = ‘P9_16’
lcd_backlight = ‘P9_22’

lcd_columns = 16
lcd_rows = 2

lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight)

GPIO.setup(“P9_21”, GPIO.IN) #Declares the button pin as an input
GPIO.add_event_detect(“P9_21”, GPIO.RISING)#This event detect checks if the button has been pressed
lcd.set_cursor(0, 0)#This whole thing from here to lcd.clear() does the Initializing… animation
lcd.message(‘Initializing’)
time.sleep(0.2)
lcd.message(’.’)
time.sleep(0.2)
lcd.message(’.’)
time.sleep(0.2)
lcd.message(’.’)
time.sleep(0.2)
lcd.clear()
lcd.message(‘Initializing’)
time.sleep(0.2)
lcd.message(’.’)
time.sleep(0.2)
lcd.message(’.’)
time.sleep(0.2)
lcd.message(’.’)
time.sleep(0.2)
lcd.clear()
lcd.message(‘Initializing’)
time.sleep(0.2)
lcd.message(’.’)
time.sleep(0.2)
lcd.message(’.’)
time.sleep(0.2)
lcd.message(’.’)
time.sleep(0.2)
lcd.clear()

fromaddr = ‘’ #The address which will send the email
toaddrs = ‘’ #the recipient of the email
msg = ‘Test’ #The email’s text
username = ‘’ #Your gmail username
password = ‘’ #your gmail password
server = smtplib.SMTP(‘smtp.gmail.com:587’) #gmail’s smtp server
server.ehlo() #I really don’t know what this line does
server.starttls() #This initializes the connection with the server
server.login(username,password) #This logs into the server

while True:

lcd.set_cursor(0, 0)
lcd.message(’ Ready’)
lcd.set_backlight(1) #turns on the lcd’s backlight

if GPIO.event_detected(“P9_21”): #This detects if the button has been pressed
lcd.clear() #This from here to lcd.clear() does the Sending mail… animation
lcd.set_cursor(0, 0)
lcd.message(‘Sending mail’)
time.sleep(0.2)
lcd.message(’.’)
time.sleep(0.2)
lcd.message(’.’)
time.sleep(0.2)
lcd.message(’.’)
time.sleep(0.2)
lcd.clear()
lcd.message(‘Sending mail’)
time.sleep(0.2)
lcd.message(’.’)
time.sleep(0.2)
lcd.message(’.’)
time.sleep(0.2)
lcd.message(’.’)
time.sleep(0.2)
lcd.clear()
server.sendmail(fromaddr, toaddrs, msg) #This command send’s the email
lcd.message(’ Done!’) #Prints “done” on the lcd’s screen for 2 seconds and returns back to “ready”
time.sleep(2)
lcd.clear()

Also here is the link for a video of my setup:
https://www.youtube.com/watch?v=asHR8ebxG0E

I am hoping to get help as soon as possible and thank you for trying to help me.

P.S I am not a native english speaker so there might be little mistakes. :slight_smile: