Hello everyone,
I've got a few questions regarding the feasibility of controlling a servo over the web using serial communication in conjunction with a momentary push button. Let me explain a bit more so that you all can understand my objective.
I would like to attach a servo to my door lock at home with a momentary push button located next to the door. I want to be able to lock or unlock my door from my web server hosted at home. Finally, I want to be able to push the momentary button to either lock the door if it's currently unlocked, or lock the door if it's currently locked.
So far I have been able to lock and unlock the door over the web. I have Arduino send over the servo's position to my processing script, and the processing script updates a text file with either the locked or unlocked position based on the servo's position. Php reads and the text file as either locked or unlocked and displays the position on my website. If the door is locked, then the only button that will update the text file is the UNLOCK button for example. The processing script is continuously reading the text file for lock and unlock commands coming from the web server, and sends it back to the Arduino.
Problem:
I can control the servo either using the web interface or the push button, not both. I can't seem to properly execute my code so that the button and the web buttons work. I've looked all over for a similar project online as a reference, but I can't seem to find anything.
I am not worried about the server side of things, just transferring data between the Arduino and the processing script. The code is a bit different from my description above, and I'm sure that I've made lots of mistakes; I wrote it up before bed last night! I am using an LED rather than the servo in my code below for debugging purposes. Feedback, tips, and solutions are all welcome. ![]()
/ARDUINO---------------------------------------------/
int button_pin = 2;
int led = 13;
void setup() {
pinMode( button_pin, INPUT);
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop() {
int button;
button = digitalRead( button_pin );
if (Serial.available() > 0) {
//int state;
char inByte = Serial.read();
if(inByte == '0'){
digitalWrite(led,LOW);
Serial.write('0');
//state = 0;
}
if(inByte == '1'){
digitalWrite(led,HIGH);
Serial.write('1');
//state = 1;
}
if(button == HIGH && inByte == '2'){
digitalWrite(led, HIGH);
Serial.write('2');
//state = 1;
}
if(button == HIGH && inByte == '3'){
digitalWrite(led, LOW);
Serial.write('3');
//state = 0;
}
Serial.flush();
}
}
/PROCESSING---------------------/
import processing.serial.*;
Serial port;
PrintWriter arduinoText;
void setup(){
port = new Serial(this, "COM3", 9600);
port.bufferUntil('\n');
}
void draw() {
String onoroff[] = loadStrings("http://localhost/arduino/LEDstate.txt");
String ardonoroff[] = loadStrings("http://localhost/arduino/arduino.txt");
byte[] inBuffer = new byte[7];
while (port.available() > 0) {
inBuffer = port.readBytes();
port.readBytes(inBuffer);
String myString = new String(inBuffer);
}
if(inBuffer[0] == '0'){
println("arduino sent LOW");
arduinoText = createWriter("C:/AppServ/www/arduino/arduino.txt"); //Same location as the ardonoroff[] above.
arduinoText.print("webUNLOCKED");
arduinoText.flush();
}
if(inBuffer[0] == '1'){
println("arduino sent HIGH");
arduinoText = createWriter("C:/AppServ/www/arduino/arduino.txt"); //Same location as the onoroff[] above.
arduinoText.print("webLOCKED");
arduinoText.flush();
}
if(inBuffer[0] == '2'){
println("arduino sent HIGH");
arduinoText = createWriter("C:/AppServ/www/arduino/arduino.txt");
arduinoText.print("ardLocked");
arduinoText.flush();
}
if(inBuffer[0] == '3'){
println("arduino sent LOW");
arduinoText = createWriter("C:/AppServ/www/arduino/arduino.txt");
arduinoText.print("ardUnlocked");
arduinoText.flush();
}
if(onoroff[0].equals("LOCKED") != true){
println("php sent UNLOCKED");
port.write('0');
}
if (onoroff[0].equals("LOCKED") == true) {
println("php sent LOCKED");
port.write('1');
}
//Here I am trying to compare the two text files. One text file arduino writes to and the other the php updates.
if(ardonoroff[0].equals("ardLocked") != true && inBuffer[0] == 2){
println("ARDUINO OVERRIDE, UNLOCKING DOOR");
port.write('2');
}
if(ardonoroff[0].equals("ardLocked") == true && inBuffer[0] == 3){
println("ARDUINO OVERRIDE, LOCKING DOOR");
port.write('3');
}
}

It's a work in progress and certainly a prototype... I used the linear gears from an old cd burner and rigged it up to the servo. My roommates think that I've gone insane!