I'm trying to make the arduino send a word (or string) over the serial port and interpret that word with a python script. The python script will execute different functions based on what word the arduino sends over the serial port. I'm having trouble getting the python script to interpret the different words. This is for an alarm system. I have a door sensor hooked up to a digital pin and when it is high it sends the word "alert" over the serial port, when it is low it sends nothing. I want to add in different "alerts" like alert1 and alert2 and the python program will execute different functions based on the different alerts.
Arduino code (note: this is not my actual code, but the code that matters):
int inputPin = 2;
int val = 0;
void setup() {
pinMode(inputPin, INPUT);
Serial.begin(9600);
}
void loop() {
val = digitalRead(inputPin);
if (val == HIGH) {
Serial.println("alert"); // Serial.print("alert"); doesn't work with python at all for me
delay(1000);
}
}
And here is the python code that receives the alert (note: only the code that matters):
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600)
while 1:
result = ser.readline()
if result: # This works but not if i want to do different stuff for different alerts
print result
I know there is something stupid I'm not doing correctly, but I cant figure it out.. I have tried:
if result == "alert"
if result == "alert\n"
I've tried changing this in the arduino code:
Serial.println("alert"); to Serial.print("alert");
as well and no luck...
I've got a parallax pir sensor and rfid reader that I'm dying to hook up to the arduino, but I have to get the python code I've written to execute properly on these events... The python code also emails and text messages me when the alerts happen. Any suggestions would be greatly appreciated. I will post my whole project with schematics and source code once I am done.
Please help, and thanks in advance!