0

Arduino Lesson 17 updated code and info
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Arduino Lesson 17 updated code and info

by Blueshark on Wed Feb 06, 2019 12:16 am

if anyone is or has done Arduino Lesson 17, info needs to be added to the lesson...

heres the lesson...
https://learn.adafruit.com/arduino-lesson-17-email-sending-movement-detector/overview

What you need to know is that it works, theres just a few things that need a little more explanation to get it working. The code below for the Python file is up and ready, it will work; use this one instead of the lesson 17 version. The Arduino code works as is no need to modify unless you add your own code.


NOTE!!!
I'm using Ubuntu Linux 16.04 and have Python2 and Python3 installed
This code if for Python2
when calling this code from the terminal/command prompt since I have both Python versions I had to call Python2 like this to get it working "python2 movement.py" and off it goes.... if you only have Python2 installed you can just call "python movement.py" its the default...

and if your a Windows user you need to change this line of code

Code: Select all | TOGGLE FULL SIZE
   ser = serial.Serial('/dev/ttyACM0', 9600)


To whatever COM# your Arduino is using example like this...

Code: Select all | TOGGLE FULL SIZE
   ser = serial.Serial('COM3', 9600)



------------------------------------------------------------------------------------------------------------------

Use this code and name it “movement.py”

Code: Select all | TOGGLE FULL SIZE
     
import time
import serial
import smtplib
 
TO = 'receiving gmail address'
GMAIL_USER = 'another diffirent gmail address'
GMAIL_PASS = 'your password'
 
SUBJECT = 'Intrusion!!'
TEXT = 'Your PIR sensor detected movement'
 
ser = serial.Serial('/dev/ttyACM0', 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:
    message = ser.readline()
    print(message)
    if message[0] == 'M' :
        send_email()
    time.sleep(0.5) 



Just add your receiving gmail address, and a diffirent gmail address and place its password in the password section.
(This code only works with gmail addresses....)

Just one more thing you need to do before you get going!!!!

The sending gmail that includes the password in the code needs to let its security down...... Google prevents low security devices from using it/or accessing its gmail service.

To get over this security problem you need to go to the gmail user settings

------ go to security tab/section

------- look for the " Less secure app access " window; its set to OFF.... you need to set it to ON

and thats it.... your cooking....

This code is interesting because you can add more sensors to this...

Enjoy, have fun...

Cheers..

Blueshark
 
Posts: 101
Joined: Thu Dec 21, 2017 7:19 pm

Re: Arduino Lesson 17 updated code and info

by adafruit_support_bill on Wed Feb 06, 2019 6:44 am

Thanks for the feedback. I will forward it to the authors of the guide.

adafruit_support_bill
 
Posts: 71816
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino Lesson 17 updated code and info

by Blueshark on Wed Feb 06, 2019 9:35 pm

adafruit_support_bill …… can you include this Arduino code

This Arduino code will work on the Ultra Sonic Distance Sensor instead of the PIR Sensor.

https://www.adafruit.com/product/4007


To read the distance in Inches (its the default) or Centimeters, just comment out the code where I point it out; don't forget to comment it back or else you will send a bunch of data to the serial port for the Python code to look at.

Code: Select all | TOGGLE FULL SIZE
     
const int pingPin = 7;  // Trigger Pin of Ultrasonic Sensor
const int echoPin = 6;  // Echo Pin of Ultrasonic Sensor
// Using Ultrasonic Distance Sensor Model "HC-SR04"
 
int minSecsBetweenEmails = 60; // 1 min
 
long lastSend = -minSecsBetweenEmails * 1000l;
 
void setup()
{
 
  Serial.begin(9600);  // Starting Serial Terminal
}
 
void loop()
{
  //----------- variables ----------------------------------
  long now = millis();
  long duration, inches, cm;
  //--------------------------------------------------------

  //--- ultrasonic distance sensor code --------------------
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
//----------------------------------------------------------

  //uncomment these lines to see distance in Arduino Serial Monitor
  // then comment them back so you wont send too much data to the serial port
  // to use with the python code
 
  //Serial.print(inches);
  //Serial.print("in");
  //Serial.print(cm);
  //Serial.print("cm");
  //Serial.println();
 
 //---------------------------------------------------------

 
  if(inches<=30 && inches>=1)// distance in which the Distance Sensor is set for alarm.
  // if you want to use Centimeters replace "inches" to "cm" in the "if" statement
  {
    if (now > (lastSend + minSecsBetweenEmails * 1000l))
    {
      Serial.println("MOVEMENT");
      lastSend = now;
    }
    else
    {
      Serial.println("Too soon");
    }
  }
  delay(100); // original delay was 500
}


//---------- Ultrasonic Distance Sensor Math conversions ---

long microsecondsToInches(long microseconds){
 
  // 73.746 microseconds per "INCH" (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the soundwave, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
 
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds){
 
  // The speed of sound is 340 m/s or 29 microseconds per "CENTEMETER".
  // The sound travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
 
  return microseconds / 29 / 2;
}   



Cheers

Blueshark
 
Posts: 101
Joined: Thu Dec 21, 2017 7:19 pm

Please be positive and constructive with your questions and comments.