I am trying to make a notification system using an ultrasonic sensor with an Arduino to detect when there is something in the way of a door, once the obstruction is detected, I have a timer start, once the timer goes off data is sent to the Raspberry Pi. I have figured out how to get the Raspberry Pi to read the data through the serial port when it is sent from the Arduino and email a notification, but it requires me to install py.serial
in the terminal and initialize the reading on Python to begin with. I don't want the system to be dependent on a monitor or a person to be constantly typing in the command.
Is there any way to automate this process?
This is what I have for the Arduino:
int vcc = 2;
int trig = 3;
int echo = 4;
int gnd = 5;
long previouscm = 0; // will store last time LED was updated
long previousinches= 0; // will store last time LED was updated
long interval = 25; // distance of door
unsigned long previousMillis = 0;
void setup() {
//initialize USS
pinMode (vcc,OUTPUT);
pinMode (gnd,OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
//constantly checking distance
//act based on this change
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
unsigned long currentMillis = millis();
long duration, inches, cm, currentinch, currentcm; //variables
long timespan;
//setup USS to read
pinMode(trig, OUTPUT);
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(5);
digitalWrite(trig, LOW);
pinMode(echo, INPUT);
duration = pulseIn(echo, HIGH);
// convert the time into a distance
inches = duration / 74 / 2;
cm = duration / 29 / 2;
//previouscm=0 interval= 25 cm(to door)
if (cm - previouscm < interval) {
previousMillis= currentMillis - previousMillis;
}
delay(300000);
if (previousMillis=60000){
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
}
}
and this is what I have for the Raspberry Pi:
import serial
ser=serial.Serial('/dev/ttyACM0',9600)
while 1:
data = ser.readline()[:-2]
if data:
import smtplib
content = ("Blood has been stored for 72 hours.")
mail = smtplib.SMTP("smtp.gmail.com",587)
mail.ehlo()
mail.starttls()
mail.login('email','pass')
mail.sendmail('email','toemail',content)
mail.close()
print("Sent")
To install pyserial in the terminal I always have to input:
cd pyserial-3.0.1
sudo python setup.py install