Skip to content

Instantly share code, notes, and snippets.

@abrahamjso
Created May 8, 2012 04:56
Show Gist options
  • Save abrahamjso/2632706 to your computer and use it in GitHub Desktop.
Save abrahamjso/2632706 to your computer and use it in GitHub Desktop.
communication ardiono-android2
#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 0, 110 }; // ip in lan
byte gateway[] = { 192, 168, 0, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
Server server(80); //server port
byte sampledata=50; //some sample data - outputs 2 (ascii = 50 DEC)
int ledPin = 2; // LED pin
String readString = String(30); //string for fetching data from address
boolean LEDON = false; //LED status flag
Servo myservo;
int pos = 90;
void setup(){
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
//Set pin 4 to output
pinMode(ledPin, OUTPUT);
//enable serial datada print
Serial.begin(9600);
myservo.attach(5);
myservo.write(pos);
}
void loop(){
// Create a client connection
Client client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100)
{
//store characters to string
readString += c; //replaces readString.append(c);
}
//output chars to serial port
Serial.print(c);
//if HTTP request has ended
if (c == '\n') {
if (readString.indexOf("?") <0)
{
//skip everything
}
else
if(readString.indexOf("L=1") >0)
{
digitalWrite(ledPin, HIGH); // set the LED on
LEDON = true;
}else{
digitalWrite(ledPin, LOW); // set the LED OFF
LEDON = false;
}
if(readString.indexOf("S=1") >0)
{
if( pos > 0)
pos = 10;
myservo.write(pos);
}
if(readString.indexOf("S=0") >0){
if( pos < 180)
pos = 170;
myservo.write(pos);
}
// now output HTML data starting with standart header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.print("<body>");
client.println("<h1>LED control</h1>");
if (LEDON)
client.println("<form method=get name=LED><input type=checkbox name=L value=1 CHECKED>LED<br><input type=submit value=submit></form>");
else
client.println("<form method=get name=LED><input type=checkbox name=L value=1>LED<br><input type=submit value=submit></form>");
client.println("<br />");
client.println("<form method=get name=SERVO><button name=S value=1>IZQUIERDA</button>");
client.println("<br/><br/>");
client.println("<button name=S value=0>DERECHA</button></form>");
client.println("</body></html>");
readString="";
client.stop();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment