import mqtt.*;
import processing.serial.*;

Serial myPort;

MQTTClient client;

String nodeName = "OuluBoard";
String s = "";

void setup() {
  client = new MQTTClient(this);
  client.connect("mqtt://public:public@public.cloud.shiftr.io", "processing");
  client.subscribe("fab22");
  println(Serial.list());
  
  /*** Set this so it picks up your board ***/
  myPort = new Serial(this, Serial.list()[0], 9600);
  /*****/
}

// Check for button presses coming in from the board, relay character through MQTT
void draw() { 
  if(myPort.available() > 0){
    client.publish("fab22", nodeName + ":" + myPort.readChar());
  }
}

// if a message is received from somewhere else, realy it to the board.
void messageReceived(String topic, byte[] payload) {
  s = new String(payload);
  String[] list = split(s,':');
  if(list[0].equals(nodeName) == false){
    myPort.write(list[1]);
  }
}
