Project tutorial
Automation of ventilation system

Automation of ventilation system

Simple smart automation of ventilation system to react to open windows and air quality

  • 3,529 views
  • 0 comments
  • 7 respects

Components and supplies

Arduino Ethernet
×1
Seeed Grove - LED Bar (SKU: LED05031P)
×1
Seeed Grove - Air quality sensor (SKU: SEN01111P)
Can be replaced by a Grove - Rotary Angle Sensor to imitate air quality.
×1
Seeed Grove - Mini Fan (SKU: 811005001)
×1
ZWave gateway
×1
ZWave contact sensor
×1

About this project

Many systems exist to manage the in-house ventilation. Ventilation is required to improve air quality and efficiency  of the heating system (dry air heats faster). None of these systems take into account the current air quality and if there is natural ventilation when a window is opened. As an experiment we build a proof-of-concept application on top of the SmartLiving.io platform and an Arduino with Grove sensors.

Goals:

  • Allow contact sensor (zwave) to activate ventilation system
  • Control fan speed based on air quality
  • Visual representation of air quality

Recipe :

  1. Create an account in SmartLiving (if you don’t have one yet)
  2. Add the ZWave gateway to SmartLiving platform
  3. Add ZWave devices to the gateway
  4. Create a virtual asset which wil state the value of the ZWave door sensor. This can be used to inform the Arduino of updates from the ZWave.
  5. Create a rule to set the state of the virtual asset to the same of the ZWave door sensor. 
  6. Create your Arduino device under your account
  7. Connect the Grove ledbar to PIN D8 on the Grove shield
  8. Connect the Grove mini fan on PIN D5 on the Grove Shield (this needs to be an pwm enabled pin)
  9. Connect the Grove air quality sensor to PIN A0 on the Grove shield (this can be replaced by an rotary angle sensor to emulate the air quality)
  10. Upload sketch on the Arduino Ethernet (code provided below)
Arduino sketch
Untitled file

Warning: Embedding code files within the project story has been deprecated. To edit this file or add more files, go to the "Software" tab. To remove this file from the story, click on it to trigger the context menu, then click the trash can button (this won't delete it from the "Software" tab).

#include <Ethernet.h>
#include <EthernetClient.h>

#include <PubSubClient.h>

#include <ATT_IOT.h>
#include <SPI.h>                //required to have support for signed/unsigned long type.

#include <Grove_LED_Bar.h>

Grove_LED_Bar bar(9, 8, 0);  // Clock pin, Data pin, Orientation

/*
  AllThingsTalk Makers Arduino Example 

  ### Instructions

  1. Setup the Arduino hardware
    - USB2Serial
    - Grove kit shield
    - Potentiometer to A0
    - Led light to D8
  2. Add 'allthingstalk_arduino_standard_lib' library to your Arduino Environment. [Try this guide](http://arduino.cc/en/Guide/Libraries)
  3. Fill in the missing strings (deviceId, clientId, clientKey, mac) and optionally change/add the sensor & actuator names, ids, descriptions, types
     For extra actuators, make certain to extend the callback code at the end of the sketch.
  4. Upload the sketch

  ### Troubleshooting

  1. 'Device' type is reported to be missing. 
  - Make sure to properly add the arduino/libraries/allthingstalk_arduino_standard_lib/ library
  2. No data is showing up in the cloudapp
  - Make certain that the data type you used to create the asset is the expected data type. Ex, when you define the asset as 'int', don't send strings or boolean values.
*/

char deviceId[] = "";
char clientId[] = "";
char clientKey[] = "";

ATTDevice Device(deviceId, clientId, clientKey);            //create the object that provides the connection to the cloud to manager the device.
char httpServer[] = "api.smartliving.io";                  	// HTTP API Server host
char* mqttServer = "broker.smartliving.io";                   

int airQualityPin = 0;                                            // Analog 0 is the input pin + identifies the asset on the cloud platform
int fanPin = 5;
int virtualPin = 2;
int ledbarPin = 8;

bool doorValue = false;

//required for the device
void callback(char* topic, byte* payload, unsigned int length);
EthernetClient ethClient;
PubSubClient pubSub(mqttServer, 1883, callback, ethClient);

void setup()
{      
  Serial.begin(9600);                                   	// init serial link for debugging
  pinMode(fanPin, OUTPUT);
  pinMode(ledbarPin, OUTPUT);
  
  byte mac[] = {0x90, 0xA2, 0xDA, 0x0F, 0x1A, 0xE3};        // Adapt to your Arduino MAC Address  
  if (Ethernet.begin(mac) == 0)                             // Initialize the Ethernet connection:
  { 
    Serial.println(F("DHCP failed,end"));
    while(true);                                    		//we failed to connect, halt execution here. 
  }
  delay(1000);                                          	//give the Ethernet shield a second to initialize:
  
  if(Device.Connect(&ethClient, httpServer))                //connect the device with the IOT platform.
  {
    Device.AddAsset(airQualityPin, "airquality", "Air Quality Sensor",false, "int");
    Device.AddAsset(fanPin, "fan", "Fan Actuator", false, "int");
    Device.AddAsset(virtualPin, "virtual", "Virtual Door", true, "bool");
    Device.AddAsset(ledbarPin, "ledBar", "Ledbar for air quality", false, "int");
    Device.Subscribe(pubSub);                               // make certain that we can receive message from the iot platform (activate mqtt)
  }
  else 
    while(true);                                            //can't set up the device on the cloud, can't continue, so put the app in an ethernal loop so it doesn't do anything else anymore.                              
}

unsigned long time;                                 		//only send every x amount of time.
unsigned int prevVal =0;
void loop()
{
  unsigned long curTime = millis();
  if (curTime > (time + 1000))                          	// publish light reading every 5 seconds to sensor 1
  {
    unsigned int airRead = analogRead(airQualityPin);           // read from light sensor (photocell)
    //airRead = map(airRead, 0, 100, 0, 255);
    Serial.print("READ: ");
    Serial.println(airRead);
    airRead = map(airRead, 0, 700, 0, 255);
    if(prevVal != airRead){
      Device.Send(String(airRead), airQualityPin);
      prevVal = airRead;
      int snelheid = 0;
      if (doorValue == false) {
        snelheid = prevVal;
        Serial.print("Snelheid: ");
        Serial.println(snelheid);
        Serial.print("Door: ");
        Serial.println(doorValue);
        
      }
      analogWrite(fanPin, snelheid);
      Device.Send(String(snelheid), fanPin);
      int level = map(airRead, 0, 255, 10, 0);
      bar.setLevel(level);
      Device.Send(String(level), ledbarPin);
    }
    time = curTime;
  }
  Device.Process(); 
}

// Callback function: handles messages that were sent from the iot platform to this device.
void callback(char* topic, byte* payload, unsigned int length) 
{ 
  String msgString; 
  {                                                     //put this in a sub block, so any unused memory can be freed as soon as possible, required to save mem while sending data
    char message_buff[length + 1];                      //need to copy over the payload so that we can add a /0 terminator, this can then be wrapped inside a string for easy manipulation.
    strncpy(message_buff, (char*)payload, length);      //copy over the data
    message_buff[length] = '\0';                        //make certain that it ends with a null         
          
    msgString = String(message_buff);
    msgString.toLowerCase();                            //to make certain that our comparison later on works ok (it could be that a 'True' or 'False' was sent)
  }
  int* idOut = NULL;
  {                                                     //put this in a sub block, so any unused memory can be freed as soon as possible, required to save mem while sending data
    int pinNr = Device.GetPinNr(topic, strlen(topic));
    
    Serial.print("Payload: ");                          //show some debugging.
    Serial.println(msgString);
    Serial.print("topic: ");
    Serial.println(topic);
    
    if (pinNr == virtualPin) {
      if (msgString == "true") {
        doorValue = true;
      } else {
        doorValue = false;
      }
      idOut = &virtualPin;
    }
  } 
  if(idOut != NULL)                                     //also let the iot platform know that the operation was succesful: give it some feedback. This also allows the iot to update the GUI's correctly & run scenarios.
    Device.Send(msgString, *idOut);    
}

Code

file_14656.txtC/C++
Arduino sketch
#include <Ethernet.h>
#include <EthernetClient.h>

#include <PubSubClient.h>

#include <ATT_IOT.h>
#include <SPI.h>                //required to have support for signed/unsigned long type.

#include <Grove_LED_Bar.h>

Grove_LED_Bar bar(9, 8, 0);  // Clock pin, Data pin, Orientation

/*
  AllThingsTalk Makers Arduino Example 

  ### Instructions

  1. Setup the Arduino hardware
    - USB2Serial
    - Grove kit shield
    - Potentiometer to A0
    - Led light to D8
  2. Add 'allthingstalk_arduino_standard_lib' library to your Arduino Environment. [Try this guide](http://arduino.cc/en/Guide/Libraries)
  3. Fill in the missing strings (deviceId, clientId, clientKey, mac) and optionally change/add the sensor & actuator names, ids, descriptions, types
     For extra actuators, make certain to extend the callback code at the end of the sketch.
  4. Upload the sketch

  ### Troubleshooting

  1. 'Device' type is reported to be missing. 
  - Make sure to properly add the arduino/libraries/allthingstalk_arduino_standard_lib/ library
  2. No data is showing up in the cloudapp
  - Make certain that the data type you used to create the asset is the expected data type. Ex, when you define the asset as 'int', don't send strings or boolean values.
*/

char deviceId[] = "";
char clientId[] = "";
char clientKey[] = "";

ATTDevice Device(deviceId, clientId, clientKey);            //create the object that provides the connection to the cloud to manager the device.
char httpServer[] = "api.smartliving.io";                  	// HTTP API Server host
char* mqttServer = "broker.smartliving.io";                   

int airQualityPin = 0;                                            // Analog 0 is the input pin + identifies the asset on the cloud platform
int fanPin = 5;
int virtualPin = 2;
int ledbarPin = 8;

bool doorValue = false;

//required for the device
void callback(char* topic, byte* payload, unsigned int length);
EthernetClient ethClient;
PubSubClient pubSub(mqttServer, 1883, callback, ethClient);

void setup()
{      
  Serial.begin(9600);                                   	// init serial link for debugging
  pinMode(fanPin, OUTPUT);
  pinMode(ledbarPin, OUTPUT);
  
  byte mac[] = {0x90, 0xA2, 0xDA, 0x0F, 0x1A, 0xE3};        // Adapt to your Arduino MAC Address  
  if (Ethernet.begin(mac) == 0)                             // Initialize the Ethernet connection:
  { 
    Serial.println(F("DHCP failed,end"));
    while(true);                                    		//we failed to connect, halt execution here. 
  }
  delay(1000);                                          	//give the Ethernet shield a second to initialize:
  
  if(Device.Connect(&ethClient, httpServer))                //connect the device with the IOT platform.
  {
    Device.AddAsset(airQualityPin, "airquality", "Air Quality Sensor",false, "int");
    Device.AddAsset(fanPin, "fan", "Fan Actuator", false, "int");
    Device.AddAsset(virtualPin, "virtual", "Virtual Door", true, "bool");
    Device.AddAsset(ledbarPin, "ledBar", "Ledbar for air quality", false, "int");
    Device.Subscribe(pubSub);                               // make certain that we can receive message from the iot platform (activate mqtt)
  }
  else 
    while(true);                                            //can't set up the device on the cloud, can't continue, so put the app in an ethernal loop so it doesn't do anything else anymore.                              
}

unsigned long time;                                 		//only send every x amount of time.
unsigned int prevVal =0;
void loop()
{
  unsigned long curTime = millis();
  if (curTime > (time + 1000))                          	// publish light reading every 5 seconds to sensor 1
  {
    unsigned int airRead = analogRead(airQualityPin);           // read from light sensor (photocell)
    //airRead = map(airRead, 0, 100, 0, 255);
    Serial.print("READ: ");
    Serial.println(airRead);
    airRead = map(airRead, 0, 700, 0, 255);
    if(prevVal != airRead){
      Device.Send(String(airRead), airQualityPin);
      prevVal = airRead;
      int snelheid = 0;
      if (doorValue == false) {
        snelheid = prevVal;
        Serial.print("Snelheid: ");
        Serial.println(snelheid);
        Serial.print("Door: ");
        Serial.println(doorValue);
        
      }
      analogWrite(fanPin, snelheid);
      Device.Send(String(snelheid), fanPin);
      int level = map(airRead, 0, 255, 10, 0);
      bar.setLevel(level);
      Device.Send(String(level), ledbarPin);
    }
    time = curTime;
  }
  Device.Process(); 
}

// Callback function: handles messages that were sent from the iot platform to this device.
void callback(char* topic, byte* payload, unsigned int length) 
{ 
  String msgString; 
  {                                                     //put this in a sub block, so any unused memory can be freed as soon as possible, required to save mem while sending data
    char message_buff[length + 1];                      //need to copy over the payload so that we can add a /0 terminator, this can then be wrapped inside a string for easy manipulation.
    strncpy(message_buff, (char*)payload, length);      //copy over the data
    message_buff[length] = '\0';                        //make certain that it ends with a null         
          
    msgString = String(message_buff);
    msgString.toLowerCase();                            //to make certain that our comparison later on works ok (it could be that a 'True' or 'False' was sent)
  }
  int* idOut = NULL;
  {                                                     //put this in a sub block, so any unused memory can be freed as soon as possible, required to save mem while sending data
    int pinNr = Device.GetPinNr(topic, strlen(topic));
    
    Serial.print("Payload: ");                          //show some debugging.
    Serial.println(msgString);
    Serial.print("topic: ");
    Serial.println(topic);
    
    if (pinNr == virtualPin) {
      if (msgString == "true") {
        doorValue = true;
      } else {
        doorValue = false;
      }
      idOut = &virtualPin;
    }
  } 
  if(idOut != NULL)                                     //also let the iot platform know that the operation was succesful: give it some feedback. This also allows the iot to update the GUI's correctly & run scenarios.
    Device.Send(msgString, *idOut);    
}

Comments

Similar projects you might like

Android App-Based Home Automation System Using IOT

Project tutorial by Team Autoshack

  • 49,261 views
  • 22 comments
  • 141 respects

SMS based Home Automation system using 1SHEELD

Project tutorial by Tanishq Jaiswal

  • 30,130 views
  • 11 comments
  • 64 respects

Basement/Crawlspace Ventilation System

Project tutorial by Christian

  • 7,283 views
  • 8 comments
  • 50 respects

JMoon MakerSpace Automation System

Project tutorial by Jasmeet Singh

  • 2,457 views
  • 1 comment
  • 21 respects

Home Automation System for a Camp with Cellular Internet

Project tutorial by Dana Mah

  • 13,712 views
  • 4 comments
  • 47 respects
Add projectSign up / Login