Skip to content

Commit 7832a99

Browse files
author
Federico Fissore
committed
spacebrew update
1 parent 8fd9b8f commit 7832a99

File tree

6 files changed

+474
-65
lines changed

6 files changed

+474
-65
lines changed

hardware/arduino/avr/libraries/Bridge/examples/SpacebrewYun/input_output/input_output.ino renamed to hardware/arduino/avr/libraries/Bridge/examples/SpacebrewYun/inputOutput/inputOutput.ino

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,54 @@
1+
/*
2+
Input Output
3+
4+
Demonstrates how to create a sketch that sends and receives all standard
5+
spacebrew data types, and a custom data type. Every time data is
6+
received it is output to the Serial monitor.
7+
8+
Make sure that your Yun is connected to the internet for this example
9+
to function properly.
10+
11+
The circuit:
12+
- No circuit required
13+
14+
created 2013
15+
by Julio Terra
16+
17+
This example code is in the public domain.
18+
19+
More information about Spacebrew is available at:
20+
http://spacebrew.cc/
21+
22+
*/
23+
124
#include <Bridge.h>
225
#include <SpacebrewYun.h>
326

4-
/**
5-
* Arduino Yun Spacebrew Library Example
6-
*
7-
* This example code is in the public domain.
8-
*
9-
* @date July 16, 2013
10-
* @author Julio Terra
11-
*
12-
*/
13-
27+
// create a variable of type SpacebrewYun and initialize it with the constructor
1428
SpacebrewYun sb = SpacebrewYun("aYun", "Arduino Yun spacebrew test");
1529

16-
int counter = 0;
30+
// create variables to manage interval between each time we send a string
1731
long last = 0;
1832
int interval = 2000;
1933

34+
int counter = 0;
35+
2036
void setup() {
2137

38+
// start the serial port
2239
Serial.begin(57600);
23-
delay(4000);
24-
while (!Serial) { Serial.println("connecting"); }
2540

41+
// for debugging, wait until a serial console is connected
42+
delay(4000);
43+
while (!Serial) { ; }
2644

27-
//Initialize Console and wait for port to open:
45+
// start-up the bridge
2846
Bridge.begin();
29-
Serial.println("Bridge Started");
3047

31-
Serial.println("Configuring Spacebrew Client");
48+
// configure the spacebrew object to print status messages to serial
3249
sb.verbose(true);
50+
51+
// configure the spacebrew publisher and subscriber
3352
sb.addPublish("string test", "string");
3453
sb.addPublish("range test", "range");
3554
sb.addPublish("boolean test", "boolean");
@@ -38,18 +57,27 @@ void setup() {
3857
sb.addSubscribe("range test", "range");
3958
sb.addSubscribe("boolean test", "boolean");
4059
sb.addSubscribe("custom test", "crazy");
60+
61+
// register the string message handler method
4162
sb.onRangeMessage(handleRange);
4263
sb.onStringMessage(handleString);
4364
sb.onBooleanMessage(handleBoolean);
4465
sb.onCustomMessage(handleCustom);
66+
67+
// connect to cloud spacebrew server at "sandbox.spacebrew.cc"
4568
sb.connect("sandbox.spacebrew.cc");
4669

4770
}
4871

4972

5073
void loop() {
74+
// monitor spacebrew connection for new data
5175
sb.monitor();
76+
77+
// connected to spacebrew then send a string every 2 seconds
5278
if ( sb.connected() ) {
79+
80+
// check if it is time to send a new message
5381
if ( (millis() - last) > interval ) {
5482
String test_str_msg = "testing, testing, ";
5583
test_str_msg += counter;
@@ -66,6 +94,8 @@ void loop() {
6694
}
6795
}
6896

97+
// define handler methods, all standard data type handlers take two appropriate arguments
98+
6999
void handleRange (String route, int value) {
70100
Serial.print("Range msg ");
71101
Serial.print(route);
@@ -87,6 +117,8 @@ void handleBoolean (String route, boolean value) {
87117
Serial.println(value ? "true" : "false");
88118
}
89119

120+
// custom data type handlers takes three String arguments
121+
90122
void handleCustom (String route, String value, String type) {
91123
Serial.print("Custom msg ");
92124
Serial.print(route);
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
Spacebrew Boolean
3+
4+
Demonstrates how to create a sketch that sends and receives a
5+
boolean value to and from Spacebrew. Every time the buttton is
6+
pressed (or other digital input component) a spacebrew message
7+
is sent. The sketch also accepts analog range messages from
8+
other Spacebrew apps.
9+
10+
Make sure that your Yun is connected to the internet for this example
11+
to function properly.
12+
13+
The circuit:
14+
- Button connected to Yun, using the Arduino's internal pullup resistor.
15+
16+
created 2013
17+
by Julio Terra
18+
19+
This example code is in the public domain.
20+
21+
More information about Spacebrew is available at:
22+
http://spacebrew.cc/
23+
24+
*/
25+
26+
#include <Bridge.h>
27+
#include <SpacebrewYun.h>
28+
29+
// create a variable of type SpacebrewYun and initialize it with the constructor
30+
SpacebrewYun sb = SpacebrewYun("spacebrewYun Boolean", "Boolean sender and receiver");
31+
32+
// variable that holds the last potentiometer value
33+
int last_value = 0;
34+
35+
// create variables to manage interval between each time we send a string
36+
void setup() {
37+
38+
// start the serial port
39+
Serial.begin(57600);
40+
41+
// for debugging, wait until a serial console is connected
42+
delay(4000);
43+
while (!Serial) { ; }
44+
45+
// start-up the bridge
46+
Bridge.begin();
47+
48+
// configure the spacebrew object to print status messages to serial
49+
sb.verbose(true);
50+
51+
// configure the spacebrew publisher and subscriber
52+
sb.addPublish("physical button", "boolean");
53+
sb.addSubscribe("virtual button", "boolean");
54+
55+
// register the string message handler method
56+
sb.onBooleanMessage(handleBoolean);
57+
58+
// connect to cloud spacebrew server at "sandbox.spacebrew.cc"
59+
sb.connect("sandbox.spacebrew.cc");
60+
61+
pinMode(3, INPUT);
62+
digitalWrite(3, HIGH);
63+
}
64+
65+
66+
void loop() {
67+
// monitor spacebrew connection for new data
68+
sb.monitor();
69+
70+
// connected to spacebrew then send a new value whenever the pot value changes
71+
if ( sb.connected() ) {
72+
int cur_value = digitalRead(3);
73+
if ( last_value != cur_value ) {
74+
if (cur_value == HIGH) sb.send("physical button", false);
75+
else sb.send("physical button", true);
76+
last_value = cur_value;
77+
}
78+
}
79+
}
80+
81+
// handler method that is called whenever a new string message is received
82+
void handleBoolean (String route, boolean value) {
83+
// print the message that was received
84+
Serial.print("From ");
85+
Serial.print(route);
86+
Serial.print(", received msg: ");
87+
Serial.println(value ? "true" : "false");
88+
}
89+
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
Spacebrew Range
3+
4+
Demonstrates how to create a sketch that sends and receives analog
5+
range value to and from Spacebrew. Every time the state of the
6+
potentiometer (or other analog input component) change a spacebrew
7+
message is sent. The sketch also accepts analog range messages from
8+
other Spacebrew apps.
9+
10+
Make sure that your Yun is connected to the internet for this example
11+
to function properly.
12+
13+
The circuit:
14+
- Potentiometer connected to Yun. Middle pin connected to analog pin A0,
15+
other pins connected to 5v and GND pins.
16+
17+
created 2013
18+
by Julio Terra
19+
20+
This example code is in the public domain.
21+
22+
More information about Spacebrew is available at:
23+
http://spacebrew.cc/
24+
25+
*/
26+
27+
#include <Bridge.h>
28+
#include <SpacebrewYun.h>
29+
30+
// create a variable of type SpacebrewYun and initialize it with the constructor
31+
SpacebrewYun sb = SpacebrewYun("spacebrewYun Range", "Range sender and receiver");
32+
33+
// variable that holds the last potentiometer value
34+
int last_value = 0;
35+
36+
// create variables to manage interval between each time we send a string
37+
void setup() {
38+
39+
// start the serial port
40+
Serial.begin(57600);
41+
42+
// for debugging, wait until a serial console is connected
43+
delay(4000);
44+
while (!Serial) { ; }
45+
46+
// start-up the bridge
47+
Bridge.begin();
48+
49+
// configure the spacebrew object to print status messages to serial
50+
sb.verbose(true);
51+
52+
// configure the spacebrew publisher and subscriber
53+
sb.addPublish("physical pot", "range");
54+
sb.addSubscribe("virtual pot", "range");
55+
56+
// register the string message handler method
57+
sb.onRangeMessage(handleRange);
58+
59+
// connect to cloud spacebrew server at "sandbox.spacebrew.cc"
60+
sb.connect("sandbox.spacebrew.cc");
61+
}
62+
63+
64+
void loop() {
65+
// monitor spacebrew connection for new data
66+
sb.monitor();
67+
68+
// connected to spacebrew then send a new value whenever the pot value changes
69+
if ( sb.connected() ) {
70+
int cur_value = analogRead(A0);
71+
if ( last_value != cur_value ) {
72+
sb.send("physical pot", cur_value);
73+
last_value = cur_value;
74+
}
75+
}
76+
}
77+
78+
// handler method that is called whenever a new string message is received
79+
void handleRange (String route, int value) {
80+
// print the message that was received
81+
Serial.print("From ");
82+
Serial.print(route);
83+
Serial.print(", received msg: ");
84+
Serial.println(value);
85+
}
86+
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
Spacebrew String
3+
4+
Demonstrates how to create a sketch that sends and receives strings
5+
to and from Spacebrew. Every time string data is received it
6+
is output to the Serial monitor.
7+
8+
Make sure that your Yun is connected to the internet for this example
9+
to function properly.
10+
11+
The circuit:
12+
- No circuit required
13+
14+
created 2013
15+
by Julio Terra
16+
17+
This example code is in the public domain.
18+
19+
More information about Spacebrew is available at:
20+
http://spacebrew.cc/
21+
22+
*/
23+
24+
#include <Bridge.h>
25+
#include <SpacebrewYun.h>
26+
27+
// create a variable of type SpacebrewYun and initialize it with the constructor
28+
SpacebrewYun sb = SpacebrewYun("spacebrewYun Strings", "String sender and receiver");
29+
30+
// create variables to manage interval between each time we send a string
31+
long last_time = 0;
32+
int interval = 2000;
33+
34+
void setup() {
35+
36+
// start the serial port
37+
Serial.begin(57600);
38+
39+
// for debugging, wait until a serial console is connected
40+
delay(4000);
41+
while (!Serial) { ; }
42+
43+
// start-up the bridge
44+
Bridge.begin();
45+
46+
// configure the spacebrew object to print status messages to serial
47+
sb.verbose(true);
48+
49+
// configure the spacebrew publisher and subscriber
50+
sb.addPublish("speak", "string");
51+
sb.addSubscribe("listen", "string");
52+
53+
// register the string message handler method
54+
sb.onStringMessage(handleString);
55+
56+
// connect to cloud spacebrew server at "sandbox.spacebrew.cc"
57+
sb.connect("sandbox.spacebrew.cc");
58+
}
59+
60+
61+
void loop() {
62+
// monitor spacebrew connection for new data
63+
sb.monitor();
64+
65+
// connected to spacebrew then send a string every 2 seconds
66+
if ( sb.connected() ) {
67+
68+
// check if it is time to send a new message
69+
if ( (millis() - last_time) > interval ) {
70+
sb.send("speak", "is anybody out there?");
71+
last_time = millis();
72+
}
73+
}
74+
}
75+
76+
// handler method that is called whenever a new string message is received
77+
void handleString (String route, String value) {
78+
// print the message that was received
79+
Serial.print("From ");
80+
Serial.print(route);
81+
Serial.print(", received msg: ");
82+
Serial.println(value);
83+
}
84+

0 commit comments

Comments
 (0)