Skip to content

Instantly share code, notes, and snippets.

@survivingwithandroid
Created August 28, 2015 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save survivingwithandroid/00686815449364ceb0ac to your computer and use it in GitHub Desktop.
Save survivingwithandroid/00686815449364ceb0ac to your computer and use it in GitHub Desktop.
#include <spi.h>
#include <ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 130); // Arduino IP Add
EthernetServer server(80); // Web server
// Http data
String reqData; // Request from Smartphone
String header;
int contentSize = -1;
String CONTENT_LENGTH_TXT = "Content-Length: ";
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(3, OUTPUT); // Set Pin 3 to OUTPUT Mode
Serial.print("Ready...");
//
Ethernet.begin(mac, ip);
server.begin();
}
void loop() {
EthernetClient client = server.available(); // Is there a client (Our Android smartphone)
if (client) {
// Let's start reading
boolean isLastLine = true;
boolean isBody = false;
header = "";
reqData = "";
int contentLen = 0;
Serial.print("Client connected!");
while (client.connected()) {
if (client.available()) {
// Read data
char c = client.read();
// Serial.print(c);
if (contentSize == contentLen) {
// Serial.println("Body ["+reqData+"]");
int idx = reqData.indexOf(":");
String status = reqData.substring(idx + 1, idx + 2);
Serial.println("Status : " + status);
if (status.equals("1")) {
digitalWrite(3, HIGH);
}
else {
digitalWrite(3, LOW);
}
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// send web page
client.println("");
client.println("");
delay(1);
break;
}
if (c == '\n' && isLastLine) {
isBody = true;
int pos = header.indexOf(CONTENT_LENGTH_TXT);
String tmp = header.substring(pos, header.length());
//Serial.println("Tmp ["+tmp+"]");
int pos1 = tmp.indexOf("\r\n");
String size = tmp.substring(CONTENT_LENGTH_TXT.length(), pos1);
Serial.println("Size ["+size+"]");
contentSize = size.toInt();
}
if (isBody) {
reqData += c;
contentLen++;
}
else {
header += c;
}
if (c == '\n' ) {
isLastLine = true;
}
else if (c != '\r' ) {
isLastLine = false;
}
}
}
// Close connection
Serial.println("Stop..");
client.stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment