-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#ifndef __CONFIG_H__ | ||
#define __CONFIG_H__ | ||
|
||
//comment out to get rid of debug output & the loop waiting for the Serial connection: | ||
#define DEBUG | ||
|
||
//io settings | ||
const byte buttonPin = 2; // button pin | ||
const byte ledPin = 9; // signalling led pin | ||
//ratpack server | ||
const char HOST[] = "YOU RATPACK SERVER"; | ||
|
||
const unsigned long IO_INTERVAL = 0.5*1337; // millisecs delay between updates | ||
|
||
#endif |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* Yun Pack - the Rat Pack using the new possibilities of the Arduino Yun. | ||
* | ||
* Check out the repository & participate on github: | ||
* | ||
* http://github.com/5v3n/ratpack | ||
* | ||
* Copyright (c) 2013 Sven Kraeuter [email protected] MIT License | ||
*/ | ||
|
||
#include <Process.h> | ||
#include "Config.h" | ||
|
||
void setup() { | ||
// Initialize Bridge | ||
Bridge.begin(); | ||
|
||
//------------------------ button setup ------------------------ | ||
pinMode(buttonPin, INPUT); | ||
digitalWrite(buttonPin, HIGH); //use internal 10k | ||
//------------------------ led setup --------------------------- | ||
pinMode(ledPin, OUTPUT); | ||
|
||
#ifdef DEBUG | ||
// Initialize Serial | ||
Serial.begin(9600); | ||
// Wait until a Serial Monitor is connected. | ||
while (!Serial){ | ||
; | ||
} | ||
#endif | ||
|
||
} | ||
|
||
void loop() { | ||
//transfer button state to server | ||
if(digitalRead(buttonPin) == HIGH){ | ||
sendState('0'); | ||
} | ||
else if(digitalRead(buttonPin) == LOW){ | ||
sendState('1'); | ||
} | ||
//read button state from server & use the LED accordingly | ||
receiveState(); | ||
//the yun is FAST - give the server a break ;-) | ||
delay(IO_INTERVAL); | ||
} | ||
|
||
void receiveState() { | ||
Process p; | ||
p.runShellCommand("curl http://ratpack.makingthingshappen.de/status.json"); | ||
|
||
while(p.running()); //wait | ||
|
||
char state[2]; | ||
state[1]='\0'; | ||
|
||
if(p.find("\"activated\":")){ | ||
p.readBytes(state, 1); | ||
if(atoi(state) == 1){ | ||
digitalWrite(ledPin, HIGH); | ||
} | ||
else if(atoi(state) == 0) { | ||
digitalWrite(ledPin, LOW); | ||
} | ||
} | ||
#ifdef DEBUG | ||
Serial.print("According to the server, the button state is: "); | ||
Serial.println(state); | ||
Serial.println(); | ||
#endif | ||
|
||
Serial.flush(); | ||
} | ||
|
||
void sendState(byte state) { | ||
#ifdef DEBUG | ||
Serial.print("sending update, state will be: "); | ||
Serial.println((char)state); | ||
#endif | ||
|
||
char commandBuffer[255]; //it's crude - feel free to handle the commandBuffer length more elegant | ||
sprintf(commandBuffer, "curl -X PUT -d \"{\\\"activated\\\": %c}\" http://%s/status.json", state, HOST); | ||
|
||
#ifdef DEBUG | ||
Serial.println(commandBuffer); | ||
#endif | ||
|
||
Process p; | ||
p.runShellCommand(commandBuffer); | ||
|
||
|
||
#ifdef DEBUG | ||
while(p.running()); //wait | ||
|
||
Serial.print("Response from PUT: "); | ||
while (p.available()>0) { | ||
char c = p.read(); | ||
Serial.print(c); | ||
} | ||
Serial.println(); | ||
#endif | ||
|
||
// Ensure the last bit of data is sent. | ||
Serial.flush(); | ||
} | ||
|