libraries / Bridge / examples / SpacebrewYun / inputOutput / inputOutput.inoon commit Added link to project report (97a3ba0)
   1/*
   2  Input Output
   3
   4 Demonstrates how to create a sketch that sends and receives all standard
   5 spacebrew data types, and a custom data type. Every time data is
   6 received it 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("aYun", "Arduino Yun spacebrew test");
  29
  30// create variables to manage interval between each time we send a string
  31long last = 0;
  32int interval = 2000;
  33
  34int counter = 0;
  35
  36void setup() {
  37
  38  // start the serial port
  39  SerialUSB.begin(57600);
  40
  41  // for debugging, wait until a serial console is connected
  42  delay(4000);
  43  while (!SerialUSB) {
  44    ;
  45  }
  46
  47  // start-up the bridge
  48  Bridge.begin();
  49
  50  // configure the spacebrew object to print status messages to serial
  51  sb.verbose(true);
  52
  53  // configure the spacebrew publisher and subscriber
  54  sb.addPublish("string test", "string");
  55  sb.addPublish("range test", "range");
  56  sb.addPublish("boolean test", "boolean");
  57  sb.addPublish("custom test", "crazy");
  58  sb.addSubscribe("string test", "string");
  59  sb.addSubscribe("range test", "range");
  60  sb.addSubscribe("boolean test", "boolean");
  61  sb.addSubscribe("custom test", "crazy");
  62
  63  // register the string message handler method
  64  sb.onRangeMessage(handleRange);
  65  sb.onStringMessage(handleString);
  66  sb.onBooleanMessage(handleBoolean);
  67  sb.onCustomMessage(handleCustom);
  68
  69  // connect to cloud spacebrew server at "sandbox.spacebrew.cc"
  70  sb.connect("sandbox.spacebrew.cc");
  71  // we give some time to arduino to connect to sandbox, otherwise the first sb.monitor(); call will give an error
  72  delay(1000);
  73}
  74
  75
  76void loop() {
  77  // monitor spacebrew connection for new data
  78  sb.monitor();
  79
  80  // connected to spacebrew then send a string every 2 seconds
  81  if (sb.connected()) {
  82
  83    // check if it is time to send a new message
  84    if ((millis() - last) > interval) {
  85      String test_str_msg = "testing, testing, ";
  86      test_str_msg += counter;
  87      counter ++;
  88
  89      sb.send("string test", test_str_msg);
  90      sb.send("range test", 500);
  91      sb.send("boolean test", true);
  92      sb.send("custom test", "youre loco");
  93
  94      last = millis();
  95
  96    }
  97  }
  98  delay(1000);
  99}
 100
 101// define handler methods, all standard data type handlers take two appropriate arguments
 102
 103void handleRange(String route, int value) {
 104  SerialUSB.print("Range msg ");
 105  SerialUSB.print(route);
 106  SerialUSB.print(", value ");
 107  SerialUSB.println(value);
 108}
 109
 110void handleString(String route, String value) {
 111  SerialUSB.print("String msg ");
 112  SerialUSB.print(route);
 113  SerialUSB.print(", value ");
 114  SerialUSB.println(value);
 115}
 116
 117void handleBoolean(String route, boolean value) {
 118  SerialUSB.print("Boolen msg ");
 119  SerialUSB.print(route);
 120  SerialUSB.print(", value ");
 121  SerialUSB.println(value ? "true" : "false");
 122}
 123
 124// custom data type handlers takes three String arguments
 125
 126void handleCustom(String route, String value, String type) {
 127  SerialUSB.print("Custom msg ");
 128  SerialUSB.print(route);
 129  SerialUSB.print(" of type ");
 130  SerialUSB.print(type);
 131  SerialUSB.print(", value ");
 132  SerialUSB.println(value);
 133}
 134