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
-
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– Mark Smith2016年03月29日 16:18:25 +00:00Commented Mar 29, 2016 at 16:18
-
I put all my info in the question thank you so muchChristina M– Christina M2016年03月29日 16:41:27 +00:00Commented 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– Ghanima ♦2016年03月29日 16:46:20 +00:00Commented Mar 29, 2016 at 16:46
-
Please check this raspberrypi.stackexchange.com/questions/8734/…Ghanima– Ghanima ♦2016年03月29日 16:54:12 +00:00Commented 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– joan2016年03月29日 17:35:50 +00:00Commented 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
.
Explore related questions
See similar questions with these tags.