Why I created the project:
Creating a manifest for the new device type.
Read moreThis project was created to automate a Nest Thermostat. Adjusting a thermostat and programming it takes time, even with a Nest Thermostat. Using Artik Cloud, we can control the Nest Thermostat with the Rules engine and will allow us to connect it with other devices. It should take around 2 to 3 hours to build, but is easily expandable.
Creating a new device type.- Sign in to or create a Samsung Account.
- Go to the Artik Cloud Developer Dashboard.
- Create a new device type.
- Name the device "Temperature Sensor" and give it a unique name, such as "example.com.sensor".
- Click "Create Device Type".
- Click "+ New Manifest".
- The page should now look like this:
- Click "Browse Standard Fields".
- Select "currentTemp"(shown below).
- Click "Done" and then "Save".
- Click "Activate Manifest", since we will not use Device Actions.
- Click "Activate Manifest" at the bottom of the menu.
- Go to My Artik Cloud Devices.
- If the menu for creating a new device doesn't show up click "+ Connect another device..."
- Search for "Temperature Sensor", the device that was added before.
- Give the device a name that you can remember.
- Click "Connect Device...".
- Click "+ Connect another device...".
- Search for "Nest Thermostat".
- Give the device a name.
- Next to the Nest Thermostat's name on the Devices menu, there should be an "Authorize" button. Click it.
- Sign in with your Nest Account.
- All your Nest Thermostats should appear.
- Download the code at Github.
- Build the circuit according to schematics available here: https://circuits.io/circuits/2557943-temperature-sensor.
- Upload the Sketch program(IoT-temp-sensor.ino) to the Arduino UNO using the Arduino IDE. The code converts the sensor readings to Celsius, and then sends it to the serial port every 5 seconds (you can change this parameter in the code later, since ARTIK Cloud has rate limits for the number of messages per day).
- Here is the full code:
const int delayBetweenReads = 5000;
const int sensorPin = A0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int sensorVal = analogRead(sensorPin);
double voltage = (sensorVal / 1024.0) * 5.0;
double currentTemp = (voltage - .5) * 100;
Serial.println(String(currentTemp));
delay(delayBetweenReads);
}
Setting up the Raspberry Pi.- Connect your Raspberry Pi to a monitor, keyboard, mouse, power supply, and the internet.
- Open the terminal.
- Update your Raspberry Pi by entering the following commands into the terminal:
sudo apt-get update -y
sudo apt-get upgrade -y
- Install node.js by following the instructions here: https://github.com/nathanjohnson320/node_arm
- Using the terminal, add the packages ws and serialport using npm:
npm install ws
npm install serialport
- Download the node.js program currentTemp.js to the Raspberry Pi. It is available here: https://github.com/ithink-so/IoT-Temp-Sensor.
- Here is the full code:
var webSocketUrl = "wss://api.artik.cloud/v1.1/websocket?ack=true";
var device_id = "<YOUR DEVICE ID>";
var device_token = "<YOUR DEVICE TOKEN>";
var isWebSocketReady = false;
var ws = null;
var serialport = require("serialport")
var SerialPort = serialport.SerialPort;
var sp = new SerialPort("/dev/ttyACM0", {
baudrate: 9600,
parser: serialport.parsers.readline("\n")
});
var WebSocket = require('ws');
/**
* Gets the current time in millis
*/
function getTimeMillis(){
return parseInt(Date.now().toString());
}
/**
* Create a /websocket device channel connection
*/
function start() {
//Create the websocket connection
isWebSocketReady = false;
ws = new WebSocket(webSocketUrl);
ws.on('open', function() {
console.log("Websocket connection is open ....");
register();
});
ws.on('message', function(data, flags) {
console.log("Received message: " + data + '\n');
});
ws.on('close', function() {
console.log("Websocket connection is closed ....");
});
}
/**
* Sends a register message to the websocket and starts the message flooder
*/
function register(){
console.log("Registering device on the websocket connection");
try{
var registerMessage = '{"type":"register", "sdid":"'+device_id+'", "Authorization":"bearer '+device_token+'", "cid":"'+getTimeMillis()+'"}';
console.log('Sending register message ' + registerMessage + '\n');
ws.send(registerMessage, {mask: true});
isWebSocketReady = true;
}
catch (e) {
console.error('Failed to register messages. Error in registering message: ' + e.toString());
}
}
/**
* Send one message to ARTIK Cloud
*/
function sendData(currentTemp){
try{
ts = ', "ts": '+getTimeMillis();
var data = {
"currentTemp": currentTemp
};
var payload = '{"sdid":"'+device_id+'"'+ts+', "data": '+JSON.stringify(data)+', "cid":"'+getTimeMillis()+'"}';
console.log('Sending payload ' + payload);
ws.send(payload, {mask: true});
} catch (e) {
console.error('Error in sending a message: ' + e.toString());
}
}
/**
* All start here
*/
start(); // create websocket connection
sp.on("open", function () {
sp.on('data', function(data) {
if (!isWebSocketReady){
console.log("Websocket is not ready. Skip sending data to ARTIK Cloud (data:" + data +")");
return;
}
console.log("Serial port received data:" + data);
var currentTemp = parseFloat(data);
sendData(currentTemp);
});
});
- Go back to My Artik Cloud Devices.
- Click the settings icon next to the temperature sensor's name.
- In the menu, click "Generate Device Token"
- In the code, replace the placeholders with the Device ID and the Device token that you just got.
- Connect the serial port on the Arduino to the USB on the Raspberry Pi
- Using the terminal on the Raspberry Pi, navigate to the directory that contains the code
- Run the code by enter this command into the terminal:
node currentTemp.js
- In the terminal, you should see the payload of the message sent to ARTIK Cloud and the corresponding response from ARTIK Cloud.
- Go back to My ARTIK Cloud Devices.
- View your device data by clicking the device name.
- Click the "+/- Charts" button and check "currentTemp".
- There should now be a chart showing the temperature in Celsius.
- Go to My Artik Cloud Rules.
- If the menu for creating a new rule doesn't show up click" + New rule"
- In the "Select a field on a device", search for the temperature sensor, and then select "currentTemp".
- Select "is more than or equal to" as the condition.
- Put any value in degrees Celsius in the "Specify a value" box.
- In the "Select an action on a device" box search for you Nest Thermostat, and then select "setCoolMode".
- Give the rule a title.
- Click "Save rule".
- To test, select the rule from the menu.
- Click "Test"
- The Nest Thermostat that you selected should be set to Cool Mode
Comments