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 Y炭n 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("aYun", "Arduino Yun spacebrew test");
|
---|
29 |
|
---|
30 | // create variables to manage interval between each time we send a string
|
---|
31 | long last = 0;
|
---|
32 | int interval = 2000;
|
---|
33 |
|
---|
34 | int counter = 0;
|
---|
35 |
|
---|
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("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");
|
---|
60 |
|
---|
61 | // register the string message handler method
|
---|
62 | sb.onRangeMessage(handleRange);
|
---|
63 | sb.onStringMessage(handleString);
|
---|
64 | sb.onBooleanMessage(handleBoolean);
|
---|
65 | sb.onCustomMessage(handleCustom);
|
---|
66 |
|
---|
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 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | void loop() {
|
---|
75 | // monitor spacebrew connection for new data
|
---|
76 | sb.monitor();
|
---|
77 |
|
---|
78 | // connected to spacebrew then send a string every 2 seconds
|
---|
79 | if ( sb.connected() ) {
|
---|
80 |
|
---|
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 ++;
|
---|
86 |
|
---|
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");
|
---|
91 |
|
---|
92 | last = millis();
|
---|
93 |
|
---|
94 | }
|
---|
95 | }
|
---|
96 | delay(1000);
|
---|
97 | }
|
---|
98 |
|
---|
99 | // define handler methods, all standard data type handlers take two appropriate arguments
|
---|
100 |
|
---|
101 | void handleRange (String route, int value) {
|
---|
102 | Serial.print("Range msg ");
|
---|
103 | Serial.print(route);
|
---|
104 | Serial.print(", value ");
|
---|
105 | Serial.println(value);
|
---|
106 | }
|
---|
107 |
|
---|
108 | void handleString (String route, String value) {
|
---|
109 | Serial.print("String msg ");
|
---|
110 | Serial.print(route);
|
---|
111 | Serial.print(", value ");
|
---|
112 | Serial.println(value);
|
---|
113 | }
|
---|
114 |
|
---|
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");
|
---|
120 | }
|
---|
121 |
|
---|
122 | // custom data type handlers takes three String arguments
|
---|
123 |
|
---|
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);
|
---|
131 | }
|
---|
132 |
|
---|