Project tutorial
Smart Vent System

Smart Vent System © GPL3+

Smart Vent System will help you save energy by heating or cooling only the rooms that you need.

  • 4,597 views
  • 2 comments
  • 17 respects

Components and supplies

Apps and online services

About this project

Artik Challenge Category: Best Smart Home Application

The Nest thermostat was a leap in automated climate control.

However, did you ever wonder why do you need to heat or cool unoccupied rooms, like the office or kitchen during the night?

Smart Vent is an IoT enhanced vent that senses when there is people occupying a room, its temperature, and sends this information to Artik Cloud to optimize home or office climate control and save energy.

Our comprehensive solution provides 4 different ways for controlling individual vents:

  • Manual Control: Each vent can be easily opened or closed (toggle) by just the press of a button.
  • Voice: Use Amazon Alexa to request opening or closing a particular vent using voice commands.
  • Mobile: We developed an Android App to control our vents.
  • Rules: Use Artik Cloud's powerful rules to automate all vent states and avoid unsafe states like having all vents closed at once.

Customize Artik Cloud rules to:

  • Close vents during the night in unoccupied rooms like office or kitchen.
  • Open vents for occupied rooms relying on the Smart Vent motion sensors.
  • Open or Close vents according to local temperature.

The android app is open source and can be found in github. The app sends Artik Cloud commands to individual vents to open or close them.

Architecture

We used Artik 10 as the main IoT Hub of the home and office. The Artik 10 runs a local MQTT broker, connects to all the vent's controllers and interacts with Artik Cloud.

Not having each vent connect directly to Artik Cloud but instead to the Artik 10 local hub has the advantages of efficiency, security and cost. Our prototype uses a Particle Photon for the vent controller but our final product will use an Artik 1 which is better suited and cheaper.

Artik Cloud get all sensors messages which are displayed in the charts section. These messages, like temperature and motion, are used by rules to automate the smart vents.

Creating an Artik Account

Go to http://developer.artik.cloud and open an account.

Creating a Device Type

On the dashboard, press on the "New Device Type" button.

Create a new device type for Smart Vent

Press "New Manifest" to create the manifest for the Smart Vent.

Create a "state" field:

then a "temp" field

last a "motion" field:

Press on "NEXT: ACTIONS" to configure the actions. Let's start with the "open" action.

then the "close" action.

Select "Activate Manifest".

Connecting a Device

Navigate to the Artik Cloud Portal for Users at https://artik.cloud and login.

Select "+ Connect another device". There search for the smart vent device type and name your device.

After creating the device, select the gear in order to get the device info.

Press "Generate Token" to create the device secret token.

Copy your DEVICE ID and the DEVICE TOKEN. These values will be used to authenticate the device to send (or receive) date to Artik Cloud.

Creating Automation Rules

Rules will allow to automate actions according to sensor information.

Let's create 4 rules:

First rule will open the office vent in the morning at 8:00am

Second rule will close the vent at 11:00pm to avoid heating/cooling the office at night when it is unoccupied.

Third rule will close the vent when the office is unoccupied for a while.

Forth rule will open the vent when it detects motion and the office is above certain temperature.

Creating an Artik App

Go to your developer dashboard and select "New Application"

Name your app to "Smart Vent System", select "client Credentials auth code implicit" and provide an auth url which should match the one used in the app. Select save application.

Set the smart permissions to read and write for the smart vent and read for the profile. Select save.

You app is set. Click in "Show client ID & secret" to get your keys. These keys are needed to configure your app.

Configuring Amazon Alexa

In order to be able to control the Smart Vent using voice then you need to configure Amazon Alexa by following these steps:

Login to Amazon Alexa portal at http://alexa.amazon.com, select "skills" and search for "Artik Cloud".

Press the "Enable" button to activate the skill.

When requested login to Artik Cloud account and authorize access to your account devices. You will get a message confirming that your skill was successfully linked.

Building the Vent Controller

  • Get all your components. If the motorized vent includes a controller you can remove it since we are going to connect our IoT project directly to the motor.
  • Connect your circuit according to the provided diagram.
  • Flash your Photon with the provided sketch from particle's ide https://build.particle.io . You need to update the ip address of your Artik 10 IoT hub in the sketch.

Configuring the Artik 10

  • Follow the instructions from Artik Tutorials to get mqtt broker and node-red installed.
  • Launch the mqtt broker by running $ mosquitto &
  • Configure your node-red program as described below.

Node-Red is a tool for wiring together hardware devices, APIs and online services using a drag and drop flow editor.

Configure your Node-Red flow by following this diagram.

Set the format temp event function as follows:

msg.topic = "/v1.1/messages/"+global.get("deviceId")   
msg.payload = { 
   "temp": parseFloat(msg.payload)  
}   
return msg; 

Set the format motion event function as follows:

msg.topic = "/v1.1/messages/"+global.get("deviceId")   
msg.payload = { 
   "motion": (msg.payload == "true") 
}   
return msg; 

Set the format action function as follows:

var json = JSON.parse(msg.payload) 
var action = json["actions"][0]["name"]; 
msg.topic = "office-vent"   
msg.payload = action;   
return msg; 

The deviceId global variable above is defined in the .node-red/settings.json file:

  // Anything in this hash is globally available to all functions. 
   // It is accessed as context.global. 
   // eg: 
   //    functionGlobalContext: { os:require('os') } 
   // can be accessed in a function block as: 
   //    context.global.os 
   functionGlobalContext: { 
       // os:require('os'), 
       // octalbonescript:require('octalbonescript'), 
       // jfive:require("johnny-five"), 
       // j5board:require("johnny-five").Board({repl:false}) 
       deviceId:"<<your device id>>" 
   }, 
...

Configure your local MQTT to your local broker:

And the external MQTT to point to Artik Cloud. You also need to set the user/password to your device_id/token values.

Code

Arduino SketchC/C++
#include "MQTT/MQTT.h"
#include <math.h>


boolean bVentStatus = false;            // true when vent is open; false when close
boolean bMotionDetected = false;        // true when motion has been detected
int countSinceMotionDetected = 0;       // counts since motion detected to send no motion event
int counter = 0;                        // loop count

// PIN DEFINITIONS

#define OPEN_VENT_PIN           1
#define CLOSE_VENT_PIN          0
#define PIR_MOTION_SENSOR       4
#define RED_LED                 3
#define BUTTON_PIN              5
#define BOARD_LED               7

// MQTT Configuration and Callbacks

byte server[] = { 10, 0, 0, 45 };
void callback(char* topic, byte* payload, unsigned int length);
MQTT client(server, 1883, callback);

void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;
    String message(p);

    if (message.equals("open")) {
        openVent();
    }
    else if (message.equals("close")) {
        closeVent();
    }
}


// INITIALIZATION

void setup() {
    RGB.control(true);                // Enable on board Photon color LED

    pinMode(RED_LED, OUTPUT);
    pinMode(BOARD_LED, OUTPUT);
    pinMode(BUTTON_PIN, INPUT);

    pinMode(CLOSE_VENT_PIN, OUTPUT);
    digitalWrite(CLOSE_VENT_PIN, HIGH);
  
    pinMode(OPEN_VENT_PIN, OUTPUT);
    digitalWrite(OPEN_VENT_PIN, HIGH);

    pinMode(PIR_MOTION_SENSOR, INPUT);
  
    client.connect(System.deviceID());
    if (client.isConnected()) {
        client.subscribe("office-vent");    // subscribe to vent actions
    }
}


// ARDUINO LOOP

void loop() {
    counter++;

    handleMantualControl();

    if (client.isConnected()) {
        client.loop();

        handleTemp();
        handleMotion();
    }

    delay(600);
}


// handleMantualControl
// if button is pressed then toggle between opening and closing vent
// update on board led color to green when open and red when closed

void handleMantualControl() {
    int state = digitalRead(BUTTON_PIN);

    boolean action = false;
    if (state == HIGH) {
        digitalWrite(BOARD_LED, HIGH);
        if (!action) {
            if (bVentStatus) {
                closeVent();
            } else {
                openVent();
            }       
        }
        action = true;
    } else {
        digitalWrite(BOARD_LED, LOW); 
        action = false;
    }
}

// handleTemp
// publish temperature to MQTT channel

void handleTemp() {
    // publish temp
    char tempBuffer[32];

    if (counter % 500 == 0) {
        float temp = readTemp();
        sprintf(tempBuffer,"%2.2f", temp);
        client.publish("office-vent-temp", tempBuffer);
    }
}

// handleMotion
// When motion is detected then turn on led
// publish motion event to MQTT
// after period of inactivity send no motion event (false)

void handleMotion() {
    int motion = digitalRead(PIR_MOTION_SENSOR);
    if (motion == HIGH) {
        bMotionDetected = true;
    }
    digitalWrite(RED_LED, motion); 
    
    if (counter % 10 == 0 && bMotionDetected) {
        client.publish("office-vent-motion", "true");
        bMotionDetected = false;
        countSinceMotionDetected = 0;
    }
    
    countSinceMotionDetected++;
    if (countSinceMotionDetected % 2000 == 0) {
        client.publish("office-vent-motion", "false");
    }
}


// openVent
// Open vent and send MQTT notification

void openVent() {
    if (!bVentStatus) {
        // set to green once opened
        RGB.color(0, 255, 0);
        digitalWrite(OPEN_VENT_PIN, LOW);               
        delay(300);
        digitalWrite(OPEN_VENT_PIN, HIGH);
        bVentStatus = true;
        client.publish("office-vent-state", "open");
    }
}

// closeVent
// Close vent and send MQTT notification

void closeVent() {
    if (bVentStatus) {
        // set to red once closed
        RGB.color(255, 0, 0);
        digitalWrite(CLOSE_VENT_PIN, LOW);               
        delay(300);
        digitalWrite(CLOSE_VENT_PIN, HIGH);
        bVentStatus = false;
        client.publish("office-vent-state", "closed");
    } 
}


// readTemp
// Temperature calculation helper

float readTemp() {
    int a;
    float temperature;
    int B=3975;                  //B value of the thermistor
    float resistance;
  
    a=analogRead(3);
    resistance=(float)(4300-a)*10000/a; //get the resistance of the sensor
    temperature=1/(log(resistance/10000)/B+1/298.15)-273.15;//convert to temperature via datasheet
    return temperature;
}
Android App
Open Source in GitHub

Schematics

Circuit Diagram
Screen%20shot%202016 10 01%20at%204.33.22%20pm
Vent Controller Schematics
vent-photon.fzz

Comments

Similar projects you might like

Smart Home - Smart Rules using ARTIK Cloud & Photon

Project tutorial by Raghavendra Ponuganti

  • 4,026 views
  • 2 comments
  • 13 respects

Home/Hotel Monitoring System with Automated controls

Project tutorial by Anas Siddiqui

  • 4,183 views
  • 10 comments
  • 15 respects

Smart Personal Money Vault Monitoring System Based on IoT

Project tutorial by Salah Uddin

  • 849 views
  • 0 comments
  • 6 respects

Smart Parking System

Project in progress by Md. Khairul Alam

  • 43,049 views
  • 32 comments
  • 104 respects

Smart Garbage Monitoring System Using Arduino 101

Project tutorial by Technovation

  • 22,744 views
  • 7 comments
  • 37 respects

Smart Parking System

Project tutorial by Husinul

  • 9,589 views
  • 14 comments
  • 23 respects
Add projectSign up / Login