Community
cancel
Showing results for 
Search instead for 
Did you mean: 
TBhon
New Contributor I
1,203 Views

Running Python Script From Arduino IDE doesn't work for some cases (smtplib example)...

Here is a python script named gmail.py I've made using smtplib as part of a project I'm working on:

import smtplib t = open('/home/root/testLog.txt', 'a+') # writes to a log file to confirm that the script has been started t.write("start") t.close() fromaddr = 't@gmail.com' toaddrs = 't@gmail.com' file = open('message.txt', 'r+').read() # Uncomment for debugging # print file # print file.split('\n') username = 't@gmail.com' password = '***' server = smtplib.SMTP('smtp.gmail.com:587') server.ehlo() server.starttls() server.login(username,password) server.sendmail(fromaddr, toaddrs, file) server.quit()

The script basically opens a text file name message.txt and sends me an email with its contents.

When i run the code with python gmail.py, everyone works fine and after a few seconds I receive the email just like I wanted. However, when I run the code from the arduino IDE using the system command it doesn't work, i.e I don't get any email!

void setup() { system(" python /home/root/gmail.py"); delay(10000); } void loop() { }

The strange thing is that if I try to run any other simple python script, it works perfectly fine; I've seen/message/277722 this post and have tried the code that writes a "hello" into a log text file, and it works.

IN FACT, when I run the above Arduino sketch, even though I dont get an email, i know the python code has been run because there is a new entry in the logfile (see the 3rd line of my python code). Maybe this problem is beacuse of the delay created while the python code is sending an email, but I can;t say.

I'd be greatful to anyone who can solve the problem!

Details:

1. Edison image version -159

2. Windows 7 professional with Arduino IDE 1.6.5

Tags (1)
7 Replies
Pablo_M_Intel
Employee
163 Views

Hi DangerousTim,

I will test your code using using system calls from Arduino. In the meantime, why don't you try adding delays between the server lines? Just to make sure that your code has enough time to perform correctly.

Regards,

PabloM_Intel

TBhon
New Contributor I
163 Views

Where should I add a delay? In the python code or the Arduino code? And where exactly? Server client stuff is very new to me,

TBhon
New Contributor I
163 Views

Hi Pablo,

any results yet? I've tried adding delays in the Arduino sketch to no avail

Thanks.

Pablo_M_Intel
Employee
163 Views

Hi DangerousTim,

I've running some tests but I'm getting the same behavior. The code runs well if run directly with python, but once you try to call it using system calls in the Arduino IDE nothing happens. I tested your code and some other email code from this GitHub link https://github.com/MakersTeam/Edison/tree/master/Python-Examples/Email https://github.com/MakersTeam/Edison/tree/master/Python-Examples/Email, maybe you'd like to take a look.

Regarding your first question about the delays, what I meant was to add delays between server calls, so the code had enough time (if that was the case) to run correctly. But that didn't solve the issue. I'll try some other things and I'll let you know soon.

Regards,

PabloM_Intel

TBhon
New Contributor I
163 Views

Here's my updated python script with delays all over the place:

import smtplib

import time

t = open('/home/root/testLog.txt', 'a+')

t.write("start")

t.close()

fromaddr = mailto:'tpb2k99@gmail.com 'tpb2k99@gmail.com'

toaddrs = mailto:'tpb2k99@gmail.com 'tpb2k99@gmail.com'

file = open('message.txt', 'r+').read()

# Uncomment for debugging

# print file

# print file.split('\n')

username = mailto:'tpb2k99@gmail.com 'tpb2k99@gmail.com'

password = 'heyjude3141'

server = smtplib.SMTP('smtp.gmail.com:587')

server.ehlo()

time.sleep(1)

server.starttls()

time.sleep(2)

server.login(username,password)

time.sleep(1)

server.sendmail(fromaddr, toaddrs, file)

time.sleep(10)

server.quit()

Doesn't work.

What could be the problem here? Maybe after the system command is executed by the arduino code, the process initiated by that command immediately comes to a halt, unlike when running the command directly on the command line.

Anyways I'm looking for other ways, though i can't find many. Sending mail using Arduino code is the only option. I've downlobaded this library but can't seem to figure out how it works (documentation is poor): https://github.com/gregington/SMTPClient gregington/SMTPClient · GitHub Any help with that? And maybe any other arduino code for sending emails via gmail?

Pablo_M_Intel
Employee
163 Views

Hi DangerousTim,

Adding delays doesn't seem to make any difference. We also tried this a week ago.

If you can only use Arduino for this, you can also try with https://github.com/bibi21000/arduino-fullip FullIP, another SMTP library for Arduino. It's similar to SMTPClient. I would suggest you to take a look at this library, in the meantime, we will check the SMTPClient library and will try to help you with that one.

Regards,

PabloM_Intel

Matthias_H_Intel
Employee
163 Views

is there a need to run on Arduino? you surely could run an SMTP server ... likewise directly on Linux.

Otherwise you might also consider having a listening process running on Linux which is triggered from Arduino (see e.g. https://software.intel.com/en-us/blogs/2014/09/22/efficient-communication-between-arduino-and-linux-... IoT - Efficient communication between Arduino* and Linux native processes | Intel® Developer Zone)

Reply