Gabi Zuniga
Published © GPL3+

Smart Vent System

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

AdvancedFull instructions provided6 hours9,509
Smart Vent System

Things used in this project

Hardware components

ARTIK 10
Samsung ARTIK 10
×1
Photon
Particle Photon
×1
Amazon Echo
Amazon Alexa Amazon Echo
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
Grove Temperature Sensor
×1
Grove - Button
×1
L9110 Dual Channel Motor Driver Module
×1
LED (generic)
LED (generic)
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1
Android device
Android device
×1

Software apps and online services

ARTIK Cloud for IoT
Samsung ARTIK Cloud for IoT
Node-RED
Node-RED
Arduino Web Editor
Arduino Web Editor
Android Studio
Android Studio

Story

Read more

Schematics

Circuit Diagram

Vent Controller Schematics

Code

Arduino Sketch

C/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

Credits

Gabi Zuniga

Gabi Zuniga

2 projects • 7 followers
Dreams in Code

Comments

Add projectSign up / Login