Arduino and Python - Weird Serial Issue

I'm getting a strange phenomenon when I try to communicate with Python via my Arduino over the serial link. It outputs "b'" before the desired phrases. Those being "Too Soon" and "Movement". I'm simply trying to send an email when motion is detected via the Arduino and a PIR sensor.

I'm working from the following tutorial (It's a PDF by the way): https://learn.adafruit.com/arduino-lesson-17-email-sending-movement-detector/python-code

Here is a screenshot of what is happening in Python:

And the Arduino code:

int calibrationTime = 30;  
int pirPin = 7;
int minSecsBetweenEmails = 60; // 1 min
long lastSend = -minSecsBetweenEmails * 1000l;

void setup() 
{
  pinMode(pirPin, INPUT);
  Serial.begin(9600); 
}

void loop() 

  {
  long now = millis();
  if (digitalRead(pirPin) == HIGH) {
  if (now > (lastSend + minSecsBetweenEmails * 1000l)) 
  {
  Serial.println("MOVEMENT");
    lastSend = now; 
  }
  else
    {
    Serial.println("Too soon");
    } 
  }
delay(1000); 
}

Python code:

import time
import serial
import smtplib

TO = '***'
GMAIL_USER = '***'
GMAIL_PASS = '***'

SUBJECT = 'Intrusion!'
TEXT = 'Your PIR sensor detected something'


ser = serial.Serial('/dev/tty.usbmodem1421',9600)


def send_email():
    print("Sending Email")
    smtpserver = smtplib.SMTP("smtp.gmail.com",587)
    smtpserver.ehlo()
    smtpserver.starttls()
    smtpserver.ehlo
    smtpserver.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'
    smtpserver.sendmail(GMAIL_USER, TO, msg)
    smtpserver.close()

while True:
    try:
        message = ser.readline()
        print(message)
        if message[0] == 'M':
            send_email()
        time.sleep(0.5)
    except:
        print("")

Any ideas?

I really appreciate any insight you may have!

I should add that I'd like to be able to trigger the email by simply pushing a button on my arduino, but I don't want to use an ethernet shield. If you know how I should approach that bit of arduino code I'd also appreciate your thoughts.

Here is a screenshot of what is happening in Python:

Do you really expect anyone to be able to read that? There is nothing in the Arduino code that outputs a 'b', so the problem has to be with the way that you are dealing with the Arduino output in python.

You're totally right. My apologies. Here is a better one:

And the problem:

Again, my apologies. Thanks for the help in any case!

This demo illustrates communicating with an Arduino using Python

The data is prefixed by < and followed by > so that the Arduino and the PC can recognize the start and end of the data.

…R

Thanks Robin.

So where the data is received by Python I should have < followed by >? Could you tell me exactly where? I wish I were better at this. My apologies for the stupid questions.

Here is a better one:

No, it isn't. We can't copy and paste you code from a picture. No more pictures. Paste your code and your output as text!

bcbr:
So where the data is received by Python I should have < followed by >? Could you tell me exactly where? I wish I were better at this. My apologies for the stupid questions.

I hope that should be clear in the Python code in my link. If there is some particular part of that code you don’t understand let me know and I will try to explain in more detail.

…R