Skip to content

Instantly share code, notes, and snippets.

@kr1sp1n
Last active June 2, 2022 12:30
Show Gist options
  • Save kr1sp1n/7006451 to your computer and use it in GitHub Desktop.
Save kr1sp1n/7006451 to your computer and use it in GitHub Desktop.
Arduino script to open a door via HTTP request. A relay is used to separate Arduino and door-opening-sytem circuit.
/*
Klingel 2.0 by Krispin
2013-11-01
*/
#include <SPI.h>
#include <Ethernet.h>
// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// assign an IP address for the controller:
IPAddress ip(10,1,3,235);
IPAddress gateway(10,1,3,255);
IPAddress subnet(255, 255, 255, 0);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
// Relay Pin D7
int Relay = 7;
boolean isRinging = false;
// is peak time?
boolean isPeakTime = false;
void setup() {
// Set Pin7 as output
pinMode(Relay, OUTPUT);
// turn Relay off
digitalWrite(Relay, LOW); //Turn off relay
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.begin(9600);
// give the sensor and Ethernet shield time to set up:
delay(3000);
}
void loop() {
// listen for incoming Ethernet connections:
listenForEthernetClients();
listenForRinger();
}
void openDoor() {
digitalWrite(Relay, LOW); //Turn off relay
delay(1000);
digitalWrite(Relay, HIGH); //Turn on relay
delay(3000); // Activate door for 3 seconds
digitalWrite(Relay, LOW); // Turn off relay
}
void listenForRinger() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
if(voltage>=5 && !isRinging && isPeakTime) {
isRinging = true;
openDoor();
isRinging = false;
}
}
void listenForEthernetClients() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("Got a client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
boolean recordPath = false;
String req = String();
String path = String();
while (client.connected()) {
if (client.available()) {
char c = client.read();
req += c;
if(c == ' ' && recordPath) {
recordPath = false;
}
if(recordPath) {
path += c;
}
if(req=="GET ") {
recordPath = true;
}
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
if(path == "/") {
openDoor();
}
if(path == "/activate") {
isPeakTime = true;
}
if(path == "/deactivate") {
isPeakTime = false;
}
Serial.println(path);
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
// print the current readings, in HTML format:
client.println("<h1>You opened the door!</h1>");
client.println("<p><img src='http://www.drsfostersmith.com/images/Categoryimages/normal/p_25642_FS35095P_007.jpg' /></p>");
client.println("<hr/>");
client.println("<p>Developed by Krispin.");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(10);
// close the connection:
client.stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment