Skip to content

Commit

Permalink
added support for arduino yun
Browse files Browse the repository at this point in the history
  • Loading branch information
5v3n committed Sep 15, 2013
1 parent 6b073fc commit bec596c
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
15 changes: 15 additions & 0 deletions arduino/ratpack_yun/Config.h
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
106 changes: 106 additions & 0 deletions arduino/ratpack_yun/ratpack_yun.ino
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();
}

0 comments on commit bec596c

Please sign in to comment.