[136] | 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 Y炭n is connected to the internet for this example
|
---|
| 11 | to function properly.
|
---|
| 12 |
|
---|
| 13 | The circuit:
|
---|
| 14 | - Button connected to Y炭n, 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 |
|
---|