From this project we know how to use ARTIK Cloud control LED, it means ARTIK Cloud can do most thing when MCU/MPU is connected.
Here I have another idea for using ARTIK Cloud. Customized Hotel Room.
When we traveling most time we live in hotel. If we can presetting hotel room's light color, air condition temperature, clock alarm time, TV channel, curtain switch level, fridge power.... It will be comfortable.
The architecture diagram as below
======================================================================
Since resources limited I just build two model Light and Air condition.
======================================================================
Device type======================================================================
First we need to guild device types. Here we build three device type controller, light and air condition.
The controller device used to receive customer setting data and have flag to support other device set (for ARTIK cloud rule use).
There have a bug you can't make your publishing data too long or ARTIK Cloud will not receive your data. So make your field name as short as possible. I stuck here 8 hours...
And can't use twice _ at action's PARAMETER NAME. Here light_color_flg need to change to lightcolor_flg. I am not update the picture.
The light device used to send setting light color data to light and receive light current setting to compare.
The air condition device is similar with light device but setting temperature.
======================================================================
Device======================================================================
======================================================================
Rule======================================================================
After build device type we connect devices at ARTIK cloud and start set rules.
This rule is used to check if all set done and avoided double set( after customer set the room controller will only set one time that when we check and manual change setting it will not be change again).
This rule check if device data set is equal to controller target data if not send an action to make device set.
When device data is equal to controller target send an action to make controller set up flag. Back to first rule when all flag set there is a customized room.
======================================================================
Hardware======================================================================
This project I use Arduino MKR1000 as controller and two Ameba RTL8195 as device.
Arduino MKR1000 since it is a controller here we just need to power it.
Ameba RTL8195 air condition: a temperature senor and a LED to simulation compressor(light on/off).
Ameba RTL8195 RGB light : a RGB LED to simulation RGB light.
======================================================================
Software======================================================================
Arduino MKR1000 code :
#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);
}
}
Ameba RTL8195 air condition code:
/*
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[] = "*******"; // your network SSID (name)
char 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:
//{"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'); //set temp from callback
}
}
}
//******************************************************************************
//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();
/* Serial check
memset(publishPayload, 0, sizeof(publishPayload));
sprintf(publishPayload, "{\"temp\":%d}",tempset);
printf("\t%s\r\n", publishPayload);
Serial.println(publishPayload);
*/
delay(6000);
}
}
Ameba RTL8195 RGB light code :
/*
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[] = "password "; // 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);
}
}
======================================================================
Video and Capture======================================================================
Comments