1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-01-30 19:52:13 +01:00

Spacebrew update

This commit is contained in:
Federico Fissore 2014-04-01 09:35:56 +02:00
parent 2f7e712fb9
commit 88ed5d53a6
8 changed files with 679 additions and 683 deletions

View File

@ -2,23 +2,23 @@
Input Output Input Output
Demonstrates how to create a sketch that sends and receives all standard Demonstrates how to create a sketch that sends and receives all standard
spacebrew data types, and a custom data type. Every time data is spacebrew data types, and a custom data type. Every time data is
received it is output to the Serial monitor. received it is output to the Serial monitor.
Make sure that your Yun is connected to the internet for this example Make sure that your Yun is connected to the internet for this example
to function properly. to function properly.
The circuit: The circuit:
- No circuit required - No circuit required
created 2013 created 2013
by Julio Terra by Julio Terra
This example code is in the public domain. This example code is in the public domain.
More information about Spacebrew is available at: More information about Spacebrew is available at:
http://spacebrew.cc/ http://spacebrew.cc/
*/ */
#include <Bridge.h> #include <Bridge.h>
@ -33,100 +33,100 @@ int interval = 2000;
int counter = 0; int counter = 0;
void setup() { void setup() {
// start the serial port // start the serial port
Serial.begin(57600); Serial.begin(57600);
// for debugging, wait until a serial console is connected // for debugging, wait until a serial console is connected
delay(4000); delay(4000);
while (!Serial) { while (!Serial) { ; }
;
}
// start-up the bridge // start-up the bridge
Bridge.begin(); Bridge.begin();
// configure the spacebrew object to print status messages to serial // configure the spacebrew object to print status messages to serial
sb.verbose(true); sb.verbose(true);
// configure the spacebrew publisher and subscriber // configure the spacebrew publisher and subscriber
sb.addPublish("string test", "string"); sb.addPublish("string test", "string");
sb.addPublish("range test", "range"); sb.addPublish("range test", "range");
sb.addPublish("boolean test", "boolean"); sb.addPublish("boolean test", "boolean");
sb.addPublish("custom test", "crazy"); sb.addPublish("custom test", "crazy");
sb.addSubscribe("string test", "string"); sb.addSubscribe("string test", "string");
sb.addSubscribe("range test", "range"); sb.addSubscribe("range test", "range");
sb.addSubscribe("boolean test", "boolean"); sb.addSubscribe("boolean test", "boolean");
sb.addSubscribe("custom test", "crazy"); sb.addSubscribe("custom test", "crazy");
// register the string message handler method // register the string message handler method
sb.onRangeMessage(handleRange); sb.onRangeMessage(handleRange);
sb.onStringMessage(handleString); sb.onStringMessage(handleString);
sb.onBooleanMessage(handleBoolean); sb.onBooleanMessage(handleBoolean);
sb.onCustomMessage(handleCustom); sb.onCustomMessage(handleCustom);
// connect to cloud spacebrew server at "sandbox.spacebrew.cc" // connect to cloud spacebrew server at "sandbox.spacebrew.cc"
sb.connect("sandbox.spacebrew.cc"); sb.connect("sandbox.spacebrew.cc");
// we give some time to arduino to connect to sandbox, otherwise the first sb.monitor(); call will give an error
} delay(1000);
}
void loop() { void loop() {
// monitor spacebrew connection for new data // monitor spacebrew connection for new data
sb.monitor(); sb.monitor();
// connected to spacebrew then send a string every 2 seconds // connected to spacebrew then send a string every 2 seconds
if ( sb.connected() ) { if ( sb.connected() ) {
// check if it is time to send a new message // check if it is time to send a new message
if ( (millis() - last) > interval ) { if ( (millis() - last) > interval ) {
String test_str_msg = "testing, testing, "; String test_str_msg = "testing, testing, ";
test_str_msg += counter; test_str_msg += counter;
counter ++; counter ++;
sb.send("string test", test_str_msg); sb.send("string test", test_str_msg);
sb.send("range test", 500); sb.send("range test", 500);
sb.send("boolean test", true); sb.send("boolean test", true);
sb.send("custom test", "youre loco"); sb.send("custom test", "youre loco");
last = millis(); last = millis();
} }
} }
} delay(1000);
}
// define handler methods, all standard data type handlers take two appropriate arguments // define handler methods, all standard data type handlers take two appropriate arguments
void handleRange (String route, int value) { void handleRange (String route, int value) {
Serial.print("Range msg "); Serial.print("Range msg ");
Serial.print(route); Serial.print(route);
Serial.print(", value "); Serial.print(", value ");
Serial.println(value); Serial.println(value);
} }
void handleString (String route, String value) { void handleString (String route, String value) {
Serial.print("String msg "); Serial.print("String msg ");
Serial.print(route); Serial.print(route);
Serial.print(", value "); Serial.print(", value ");
Serial.println(value); Serial.println(value);
} }
void handleBoolean (String route, boolean value) { void handleBoolean (String route, boolean value) {
Serial.print("Boolen msg "); Serial.print("Boolen msg ");
Serial.print(route); Serial.print(route);
Serial.print(", value "); Serial.print(", value ");
Serial.println(value ? "true" : "false"); Serial.println(value ? "true" : "false");
} }
// custom data type handlers takes three String arguments // custom data type handlers takes three String arguments
void handleCustom (String route, String value, String type) { void handleCustom (String route, String value, String type) {
Serial.print("Custom msg "); Serial.print("Custom msg ");
Serial.print(route); Serial.print(route);
Serial.print(" of type "); Serial.print(" of type ");
Serial.print(type); Serial.print(type);
Serial.print(", value "); Serial.print(", value ");
Serial.println(value); Serial.println(value);
} }

View File

@ -1,26 +1,26 @@
/* /*
Spacebrew Boolean Spacebrew Boolean
Demonstrates how to create a sketch that sends and receives a Demonstrates how to create a sketch that sends and receives a
boolean value to and from Spacebrew. Every time the buttton is boolean value to and from Spacebrew. Every time the buttton is
pressed (or other digital input component) a spacebrew message pressed (or other digital input component) a spacebrew message
is sent. The sketch also accepts analog range messages from is sent. The sketch also accepts analog range messages from
other Spacebrew apps. other Spacebrew apps.
Make sure that your Yun is connected to the internet for this example Make sure that your Yun is connected to the internet for this example
to function properly. to function properly.
The circuit: The circuit:
- Button connected to Yun, using the Arduino's internal pullup resistor. - Button connected to Yun, using the Arduino's internal pullup resistor.
created 2013 created 2013
by Julio Terra by Julio Terra
This example code is in the public domain. This example code is in the public domain.
More information about Spacebrew is available at: More information about Spacebrew is available at:
http://spacebrew.cc/ http://spacebrew.cc/
*/ */
#include <Bridge.h> #include <Bridge.h>
@ -33,59 +33,57 @@ SpacebrewYun sb = SpacebrewYun("spacebrewYun Boolean", "Boolean sender and recei
int last_value = 0; int last_value = 0;
// create variables to manage interval between each time we send a string // create variables to manage interval between each time we send a string
void setup() { void setup() {
// start the serial port // start the serial port
Serial.begin(57600); Serial.begin(57600);
// for debugging, wait until a serial console is connected // for debugging, wait until a serial console is connected
delay(4000); delay(4000);
while (!Serial) { while (!Serial) { ; }
;
}
// start-up the bridge // start-up the bridge
Bridge.begin(); Bridge.begin();
// configure the spacebrew object to print status messages to serial // configure the spacebrew object to print status messages to serial
sb.verbose(true); sb.verbose(true);
// configure the spacebrew publisher and subscriber // configure the spacebrew publisher and subscriber
sb.addPublish("physical button", "boolean"); sb.addPublish("physical button", "boolean");
sb.addSubscribe("virtual button", "boolean"); sb.addSubscribe("virtual button", "boolean");
// register the string message handler method // register the string message handler method
sb.onBooleanMessage(handleBoolean); sb.onBooleanMessage(handleBoolean);
// connect to cloud spacebrew server at "sandbox.spacebrew.cc" // connect to cloud spacebrew server at "sandbox.spacebrew.cc"
sb.connect("sandbox.spacebrew.cc"); sb.connect("sandbox.spacebrew.cc");
pinMode(3, INPUT); pinMode(3, INPUT);
digitalWrite(3, HIGH); digitalWrite(3, HIGH);
} }
void loop() { void loop() {
// monitor spacebrew connection for new data // monitor spacebrew connection for new data
sb.monitor(); sb.monitor();
// connected to spacebrew then send a new value whenever the pot value changes // connected to spacebrew then send a new value whenever the pot value changes
if ( sb.connected() ) { if ( sb.connected() ) {
int cur_value = digitalRead(3); int cur_value = digitalRead(3);
if ( last_value != cur_value ) { if ( last_value != cur_value ) {
if (cur_value == HIGH) sb.send("physical button", false); if (cur_value == HIGH) sb.send("physical button", false);
else sb.send("physical button", true); else sb.send("physical button", true);
last_value = cur_value; last_value = cur_value;
} }
} }
} }
// handler method that is called whenever a new string message is received // handler method that is called whenever a new string message is received
void handleBoolean (String route, boolean value) { void handleBoolean (String route, boolean value) {
// print the message that was received // print the message that was received
Serial.print("From "); Serial.print("From ");
Serial.print(route); Serial.print(route);
Serial.print(", received msg: "); Serial.print(", received msg: ");
Serial.println(value ? "true" : "false"); Serial.println(value ? "true" : "false");
} }

View File

@ -1,27 +1,27 @@
/* /*
Spacebrew Range Spacebrew Range
Demonstrates how to create a sketch that sends and receives analog Demonstrates how to create a sketch that sends and receives analog
range value to and from Spacebrew. Every time the state of the range value to and from Spacebrew. Every time the state of the
potentiometer (or other analog input component) change a spacebrew potentiometer (or other analog input component) change a spacebrew
message is sent. The sketch also accepts analog range messages from message is sent. The sketch also accepts analog range messages from
other Spacebrew apps. other Spacebrew apps.
Make sure that your Yun is connected to the internet for this example Make sure that your Yun is connected to the internet for this example
to function properly. to function properly.
The circuit: The circuit:
- Potentiometer connected to Yun. Middle pin connected to analog pin A0, - Potentiometer connected to Yun. Middle pin connected to analog pin A0,
other pins connected to 5v and GND pins. other pins connected to 5v and GND pins.
created 2013 created 2013
by Julio Terra by Julio Terra
This example code is in the public domain. This example code is in the public domain.
More information about Spacebrew is available at: More information about Spacebrew is available at:
http://spacebrew.cc/ http://spacebrew.cc/
*/ */
#include <Bridge.h> #include <Bridge.h>
@ -34,55 +34,53 @@ SpacebrewYun sb = SpacebrewYun("spacebrewYun Range", "Range sender and receiver"
int last_value = 0; int last_value = 0;
// create variables to manage interval between each time we send a string // create variables to manage interval between each time we send a string
void setup() { void setup() {
// start the serial port // start the serial port
Serial.begin(57600); Serial.begin(57600);
// for debugging, wait until a serial console is connected // for debugging, wait until a serial console is connected
delay(4000); delay(4000);
while (!Serial) { while (!Serial) { ; }
;
}
// start-up the bridge // start-up the bridge
Bridge.begin(); Bridge.begin();
// configure the spacebrew object to print status messages to serial // configure the spacebrew object to print status messages to serial
sb.verbose(true); sb.verbose(true);
// configure the spacebrew publisher and subscriber // configure the spacebrew publisher and subscriber
sb.addPublish("physical pot", "range"); sb.addPublish("physical pot", "range");
sb.addSubscribe("virtual pot", "range"); sb.addSubscribe("virtual pot", "range");
// register the string message handler method // register the string message handler method
sb.onRangeMessage(handleRange); sb.onRangeMessage(handleRange);
// connect to cloud spacebrew server at "sandbox.spacebrew.cc" // connect to cloud spacebrew server at "sandbox.spacebrew.cc"
sb.connect("sandbox.spacebrew.cc"); sb.connect("sandbox.spacebrew.cc");
} }
void loop() { void loop() {
// monitor spacebrew connection for new data // monitor spacebrew connection for new data
sb.monitor(); sb.monitor();
// connected to spacebrew then send a new value whenever the pot value changes // connected to spacebrew then send a new value whenever the pot value changes
if ( sb.connected() ) { if ( sb.connected() ) {
int cur_value = analogRead(A0); int cur_value = analogRead(A0);
if ( last_value != cur_value ) { if ( last_value != cur_value ) {
sb.send("physical pot", cur_value); sb.send("physical pot", cur_value);
last_value = cur_value; last_value = cur_value;
} }
} }
} }
// handler method that is called whenever a new string message is received // handler method that is called whenever a new string message is received
void handleRange (String route, int value) { void handleRange (String route, int value) {
// print the message that was received // print the message that was received
Serial.print("From "); Serial.print("From ");
Serial.print(route); Serial.print(route);
Serial.print(", received msg: "); Serial.print(", received msg: ");
Serial.println(value); Serial.println(value);
} }

View File

@ -1,24 +1,24 @@
/* /*
Spacebrew String Spacebrew String
Demonstrates how to create a sketch that sends and receives strings Demonstrates how to create a sketch that sends and receives strings
to and from Spacebrew. Every time string data is received it to and from Spacebrew. Every time string data is received it
is output to the Serial monitor. is output to the Serial monitor.
Make sure that your Yun is connected to the internet for this example Make sure that your Yun is connected to the internet for this example
to function properly. to function properly.
The circuit: The circuit:
- No circuit required - No circuit required
created 2013 created 2013
by Julio Terra by Julio Terra
This example code is in the public domain. This example code is in the public domain.
More information about Spacebrew is available at: More information about Spacebrew is available at:
http://spacebrew.cc/ http://spacebrew.cc/
*/ */
#include <Bridge.h> #include <Bridge.h>
@ -31,56 +31,54 @@ SpacebrewYun sb = SpacebrewYun("spacebrewYun Strings", "String sender and receiv
long last_time = 0; long last_time = 0;
int interval = 2000; int interval = 2000;
void setup() { void setup() {
// start the serial port // start the serial port
Serial.begin(57600); Serial.begin(57600);
// for debugging, wait until a serial console is connected // for debugging, wait until a serial console is connected
delay(4000); delay(4000);
while (!Serial) { while (!Serial) { ; }
;
}
// start-up the bridge // start-up the bridge
Bridge.begin(); Bridge.begin();
// configure the spacebrew object to print status messages to serial // configure the spacebrew object to print status messages to serial
sb.verbose(true); sb.verbose(true);
// configure the spacebrew publisher and subscriber // configure the spacebrew publisher and subscriber
sb.addPublish("speak", "string"); sb.addPublish("speak", "string");
sb.addSubscribe("listen", "string"); sb.addSubscribe("listen", "string");
// register the string message handler method // register the string message handler method
sb.onStringMessage(handleString); sb.onStringMessage(handleString);
// connect to cloud spacebrew server at "sandbox.spacebrew.cc" // connect to cloud spacebrew server at "sandbox.spacebrew.cc"
sb.connect("sandbox.spacebrew.cc"); sb.connect("sandbox.spacebrew.cc");
} }
void loop() { void loop() {
// monitor spacebrew connection for new data // monitor spacebrew connection for new data
sb.monitor(); sb.monitor();
// connected to spacebrew then send a string every 2 seconds // connected to spacebrew then send a string every 2 seconds
if ( sb.connected() ) { if ( sb.connected() ) {
// check if it is time to send a new message // check if it is time to send a new message
if ( (millis() - last_time) > interval ) { if ( (millis() - last_time) > interval ) {
sb.send("speak", "is anybody out there?"); sb.send("speak", "is anybody out there?");
last_time = millis(); last_time = millis();
} }
} }
} }
// handler method that is called whenever a new string message is received // handler method that is called whenever a new string message is received
void handleString (String route, String value) { void handleString (String route, String value) {
// print the message that was received // print the message that was received
Serial.print("From "); Serial.print("From ");
Serial.print(route); Serial.print(route);
Serial.print(", received msg: "); Serial.print(", received msg: ");
Serial.println(value); Serial.println(value);
} }

View File

@ -1,4 +1,4 @@
SpacebrewYun KEYWORD3 SpacebrewYun KEYWORD1
addPublish KEYWORD2 addPublish KEYWORD2
addSubscribe KEYWORD2 addSubscribe KEYWORD2
connect KEYWORD2 connect KEYWORD2
@ -12,4 +12,4 @@ onBooleanMessage KEYWORD2
onCustomMessage KEYWORD2 onCustomMessage KEYWORD2
onOpen KEYWORD2 onOpen KEYWORD2
onClose KEYWORD2 onClose KEYWORD2
onError KEYWORD2 onError KEYWORD2

View File

@ -1,8 +1,10 @@
name=SpacebrewYun name=SpacebrewYun
version=1.0
author=Julio Terra author=Julio Terra
maintainer=Julio Terra <julioterra@gmail.com> email=julioterra@gmail.com
sentence=Easily connect the Arduino Yun to Spacebrew sentence=Easily connect the Arduino Yun to Spacebrew
paragraph=This library was developed to enable you to easily connect the Arduino Yun to Spacebrew. To learn more about Spacebrew visit Spacebrew.cc paragraph=This library was developed to enable you to easily connect the Arduino Yun to Spacebrew. To learn more about Spacebrew visit Spacebrew.cc
url=https://github.com/julioterra/yunSpacebrew url=https://github.com/julioterra/yunSpacebrew
architectures=* architectures=avr
version=1.0
dependencies=Bridge
core-dependencies=arduino (>=1.5.0)

View File

@ -2,34 +2,38 @@
SpacebrewYun::SpacebrewYun(const String& _name, const String& _description) { SpacebrewYun::SpacebrewYun(const String& _name, const String& _description) {
name = _name; name = _name;
description = _description; description = _description;
subscribers = NULL; subscribers = NULL;
publishers = NULL; publishers = NULL;
server = "sandbox.spacebrew.cc"; server = "sandbox.spacebrew.cc";
port = 9000; port = 9000;
_connected = false; _started = false;
_verbose = false; _connected = false;
_error_msg = false; _verbose = false;
_error_msg = false;
sub_name = ""; sub_name = "";
sub_msg = ""; sub_msg = "";
sub_type = ""; sub_type = "";
read_name = false; read_name = false;
read_msg = false; read_msg = false;
for ( int i = 0; i < pidLength; i++ ) { connect_attempt = 0;
pid [i] = '\0'; connect_attempt_inter = 10000;
}
for ( int i = 0; i < sbPidsLen; i++ ) { for ( int i = 0; i < pidLength; i++ ) {
sbPids [i] = '\0'; pid [i] = '\0';
} }
Console.buffer(64); for ( int i = 0; i < sbPidsLen; i++ ) {
sbPids [i] = '\0';
}
Console.buffer(64);
} }
@ -45,348 +49,356 @@ SpacebrewYun::OnSBOpen SpacebrewYun::_onOpen = NULL;
SpacebrewYun::OnSBClose SpacebrewYun::_onClose = NULL; SpacebrewYun::OnSBClose SpacebrewYun::_onClose = NULL;
SpacebrewYun::OnSBError SpacebrewYun::_onError = NULL; SpacebrewYun::OnSBError SpacebrewYun::_onError = NULL;
void SpacebrewYun::onOpen(OnSBOpen function) { void SpacebrewYun::onOpen(OnSBOpen function){
_onOpen = function; _onOpen = function;
} }
void SpacebrewYun::onClose(OnSBClose function) { void SpacebrewYun::onClose(OnSBClose function){
_onClose = function; _onClose = function;
} }
void SpacebrewYun::onRangeMessage(OnRangeMessage function) { void SpacebrewYun::onRangeMessage(OnRangeMessage function){
_onRangeMessage = function; _onRangeMessage = function;
} }
void SpacebrewYun::onStringMessage(OnStringMessage function) { void SpacebrewYun::onStringMessage(OnStringMessage function){
_onStringMessage = function; _onStringMessage = function;
} }
void SpacebrewYun::onBooleanMessage(OnBooleanMessage function) { void SpacebrewYun::onBooleanMessage(OnBooleanMessage function){
_onBooleanMessage = function; _onBooleanMessage = function;
} }
void SpacebrewYun::onCustomMessage(OnCustomMessage function) { void SpacebrewYun::onCustomMessage(OnCustomMessage function){
_onCustomMessage = function; _onCustomMessage = function;
} }
void SpacebrewYun::onError(OnSBError function) { void SpacebrewYun::onError(OnSBError function){
_onError = function; _onError = function;
} }
void SpacebrewYun::addPublish(const String& name, const String& type) { void SpacebrewYun::addPublish(const String& name, const String& type) {
struct Publisher *p = new Publisher(); struct Publisher *p = new Publisher();
p->name = createString(name.length() + 1); p->name = createString(name.length() + 1);
p->type = createString(type.length() + 1); p->type = createString(type.length() + 1);
p->confirmed = false; p->confirmed = false;
p->time = 0; p->time = 0;
if (type == "range") { if (type == "range") {
p->lastMsg = createString(sub_msg_int_max); p->lastMsg = createString(sub_msg_int_max);
emptyString(p->lastMsg, sub_msg_int_max); emptyString(p->lastMsg, sub_msg_int_max);
} }
else if (type == "boolean") { else if (type == "boolean") {
p->lastMsg = createString(sub_msg_bool_max); p->lastMsg = createString(sub_msg_bool_max);
emptyString(p->lastMsg, sub_msg_bool_max); emptyString(p->lastMsg, sub_msg_bool_max);
} }
else { else {
p->lastMsg = createString(sub_msg_str_max); p->lastMsg = createString(sub_msg_str_max);
emptyString(p->lastMsg, sub_msg_str_max); emptyString(p->lastMsg, sub_msg_str_max);
} }
name.toCharArray(p->name, name.length() + 1); name.toCharArray(p->name, name.length() + 1);
type.toCharArray(p->type, type.length() + 1); type.toCharArray(p->type, type.length() + 1);
if (publishers == NULL) { if (publishers == NULL){
publishers = p; publishers = p;
} }
else { else {
struct Publisher *curr = publishers; struct Publisher *curr = publishers;
int counter = 1; int counter = 1;
while (curr->next != NULL) { while(curr->next != NULL){
curr = curr->next; curr = curr->next;
counter++; counter++;
} }
curr->next = p; curr->next = p;
} }
p->next = NULL; p->next = NULL;
} }
void SpacebrewYun::addSubscribe(const String& name, const String& type) { void SpacebrewYun::addSubscribe(const String& name, const String& type) {
Subscriber *s = new Subscriber(); Subscriber *s = new Subscriber();
s->name = createString(name.length() + 1); s->name = createString(name.length() + 1);
s->type = createString(type.length() + 1); s->type = createString(type.length() + 1);
name.toCharArray(s->name, name.length() + 1); name.toCharArray(s->name, name.length() + 1);
type.toCharArray(s->type, type.length() + 1); type.toCharArray(s->type, type.length() + 1);
if (subscribers == NULL) { if (subscribers == NULL){
subscribers = s; subscribers = s;
} }
else { else {
struct Subscriber *curr = subscribers; struct Subscriber *curr = subscribers;
while (curr->next != NULL) { while(curr->next != NULL){
curr = curr->next; curr = curr->next;
} }
curr->next = s; curr->next = s;
} }
} }
void SpacebrewYun::connect(String _server, int _port) { void SpacebrewYun::connect(String _server, int _port) {
server = _server; Serial.print(F("NEW LIB"));
port = _port; _started = true;
server = _server;
port = _port;
connect_attempt = millis();
killPids(); killPids();
brew.begin("run-spacebrew"); // Process should launch the "curl" command brew.begin("run-spacebrew"); // Process should launch the "curl" command
// brew.begin("python"); // Process should launch the "curl" command // brew.begin("python"); // Process should launch the "curl" command
// brew.addParameter("/usr/lib/python2.7/spacebrew/spacebrew.py"); // Process should launch the "curl" command // brew.addParameter("/usr/lib/python2.7/spacebrew/spacebrew.py"); // Process should launch the "curl" command
brew.addParameter("--server"); brew.addParameter("--server");
brew.addParameter(server); brew.addParameter(server);
brew.addParameter("--port"); brew.addParameter("--port");
brew.addParameter(String(port)); brew.addParameter(String(port));
brew.addParameter("-n"); brew.addParameter("-n");
brew.addParameter(name); brew.addParameter(name);
brew.addParameter("-d"); brew.addParameter("-d");
brew.addParameter(description); brew.addParameter(description);
if (subscribers != NULL) { if (subscribers != NULL) {
struct Subscriber *curr = subscribers; struct Subscriber *curr = subscribers;
while (curr != NULL) { while(curr != NULL){
if (_verbose) { if (_verbose) {
Serial.print(F("Creating subscribers: ")); Serial.print(F("Creating subscribers: "));
Serial.print(curr->name); Serial.print(curr->name);
Serial.print(F(" of type: ")); Serial.print(F(" of type: "));
Serial.println(curr->type); Serial.println(curr->type);
} }
brew.addParameter("-s"); // Add the URL parameter to "curl" brew.addParameter("-s"); // Add the URL parameter to "curl"
brew.addParameter(curr->name); // Add the URL parameter to "curl" brew.addParameter(curr->name); // Add the URL parameter to "curl"
brew.addParameter(","); // Add the URL parameter to "curl" brew.addParameter(","); // Add the URL parameter to "curl"
brew.addParameter(curr->type); // Add the URL parameter to "curl" brew.addParameter(curr->type); // Add the URL parameter to "curl"
// if (curr->next == NULL) curr = NULL; // if (curr->next == NULL) curr = NULL;
// else curr = curr->next; // else curr = curr->next;
curr = curr->next; curr = curr->next;
} }
} }
if (publishers != NULL) { if (publishers != NULL) {
struct Publisher *curr = publishers; struct Publisher *curr = publishers;
while (curr != NULL) { while(curr != NULL){
if (_verbose) { if (_verbose) {
Serial.print(F("Creating publishers: ")); Serial.print(F("Creating publishers: "));
Serial.print(curr->name); Serial.print(curr->name);
Serial.print(F(" of type: ")); Serial.print(F(" of type: "));
Serial.println(curr->type); Serial.println(curr->type);
} }
brew.addParameter("-p"); // Add the URL parameter to "curl" brew.addParameter("-p"); // Add the URL parameter to "curl"
brew.addParameter(curr->name); // Add the URL parameter to "curl" brew.addParameter(curr->name); // Add the URL parameter to "curl"
brew.addParameter(","); // Add the URL parameter to "curl" brew.addParameter(","); // Add the URL parameter to "curl"
brew.addParameter(curr->type); // Add the URL parameter to "curl" brew.addParameter(curr->type); // Add the URL parameter to "curl"
curr = curr->next; curr = curr->next;
} }
} }
Console.begin(); Console.begin();
if (_verbose) { if (_verbose) {
Serial.println(F("Console started ")); Serial.println(F("Console started "));
} }
brew.runAsynchronously(); brew.runAsynchronously();
if (_verbose) { if (_verbose) {
Serial.println(F("Brew started ")); Serial.println(F("Brew started "));
} }
while (!Console) { while (!Console) { ; }
;
}
} }
void SpacebrewYun::monitor() { void SpacebrewYun::monitor() {
while (Console.available() > 0) {
char c = Console.read();
if (c == char(CONNECTION_START) && !_connected) { // if not connected try to reconnect after appropriate interval
if (_verbose) { if (_started && !_connected) {
Serial.print(F("Connected to spacebrew server at: ")); if ((millis() - connect_attempt) > connect_attempt_inter) {
Serial.println(server); connect(server, port);
Serial.print(F("Application name set to: ")); }
Serial.println(name); }
}
if (_onOpen != NULL) {
_onOpen();
}
_connected = true;
}
else if (c == char(CONNECTION_END) && _connected) {
_connected = false;
if (_verbose) {
Serial.print(F("Disconnected from spacebrew server at: "));
Serial.println(server);
}
if (_onClose != NULL) {
_onClose();
}
}
if (_verbose) { // if message received from console, then process it
if (c == char(CONNECTION_ERROR)) { while (Console.available() > 0) {
_error_msg = true; char c = Console.read();
Serial.println(F("ERROR :: with Spacebrew.py Connection ::"));
}
else if (_error_msg && c == char(MSG_END)) {
_error_msg = false;
Serial.println();
}
if (_error_msg) {
Serial.print(c);
}
}
if (_connected) { if (c == char(CONNECTION_START) && _started && !_connected) {
if (c == char(MSG_START)) { if (_verbose) {
read_name = true; Serial.print(F("Connected to spacebrew server at: "));
} else if (c == char(MSG_DIV) || sub_name.length() > sub_name_max) { Serial.println(server);
read_name = false; Serial.print(F("Application name set to: "));
read_msg = true; Serial.println(name);
} else if (c == char(MSG_END) || sub_msg.length() > sub_msg_str_max) { }
if (read_msg == true) { if (_onOpen != NULL){
read_msg = false; _onOpen();
onMessage(); }
// delay(2); _connected = true;
} }
if (read_confirm == true) {
read_confirm = false;
onConfirm();
delay(2);
}
} else if (c == char(MSG_CONFIRM)) {
read_confirm = true;
} else {
if (read_name == true) {
sub_name += c;
} else if (read_confirm == true) {
sub_name += c;
} else if (read_msg == true) {
sub_msg += c;
}
else if (_verbose) {
Serial.print(c);
}
}
}
}
if (publishers != NULL) { else if (c == char(CONNECTION_END) && _connected) {
struct Publisher *curr = publishers; _connected = false;
while ((curr != NULL)) { if (_verbose) {
Serial.print(F("Disconnected from spacebrew server at: "));
Serial.println(server);
}
if (_onClose != NULL){
_onClose();
}
}
if ( (curr->confirmed == 0) && ((millis() - curr->time) > 50) ) { if (_verbose) {
if (_verbose) { if (c == char(CONNECTION_ERROR)) {
Serial.print(F("resending msg: ")); _error_msg = true;
Serial.println(curr->name); Serial.println(F("ERROR :: with Spacebrew.py Connection ::"));
} }
send(curr->name, curr->lastMsg); else if (_error_msg && c == char(MSG_END)) {
} _error_msg = false;
curr = curr->next; Serial.println();
} }
} if (_error_msg) {
Serial.print(c);
}
}
if (_connected) {
if (c == char(MSG_START)) {
read_name = true;
} else if (c == char(MSG_DIV) || sub_name.length() > sub_name_max) {
read_name = false;
read_msg = true;
} else if (c == char(MSG_END) || sub_msg.length() > sub_msg_str_max) {
if (read_msg == true) {
read_msg = false;
onMessage();
// delay(2);
}
if (read_confirm == true) {
read_confirm = false;
onConfirm();
delay(2);
}
} else if (c == char(MSG_CONFIRM)) {
read_confirm = true;
} else {
if (read_name == true) {
sub_name += c;
} else if (read_confirm == true) {
sub_name += c;
} else if (read_msg == true) {
sub_msg += c;
}
else if (_verbose) {
Serial.print(c);
}
}
}
}
// check if received confirmation that linino received messages
if (publishers != NULL && _connected) {
struct Publisher *curr = publishers;
while((curr != NULL)){
if ( (curr->confirmed == 0) && ((millis() - curr->time) > 50) ) {
if (_verbose) {
Serial.print(F("resending msg: "));
Serial.println(curr->name);
}
send(curr->name, curr->lastMsg);
}
curr = curr->next;
}
}
} }
void SpacebrewYun::onConfirm() { void SpacebrewYun::onConfirm() {
if (publishers != NULL) { if (publishers != NULL) {
struct Publisher *curr = publishers; struct Publisher *curr = publishers;
while ((curr != NULL)) { while((curr != NULL)){
if (sub_name.equals(curr->name) == true) { if (sub_name.equals(curr->name) == true) {
curr->confirmed = true; curr->confirmed = true;
// if (_verbose) { break;
// Serial.print(F("confirmed ")); }
// Serial.println(curr->name); curr = curr->next;
// } }
break; }
}
curr = curr->next;
}
}
sub_name = ""; sub_name = "";
sub_msg = ""; sub_msg = "";
sub_type = ""; sub_type = "";
} }
boolean SpacebrewYun::connected() { boolean SpacebrewYun::connected() {
return SpacebrewYun::_connected; return SpacebrewYun::_connected;
} }
void SpacebrewYun::verbose(boolean verbose = true) { void SpacebrewYun::verbose(boolean verbose = true) {
_verbose = verbose; _verbose = verbose;
} }
void SpacebrewYun::onMessage() { void SpacebrewYun::onMessage() {
if (subscribers != NULL) { if (subscribers != NULL) {
struct Subscriber *curr = subscribers; struct Subscriber *curr = subscribers;
while ((curr != NULL) && (sub_type == "")) { while((curr != NULL) && (sub_type == "")){
if (sub_name.equals(curr->name) == true) { if (sub_name.equals(curr->name) == true) {
sub_type = curr->type; sub_type = curr->type;
} }
curr = curr->next; curr = curr->next;
} }
} }
if ( sub_type.equals("range") ) { if ( sub_type.equals("range") ) {
if (_onRangeMessage != NULL) { if (_onRangeMessage != NULL) {
_onRangeMessage( sub_name, int(sub_msg.toInt()) ); _onRangeMessage( sub_name, int(sub_msg.toInt()) );
} else { } else {
Serial.println(F("ERROR :: Range message received, no callback method is registered")); Serial.println(F("ERROR :: Range message received, no callback method is registered"));
} }
} else if ( sub_type.equals("boolean") ) { } else if ( sub_type.equals("boolean") ) {
if (_onBooleanMessage != NULL) { if (_onBooleanMessage != NULL) {
_onBooleanMessage( sub_name, ( sub_msg.equals("false") ? false : true ) ); _onBooleanMessage( sub_name, ( sub_msg.equals("false") ? false : true ) );
} else { } else {
Serial.println(F("ERROR :: Boolean message received, no callback method is registered")); Serial.println(F("ERROR :: Boolean message received, no callback method is registered"));
} }
} else if ( sub_type.equals("string") ) { } else if ( sub_type.equals("string") ) {
if (_onStringMessage != NULL) { if (_onStringMessage != NULL) {
_onStringMessage( sub_name, sub_msg ); _onStringMessage( sub_name, sub_msg );
} else { } else {
Serial.println(F("ERROR :: String message received, no callback method is registered")); Serial.println(F("ERROR :: String message received, no callback method is registered"));
} }
} else { } else {
if (_onCustomMessage != NULL) { if (_onCustomMessage != NULL) {
_onCustomMessage( sub_name, sub_msg, sub_type ); _onCustomMessage( sub_name, sub_msg, sub_type );
} else { } else {
Serial.println(F("ERROR :: Custom message received, no callback method is registered")); Serial.println(F("ERROR :: Custom message received, no callback method is registered"));
} }
} }
sub_name = ""; sub_name = "";
sub_msg = ""; sub_msg = "";
sub_type = ""; sub_type = "";
} }
void SpacebrewYun::send(const String& name, const String& value) { void SpacebrewYun::send(const String& name, const String& value){
if (publishers != NULL) { if (publishers != NULL) {
Console.print(char(29)); Console.print(char(29));
Console.print(name); Console.print(name);
Console.print(char(30)); Console.print(char(30));
Console.print(value); Console.print(value);
Console.print(char(31)); Console.print(char(31));
Console.flush(); Console.flush();
struct Publisher *curr = publishers; struct Publisher *curr = publishers;
while (curr != NULL) { while(curr != NULL){
if (name.equals(curr->name) == true) { if (name.equals(curr->name) == true) {
int msg_len = 0; int msg_len = 0;
if (curr->type == "range") msg_len = sub_msg_int_max; if (curr->type == "range") msg_len = sub_msg_int_max;
else if (curr->type == "boolean") msg_len = sub_msg_bool_max; else if (curr->type == "boolean") msg_len = sub_msg_bool_max;
else msg_len = sub_msg_str_max; else msg_len = sub_msg_str_max;
if (value.length() < msg_len) msg_len = value.length() + 1; if (value.length() < msg_len) msg_len = value.length() + 1;
value.toCharArray(curr->lastMsg, msg_len); value.toCharArray(curr->lastMsg, msg_len);
curr->confirmed = false; curr->confirmed = false;
curr->time = millis(); curr->time = millis();
} }
curr = curr->next; curr = curr->next;
} }
} }
} }
@ -395,67 +407,67 @@ void SpacebrewYun::send(const String& name, const String& value) {
*/ */
void SpacebrewYun::getPids() { void SpacebrewYun::getPids() {
// request the pid of all python processes // request the pid of all python processes
// brew.begin("run-getsbpids"); // Process should launch the "curl" command // brew.begin("run-getsbpids"); // Process should launch the "curl" command
pids.begin("python"); pids.begin("python");
pids.addParameter("/usr/lib/python2.7/spacebrew/getprocpid.py"); // Process should launch the "curl" command pids.addParameter("/usr/lib/python2.7/spacebrew/getprocpid.py"); // Process should launch the "curl" command
pids.run(); pids.run();
if (_verbose) { if (_verbose) {
Serial.println(F("Checking if spacebrew process already running")); Serial.println(F("Checking if spacebrew process already running"));
} }
int sbPidsIndex = 0; int sbPidsIndex = 0;
int pidCharIndex = 0; int pidCharIndex = 0;
char c = '\0'; char c = '\0';
while ( pids.available() > 0 ) { while ( pids.available() > 0 ) {
c = pids.read(); c = pids.read();
if ( c >= '0' && c <= '9' ) { if ( c >= '0' && c <= '9' ) {
pid[pidCharIndex] = c; pid[pidCharIndex] = c;
pidCharIndex = (pidCharIndex + 1) % pidLength; pidCharIndex = (pidCharIndex + 1) % pidLength;
} }
else if ( (c == ' ' || c == '\n') && pidCharIndex > 0) { else if ( (c == ' ' || c == '\n') && pidCharIndex > 0) {
sbPids[sbPidsIndex] = atoi(pid); sbPids[sbPidsIndex] = atoi(pid);
if ( sbPidsIndex < (sbPidsLen - 1) ) sbPidsIndex = (sbPidsIndex + 1); if ( sbPidsIndex < (sbPidsLen - 1) ) sbPidsIndex = (sbPidsIndex + 1);
for ( int i = 0; i < pidLength; i++ ) { for( int i = 0; i < pidLength; i++ ){
pid[i] = '\0'; pid[i] = '\0';
pidCharIndex = 0; pidCharIndex = 0;
} }
} }
} }
} }
/** /**
* method that kills all of the spacebrew.py instances that are running * method that kills all of the spacebrew.py instances that are running
* on the linino. * on the linino.
*/ */
void SpacebrewYun::killPids() { void SpacebrewYun::killPids() {
getPids(); getPids();
delay(400); delay(400);
for (int i = 0; i < sbPidsLen; i ++) { for (int i = 0; i < sbPidsLen; i ++) {
if (sbPids[i] > 0) { if (sbPids[i] > 0) {
char * newPID = itoa(sbPids[i], pid, 10); char * newPID = itoa(sbPids[i], pid, 10);
if (_verbose) { if (_verbose) {
Serial.print(F("Stopping existing spacebrew processes with pids: ")); Serial.print(F("Stopping existing spacebrew processes with pids: "));
Serial.println(newPID); Serial.println(newPID);
} }
Process p; Process p;
p.begin("kill"); p.begin("kill");
p.addParameter("-9"); p.addParameter("-9");
p.addParameter(newPID); // Process should launch the "curl" command p.addParameter(newPID); // Process should launch the "curl" command
p.run(); // Run the process and wait for its termination p.run(); // Run the process and wait for its termination
delay(400); delay(400);
} }
} }
} }

View File

@ -7,29 +7,29 @@
#include <Console.h> #include <Console.h>
#include <Process.h> #include <Process.h>
enum SBmsg { enum SBmsg {
CONNECTION_START = char(28), CONNECTION_START = char(28),
CONNECTION_END = char(27), CONNECTION_END = char(27),
CONNECTION_ERROR = char(26), CONNECTION_ERROR = char(26),
MSG_CONFIRM = char(7), MSG_CONFIRM = char(7),
MSG_START = char(29), MSG_START = char(29),
MSG_DIV = char(30), MSG_DIV = char(30),
MSG_END = char(31) MSG_END = char(31)
}; };
struct Publisher { struct Publisher {
char *name; char *name;
char *type; char *type;
char *lastMsg; char *lastMsg;
Publisher *next; Publisher *next;
int confirmed; int confirmed;
long time; long time;
}; };
struct Subscriber { struct Subscriber{
char *name; char *name;
char *type; char *type;
Subscriber *next; Subscriber *next;
}; };
int const pidLength = 6; int const pidLength = 6;
@ -37,114 +37,102 @@ int const sbPidsLen = 4;
class SpacebrewYun { class SpacebrewYun {
public: public:
SpacebrewYun(const String&, const String&); SpacebrewYun(const String&, const String&);
void addPublish(const String&, const String&); void addPublish(const String&, const String&);
void addSubscribe(const String&, const String&); void addSubscribe(const String&, const String&);
void connect(String, int); void connect(String, int);
void connect() { void connect() { connect(server, port); };
connect(server, port); void connect(String _server) { connect(String(_server), port); };
};
void connect(String _server) {
connect(String(_server), port);
};
void monitor(); void monitor();
void onMessage(); void onMessage();
void onConfirm(); void onConfirm();
boolean connected(); boolean connected();
void send(const String&, const String&); void send(const String&, const String&);
void send(const String& name, char * value) { void send(const String& name, char * value) { send(name, String(value)); }
send(name, String(value)); void send(const String& name, bool value){ send(name, (value ? String("true") : String("false"))); };
} void send(const String& name, int value) { send(name, String(value)); };
void send(const String& name, bool value) { void send(const String& name, long value) { send(name, String(value)); };
send(name, (value ? String("true") : String("false"))); void send(const String& name, float value) { send(name, String(value)); };
};
void send(const String& name, int value) {
send(name, String(value));
};
void send(const String& name, long value) {
send(name, String(value));
};
void send(const String& name, float value) {
send(name, String(value));
};
void verbose(boolean); void verbose(boolean);
typedef void (*OnBooleanMessage)(String name, boolean value); typedef void (*OnBooleanMessage)(String name, boolean value);
typedef void (*OnRangeMessage)(String name, int value); typedef void (*OnRangeMessage)(String name, int value);
typedef void (*OnStringMessage)(String name, String value); typedef void (*OnStringMessage)(String name, String value);
typedef void (*OnCustomMessage)(String name, String value, String type); typedef void (*OnCustomMessage)(String name, String value, String type);
typedef void (*OnSBOpen)(); typedef void (*OnSBOpen)();
typedef void (*OnSBClose)(); typedef void (*OnSBClose)();
typedef void (*OnSBError)(int code, String message); typedef void (*OnSBError)(int code, String message);
void onOpen(OnSBOpen function); void onOpen(OnSBOpen function);
void onClose(OnSBClose function); void onClose(OnSBClose function);
void onRangeMessage(OnRangeMessage function); void onRangeMessage(OnRangeMessage function);
void onStringMessage(OnStringMessage function); void onStringMessage(OnStringMessage function);
void onBooleanMessage(OnBooleanMessage function); void onBooleanMessage(OnBooleanMessage function);
void onCustomMessage(OnCustomMessage function); void onCustomMessage(OnCustomMessage function);
void onError(OnSBError function); void onError(OnSBError function);
private: private:
Process brew; Process brew;
String name; String name;
String server; String server;
String description; String description;
boolean _connected; boolean _started;
boolean _error_msg; boolean _connected;
boolean _verbose; boolean _error_msg;
int port; boolean _verbose;
int port;
/**Output should be at least 5 cells**/ /**Output should be at least 5 cells**/
static OnBooleanMessage _onBooleanMessage; static OnBooleanMessage _onBooleanMessage;
static OnRangeMessage _onRangeMessage; static OnRangeMessage _onRangeMessage;
static OnStringMessage _onStringMessage; static OnStringMessage _onStringMessage;
static OnCustomMessage _onCustomMessage; static OnCustomMessage _onCustomMessage;
static OnSBOpen _onOpen; static OnSBOpen _onOpen;
static OnSBClose _onClose; static OnSBClose _onClose;
static OnSBError _onError; static OnSBError _onError;
Subscriber * subscribers; Subscriber * subscribers;
Publisher * publishers; Publisher * publishers;
String sub_name; String sub_name;
String sub_msg; String sub_msg;
String sub_type; String sub_type;
boolean read_name; boolean read_name;
boolean read_msg; boolean read_msg;
boolean read_confirm; boolean read_confirm;
static int sub_name_max; static int sub_name_max;
static int sub_msg_str_max; static int sub_msg_str_max;
static int sub_msg_int_max; static int sub_msg_int_max;
static int sub_msg_bool_max; static int sub_msg_bool_max;
// int sub_name_max;
// int sub_msg_str_max;
Process pids; long connect_attempt;
char pid [6]; int connect_attempt_inter;
int sbPids [4];
void killPids(); Process pids;
void getPids(); char pid [6];
int sbPids [4];
static char * createString(int len) { void killPids();
char * out = ( char * ) malloc ( len + 1 ); void getPids();
return out;
}
static void emptyString(char * str, int len) { static char * createString(int len){
for (int i = 0; i < len; i++) { char * out = ( char * ) malloc ( len + 1 );
str[i] = '\0'; return out;
} }
}
static void emptyString(char * str, int len){
for (int i = 0; i < len; i++) {
str[i] = '\0';
}
}
}; };