1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-11 22:24:13 +01:00
Arduino/libraries/Bridge/examples/SpacebrewYun/spacebrewString/spacebrewString.ino

85 lines
2.0 KiB
Arduino
Raw Normal View History

2013-08-07 17:16:13 +02:00
/*
Spacebrew String
2014-04-01 09:35:56 +02:00
2013-08-07 17:16:13 +02:00
Demonstrates how to create a sketch that sends and receives strings
2014-04-01 09:35:56 +02:00
to and from Spacebrew. Every time string data is received it
2013-08-07 17:16:13 +02:00
is output to the Serial monitor.
2014-05-30 12:17:09 +02:00
Make sure that your Yún is connected to the internet for this example
2013-08-07 17:16:13 +02:00
to function properly.
2014-04-01 09:35:56 +02:00
2013-08-07 17:16:13 +02:00
The circuit:
- No circuit required
2014-04-01 09:35:56 +02:00
2013-08-07 17:16:13 +02:00
created 2013
by Julio Terra
2014-04-01 09:35:56 +02:00
2013-08-07 17:16:13 +02:00
This example code is in the public domain.
2014-04-01 09:35:56 +02:00
More information about Spacebrew is available at:
2013-08-07 17:16:13 +02:00
http://spacebrew.cc/
2014-04-01 09:35:56 +02:00
2013-08-07 17:16:13 +02:00
*/
#include <Bridge.h>
#include <SpacebrewYun.h>
// create a variable of type SpacebrewYun and initialize it with the constructor
SpacebrewYun sb = SpacebrewYun("spacebrewYun Strings", "String sender and receiver");
// create variables to manage interval between each time we send a string
long last_time = 0;
int interval = 2000;
2014-04-01 09:35:56 +02:00
void setup() {
2013-08-07 17:16:13 +02:00
2014-04-01 09:35:56 +02:00
// start the serial port
Serial.begin(57600);
2013-08-07 17:16:13 +02:00
2014-04-01 09:35:56 +02:00
// for debugging, wait until a serial console is connected
delay(4000);
while (!Serial) { ; }
2013-08-07 17:16:13 +02:00
2014-04-01 09:35:56 +02:00
// start-up the bridge
Bridge.begin();
2013-08-07 17:16:13 +02:00
2014-04-01 09:35:56 +02:00
// configure the spacebrew object to print status messages to serial
sb.verbose(true);
2013-08-07 17:16:13 +02:00
2014-04-01 09:35:56 +02:00
// configure the spacebrew publisher and subscriber
sb.addPublish("speak", "string");
sb.addSubscribe("listen", "string");
2013-08-07 17:16:13 +02:00
2014-04-01 09:35:56 +02:00
// register the string message handler method
sb.onStringMessage(handleString);
2013-08-07 17:16:13 +02:00
2014-04-01 09:35:56 +02:00
// connect to cloud spacebrew server at "sandbox.spacebrew.cc"
sb.connect("sandbox.spacebrew.cc");
}
2013-08-07 17:16:13 +02:00
2014-04-01 09:35:56 +02:00
void loop() {
// monitor spacebrew connection for new data
sb.monitor();
2013-08-07 17:16:13 +02:00
2014-04-01 09:35:56 +02:00
// connected to spacebrew then send a string every 2 seconds
if ( sb.connected() ) {
2013-08-07 17:16:13 +02:00
2014-04-01 09:35:56 +02:00
// check if it is time to send a new message
if ( (millis() - last_time) > interval ) {
sb.send("speak", "is anybody out there?");
last_time = millis();
}
}
}
2013-08-07 17:16:13 +02:00
2014-04-01 09:35:56 +02:00
// handler method that is called whenever a new string message is received
2013-08-07 17:16:13 +02:00
void handleString (String route, String value) {
2014-04-01 09:35:56 +02:00
// print the message that was received
Serial.print("From ");
Serial.print(route);
Serial.print(", received msg: ");
Serial.println(value);
2013-08-07 17:16:13 +02:00
}