import processing.serial.*;

Serial myPort;

float temp = 0;
float hum = 0;

void setup() {

  size(800, 600);

  println(Serial.list());

  myPort = new Serial(this, "COM9", 9600);

  myPort.bufferUntil('\n');

  textAlign(CENTER, CENTER);
}

void draw() {

  background(15, 23, 42);

  fill(56, 189, 248);
  textSize(36);
  text("SMART ROOM MONITOR", width/2, 60);

  fill(255);

  textSize(28);
  text("Temperature", width/2, 180);

  textSize(60);
  text(nf(temp, 0, 1) + " °C", width/2, 250);

  textSize(28);
  text("Humidity", width/2, 360);

  textSize(60);
  text(nf(hum, 0, 1) + " %", width/2, 430);

  textSize(32);

  if(temp < 18) {

    fill(0, 150, 255);
    text("COLD", width/2, 530);

  } else if(temp > 28) {

    fill(255, 80, 80);
    text("HOT", width/2, 530);

  } else {

    fill(0, 255, 100);
    text("COMFORTABLE", width/2, 530);
  }
}

void serialEvent(Serial myPort) {

  String data = trim(myPort.readStringUntil('\n'));

  if(data != null) {

    String[] values = split(data, ',');

    if(values.length == 2) {

      temp = float(values[0]);
      hum = float(values[1]);
    }
  }
}
