Want to know if someone entered your room? Sometimes a "Keep out" sign is not enough to keep out your siblings, so here we built a simple presence detector that lets you know if someone enters your room and how many times it happened.

As a cherry on top, we added an LED Matrix that displays a happy face when the room is quiet, and the opposite when it's not!

1. Materials

  • Some jumper wires

3. Wiring

Please keep in mind that there are several LED matrixes in the market, each with different pinout distributions. Make sure your pin connections match the following schema.

3. Set up your Dashboard.

Login to your Ubidots account, create a Device source called “Spark Core” and then add a new variable called "Presence”. 

  1. Go to the "devices" tab and create a device source called "Spark Core" by clicking in the 'plus' button located in the upper right corner.
  2. Click on the created device and then “Add New Variable”.

Pro Tip: take note of your variable's ID, we need it later.

3. Create a token under "My profile" tab.

      5.  Go back to the "Dashboard" tab and add a new widget by clicking on the orange "plus" button. First choose Indicator then Sparkcore as Datasource and Presence as Variable. For the second "widget" choose "Statement"--> Sparkcore --> Presence --> Sum --> Today. The sum of Presence will tell you how many times the sensor has been activated.

4. Coding

 We will create a new app called "Presence" in the Spark Build IDE, then we need to include in our app the "HTTPCLIENT" Library to send the data to Ubidots and the "LedControl-MAX7219-MAX7221". Do not forget to include your Ubidots token and variable's ID into the code.

  1. Create a new App called "Presence" and then click on the "Libraries" icon and look for an HTTP library called “HTTPCLIENT”.

2. Click on the “HTTPCLIENT” library and then click onthe button “INCLUDE IN APP”.

3. Repeat the same steps  but this time look for the "LedControl-MAX7219-MAX7221" library

4.  Now go to the presence app, then copy and paste the following code into your code:

#include "LedControl-MAX7219-MAX7221/LedControl-MAX7219-MAX7221.h"#include "HttpClient/HttpClient.h"HttpClient http;#define VARIABLE_ID "544e4as8f7625426e332b1e8e"#define TOKEN "pSZJt9W7vdfgW3fpUsRYNKiDMgJ770NK"int presence = 0;LedControl *led;// LedControl objectuint8_t data = A5;uint8_t load = A4;uint8_t myclock = A3;// Headers needed by Ubidots APIhttp_header_t headers[] = {      { "Content-Type", "application/json" },      { "X-Auth-Token" , TOKEN },    { NULL, NULL } // NOTE: Always terminate headers will NULL};http_request_t request;http_response_t response;void setup() {    pinMode(D1, INPUT);    request.port = 80;    request.hostname = "things.ubidots.com";    request.path = "/api/v1.6/variables/"VARIABLE_ID"/values";    //Serial.begin(9600);//For Debug purposes    led = new LedControl(data,myclock,load,1); //DIN,CLK,CS,HowManyDisplays    led-> shutdown(0,false); //Turn it on    led-> setIntensity(0,7);//Set Led's Intensity, max value=15}void loop() {     presence=digitalRead(D1);     //Serial.print("presence: ");//     //.Serial.println(presence);    if(presence)    {            request.body = "{\"value\":" + String(presence) + "}"; //Sending presence to Ubidots         http.post(request, response, headers);       // Serial.println(response.status); //For debug only        //Serial.println(response.body);//See the response from the Ubidots Api        do{            selectColor(3);//Red color            led->setColumn(0,0,0x00);            led->setColumn(0,1,0x22);            led->setColumn(0,2,0x14);            led->setColumn(0,3,0x10);            led->setColumn(0,4,0x10);            led->setColumn(0,5,0x14);            led->setColumn(0,6,0x22);            led->setColumn(0,7,0x00);            delay(2000);            presence=digitalRead(D1);        }while(presence);    }        request.body = "{\"value\":" + String(presence) + "}"; //Sending presence to Ubidots        http.post(request, response, headers);        selectColor(2);//Green color        led->setColumn(0,0,0x7E);        led->setColumn(0,1,0x81);        led->setColumn(0,2,0x95);        led->setColumn(0,3,0xB1);        led->setColumn(0,4,0xB1);        led->setColumn(0,5,0x95);        led->setColumn(0,6,0x81);        led->setColumn(0,7,0x7E);}void putByte(byte data_1) //not my function{  byte i = 8;  byte mask;  while(i > 0)  {    mask = 0x01 << (i - 1); // get bitmask    digitalWrite(myclock, LOW); // tick    if (data_1 & mask) // choose bit    {    digitalWrite(data, HIGH);// send 1    }    else    {    digitalWrite(data, LOW); // send 0    }    digitalWrite(myclock, HIGH); // tock    --i; // move to lesser bit  }}void selectColor(int cmd) //{  byte reg = 0x0c;  //max7219_reg_shutdown  byte col = 0x01;  //shutdown false  byte col2 = 0x00;  //shutdown true  int c = 0;  digitalWrite(load, LOW);if (cmd == 0)//Off{  for ( c =1; c<= 4; c++) {    putByte(reg);// specify register    putByte(col2);//((data & 0x01) * 256) + data >> 1); // put data    putByte(reg);// specify register    putByte(col2);//((data & 0x01) * 256) + data >> 1); // put data  }}else if (cmd == 2)//Green{     led-> setIntensity(0,14);  for ( c =1; c<= 4; c++) {    putByte(reg);// specify register    putByte(col);//((data & 0x01) * 256) + data >> 1); // put data    putByte(reg);// specify register    putByte(col2);//((data & 0x01) * 256) + data >> 1); // put data  }}else if (cmd == 3)//Red{       led-> setIntensity(0,3);  for ( c =1; c<= 4; c++)   {    putByte(reg);// specify register    putByte(col2);//((data & 0x01) * 256) + data >> 1); // put data    putByte(reg);// specify register    putByte(col);//((data & 0x01) * 256) + data >> 1); // put data  }}  digitalWrite(load, LOW);  digitalWrite(load,HIGH);}

view rawpresence.ino hosted with ❤ by GitHub

      5. Flash the code, wait until the core stops flashing magenta and we are done! 

5. Wrapping Up

Do you like this project? You could make some improvements  by adding events in Ubidots, creating email or SMS alerts, or adding more widgets. Don't forget to check out some of our latest posts:

Do you have an IoT project in mind? Make it happen in days, not months!

Content originally posted in Ubidots Blog on December 9, 2014. 

Did this answer your question?