Skip to content

SerialController.hpp

Brandon W. Kipp edited this page Feb 13, 2020 · 1 revision

This library is used to handle sending and parsing serial data. It serves as a link between stele and exhibit-specific apps by sending and receiving JSON-like messages.

If you need/choose to use this library in your app, make sure to run the serialController.setup() function before any other setup function to ensure anything dependent on SerialController.hpp is available.

To setup a new SerialController, you need to pass in two parameters:

  1. long, the serial baud rate
  2. void (*callback)(String, int), a callback function that accepts a String and an int.

Example

#include "Libraries/SerialController.hpp"

SerialController serialController;

long baudRate = 115200;

void setup() {

  serialController.setup(baudRate, [[](String message, int value) {
    onParse(message, value);
  });

}

void onParse(String message, int value) {
  // This runs when the Arduino receives serial data
  if (message == "specific-message" && value == 1)
    // Do something
  }
}

void loop() {
  serialController.update();
}

Serial Messaging

To send a serial message from the Arduino, you can use the sendMessage function like this:

// Arduino sends
serialController.sendMessage("some-message-type", 99);

...

// Computer/stele receives
{some-message-type:99}

Serial Parsing

In this example code, data sent and received by the Arduino is formatted in a JSON-like format. To start communication with the Arduino, send a "{" character, without the quotes.

All messages sent to the Arduino from the computer will be echoed back to the computer as verification that the message was correctly received.

Clone this wiki locally