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:

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);
digitalWrite(trig, HIGH);
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;
  if (previousMillis=60000){
    Serial.print("in, ");

and this is what I have for the Raspberry Pi:

import serial

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)







To install pyserial in the terminal I always have to input:

cd pyserial-3.0.1
sudo python setup.py install
  • If you edit your question to include what you have to type, I'll try to indicate how to convert it to a command to run at boot.
    – Mark Smith
    Mar 29, 2016 at 16:18
  • I put all my info in the question thank you so much Mar 29, 2016 at 16:41
  • Please consider investigating in the direction pointed to by Mark's answer. The details of the script as inserted by the last edit will probably not help any further.
    – Ghanima
    Mar 29, 2016 at 16:46
  • Please check this raspberrypi.stackexchange.com/questions/8734/…
    – Ghanima
    Mar 29, 2016 at 16:54
  • Are you installing this on multiple Pis? You should only need to run the command "cd pyserial-3.0.1; sudo python setup.py install" once.
    – joan
    Mar 29, 2016 at 17:35

1 Answer 1


I think your question simplifies to "how do I run a command automatically at startup?"

The answer to that is to put the command into a file such as /etc/rc.local.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.