source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/libraries/Bridge/examples/SpacebrewYun/spacebrewString/spacebrewString.ino @ 46

Last change on this file since 46 was 46, checked in by jrpelegrina, 4 years ago

First release to Xenial

File size: 2.0 KB
Line 
1/*
2  Spacebrew String
3
4 Demonstrates how to create a sketch that sends and receives strings
5 to and from Spacebrew. Every time string data is received it
6 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
28SpacebrewYun sb = SpacebrewYun("spacebrewYun Strings", "String sender and receiver");
29
30// create variables to manage interval between each time we send a string
31long last_time = 0;
32int interval = 2000;
33
34void setup() {
35
36  // start the serial port
37  Serial.begin(57600);
38
39  // for debugging, wait until a serial console is connected
40  delay(4000);
41  while (!Serial) {
42    ;
43  }
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("speak", "string");
53  sb.addSubscribe("listen", "string");
54
55  // register the string message handler method
56  sb.onStringMessage(handleString);
57
58  // connect to cloud spacebrew server at "sandbox.spacebrew.cc"
59  sb.connect("sandbox.spacebrew.cc");
60}
61
62
63void loop() {
64  // monitor spacebrew connection for new data
65  sb.monitor();
66
67  // connected to spacebrew then send a string every 2 seconds
68  if (sb.connected()) {
69
70    // check if it is time to send a new message
71    if ((millis() - last_time) > interval) {
72      sb.send("speak", "is anybody out there?");
73      last_time = millis();
74    }
75  }
76}
77
78// handler method that is called whenever a new string message is received
79void handleString(String route, String value) {
80  // print the message that was received
81  Serial.print("From ");
82  Serial.print(route);
83  Serial.print(", received msg: ");
84  Serial.println(value);
85}
86
Note: See TracBrowser for help on using the repository browser.