Luminance IoT

 

        This project allows you to check the luminance from a TSL2561 luminance sensor on a mobile App.

 

    Complexity

Project  Luminance Monitor
Time to build  1 hour
Cost $35
Arduino Code download
Cloud Device Code N/A
Mobile App Code download

 

    Hardware

        Here you have the list of the components required to build the hardware. We assume you have a computer, a router and a network cable. If you purchase the Arduino board from the specified source it comes with the USB cable. Otherwise you might need to provide a USB cable too. You can find better options for hardware. Be careful what components you buy. For Ethernet boards there are many versions with different chipsets but only the standard Arduino Shield or equivalent works with the software provided here. The prices are just a guide. These are the values we found when we created this project.

  
Picture Name Price Source

Arduino Uno

 $13 Link
Arduino Ethernet Shield $10 Link
Luminance Sensor (TSL2561) $6 Link
Single Port Female to Female Jumper Wires $3 Link
Single Port Male to Male Jumper Wires $2 Link

 

        We recommend to buy an Arduino board with a smaller USB connector (mini or micro USB) because the Ethernet Shield will not insert properly most of the time if you do not use original Arduino boards.

        Here you have a picture with the components we used:

 

 

     Specifications

       This section shows the pinout of the components or provide a link to the specifications of the component.

 

Component Name Pins
Arduino Uno See: http://arduino.cc/en/Main/arduinoBoardUno
Arduino Ethernet See: http://arduino.cc/en/Main/ArduinoEthernetShield
Luminance Sensor
Pin Description
Vin 3-5 V
GND Ground
3v 5V
Addr  I2C Signals
Int
SDA
SCL

 

     Wiring

        This section shows you how to connect the components together. First you have a pin-out connection and below there are pictures showing how it's done on the real boards.

 

Luminance Sensor Pin ArduinoUno
GND GND
Vin +5V
SDA A4
SCL A5

 

Arduino Ethernet ArduinoUno
Just insert the Ethernet Shield into Arduino Uno
 

       Here you have pictures with the wiring

 

 

 

 

        The schematic is

 

 

    Software

        To run the system you need two software components. One is the software to run the hardware (Arduino Software) and the other one is the mobile App (Mobile App).

 

     Arduino Software

        To run the software you need the Arduino development environment. You need an external Library that is not included by default with Arduino IDE.

Library Location Author Comments
TSL2561 Link Check Site Please be aware there are two components you need to download. Foloow the instructions on the website

 

        You have to load the software provided here and configure it. There is only one value you need to change in the code.

 

Field Description
device_id

This is a unique id you have to create for your hardware device to identify it from others in the cloud. Use this www.guidgenerator.com to generate GUID (Global Unique ID)

String devId      = "b5ebf7a7-e63a-4660-8f83-cf1e5d39cf5b";
auth

This is the authentication string requred for basic authentication of the http requests. Create a string from <email_address>:<API_Password> and encode it base64. The sample here "dXNlcjpwYXNzd29yZA==" is the encoding for "user:password"

String auth    = "dXNlcjpwYXNzd29yZA==";

 

       And here you have the sketch (download)

 

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xED, 0xBE, 0xEF, 0xFE, 0xD2 };        // mac address for ethernet board
EthernetClient eth;                                         // the Ethernet client

char server[]  = "www.ic2pro.com";                          // Wiring Cloud host name
int port       = 80;
String devId   = "111-222-333";                             // Device ID. CREATE YOUR OWN GUID; Use this http://www.guidgenerator.com/
String auth    = "dXNlcjpwYXNzd29yZA==";                    // Authentication credentials Create a string from <email_address>:<API_Password> and encode it base64
                                                            // The sample here "dXNlcjpwYXNzd29yZA==" is the encoding for "user:password"
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);


// Update LUMINANCE signal on the Wiring Cloud
void sendLuminance(String luminance)
{
  Serial.println("Luminance = " + luminance);
  Serial.println("Send Luminance...");
  if (eth.connect(server, port)) {
    eth.println("GET http://" + String(server) + ":" + String(port) + "/Wire/connector/set?id=" + devId + "&LUMINANCE=" + luminance+ " HTTP/1.1");
    eth.println("Authorization: Basic " + auth);
    eth.println("Connection: close");
    eth.println();
    Serial.println("Luminance sent to server.");
  }
  else {
    Serial.println("Connection Error");
  }
  eth.stop();
}


// retrieve luminance from sensor (Check Adafruit documentation to see how it's done)
String getLuminance()
{
  String rs = "";
  sensors_event_t event;
  tsl.getEvent(&event);
 
  if (event.light)
    rs = String(event.light);
  else
    rs = "N/A";
    
  Serial.print("Luminance: ");  Serial.println(rs);

  return rs;
}

void displaySensorDetails(void)
{
  sensor_t sensor;
  tsl.getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" lux");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" lux");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" lux");
  Serial.println("------------------------------------");
  Serial.println("");
  delay(500);
}

void configureSensor(void)
{
  tsl.enableAutoRange(true);            // Auto-gain ... switches automatically between 1x and 16x 
  tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);      // fast but low resolution

  /* Update these values depending on what you've set above! */
  Serial.println("------------------------------------");
  Serial.print  ("Gain:         "); Serial.println("Auto");
  Serial.print  ("Timing:       "); Serial.println("13 ms");
  Serial.println("------------------------------------");
}


void setup() {
  Serial.begin(9600);
  Serial.println("Start...");

  Ethernet.begin(mac);                                           // start the Ethernet connection:
  delay(1000);                                                   // give the Ethernet shield a second to initialize:

  if(!tsl.begin())                                               // check if luminance sensor is workign
  {
    Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!");
    while(1);
  }

  /* Display some basic information on this sensor */
  displaySensorDetails();
  
  /* Setup the sensor gain and integration time */
  configureSensor();
}

void loop() {
    sendLuminance(getLuminance());
    delay(5000);                                                  // send luminance only every 5 seconds
}

    

Mobile App

 

        The mobile application to control the security system is built with "App Inventor" (ai2.appinventor.mit.edu). If you just want to import the .aia file you can download it here. Otherwise you have to create next components.

 

Component Name Description
Label LuminanceMsg A text that shows the luminance.
Clock Timer A timer that will send regular requests to get the luminance from the Wiring Cloud.
Web APIGetLuminance

The HTTP request that will be sent to retrieve the luminance value. Set URL to:

http://www.ic2pro.com/Wire/connector/get?id=<device_id>&LUMINANCE

don't forget to put the device_id you set in the Arduino Code

        In the code you have to set a proper authentication string. Replace the string "Basic dXNlcjpwYXNzd29yZA==" with your authentication string. Create a string from <email_address>:<API_Password> and encode it base64. The sample here "dXNlcjpwYXNzd29yZA==" is the encoding for "user:password". Make sure you append "Basic " at the beginning of it.

        And this is the logic you have to create in App Inventor. Once that is done you just build the APK and install it on your Android phone.

 

    Final Product

       Here you have a picture of the "final" product, hardware and screenshots from App.