Skip to content

Commit

Permalink
Existing garage opener code + restduino
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwan Hilmi committed Jul 18, 2012
1 parent 8b6da35 commit fa12161
Show file tree
Hide file tree
Showing 412 changed files with 68,732 additions and 0 deletions.
278 changes: 278 additions & 0 deletions garage/garage.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
/*
RESTduino
A REST-style interface to the Arduino via the
Wiznet Ethernet shield.
Based on David A. Mellis's "Web Server" ethernet
shield example sketch.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 04/12/2011
by Jason J. Gullickson
added 10/16/2011
by Edward M. Goldberg - Optional Debug flag
*/



#include <SPI.h>
#include <Ethernet.h>


#define RED_PIN 9
#define GREEN_PIN 7
#define YELLOW_PIN 8
#define RELAY_PIN 2
#define SWITCH_PIN 5

bool red_state = false;
bool yellow_state = false;
bool green_state = false;
bool garage_state = false;
bool relay_state = false;
int switch_state = 0;
unsigned long triggertime;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
//byte ip[] = {192,168,1,177};

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
#if defined(ARDUINO) && ARDUINO >= 100
EthernetServer server(80);
#else
Server server(80);
#endif

void setup()
{
triggertime = millis();
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(YELLOW_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
pinMode(SWITCH_PIN, INPUT);
digitalWrite(SWITCH_PIN, HIGH);
Serial.begin(9600);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// print your local IP address:
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
}

// url buffer size
#define BUFSIZE 255
// Toggle case sensitivity
#define CASESENSE true

void loop()
{
char clientline[BUFSIZE];
readSwitch();
int index = 0;
// listen for incoming clients
#if defined(ARDUINO) && ARDUINO >= 100
EthernetClient client = server.available();
#else
Client client = server.available();
#endif
if (client) {
// reset input buffer
index = 0;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// fill url the buffer
if(c != '\n' && c != '\r' && index < BUFSIZE){ // Reads until either an eol character is reached or the buffer is full
clientline[index++] = c;
continue;
}
// Serial.print("client available bytes before flush: ");
// Serial.println(client.available());
Serial.print("request = ");
Serial.println(clientline);
// Flush any remaining bytes from the client buffer
client.flush();
// Should be 0
// Serial.print("client available bytes after flush: ");
//Serial.println(client.available());
String urlString = String(clientline);
urlString = urlString.substring(urlString.indexOf('/'), urlString.indexOf(' ', urlString.indexOf('/')));
urlString.toUpperCase();
Serial.print("URL STRING: ");
Serial.println(urlString);
int urlStringLength = urlString.length();
int slashCount = 0;
for (int i = 0; i < urlStringLength; i++)
{
if (urlString.charAt(i) == '/')
{
slashCount++;
}
}
// get the first two parameters
int firstSlash = urlString.indexOf('/');
int lastSlash = urlString.lastIndexOf('/');

if (slashCount == 1)
{
if (urlString == "/")
{
//default / request
//Print Status
String jsonOut = String();
jsonOut += "{\"";
jsonOut += "Garage Status";
jsonOut += "\":\"";
jsonOut += garage_state;
jsonOut += "\"}";
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Access-Control-Allow-Origin: *");
client.println();
client.println(jsonOut);
}
else if (urlString == "/FAVICON.ICO")
{
//do nothing
}
else {
Serial.println("erroring");
client.println("HTTP/1.1 404 Not Found");
client.println("Content-Type: text/html");
client.println();
}
}
else if (slashCount == 2)
{
//We have a command
Serial.println("Command");
if (urlString == "/TOGGLE/")
{
if (triggerRelay()){
String jsonOut = String();
jsonOut += "{\"";
jsonOut += "Garage Status";
jsonOut += "\":\"";
jsonOut += garage_state;
jsonOut += "\"}";
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Access-Control-Allow-Origin: *");
client.println();
client.println(jsonOut);
}
else
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Access-Control-Allow-Origin: *");
client.println();
client.println("Please wait 10 seconds before toggling garage door again.");
}
}
}
else if (slashCount == 3)
{
//We have a command + parameter
Serial.println("Command + Parameter");
}

else
{
//Error
Serial.println("erroring");
client.println("HTTP/1.1 404 Not Found");
client.println("Content-Type: text/html");
client.println();
}
break;
}
}

// give the web browser time to receive the data
delay(1);

// close the connection:
client.stop();
}
}

bool triggerRelay()
{
unsigned long x = millis();
unsigned long diff = x - triggertime;
if (diff < 10000)
{
//too soon
//Serial.println("Wait 10 seconds before toggling relay again.");
//return false;
}

digitalWrite(RELAY_PIN, HIGH);
digitalWrite(YELLOW_PIN, HIGH);
delay(1000);
digitalWrite(RELAY_PIN, LOW);
digitalWrite(YELLOW_PIN, LOW);

triggertime = millis();
return true;



/* if (garage_state == false){
garage_state = true;
//digitalWrite(RED_PIN, true);
//digitalWrite(GREEN_PIN, false);
Serial.println("Garage open.");
return;
}
else if (garage_state == true);
{
garage_state = false;
//digitalWrite(RED_PIN, false);
//digitalWrite(GREEN_PIN, true);
Serial.println("Garage closed.");
return;
}
*/
}
void readSwitch()
{
switch_state = digitalRead(SWITCH_PIN);
if (switch_state == 1)
{
garage_state = true;
// Serial.println("Garage open.");
digitalWrite(RED_PIN, true);
digitalWrite(GREEN_PIN, false);
}
else if (switch_state == 0)
{
garage_state = false;
// Serial.println("Garage closed.");
digitalWrite(RED_PIN, false);
digitalWrite(GREEN_PIN, true);
}
}

43 changes: 43 additions & 0 deletions restduino/jjg-RESTduino-6283800/DemoApp.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<html>
<head>
<link type="text/css" href="jquery-ui-1/css/blitzer/jquery-ui-1.8.11.custom.css" rel="stylesheet" />
<script type="text/javascript" src="jquery-ui-1/js/jquery-1.5.1.min.js"></script>
<script type="text/javascript" src="jquery-ui-1/js/jquery-ui-1.8.11.custom.min.js"></script>
<meta charset="utf-8">
<script>
$(function() {
$( "#slider-vertical" ).slider({
orientation: "vertical",
range: "min",
min: 0,
max: 255,
value: 100,
slide: function( event, ui ) {
$( "#amount" ).val( ui.value );

// build a URL using the value from the slider
var resturl = "http://192.168.1.177/9/" + ui.value;

// make an AJAX call to the Arduino
$.get(resturl, function(data) {
});
}
});

$( "#amount" ).val( $( "#slider-vertical" ).slider( "value" ) );

});
</script>
</head>
<body>

<p>
<label for="amount">PWM Value:</label>
<input type="text" id="amount" style="border:0; color:#f6931f; font-weight:bold;" />
</p>

<div id="slider-vertical" style="height:200px;"></div>

</body>
</html>

72 changes: 72 additions & 0 deletions restduino/jjg-RESTduino-6283800/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
RESTduino
=========

RESTduino is a simple sketch to provide a REST-like interface to the Arduino via the Ethernet Shield. The idea is to allow developers familiar with interacting with REST services with a way to control physical devices using the Arduino (without having to write any Arduino code).

You can see a crude demo video on YouTube here: http://www.youtube.com/watch?v=X-s2se-34-g

Of course some flexibility is traded for this convenience; only basic operations are currently supported:

* Digital pin I/O (HIGH, LOW and PWM)
* Analog pin input

Later versions of the sketch may provide additional functionality (servo control, etc.) however if you need more than just basic pin control you're probably better off learning the basics of programming the Arduino and offloading some of your processing to the board itself.

Getting Started
---------------

First you'll need an Arduino, a Wiznet-based Ethernet shield and the Arduino development tools; here's some links to get you started:

* Arduino Uno (adafruit): http://www.adafruit.com/index.php?main_page=product_info&cPath=17&products_id=50
* Ethernet Shield (adafruit): http://www.adafruit.com/index.php?main_page=product_info&cPath=17_21&products_id=201
* Arduino development tools: http://www.arduino.cc/en/Main/Software

For testing you'll want some hardware to connect to the Arduino (a green LED is enough to get started). Connect the LED between pins 9 and ground (GND).

Load up the sketch (RESTduino.pde) and modify the following lines to match your setup:

byte mac[]={0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

This line sets the MAC address of your ethernet board; if your board has one written on it, you should use that instead.

byte ip[] = {192,168,1,177};

The next line you'll need to modify is this one which sets the IP address; set it to something valid for your network.

Now you should be ready to upload the code to your Arduino. Once the upload is complete you can open the "Serial Monitor" to get some debug info from the sketch.

Now you're ready to start talking REST to your Arduino!

To turn on the LED attached to pin #9 (currently case sensitive!):

http://192.168.1.177/9/HIGH

This will set the pin to the HIGH state and the LED should light. Next try this:

http://192.168.1.177/100

This will use PWM to illuminate the LED at around 50% brightness (valid PWM values are 0-255).

Now if we connect a switch to pin #9 we can read the digital (on/off) value using this URL:

http://192.168.1.177/9

This returns a tiny chunk of JSON containing the pin requested and its current value:

{"9":"LOW"}

Analog reads are similar; reading the value of Analog pin #1 looks like this:

http://192.168.1.177/a1

...and return the same JSON formatted result as above:

{"a1":"432"}

Javascript/jQuery Demo
----------------------
A simple example of how to interface with RESTduino via jQuery is included as DemoApp.html.

This page displays a slider control (via jQuery UI) which when adjusted will set the PWM value of Pin #9 to the value selected by the slider.

If you look at line 19 you can see where the REST URL (you'll need to adjust this for the IP address of your device) is constructed based on the selected value of the slider and on line 22 an AJAX request is executed passing the URL constructed above to the Arduino.
Loading

0 comments on commit fa12161

Please sign in to comment.