Jolien EggermontBrecht Vercauteren
Published

aLUXander

To prevent energy loss in larger buildings, we created aLUXander: an assistant to keep track of unnecessary artificial lighting.

IntermediateShowcase (no instructions)1 hour1,923
aLUXander

Things used in this project

Hardware components

IOTOPIA Rapid Development kit
AllThingsTalk IOTOPIA Rapid Development kit
×1

Story

Read more

Schematics

Arduino/Genuino 101

Here you see how we would attach the components (Except for the Grove UART Wifi), if we wouldn't have used the grove shield.
Prototype%20fritzing%201

Code

Arduino/Genuino 101 sketch

Arduino
The sketch the Genuino 101 uses. Most of the proces happens here, the All Things Talk platform is mostly for being able to check the situation anywhere.
/*Author: Brecht Vercauteren, Student at OLVP Sint-Niklaas
28/03/2016
This code is written for a Genuino 101 with a grove shield and components of the Iotopia rapid development kit.
The code makes use of the AllThingsTalk platform.

Hardware:
- Genuino 101
- Arduino Grove shield
- Grove light sensor on pin A0
- Grove button on digital pin 5
- Grove led on digital pin 4
- Grove buzzer on digital pin 8
- Grove UART Wifi on pin UART
*/

//Include libraries
#include "ATT_IOT_UART.h"
#include <SPI.h>
//Include credentials
#include "keys.h"

//make an 'All Things Talk' Device
ATTDevice Device(&Serial1);
char httpServer[] = "api.smartliving.io";
char mqttServer[] = "broker.smartliving.io";

//Pins
const int ledPin = 4;
const int buttonPin = 5;
const int buzzerPin = 8;
const int ldrPin = 0;

//Buzzer
int signalState;
long lastBuzz = 0;
int buzzState;
int buzzDelay = 500;

//Ldr
int ldrState; //Value > or = ldrThreshold: HIGH; value < ldrThreshold: LOW
int ldrThreshold = 700;
int ldrRead; //The value of the ldr on every moment
int prevLdrState = 0;

//Button
int buttonState; //Supposed state of the button (for debounce function)
int buttonRead; //Current measured state of the button
int prevButtonState = LOW; //For debounce fuction
long lastDebounceTime = 0; //For debounce function
long bounceDelay = 50; //For debounce function
int actionState = LOW; //The value sent to the ATT platform

//Control
const int warningId = 1; //Virtual sensorId
int warningState; //Should there be a warning?
int prevWarningState = LOW;

void callback(int pin, String& value);

void setup()
{
  pinMode(buzzerPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(ldrPin, INPUT);
  
  //Start serial communication.
  Serial.begin(57600);
  while(!Serial && millis() < 3000); //Only wait 3 seconds for serial comunication.
  Serial.println("Starting sketch");
  Serial1.begin(115200);
  while(!Serial1);

  //Start connection with the ATT platform
  while(!Device.StartWifi())
    Serial.println("Retrying...");
  while(!Device.Init(DEVICEID, CLIENTID, CLIENTKEY))
    Serial.println("Retrying...");
  while(!Device.Connect(httpServer))
    Serial.println("Retrying...");

  //Add assets: pin, name, description, sensor/actuator, type
  Device.AddAsset(ledPin, "Light", "Switches light on/off", true, "boolean");
  Device.AddAsset(buttonPin, "Switch", "State of the switch", false, "boolean");
  Device.AddAsset(ldrPin, "Ldr", "Ldr value", false, "string");
  Device.AddAsset(buzzerPin, "Buzzer", "Buzzes if needed", true, "boolean");
  Device.AddAsset(warningId, "Warning", "Light is unnecessarily switched on.", false, "boolean");

  delay(1000);
  while(!Device.Subscribe(mqttServer, callback)) {
    Serial.println("Retrying...");
  }
  Serial.println("Ready");
}

void loop()
{
  //Switch: debounce function + send "true"/"false" to ATT
  buttonRead = digitalRead(buttonPin);
  if(buttonRead != prevButtonState) {
    lastDebounceTime = millis();
  }
  if((millis() - lastDebounceTime) > bounceDelay) {
    if(buttonRead != buttonState) {
      buttonState = buttonRead;
      if(buttonState == HIGH) {
        actionState = !actionState;
        if(actionState == HIGH) {
          Device.Send("true", buttonPin);
        }
        else if(actionState == LOW) {
          Device.Send("false", buttonPin);
        }
      }
    }
  }
  prevButtonState = buttonRead;

  //Ldr: Send 1 if there is enough light without artificial lighting, otherwise, send 0.
  ldrRead = analogRead(ldrPin);
  if(ldrRead < ldrThreshold) {
    ldrState = 0;
  }
  if(ldrRead >= ldrThreshold) {
    ldrState = 1;
  }
  if (ldrState != prevLdrState) {
    if(ldrState == HIGH) {
      Device.Send("Enough light", ldrPin);
    }
    if(ldrState == LOW) {
      Device.Send("Too dark", ldrPin);
    }
    prevLdrState = ldrState;
  }

  //Buzzer: Buzzes if ATT says so.
  if(signalState == HIGH) {
    if((millis() - lastBuzz) > 500) {
      buzzState = !buzzState;
      digitalWrite(buzzerPin, buzzState);
      lastBuzz = millis();
    }
  }

  //Control: If there is enough light and light is switched on: send warning to ATT.
  if(ldrState == 1 && actionState == HIGH) {
    warningState = HIGH;
  }
  else {
    warningState = LOW;
  }
  if(warningState != prevWarningState) {
    if(warningState == HIGH) {
      Device.Send("true", warningId);
    }
    if(warningState == LOW) {
      Device.Send("false", warningId);
    }
  }
  prevWarningState = warningState;
  
  Device.Process();
}

//Handle incoming information
void callback(int pin, String& value)
{
  Serial.print("Incoming data for: ");
  Serial.print(pin);
  Serial.print(", Value: ");
  Serial.println(value);

  //If ATT says so, switch light on/off
  if(pin == ledPin) {
    if(value == "true") {
      digitalWrite(ledPin, HIGH);
    }
    if(value == "false") {
      digitalWrite(ledPin, LOW);
    }
  }

  //If ATT says so, make the buzzer buzz.
  if(pin == buzzerPin) {
    if(value == "true") {
      signalState = HIGH;
    }
    if(value == "false") {
      signalState = LOW;
      digitalWrite(buzzerPin, LOW);
    }
  }
}

Credits

Jolien Eggermont

Jolien Eggermont

1 project • 0 followers
Hi
Brecht Vercauteren

Brecht Vercauteren

1 project • 0 followers
Arduino enthusiast

Comments

Add projectSign up / Login