Created
March 26, 2015 20:00
-
-
Save lrvick/05f3f6f80fb82ca803d6 to your computer and use it in GitHub Desktop.
Arduino Bathroom Lock State Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include <SPI.h> | |
#include <Ethernet.h> | |
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xCA, 0xFE }; | |
EthernetServer server(80); | |
const int lockPin = 2; | |
const int ledPin = 13; | |
int lockState = 0; | |
void setup(){ | |
Serial.begin(9600); | |
pinMode(ledPin, OUTPUT); | |
pinMode(lockPin, INPUT); | |
Ethernet.begin(mac); | |
server.begin(); | |
Serial.print("server is at "); | |
Serial.println(Ethernet.localIP()); | |
} | |
void loop(){ | |
EthernetClient client = server.available(); | |
if (client) { | |
while (client.connected()) { | |
if (client.available()) { | |
lockState = digitalRead(lockPin); | |
if (lockState == HIGH) { | |
// turn LED on: | |
digitalWrite(ledPin, HIGH); | |
} else { | |
// turn LED off: | |
digitalWrite(ledPin, LOW); | |
} | |
client.println("HTTP/1.1 200 OK"); | |
client.println("Content-Type: application/json"); | |
client.println(); | |
client.print("{id:0,locked:"); | |
client.print(lockState); | |
client.print("} "); | |
hits = ++hits; | |
break; | |
} | |
} | |
client.stop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment