1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-13 23:48:46 +01:00
Arduino/libraries/Bridge/examples/SpacebrewYun/inputOutput/inputOutput.ino

133 lines
3.2 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
2013-08-07 17:16:13 +02:00
received it is output to the Serial monitor.
Make sure that your Yun is connected to the internet for this example
2013-08-07 17:16:13 +02:00
to function properly.
2013-08-07 17:16:13 +02:00
The circuit:
- No circuit required
2013-08-07 17:16:13 +02:00
created 2013
by Julio Terra
2013-08-07 17:16:13 +02:00
This example code is in the public domain.
More information about Spacebrew is available at:
2013-08-07 17:16:13 +02:00
http://spacebrew.cc/
2013-08-07 17:16:13 +02:00
*/
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;
void setup() {
2013-07-18 14:07:39 +02:00
// start the serial port
Serial.begin(57600);
2013-07-18 14:07:39 +02:00
// for debugging, wait until a serial console is connected
delay(4000);
while (!Serial) {
;
}
2013-07-18 14:07:39 +02:00
// start-up the bridge
Bridge.begin();
2013-07-18 14:07:39 +02:00
// configure the spacebrew object to print status messages to serial
sb.verbose(true);
2013-08-07 17:16:13 +02:00
// configure the spacebrew publisher and subscriber
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
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"
sb.connect("sandbox.spacebrew.cc");
2013-07-18 14:07:39 +02:00
}
2013-07-18 14:07:39 +02:00
void loop() {
// monitor spacebrew connection for new data
sb.monitor();
2013-08-07 17:16:13 +02:00
// connected to spacebrew then send a string every 2 seconds
if ( sb.connected() ) {
2013-08-07 17:16:13 +02:00
// check if it is time to send a new message
if ( (millis() - last) > interval ) {
String test_str_msg = "testing, testing, ";
test_str_msg += counter;
counter ++;
2013-07-18 14:07:39 +02:00
sb.send("string test", test_str_msg);
sb.send("range test", 500);
sb.send("boolean test", true);
sb.send("custom test", "youre loco");
2013-07-18 14:07:39 +02:00
last = millis();
2013-07-18 14:07:39 +02:00
}
}
}
2013-07-18 14:07:39 +02:00
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);
2013-07-18 14:07:39 +02:00
}
void handleString (String route, String value) {
Serial.print("String msg ");
Serial.print(route);
Serial.print(", value ");
Serial.println(value);
2013-07-18 14:07:39 +02:00
}
void handleBoolean (String route, boolean value) {
Serial.print("Boolen msg ");
Serial.print(route);
Serial.print(", value ");
Serial.println(value ? "true" : "false");
2013-07-18 14:07:39 +02:00
}
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);
2013-07-18 14:07:39 +02:00
}