2
2
Input Output
3
3
4
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
5
+ spacebrew data types, and a custom data type. Every time data is
6
6
received it is output to the Serial monitor.
7
7
8
- Make sure that your Yun is connected to the internet for this example
8
+ Make sure that your Yun is connected to the internet for this example
9
9
to function properly.
10
-
10
+
11
11
The circuit:
12
12
- No circuit required
13
-
13
+
14
14
created 2013
15
15
by Julio Terra
16
-
16
+
17
17
This example code is in the public domain.
18
-
19
- More information about Spacebrew is available at:
18
+
19
+ More information about Spacebrew is available at:
20
20
http://spacebrew.cc/
21
-
21
+
22
22
*/
23
23
24
24
#include < Bridge.h>
@@ -33,100 +33,100 @@ int interval = 2000;
33
33
34
34
int counter = 0 ;
35
35
36
- void setup () {
37
-
38
- // start the serial port
39
- Serial.begin (57600 );
36
+ void setup () {
40
37
41
- // for debugging, wait until a serial console is connected
42
- delay (4000 );
43
- while (!Serial) {
44
- ;
45
- }
38
+ // start the serial port
39
+ Serial.begin (57600 );
46
40
47
- // start-up the bridge
48
- Bridge.begin ();
41
+ // for debugging, wait until a serial console is connected
42
+ delay (4000 );
43
+ while (!Serial) { ; }
49
44
50
- // configure the spacebrew object to print status messages to serial
51
- sb. verbose ( true );
45
+ // start-up the bridge
46
+ Bridge. begin ( );
52
47
53
- // configure the spacebrew publisher and subscriber
54
- sb.addPublish (" string test" , " string" );
55
- sb.addPublish (" range test" , " range" );
56
- sb.addPublish (" boolean test" , " boolean" );
57
- sb.addPublish (" custom test" , " crazy" );
58
- sb.addSubscribe (" string test" , " string" );
59
- sb.addSubscribe (" range test" , " range" );
60
- sb.addSubscribe (" boolean test" , " boolean" );
61
- sb.addSubscribe (" custom test" , " crazy" );
48
+ // configure the spacebrew object to print status messages to serial
49
+ sb.verbose (true );
62
50
63
- // register the string message handler method
64
- sb.onRangeMessage (handleRange);
65
- sb.onStringMessage (handleString);
66
- sb.onBooleanMessage (handleBoolean);
67
- sb.onCustomMessage (handleCustom);
51
+ // configure the spacebrew publisher and subscriber
52
+ sb.addPublish (" string test" , " string" );
53
+ sb.addPublish (" range test" , " range" );
54
+ sb.addPublish (" boolean test" , " boolean" );
55
+ sb.addPublish (" custom test" , " crazy" );
56
+ sb.addSubscribe (" string test" , " string" );
57
+ sb.addSubscribe (" range test" , " range" );
58
+ sb.addSubscribe (" boolean test" , " boolean" );
59
+ sb.addSubscribe (" custom test" , " crazy" );
68
60
69
- // connect to cloud spacebrew server at "sandbox.spacebrew.cc"
70
- sb.connect (" sandbox.spacebrew.cc" );
61
+ // register the string message handler method
62
+ sb.onRangeMessage (handleRange);
63
+ sb.onStringMessage (handleString);
64
+ sb.onBooleanMessage (handleBoolean);
65
+ sb.onCustomMessage (handleCustom);
71
66
72
- }
67
+ // connect to cloud spacebrew server at "sandbox.spacebrew.cc"
68
+ sb.connect (" sandbox.spacebrew.cc" );
69
+ // we give some time to arduino to connect to sandbox, otherwise the first sb.monitor(); call will give an error
70
+ delay (1000 );
71
+ }
73
72
74
73
75
- void loop () {
76
- // monitor spacebrew connection for new data
77
- sb.monitor ();
74
+ void loop () {
75
+ // monitor spacebrew connection for new data
76
+ sb.monitor ();
78
77
79
- // connected to spacebrew then send a string every 2 seconds
80
- if ( sb.connected () ) {
78
+ // connected to spacebrew then send a string every 2 seconds
79
+ if ( sb.connected () ) {
81
80
82
- // check if it is time to send a new message
83
- if ( (millis () - last) > interval ) {
84
- String test_str_msg = " testing, testing, " ;
85
- test_str_msg += counter;
86
- counter ++;
81
+ // check if it is time to send a new message
82
+ if ( (millis () - last) > interval ) {
83
+ String test_str_msg = " testing, testing, " ;
84
+ test_str_msg += counter;
85
+ counter ++;
87
86
88
- sb.send (" string test" , test_str_msg);
89
- sb.send (" range test" , 500 );
90
- sb.send (" boolean test" , true );
91
- sb.send (" custom test" , " youre loco" );
87
+ sb.send (" string test" , test_str_msg);
88
+ sb.send (" range test" , 500 );
89
+ sb.send (" boolean test" , true );
90
+ sb.send (" custom test" , " youre loco" );
92
91
93
- last = millis ();
92
+ last = millis ();
94
93
95
- }
96
- }
97
- }
94
+ }
95
+ }
96
+ delay (1000 );
97
+ }
98
98
99
99
// define handler methods, all standard data type handlers take two appropriate arguments
100
100
101
101
void handleRange (String route, int value) {
102
- Serial.print (" Range msg " );
103
- Serial.print (route);
104
- Serial.print (" , value " );
105
- Serial.println (value);
102
+ Serial.print (" Range msg " );
103
+ Serial.print (route);
104
+ Serial.print (" , value " );
105
+ Serial.println (value);
106
106
}
107
107
108
108
void handleString (String route, String value) {
109
- Serial.print (" String msg " );
110
- Serial.print (route);
111
- Serial.print (" , value " );
112
- Serial.println (value);
109
+ Serial.print (" String msg " );
110
+ Serial.print (route);
111
+ Serial.print (" , value " );
112
+ Serial.println (value);
113
113
}
114
114
115
115
void handleBoolean (String route, boolean value) {
116
- Serial.print (" Boolen msg " );
117
- Serial.print (route);
118
- Serial.print (" , value " );
119
- Serial.println (value ? " true" : " false" );
116
+ Serial.print (" Boolen msg " );
117
+ Serial.print (route);
118
+ Serial.print (" , value " );
119
+ Serial.println (value ? " true" : " false" );
120
120
}
121
121
122
122
// custom data type handlers takes three String arguments
123
123
124
124
void handleCustom (String route, String value, String type) {
125
- Serial.print (" Custom msg " );
126
- Serial.print (route);
127
- Serial.print (" of type " );
128
- Serial.print (type);
129
- Serial.print (" , value " );
130
- Serial.println (value);
125
+ Serial.print (" Custom msg " );
126
+ Serial.print (route);
127
+ Serial.print (" of type " );
128
+ Serial.print (type);
129
+ Serial.print (" , value " );
130
+ Serial.println (value);
131
131
}
132
132
0 commit comments