Elias De Coninck
Published

Automation of ventilation system

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

Full instructions provided6,625
Automation of ventilation system

Things used in this project

Hardware components

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

Story

Read more

Code

file_14656.txt

C/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);    
}

Credits

Elias De Coninck
1 project • 3 followers

Comments