1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-01-05 20:46:08 +01:00
Arduino/libraries/Bridge/examples/SpacebrewYun/inputOutput/inputOutput.ino

131 lines
3.1 KiB
Arduino
Raw Normal View History

2013-08-07 17:16:13 +02:00
/*
Input Output
Demonstrates how to create a sketch that sends and receives all standard
spacebrew data types, and a custom data type. Every time data is
received it is output to the Serial monitor.
Make sure that your Yun is connected to the internet for this example
to function properly.
The circuit:
- No circuit required
created 2013
by Julio Terra
This example code is in the public domain.
More information about Spacebrew is available at:
http://spacebrew.cc/
*/
2013-07-18 14:07:39 +02:00
#include <Bridge.h>
#include <SpacebrewYun.h>
2013-08-07 17:16:13 +02:00
// create a variable of type SpacebrewYun and initialize it with the constructor
2013-07-18 14:07:39 +02:00
SpacebrewYun sb = SpacebrewYun("aYun", "Arduino Yun spacebrew test");
2013-08-07 17:16:13 +02:00
// create variables to manage interval between each time we send a string
2013-07-18 14:07:39 +02:00
long last = 0;
int interval = 2000;
2013-08-07 17:16:13 +02:00
int counter = 0;
2013-07-18 14:07:39 +02:00
void setup() {
2013-08-07 17:16:13 +02:00
// start the serial port
2013-07-18 14:07:39 +02:00
Serial.begin(57600);
2013-08-07 17:16:13 +02:00
// for debugging, wait until a serial console is connected
delay(4000);
while (!Serial) { ; }
2013-07-18 14:07:39 +02:00
2013-08-07 17:16:13 +02:00
// start-up the bridge
2013-07-18 14:07:39 +02:00
Bridge.begin();
2013-08-07 17:16:13 +02:00
// configure the spacebrew object to print status messages to serial
2013-07-18 14:07:39 +02:00
sb.verbose(true);
2013-08-07 17:16:13 +02:00
// configure the spacebrew publisher and subscriber
2013-07-18 14:07:39 +02:00
sb.addPublish("string test", "string");
sb.addPublish("range test", "range");
sb.addPublish("boolean test", "boolean");
sb.addPublish("custom test", "crazy");
sb.addSubscribe("string test", "string");
sb.addSubscribe("range test", "range");
sb.addSubscribe("boolean test", "boolean");
sb.addSubscribe("custom test", "crazy");
2013-08-07 17:16:13 +02:00
// register the string message handler method
2013-07-18 14:07:39 +02:00
sb.onRangeMessage(handleRange);
sb.onStringMessage(handleString);
sb.onBooleanMessage(handleBoolean);
sb.onCustomMessage(handleCustom);
2013-08-07 17:16:13 +02:00
// connect to cloud spacebrew server at "sandbox.spacebrew.cc"
2013-07-18 14:07:39 +02:00
sb.connect("sandbox.spacebrew.cc");
}
void loop() {
2013-08-07 17:16:13 +02:00
// monitor spacebrew connection for new data
2013-07-18 14:07:39 +02:00
sb.monitor();
2013-08-07 17:16:13 +02:00
// connected to spacebrew then send a string every 2 seconds
2013-07-18 14:07:39 +02:00
if ( sb.connected() ) {
2013-08-07 17:16:13 +02:00
// check if it is time to send a new message
2013-07-18 14:07:39 +02:00
if ( (millis() - last) > interval ) {
String test_str_msg = "testing, testing, ";
test_str_msg += counter;
counter ++;
sb.send("string test", test_str_msg);
sb.send("range test", 500);
sb.send("boolean test", true);
sb.send("custom test", "youre loco");
last = millis();
}
}
}
2013-08-07 17:16:13 +02:00
// define handler methods, all standard data type handlers take two appropriate arguments
2013-07-18 14:07:39 +02:00
void handleRange (String route, int value) {
Serial.print("Range msg ");
Serial.print(route);
Serial.print(", value ");
Serial.println(value);
}
void handleString (String route, String value) {
Serial.print("String msg ");
Serial.print(route);
Serial.print(", value ");
Serial.println(value);
}
void handleBoolean (String route, boolean value) {
Serial.print("Boolen msg ");
Serial.print(route);
Serial.print(", value ");
Serial.println(value ? "true" : "false");
}
2013-08-07 17:16:13 +02:00
// custom data type handlers takes three String arguments
2013-07-18 14:07:39 +02:00
void handleCustom (String route, String value, String type) {
Serial.print("Custom msg ");
Serial.print(route);
Serial.print(" of type ");
Serial.print(type);
Serial.print(", value ");
Serial.println(value);
}