Hello,
I'm working on an Arduino sketch that will act as a socket server to communicate with my Android phone over my network and/or the internet. I'm using an Arduino Duiemilanove, Arduino v021, and an Ethernet Shield (Arduino Ethernet Shield - DEV-09026 - SparkFun Electronics).
My sketch so far is:
/*
Chat Server
A simple server that distributes any incoming messages to all
connected clients. To use telnet to your device's IP address and type.
You can see the client's input in the serial monitor as well.
Using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pins A0 through A5 (optional)
created 18 Dec 2009
by David A. Mellis
modified 10 August 2010
by Tom Igoe
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0, 177 };
byte gateway[] = { 192,168,0, 1 };
byte subnet[] = { 255, 255, 255, 0 };
// telnet defaults to port 23
Server server(23);
boolean gotAMessage = false; // whether or not you got a message from the client yet
String myLittleBuffer = "";
String myPass = "mypass"; // *** Any string comparisons to Android commands must be declared up here as Strings !!! ***
String cmdGarageDoorStatusRequest = "cmd=statusRequest";
String cmdGarageDoorToggle = "cmd=gdToggle";
String cmdGarageDoorCrack = "cmd=gdCrack";
String disconnect = "disconnect";
void setup() {
// initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet);
// start listening for clients
server.begin();
// open the serial port
Serial.begin(9600);
}
void loop() {
// wait for a new client:
Client client = server.available();
if (client) {
// read the bytes incoming from the client:
char thisChar = client.read();
if (thisChar == '*') {
if (myLittleBuffer.equals(myPass)) {
Serial.print(myLittleBuffer);
Serial.print("correct password!");
server.write("status:open"); // Send the initial garage door status here to the client
Serial.print("Sent: status:open");
//client.stop(); ***** THIS IS THE LINE I HAVE TO UNCOMMENT IN ORDER FOR THE ARDUINO TO ACTUALLY SEND THE ABOVE 'server.write("status:open");' COMMAND. :(
do
{
char thisChar = client.read();
if (thisChar == '*') {
if (myLittleBuffer.equals(cmdGarageDoorStatusRequest)) {
// TODO - Respond to Garage Door Status Request
Serial.print("Garage Door Status Request Block:");
Serial.print(myLittleBuffer);
} else if (myLittleBuffer.equals(cmdGarageDoorToggle)) {
// TODO - Respond to Garage Door Toggle Command
Serial.print("Garage Door Toggle Command Block:");
Serial.print(myLittleBuffer);
} else if (myLittleBuffer.equals(cmdGarageDoorCrack)) {
Serial.print("Garage Door Crack Command Block:");
Serial.print(myLittleBuffer);
// TODO - Respond to Garage Door Crack Command
} else if (myLittleBuffer.equals(disconnect)) {
Serial.print("Disconnect Block:");
Serial.print(myLittleBuffer);
// TODO - Respond to Disconnect
client.stop();
}
// Reset our 'buffer' back to empty because we received the special '*' character to denote end-of-command
myLittleBuffer = "";
} else {
// if the character is not a '*', keep building the string until it is :)
myLittleBuffer = String(myLittleBuffer + thisChar);
}
} while (client); // while the client is connected
} else {
Serial.print(myLittleBuffer);
Serial.print("incorrect password");
client.stop(); //disconnect client
}
// Reset our 'buffer' back to empty because we received the special '*' character to denote end-of-command
myLittleBuffer = "";
} else {
myLittleBuffer = String(myLittleBuffer + thisChar);
}
}
}
So my problem is that the Arduino doesn't actually seem to send the 'status: open' text unless I uncomment the client.stop(); line that directly follows it (see about halfway down in my code above...just look for server.write()). I don't understand why this is. What I'm doing is setting up a constant communication where my Android needs to send different commands to the Arduino and it's supposed to respond...but it seems like once it sends one command I have to close the connection which doesn't seem right.
Oh yeah, and I've tried server.print(), server.println, client.print(), client.write(), and client.println() all with the same results.