kathleen kuypers
Published

Smart Store Fitting Room

Queuing at fitting rooms fun? SmartStore shows the status of fitting rooms on your smartphone and lets you queue virtually!

IntermediateShowcase (no instructions)4 hours436
Smart Store Fitting Room

Things used in this project

Hardware components

IOTOPIA Rapid Development kit
AllThingsTalk IOTOPIA Rapid Development kit
×1

Software apps and online services

maker.smartliving.io

Story

Read more

Schematics

SmartStore Schematics

Code

smartstore.ino

Arduino
Adjust roomHeight and threshold to determine the region for meassurement. The default waiting is 3 persons, change dim id you want it changed. Make sure you connect sensors and actuators to the pin as defined in the code.
/*
Author: students JOMA secundair Merksem and K. Kuypers
Contact: kathleen.kuypers@gmail.com or kkuypers@jomasecundair.be

This Sketch is made for an Genuino 101 IoT board with a Grove
UART WiFi module based on the popular ESP8266 IoT SoC to communicate to the AllThingsTalk
IoT developer cloud.
The Grove UART WiFi module has firmware installed which includes the ATT_IOT library. It
communicates through Serial1 of the Genuino 101 board.

Version 1.0 dd 16_03_2016

### Instructions
1. Setup the Arduino hardware
  - Use an Arduino Genuino 101 IoT board
  - Connect the Arduino Grove shield
  - Connect USB cable to your computer
  - Connect a Grove LED to pin D2 of the Arduino shield RODE LED
  - Connect a Grove LED to pin D3 of the Arduino shield GROENE LED
  - Connect a Grove Ultrasonic ranger to pin D7 of the Arduino shield
  - There will be a virtual asset of type string on virtual port 9
  - Grove UART wifi to pin UART (D0,D1)
2. Add 'ATT_IOT_UART' library to your Arduino Environment
     More info can be found at http://arduino.cc/en/Guide/Libraries
3. Fill in the missing strings (deviceId, clientId and clientKey) in the keys.h file
4. Optionally, change sensor names, labels as appropiate
5. Upload the sketch

*/


#include "ATT_IOT_UART.h"      // AllThingsTalk Arduino UART IoT library
#include <SPI.h>               // Required to have support for signed/unsigned long type
#include "keys.h"              // Keep all your personal account information in a seperate file
#include "Ultrasonic.h"

ATTDevice Device(&Serial1);
char httpServer[] = "api.smartliving.io";          // HTTP API Server host
char mqttServer[] = "broker.smartliving.io";       // MQTT Server Address

//****************************************************************
//** PIN numbers and other constants
//****************************************************************
#define redledId 2                // Digital actuator is connected to pin D2 on grove shield
#define greenledId 3              // Digital actuator is connected to pin D6 on grove shield
#define distanceId 7              // Digital sensor is connected to pin D7 on grove shield
#define subscribeId 9             // Textbox in smartliving IOT cloud to get name to put into waitinglist
#define nameId 5                  // Virtual actuator to show tge first person of the waitinglist in label in smartliving IOT cloud
#define dim 3                     // Dimension of waitinglist array


Ultrasonic ultrasonic(distanceId);
void callback(int pin, String& value);


//****************************************************************
//** setup
//****************************************************************
void setup()
{
  Serial.begin(57600);                                 // Init serial link for debugging
  while(!Serial && millis() < 1000);                   // Make sure you see all output on the monitor. After 1 sec, it will skip this step, so that the board can also work without being connected to a pc
  Serial.println("Starting sketch");
  Serial1.begin(115200);                               // Init serial link for WiFi module
  while(!Serial1);

  while(!Device.StartWifi())
    Serial.println("Retrying...");
  while(!Device.Init(DEVICEID, CLIENTID, CLIENTKEY))   // If we can't succeed to initialize and set the device credentials, there is no point to continue
    Serial.println("Retrying...");
  while(!Device.Connect(httpServer))                   // Connect the device with the AllThingsTalk IOT developer cloud. No point to continue if we can't succeed at this
    Serial.println("Retrying");
  
  //*** assets in the smartliving IOT cloud  
  Device.AddAsset(greenledId, "FITTING ROOM FREE?", "GREEN LED", false, "boolean");     
  Device.AddAsset(subscribeId, "SUBSCRIBE TO WAITINGLIST:", "name waitinglist", true, "string");
  Device.AddAsset(nameId, "NEXT:", "next", false, "string");
  //***
    
  delay(1000);                                         // Give the WiFi some time to finish everything
  while(!Device.Subscribe(mqttServer, callback))       // Make sure that we can receive message from the AllThingsTalk IOT developer cloud (MQTT). This stops the http connection
    Serial.println("Retrying");

  pinMode(redledId, OUTPUT);                              
  Serial.println(" RED LED is ready for use!");

  pinMode(greenledId, OUTPUT);                              
  Device.Send("true", greenledId);
  Serial.println(" GREEN LED is ready for use!");

  pinMode(distanceId, INPUT);                      
  Serial.println("Ultrasonic ranger is ready for use!");

  pinMode(subscribeId, INPUT);
  Device.Send("", subscribeId);  
  Serial.println("Subscribe is ready for use!");

  pinMode(nameId, OUTPUT);
  Device.Send("", nameId);  
  Serial.println("Next is ready for use!");
}



//****************************************************************
//** variables
//****************************************************************
int roomHeight = 250;    //height of fittingroom in cm
int threshold = 50;      //we do not want to meassure up the ground in case something is lying there
int zone = roomHeight - threshold;    //the zone we meassure whether it's free

long RangeInCentimeters;
String waitinglist[dim];   //waitinglist containing names
int namesOnWaitingList=0;  //number of names on the waitinglist
boolean occupied = false;      //fresh status fitting room
boolean prev_occupied = false; //previous status fitting room



//****************************************************************
//** loop
//****************************************************************
void loop()
{
    //*** meassure
    RangeInCentimeters = ultrasonic.MeasureInCentimeters();    // read Ultrasonic Ranger Sensor  
    Serial.println("RangeInCentimeters = " + RangeInCentimeters);
    Device.Send(String(RangeInCentimeters), distanceId);
    
    if (RangeInCentimeters >= zone)
       occupied = false;   //fiiting room is free
    else
       occupied = true;    //fitting room is occupied
       
    //*** action if status has changed  
    if (!occupied && prev_occupied) //became free
    {
       Device.Send("", subscribeId);
       Serial.println("Checking waitinglist");
       tryWaitinglist();
       digitalWrite(redledId, LOW);
       digitalWrite(greenledId, HIGH);
       Device.Send("true", greenledId);
       prev_occupied = false;      
    }
    else if (occupied && !prev_occupied) //is taken
    {
       Device.Send("" , nameId);   
       Serial.println("occupied");
       digitalWrite(greenledId, LOW);
       digitalWrite(redledId, HIGH);       
       Device.Send("false", greenledId);
       prev_occupied = true;      
    }
  
    //*** show waitinglist
    Serial.println("Names on waitinglist:");
    for (int i=0;i<namesOnWaitingList;i++)
    {
       Serial.println(waitinglist[i]);
    }
    
    Device.Process(); 
    delay(2000); //meassure every 2 sec
}




//****************************************************************
//** Callback function: handles messages that were sent from the iot platform to this device.
//****************************************************************
void callback(int pin, String& value) 
{ 
	Serial.print("incoming data for: ");       // Display the value that arrived from the AllThingsTalk IOT developer cloud.
	Serial.print(pin);
	Serial.print(", value: ");
	Serial.print(value);
	Device.Send(value, pin);                   // Send the value back for confirmation   
  if(pin == subscribeId && occupied)         // One can only subscribe to the waitinglist is the fitting room is occupied
    {
        if(value != "" && value!= "FULL")
        { 
          if (namesOnWaitingList < dim)
          {
            waitinglist[namesOnWaitingList]=value;
            namesOnWaitingList++;          
            Device.Send("", subscribeId);
           }
          else
          {
            Device.Send("FULL", subscribeId);
          }        
        }
  }
}



//****************************************************************
//** Get first name of waitinglist if there is one
//****************************************************************
void tryWaitinglist()
{
  String first;
  if (namesOnWaitingList > 0)                  //there are names on the waitinglist
  {
    first = waitinglist[0];
    for (int i=1;i<namesOnWaitingList;i++)     //remove first one and move others to the front
    {
      waitinglist[i-1] = waitinglist[i];
    }
    waitinglist[namesOnWaitingList-1]="";
    namesOnWaitingList--;
    Serial.println("De first van de waitinglist is " + first);
    Device.Send(first , nameId);    
  }
}

    

keys.h

Arduino
Register at maker.smartliving.io and create a ground and a device.
Fill in the missing strings (deviceId, clientId and clientKey) in the keys.h file you get for the device
#ifndef SETTINGS
#define SETTINGS

#define DEVICEID "xxx"           // Your device id comes here
#define CLIENTID "xxx"           // Your client id comes here;
#define CLIENTKEY "xxx"          // Your client key comes here;

#endif

Credits

kathleen kuypers

kathleen kuypers

4 projects • 11 followers

Comments