22 Input Output
33
44 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
66 received it is output to the Serial monitor.
77
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
99 to function properly.
10-
10+
1111 The circuit:
1212 - No circuit required
13-
13+
1414 created 2013
1515 by Julio Terra
16-
16+
1717 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:
2020 http://spacebrew.cc/
21-
21+
2222 */
2323
2424#include < Bridge.h>
@@ -33,100 +33,100 @@ int interval = 2000;
3333
3434int counter = 0 ;
3535
36- void setup () {
37-
38- // start the serial port
39- Serial.begin (57600 );
36+ void setup () {
4037
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 );
4640
47- // start-up the bridge
48- Bridge.begin ();
41+ // for debugging, wait until a serial console is connected
42+ delay (4000 );
43+ while (!Serial) { ; }
4944
50- // configure the spacebrew object to print status messages to serial
51- sb. verbose ( true );
45+ // start-up the bridge
46+ Bridge. begin ( );
5247
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 );
6250
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" );
6860
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);
7166
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+ }
7372
7473
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 ();
7877
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 () ) {
8180
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 ++;
8786
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" );
9291
93- last = millis ();
92+ last = millis ();
9493
95- }
96- }
97- }
94+ }
95+ }
96+ delay (1000 );
97+ }
9898
9999// define handler methods, all standard data type handlers take two appropriate arguments
100100
101101void 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);
106106}
107107
108108void 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);
113113}
114114
115115void 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" );
120120}
121121
122122// custom data type handlers takes three String arguments
123123
124124void 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);
131131}
132132
0 commit comments