Wen-Liang Lin
Published © GPL3+

Customized Hotel Room

An idea make Hotel Room can be customized setting using ARTIK cloud.

AdvancedFull instructions providedOver 1 day2,437
Customized Hotel Room

Things used in this project

Hardware components

Arduino MKR1000
Arduino MKR1000
×1
Realtek Ameba RTL8195 Board
×2
Temperature Sensor
Temperature Sensor
×1
LED (generic)
LED (generic)
×1
Resistor 1k ohm
Resistor 1k ohm
×4
Resistor 100k ohm
Resistor 100k ohm
×1
RGB Diffused Common Cathode
RGB Diffused Common Cathode
×1

Software apps and online services

ARTIK Cloud for IoT
Samsung ARTIK Cloud for IoT
Arduino IDE
Arduino IDE

Story

Read more

Schematics

HOTEL ROOM controller SCH

HOTEL ROOM light SCH

HOTEL ROOM air condition SCH

Code

HOTEL ROOM controller

Arduino
#include <MQTTClient.h>
#include <ArduinoJson.h>
#include <SPI.h>
#include <WiFi101.h>

const char* _SSID     = "_SSID"; //Wi-Fi SSID 
const char* _PASSWORD = "_PASSWORD"; // Wi-Fi Password 

// MQTT - Artik Cloud Server params
char Artik_Cloud_Server[]     = "api.artik.cloud"; // Server 
int  Artik_Cloud_Port         = 8883; // MQTT Port
char Client_Name[] = "Room145"; // Any Name 
char Device_ID[]   = "DEVICE ID"; // DEVICE ID
char Device_TOKEN[]   = "DEVICE TOKEN"; //  DEVICE TOKEN
char MQTT_Publish[]      = "/v1.1/messages/DEVICE ID"; // (/v1.1/messages/"DEVICE ID")
char MQTT_Subscription[] = "/v1.1/actions/DEVICE ID"; // (/v1.1/actions/"DEVICE ID")

char buf[200]; // Json Data to Artik Cloud 
char receivebuf[200]; // Json Data receive from Artik Cloud 
WiFiSSLClient client;
MQTTClient MQTT_Artik_Client; // MQTT Protocol

//led SET
const int BROADLEDPin = 6; //when power form J2 there power LED DL1/DL2 not work so use on broad led to check state
boolean toggle;

//data ram
  int light_color_set_flg=0;
  int roomset_flg=0;
  int temp_set_flg=0;

  int targetTemp = 25; //here I preset data at mkr1000
  int target_light_color_blue = 100;
  int target_light_color_green = 100;
  int target_light_color_red = 200;


void messageReceived(String topic, String payload, char * bytes, unsigned int length) {
  Serial.print("incoming: ");
  Serial.print(topic);
  Serial.print(" - ");
  Serial.print(payload);
  Serial.println();
  String t = topic;
  if(t == MQTT_Subscription) // check DEVICE ID
  {
    parseBuffer(payload);
  }
}

void setup() {

  Serial.begin(57600);  
  // Wifi Setting
  WiFi.begin(_SSID, _PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  MQTT_Artik_Client.begin(Artik_Cloud_Server, Artik_Cloud_Port, client); // Connect to Artik Server
  while (!MQTT_Artik_Client.connect(Client_Name, Device_ID, Device_TOKEN)) { // Connect to Artik IOT Device 
    Serial.print("*");
    delay(1000);
  }
  MQTT_Artik_Client.subscribe(MQTT_Subscription);
  pinMode(BROADLEDPin, OUTPUT);
}

void loadBuffer(void) {

  StaticJsonBuffer<200> jsonBuffer;
  /*
   * smaple field
   * {"d":{"b":839,"g":60,"lf":874,"r":35,"rf":916,"t":855,"tf":930}}
   */
   //creat field data make it short or ARTIK cloud not work

  JsonObject& dataPair = jsonBuffer.createObject();
  JsonObject& data = jsonBuffer.createObject();
  data["lf"] = light_color_set_flg;
  data["rf"] = roomset_flg;
  data["tf"] = temp_set_flg;
  data["t"] = targetTemp; //here I preset data at mkr1000
  data["b"] = target_light_color_blue;
  data["g"] = target_light_color_green;
  data["r"] = target_light_color_red;
  dataPair["d"]=data;
  dataPair.printTo(buf, sizeof(buf));
  Serial.println(buf);
  
}
void parseBuffer(String _payload) {
  StaticJsonBuffer<200> jsonBuffer;
  String json = _payload;
  
  json.toCharArray(receivebuf,200);

  //*****************************
  JsonObject& root = jsonBuffer.parseObject(receivebuf);
  if (!root.success()) {
    Serial.println("parseObject() failed");
  }
  //****************************
  /*
   * {"actions":[
   * {"name":"set_light_color_flg","parameters":{"lightcolor_flg":0}},
   * {"name":"set_roomset_flg","parameters":{"roomset_flg":0}},
   * {"name":"set_temp_flg","parameters":{"temp_flg":1}}]}
   * 
   * {"actions":[{"name":"set_roomset_flg","parameters":{"roomset_flg":true}}]}
   */
  const char* nameparm = root["actions"][0]["name"];
  Serial.println(strcmp(nameparm ,"set_roomset_flg"));//compare nameparm to check if same will be 0
  if(strcmp(nameparm ,"set_roomset_flg")==0) 
  {
    int _roomset_flg = root["actions"][0]["parameters"]["roomset_flg"];
    if (_roomset_flg == 1) 
    {
       roomset_flg=1;
    }
   if (_roomset_flg == 0) 
     {
       roomset_flg=0;
    }
  }
  if(strcmp(nameparm , "set_light_color_flg")==0)
  {
  int lightcolor_flg = root["actions"][0]["parameters"]["lightcolor_flg"];   
    if (lightcolor_flg == 1) 
  {
     light_color_set_flg=1;
  }
  if (lightcolor_flg == 0) 
  {
     light_color_set_flg=0;
  }
  
  }
  //{"actions":[{"name":"set_temp_flg","parameters":{"temp_flg":1}}]}
  if(strcmp(nameparm , "set_temp_flg")==0)
  {
  int temp_flg = root["actions"][0]["parameters"]["temp_flg"];   
  if (temp_flg == 1) 
  {
     temp_set_flg=1;
  }
  if (temp_flg == 0) 
  {
     temp_set_flg=0;
  }
  }
}
void loop() {
  if(roomset_flg==0) //not yet setting
  {
  loadBuffer();
  MQTT_Artik_Client.publish(MQTT_Publish, buf);// Publishing data to the Artik Cloud 
  Serial.println("Publishing..");
  }

  for (int i =0; i<10;i++){ // delay 1 Mnts
    MQTT_Artik_Client.loop();
     if(roomset_flg==0)
     {
    toggle = !toggle;
    digitalWrite(BROADLEDPin, toggle);
     }
    Serial.println(buf);
    delay(6000);
  }  
}

HOTEL ROOM air condition

Arduino
/*
 Thanks Ameba Basic MQTT example
 I mark some point I stuck
*/

#include <WiFi.h>
#include <PubSubClient.h>
#include <math.h>
// Update these with values suitable for your network.

char ssid[] = "ssid";     // your network SSID (name)
char pass[] = "pass";  // your network password
int status  = WL_IDLE_STATUS;    // the Wifi radio's status
//******************************************************************************
//must double check when you copy the code
//******************************************************************************
// MQTT - Artik Cloud Server params
char mqttServer[]     = "api.artik.cloud";
int  mqttPort         = 8883;
char clientId[]       = "Room145AC";
char Device_ID[]   = "DEVICE ID"; // DEVICE ID
char Device_TOKEN[]   = " DEVICE TOKEN"; //  DEVICE TOKEN
char publishTopic[]   = "/v1.1/messages/DEVICE ID";
char subscribeTopic[] = "/v1.1/actions/DEVICE ID";
char publishPayload[128] = "{\"temp\":30}"; //default


const int LEDPin = 8;
const int B=4275; // B value of the thermistor
const int R0 = 100000; // R0 = 100k
const int pinTempSensor = A0; // Grove - Temperature Sensor connect to A0
int   tempset = 30;
float temperature = 30; //default
float getTemp()
{
 int a = analogRead(pinTempSensor);
 float R = 1023.0/((float)a)-1.0;
 R = 100000.0*R;
 //convert to temperature via datasheet ;
 temperature=1.0/(log(R/100000.0)/B+1/298.15)-273.15;  //Grove - Temperature Sensor example
 Serial.print("temperature = ");
 Serial.println(temperature);
 if(temperature>tempset) //if current temperature > tempset air condition work use LED simulation
 {
  digitalWrite(LEDPin,1);
 }
 else
 {
  digitalWrite(LEDPin,0);
 }
 return temperature;
}


void callback(char* topic, byte* payload, unsigned int length) {
  char buf[MQTT_MAX_PACKET_SIZE];
  char *pch;

  strncpy(buf, (const char *)payload, length);
  buf[length] = '\0';
  printf("Message arrived [%s] %s\r\n", topic, buf);

  if ((strstr(topic,subscribeTopic) != NULL)) {
    // payload format: {"state":{"reported":{"led":1},"desired":{"led":0}},"metadata":{"reported":{"led":{"timestamp":1466996558}},"desired":{"led":{"timestamp":1466996558}}},"version":7,"timestamp":1466996558}
    //{"actions":[{"name":"set","parameters":{"temp":20}}]}
    pch = strstr(buf, "\"parameters\":{\"temp\":");
    if (pch != NULL) {
      pch += strlen("\"parameters\":{\"temp\":");
      tempset = (*pch - '0')*10 + (*(pch+1) - '0');

    }
  }
}


//******************************************************************************
//must use SSL
//******************************************************************************
WiFiSSLClient wifiClient;
PubSubClient client(mqttServer, mqttPort,callback,wifiClient);

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(clientId, Device_ID, Device_TOKEN)) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish(publishTopic, publishPayload);
      // ... and resubscribe
      client.subscribe(subscribeTopic);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup()
{
  Serial.begin(38400);

  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 5 seconds for connection:
    delay(5000);
  }
  reconnect();
  
  // Allow the hardware to sort itself out
  delay(1500);
  pinMode(LEDPin, OUTPUT);
}

void loop()
{
  
  if (!client.connected()) {
    reconnect();
  }
  
  memset(publishPayload, 0, sizeof(publishPayload));
  sprintf(publishPayload, "{\"temp\":%d}",tempset);
  printf("\t%s\r\n", publishPayload);
  client.publish(publishTopic, publishPayload);

  delay(1000);
  for (int i =0; i<10;i++){ // delay 1 Mnts
  client.loop();
  getTemp();
  
  memset(publishPayload, 0, sizeof(publishPayload));
  sprintf(publishPayload, "{\"temp\":%d}",tempset);
  printf("\t%s\r\n", publishPayload);
  
  Serial.println(publishPayload);
  delay(6000);
  }  
}

HOTEL ROOM light

Arduino
/*
 Thanks Ameba Basic MQTT example
 I mark some point I stuck
*/

#include <WiFi.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.

char ssid[] = "SSID";     // your network SSID (name)
char pass[] = "pass";  // your network password
int status  = WL_IDLE_STATUS;    // the Wifi radio's status
//******************************************************************************
//must double check when you copy the code
//******************************************************************************
// MQTT - Artik Cloud Server params
char mqttServer[]     = "api.artik.cloud";
int  mqttPort         = 8883;
char clientId[]       = "Room145RGBLight";
char Device_ID[]   = "Device_ID"; // DEVICE ID
char Device_TOKEN[]   = "Device_TOKEN"; //  DEVICE TOKEN
char publishTopic[]   = "/v1.1/messages/Device_ID";
char subscribeTopic[] = "/v1.1/actions/Device_ID";
char publishPayload[128] = "{\"light_color\":{\"blue\":255,\"green\":255,\"red\":255}}"; //default


const int redPin = 10;
const int greenPin = 11;
const int bluePin = 12;
int   red = 255;  //default
int   green = 255;  //default
int   blue = 255;  //default

void LEDwrite(void)
{
  analogWrite(redPin,red);
  analogWrite(greenPin,green);
  analogWrite(bluePin,blue);
}

void callback(char* topic, byte* payload, unsigned int length) {

  char buf[MQTT_MAX_PACKET_SIZE];
  char *pch;
  char *pchg;
  char *pchr;
  char *pche;
  int   redlength = 3;
  int   greenlength = 3;
  int   bluelength = 3;

  strncpy(buf, (const char *)payload, length);
  buf[length] = '\0';
  printf("Message arrived [%s] %s\r\n", topic, buf);
  Serial.println(topic);
  Serial.println(buf);
  Serial.println(length);
  if ((strstr(topic,subscribeTopic) != NULL)) {
    // payload format:
    //{"actions":[{"name":"setColorRGB","parameters":{"colorRGB":{"blue":83,"green":16,"red":22}}}]}
    pch = strstr(buf, "\"parameters\":{\"colorRGB\":{\"blue\":");
    if (pch != NULL) {
      pchg = strstr(buf, ",\"green\":");
      pchr = strstr(buf, ",\"red\":");
      pche = strstr(buf, "}}}]}");
      
      bluelength = strstr(buf, ",\"green\":") - strstr(buf, "\"parameters\":{\"colorRGB\":{\"blue\":")-33;
//******************************************************************************
//caculate length here 33 = strlen("\"parameters\":{\"colorRGB\":{\"blue\":");
//******************************************************************************
      
      greenlength = strstr(buf, ",\"red\":") - strstr(buf, ",\"green\":")-9;

      redlength = strstr(buf, "}}}]}") - strstr(buf, ",\"red\":") -7;

      pch += strlen("\"parameters\":{\"colorRGB\":{\"blue\":");
      pchg += strlen(",\"green\":");
      pchr += strlen(",\"red\":");     
      switch(bluelength)
      {
        case 1:
        blue = *pch -'0';
        break;
        case 2:
        blue =  (*pch - '0')*10 + (*(pch+1) - '0');
        break;
        case 3:
        blue =  (*pch - '0')*100 + (*(pch+1) - '0')*10 +(*(pch+2) - '0');
        break;
        default:
        blue = 0;
      }
      switch(greenlength)
      {
        case 1:
        green = *pchg -'0';
        break;
        case 2:
        green =  (*pchg - '0')*10 + (*(pchg+1) - '0');
        break;
        case 3:
        green =  (*pchg - '0')*100 + (*(pchg+1) - '0')*10 +(*(pchg+2) - '0');
        break;
        default:
        green = 0;
      }
      switch(redlength)
      {
        case 1:
        red = *pchr -'0';
        break;
        case 2:
        red =  (*pchr - '0')*10 + (*(pchr+1) - '0');
        break;
        case 3:
        red =  (*pchr - '0')*100 + (*(pchr+1) - '0')*10 +(*(pchr+2) - '0');
        break;
        default:
        red = 0;
      }
      LEDwrite(); //change LED setting
    }
  }
}


//******************************************************************************
//must use SSL
//******************************************************************************
WiFiSSLClient wifiClient;
PubSubClient client(mqttServer, mqttPort,callback,wifiClient);

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(clientId, Device_ID, Device_TOKEN)) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish(publishTopic, publishPayload);
      // ... and resubscribe
      client.subscribe(subscribeTopic);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup()
{
  Serial.begin(38400);

  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 5 seconds for connection:
    delay(5000);
  }
  reconnect();
  
  // Allow the hardware to sort itself out
  delay(1500);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  LEDwrite();
}

void loop()
{
  
  if (!client.connected()) {
    reconnect();
  }
  
  memset(publishPayload, 0, sizeof(publishPayload));
  sprintf(publishPayload, "{\"light_color\":{\"blue\":%d,\"green\":%d,\"red\":%d}}",blue,green,red);
  printf("\t%s\r\n", publishPayload);
  client.publish(publishTopic, publishPayload);

  delay(1000);
  for (int i =0; i<10;i++){ // delay 1 Mnts
  client.loop();
  Serial.println(publishPayload);
  delay(6000);
  }  
}

Credits

Wen-Liang Lin

Wen-Liang Lin

28 projects • 34 followers
Hi, I am momososo

Comments