Home automation was my dream, as a maker I prefer to make everything myself. This is where this project started.
In my project, I have a small piece of code to handle automation rules. Since the rules are hard coded these can not be configured or modified easily, I had to re-compile the code and flash it again. I want to get rid of this code and gain flexibility by using ARTIK Cloud Rules.
In this project, I will first share my success story in publishing my data to ARTIK cloud after a long struggle of morethan a week.
The ARTIK rules I want to create
- Notification on mobile if no one at home and some of the lights are still ON.
- Reminder (notification) to ventilate the room when Humidity is high.
Home Automation is all about controlling appliances, monitoring different environmental conditions and reacting to these conditions. Definitely it is impossible to monitor and control all appliances using a single HW located at one place. So I decided to go with distributed system. Each appliance to be controlled/monitored is equipped with a Arduino board + nRF24L01 transceiver, together I call it as node. This node does monitoring using sensors and transmits the data back to Gateway via nRF24L01 transceiver. Gateway is internet connected and sends the data to cloud service, here ARTIK cloud.
Gateway:Gateway used is my all time favourite IoT development board "Particle Photon". Gateway consists of Photon + nRF24L01 transceiver.
Features:
- Receives Sensor data from different nodes via RF and updates to cloud (WiFi).
- Receives commands from cloud and transfers the control commanded to the desired node via RF.
- Monitors the live status of all the Nodes (ON, OFF, Offline).
At the moment I have built the following nodes, several others are in plan ..
Control a Light:
Control a Light and RGB LED Strip:
Battery Powered Sensor Node:
This sensor node measures the Temperature, Humidity and Battery Level every 10 mins, transmits the measured values to gateway and sleeps until next 10 mins.
Software:I used this open source library and developed my own protocol to communicate to different nodes.
ARTIK Cloud Devices:In this article I will focus on how I could successfully publish data from Particle Photon HW to ARTIK cloud.
As described in several tutorials and videos create devices in ARTIK developer cloud, I created following devices -
- AtHome - Just stores a bool value. I will explain it later in rules part.
- Environment Sensor - Stores 3 variables Temperature, Humidity and Battery Voltage
- Light - Stores the status of a light.
Create devices in Artik cloud using the above device types
- DeepuAtHome, RaghuAtHome are devices of type AtHome.
- Living Room-Light, Notification-Save Power, Notification-HighHumidity are devices of type Light.
- Living Room Sensor Data is a device of type Environment Sensor.
This I feel is the most tricky part. I could not find any tutorials which explain this clearly with Photon. I will try my best here.
After reading through the (not so clear) ARTIK cloud documentation, I realised MQTT is the easiest way to send the data to ARTIK cloud. But after several experiments and discussions with ARTIK support team got to know that Photon or MQTT library does not support SSL/TLS which is a must for ARTIK cloud and hence data could not sent over.
Alternative and best solution is "webhooks". Webhooks are a simple and flexible way to send data from Particle devices to other apps and services around the Internet.
How it works: Create a webhook to integrate Particle Cloud with ARTIK Cloud. SW in photon triggers webhook using Particle event and this webhook publishes data to ARTIK cloud. So simple ;-)
There are 2 ways to create a particle webhook, easiest way being creating it in Particle console. But the disadvantage is that once created this can not be modified. Humm no modification ..??? not useful, at least during initial testing..
Other way and the best way is using the JSON file. Create a JSON file using the following content to name it say hookLivingRoomSensor.json
{
"event": "enLivingRoomEnvData",
"url": "https://api.artik.cloud/v1.1/messages",
"requestType": "POST",
"json": {
"data": {"Humidity": "{{Humidity}}", "Temperature": "{{Temperature}}", "BatteryVolt": "{{BatteryVolt}}"},
"sdid": "__ARTIK_DEVICE_ID__",
"type": "message"
},
"headers": {
"Content-Type": "application/json",
"Authorization": "Bearer __ARTIK_DEVICE_TOKEN__"
},
"noDefaults": true
}
Note: Replace __ARTIK_DEVICE_ID__ and __ARTIK_DEVICE_TOKEN__ from the device settings created in ARTIK cloud.
Open command prompt (I assume particle CLI tools are installed) and type following command from the location where this JSON file is located.
particle webhook create hookLivingRoomSensor.json
That is all, this command will create a webhook to integrate to ARTIK Cloud and posts a JSON string with 3 values "Humidity", "Temperature" and "BatteryVolt".
Note: {{Humidity}},
{{Temperature}}, {{BatteryVolt}}
are the variables, particle cloud will replace these values with the data sent along with the event.
void CloudConn_ArtikupdLivingRoom(uint8_t temp, uint8_t humi, float battVolt)
{
char buf[1000];
/* Construct the JSON string with the sensor data */
sprintf(buf, "{ \"Humidity\": %d, \"Temperature\": %d, \"BatteryVolt\": %f}",
humi, temp, battVolt);
/* Fire the Particle event to trigger the webhook */
Particle.publish("enLivingRoomEnvData", buf, PRIVATE );
}
The above function is called by my RF Network Handler as soon as it receives the data from sensor node (every 10 mins). Build the JSON string with the sensor values and fire the Particle event with the JSON data.
For hobbist account, there is limit of 150 messages that can be posted to ARTIK cloud. One message at every 10 mins counts to 144 messages. Perfect!
Here is the data collected in my Artik cloud
Here is my another webhook to post the status of Light in my living room.
{
"event": "enLivingRoomLight",
"url": "https://api.artik.cloud/v1.1/messages",
"requestType": "POST",
"json": {
"data": {"State": "{{State}}"},
"sdid": "__ARTIK_DEVICE_ID__",
"type": "message"
},
"headers": {
"Content-Type": "application/json",
"Authorization": "Bearer __ARTIK_DEVICE_TOKEN__"
},
"noDefaults": true
}
This hook posts only one variable "State" to the ARTIK Cloud.
Photon SW to fire this webhook:
void CloudConn_ArtikUpdLRLight(LightStatus_t status)
{
static uint8_t oldStatus = 0;
if(oldStatus != status)
{
char buf[1000];
sprintf(buf, "{ \"State\": %d}", status);
Particle.publish("enLivingRoomLight", buf, PRIVATE);
oldStatus = status;
}
}
Above code fires the event "enLivingRoomLight" which publishes the status of the light to Artik Cloud.
The light node updates the status of light every 30 sec, a TimeOut on these status determines the offline status of Node. In order not to publish the data to Artik cloud every 30 sec I have a small protection. Status is published only if the current status is not same as the old published status.
Webhook Integrations in Particle Cloud Console
Until now, I have status of sensors and lights in the ARTIK cloud. Lets get more data.
Now lets discuss about the device type AtHome I created above. This is a actionable device with a state variable. This device is created with the below advanced Manifest. What it does is simple, it just copies the action received to the state variable. If the action is "setOn" then state will be "on", like wise if the action is "setOff" then state will be "off". Advanced manifest should be approved by Artik team, in my case it took less than a day for approval.
import com.samsung.sami.manifest.Manifest
import com.samsung.sami.manifest.actions.*
import com.samsung.sami.manifest.fields.*
import groovy.json.JsonSlurper
import javax.measure.unit.SI
public class PhillipsHueManifest implements Manifest, Actionable {
public final static FieldDescriptor STATE = new FieldDescriptor("state", Integer.class);
@Override
List<Field> normalize(String input) {
def slurper = new JsonSlurper()
def json = slurper.parseText(input)
return [
new Field(STATE, json.status.state.on ? 1 : 0)
]
}
@Override
List<FieldDescriptor> getFieldDescriptors() {
return [STATE]
}
@Override
List<Action> getActions() {
return [
new Action("setOn", "Sets the light state to On"),
new Action("setOff", "Sets the light state to Off"),
]
}
}
I have created 2 devices of this type RaghuAtHome and DeepuAtHome. As name suggests, these 2 devices stores the status, if me or my wife is at home.
How do I notify Artik cloud if am at home ???
This is done by monitoring my home wifi router (Google OnHub). If my phone is connected to router, I am at home if not connected I am not at home. Same for my wife with her phone. This I achieved by using IFTTT, OnHub Channel and ARTIK channel.
Create the following recipes in IFTTT - If a phone is connected or disconnected to OnHub then setOn or setOff these devices in ARTIK Cloud.
Now the Artik cloud knows either me or my wife is at home.
ARTIK Cloud Rule 1:My Artik Cloud rule goes like this "If both me and my wife are not at home and if any light at home is ON then send a notification on my phone."
Finally create this recipe in IFTTT to get the notification - If the ARTIK device "Notification - Save Power" is set to On then send IF Notification.
This is relatively simple. When the Humidity in my living room is higher than a threshold, I push a notification using IFTTT.
IFTTT receipe to get the notification - If the ARTIK device "Notification - HighHumidity" is set to On then send IF Notification.
Have fun!!
Comments