Classic Email Notifier

Hi All

My Name is Dave and I'm a .........newbie

Ok. I am trying to set up the classic James Matthews gmail notifier.

Here is the Python code on my PC

import serial
import time
import imaplib, re

ser = serial.Serial(1)
print "Starting on " +ser.portstr;
conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)
while (True):
conn.login('user','password')
unreadCount = int(re.search("UNSEEN (\d+)", conn.status("INBOX", "(UNSEEN)")[1][0]).group(1))
ser = serial.Serial(4)
if(unreadCount > 0):
print str(unreadCount) + " new mails!"
ser.write('M')
else:
print "no mail :("
ser.write('N')
time.sleep(5)

and here is the code on the Arduino Uno:

int outPin = 2; // Output connected to digital pin 2
int mail = LOW; // Is there new mail?
int val; // Value read from the serial port

void setup()
{
pinMode(outPin, OUTPUT); // sets the digital pin as output
Serial.begin(9600);
Serial.flush();
}

void loop()
{
// Read from serial port
if (Serial.available())
{
val = Serial.read();
Serial.println(val);
if (val == 'M') mail = HIGH;
else if (val == 'N') mail = LOW;
}

// Set the status of the output pin
digitalWrite(outPin, mail);
}

Points to make:

  1. The hardware is fine. I have a LED connected to Pin 2 through a resistor. This has been tested with Blink and othe programs and works fine.
  2. The python code (with proper username and password) accesses gmail and provides the positive message "xxx emails" etc

The issue is the serial transmission of a character "M" or "N" that should trigger the LED. It simply isn't happening.

The only change to what I call the "standard" code is that I have included a line to access port 5 (that only works when I use serial.Serial(4) ). I say works in that the TX/RX LEDs on the Uno flicker - only with this this port initiated.

It seems to me that the character "M" is not being transmitted or received on the serial port.

I'm sure this is a simple issue but not for this newbie!

Help much appreciated

Regards

Dave

Maybe the M or N is being transmitted, but shortly after, something else is being transmitted.
Change to Serial.println (val, HEX);

Why do you have both:

ser = serial.Serial(1)

and

ser = serial.Serial(4)

?
When you call this statement, is it causing the Uno to reset? Keep in mind everytime the arduino's serial port is open or closed, it resets.

Maybe you should call ser = serial.Serial(4) before the while statement only.

Thanks to both for your helpful replies

James:

"Why do you have both:

ser = serial.Serial(1)

and

ser = serial.Serial(4)

?
When you call this statement, is it causing the Uno to reset? Keep in mind everytime the arduino's serial port is open or closed, it resets.

Maybe you should call ser = serial.Serial(4) before the while statement only."

Originally when I strung it all together, the Python code was not accessing the gmail server. I assumed, wrongly, that it would not work with Port 5 (but it did work with COM2). Not realising the that the port would reset when opened or closed, I thought I had come up with a workaround. All seems to work fine now with your suggestion. So many thanks

Dave