Introduction: Smart Home Controller With Sensorflare and Spark Core of Particle: Read Your Sensor Values on Sensorflare

Hi everyone! This instructable shows you how you can control your smart
devices using Sensorflare and Particle core. The possibilities are many...You can use Particle core - a little Wi-Fi development board in order to connect your hardware (e.g sensors, actuators) to the Internet. Then, using the Sensorflare platform you can control and observe your devices adding the missing intelligence to your daily.

Here you can find an example of a System with Temperature, Light and PIR (for motion detection) sensors and a Led that light with different brightness values. At the end of this example, you will have the enough knowledge to include your own functionalities on your core and controlled it with Sensorflare.

Step 1: List of Elements:

Step 2: Hardware Connection

The test of the System will be made by wire connecting the elements to the core, using the included on set breadboard. Notice that the low-noise regulated 3.3V power feed the sensors, the normal regulated 3.3V power feed the PIR.

Also a PCB can be printed by using Eagle program. Add on the PCB board the necessary element that has been described before in their respective position and encloses the device in a ready box or design your own one to print using a 3D printer.

Step 3: Element Description:

  • The Particle Core is, in essence, the "brains" of your connected hardware project. Learn more about your kit and the Core pins on Particle webpage
  • Analog Sensors elements and circuitry connection with the core:
    • Photo-resistance:The resistance of the photo-resistor varies according to the amount of light that fall on it. The relationship between the resistance “Rl” and light intensity “Lux” for a typical Light Dependent Resistor is: Rl=500/Lux(kohm). With photocell connected to 3.3V regulated voltage output of the core through a 10 k resistor, the output voltage read from the Analog Input A4 is: Vo=3.3∗Rl/(Rl+10K). By reorder the equation the Resistance Value of the photocell is: Rl=Vo∗10/(3.3−Vo) Then the light intensity is calculated by: Lux=500/Rl
    • LM335AZ Temperature Sensor: M335 are precision temperature sensors. It operates as a 2-terminal Zener and the breakdown voltage is directly proportional to the absolute temperature at 10mV/°K. For this example the TO-92 package is used and a basic temperature sensor simplified schematic it implemented.The value of R1 must be chosen carefully in relation to the Power Supply. The LM335 datasheet gives a current constraint for a correct Operation Output Voltage. The current must be between 400μA and 5mA. Then the temperature range must be chosen. In this case will be enough with guarantee correct measures until 45°C then this maximum temperature value will be associated with the maximum Output Voltage provided by the sensor (3.03V). A value of R1 of 330Ω for a Power Supply of 3.3 V guarantee that for the maximum Output Voltage the minimum current will be bigger than the limit: Imin=3.3−3.03330=0.8mA. Also guarantee, for minimum temperature measures of 0°C whose Voltage Output will be 2.73V (equivalent to 273 Kelvin), maximum current lower that the limit Imax=3.3−2.73330=1.72mA.

    • PIR: The PIR Sensor detects motion by measuring changes in the infrared (heat) levels emitted by surrounding objects. When motion is detected the PIR sensor outputs a high signal on its output pin. This PIR sensor from Paralax can be fed with the 3.3V regulated voltage that provides the core. The output pin of the sensor is directly connected to a Digital Input of the core, for this example to D0.

    • One led is included. Will be connected through a 1KΩ resistor to the Analog Output A0 that can work as PWM output. The brightness of the sensor will vary with the duty cycle of the PWM signal that feed it.

Step 4: Paricle Core: Claim of Core, WiFi and Particle's Cloud Connection

  • You have to create an account on Particle in order to reclaim your core. The easiest way to add a device is via the iOS or Android app. Follow the steps that are indicated on Particle to add successfully the core to your account - easy future connection to your specific WiFi and Particles’ cloud connection.

Step 5: Particle's Web IDE: API Access Token

  • Go to the Particle's Web IDE and log in with the account that you have created before through your telephone application.
  • Now you are connected to the Web IDE of Particle. Your API key on Particle cloud is necessary for include your core on Sensorflare platform. Follow the instruction to access this information on the Particle's Web IDE.

Step 6: Upload the Code to Connect Your Particle Core Project in Sensorflare Platform

  • To include our Particle Core project on Sensorflare platform the “sensorflare.h” library will be used. A manual of how to use the library can be found on GitHub.

  • Go to "library" on Particle's WEB IDE and find the Sensorflare library on "Community Libraries".
  • Select the file sensorflare-publish.ino and click "use this example".
  • Flash the program on your core.

Step 7: Description of the Code

Variable declaration:

First of all it is included the library in the way that has been described before. As a result the next line is include in the code.

#include "sensorflare/sensorflare.h"

For every pin that we want to declare as a PWM output and be remote control through Sensorflare will be instanced a “PWMout” object and associated the specific pin. In the example it is connected a led on A0, then the object called pwm receive as parameter A0. The light intensity is controlled remotely by a generation of a PWM signal with duty cycle between 0 and 255.

SensorFlare::PWMOut pwm(A0);

Now it is necessary to declare an object for every variable which value we want to be able to access through Sensorflare platform. The variables can be published on the Spark Core cloud as PRIVATE (only you will be able to access their values) or PUBLIC (every user of Spark can access their values from the cloud). Then in the example is declared 2 object varTem and varPin that will reference 2 variables that will be published as public on Spark and access from your Sensorflare account by the names temperature and pir respectively. Another one object varLight that will reference a private variable on the Spark cloud and will access from your Sensorflare account by the name light.

//Both methods initialized the variable that will be published as PUBLIC <br>SensorFlare::VarPublish varTem("temperature");
SensorFlare::VarPublish varPir("pir","PUBLIC"); 

//Initialized the variable that will be published as PRIVATE
SensorFlare::VarPublish varLight("light","PRIVATE");

Must be declared the variables that will be published later.

float temperature;
float light;
int status;

Finally must be declare any other variable that wanted to be used in the program.

int tem_pin=A3;
int light_pin=A4;
int pir_pin=D0;
int new_status;
bool change;

Setup():

The setup function is only running one and is generally used to initialize variables, pin modes and functions. It is necessary to call the method “begin()” of every object that has been defined before.

pwm.begin();
varTem.begin(temperature);
varLight.begin(light);
varPir.begin(status);

Extra pin that will not be remote control and will be the digital input Pin for the PIR sensor.

pinMode(pir_pin,INPUT);

Loop()

The loop() function loops consecutively, allowing your program to change and respond.

The included analog sensor (Temperature and photo resistor) provides, in their respective analog inputs of a core, a voltage between 0 to 3.3V that are proportional to the variation of temperature and light in the environment. The core used an analog digital converter of 12 bits that map a signal from 0 to 3,3V in values from 0 to 4095 when the “analodRead()” function is called. First of all, it must be calculated the voltage that corresponds to the value reading.

The specific LM335 temperature sensor gives a voltage proportional to the temperature in Kelvins with a Sensitivity of 10mV/K. Then the temperature in Kelvin will be achieved by converting the voltage to mV units and divided it between the Sensitivity. For final expression of the temperature in the expected unit (°C) the conversion from Kelvin is made by subtract 273.15 (the corresponding 0 in Kelvin degrees).

The luminosity, intensity of light, can be calculated using the relations of the sensor that has been provided in the before section of the manual. Firstly the photo-resistor value in KΩ and then the equivalent luminosity in Lux.

// Temperature sensor
float tem= analogRead(tem_pin); // read the value from the sensor
// The returned value from the Core is going to be in the range from 0 to 4095
// Calculate the voltage from the sensor reading
float voltage = (tem * 3.3) / 4095;
float deg =voltage* 100; // multiply by 100 to get degrees in K
temperature = deg - 273.15; // subtract absolute zero to get degrees Celsius  
//Luminosity
float photocell= analogRead(light_pin); // read the value from the sensor
// The returned value from the Core is going to be in the range from 0 to 4095
// Calculate the voltage from the sensor reading
float Vphotocell = ((photocell * 3.3) / 4095);
float rl=(Vphotocell*10)/(3.3-Vphotocell);//Photoresistor value in KΩ
float value=500/rl;//luminosity 
light= (int) value;

The PIR provided a digital signal (1 when is detected a move and 0 if it is not moving present) that is read through the corresponding digital input of the core. The interesting event in this case is when the sensor detects a movement, represent it by a transition from 0 to 1 (the variable status will change their value to 1) in the respective input when start the move and a transition from 1 to 0 when stop (the variable status will be 0). Then an event will be published every time that a transition happened in the specific pin. The code detects the PIR transition and publish every time that happened. For this has been used the method “Publish(Variable,period)” in which the variable to publish is the status and it is publish immediately when the method is call by select a period of 0.

//Find the change of state of the PIR sensor. Recognize move
new_status=digitalRead(pir_pin);
if (status!=new_status){
	status=new_status;
        change=TRUE;
}

//Publish every time that exist a change in the pin on which is connect the PIR output
if (change==TRUE) {
       varPir.Publish(status,0);//Publish the variable at the called method time 
       change=FALSE;
}

The Temperature and luminosity must be published periodically - in this case every 15 seconds is enough. The “Publish(Variable,period)” method will be called for every object that represent each variable which want to be published by associated the respective Variable to publish and a Period of 15 seconds.

varTem.Publish(temperature,15);
varLight.Publish(light,15);

Step 8: Setup Sensorflare to Communicate With You Particle's Core

  • First you need to connect to Sensorflare and visit the Connected Accounts page to enable communication between Sensorflare and your Particle core.
  • Click the "Add new account" button and select Particle option.

Step 9: Include Your API Access Token

  • Introduce your API access token that you have found on the Step 5

Step 10: Choose the Specific Core Output Functionalities

  • All your Particle devices that has been uploaded to Particle cloud will be listed here. Select the specific one that you want to be remote control with Sensorflare.
  • Select the Digital pins that you want switch on/off remotely. Any Digital pin will be controlled remotely on this example.
  • Select the output pins in which you have connected an element that will be remotely controlled by a PWM signal. In this example is being selected the A0 pin in which has been connected the Led.

Step 11: Control PWM Switch Using Sensorflare

  • The brightness of the led will be controlled from your Sensorflare account. Switch on/of your PWM switch and select the "brightness". The parameter "brightness" select the duty cycle of the PWM signal that is created on the A0 PWM output pin in which is connected the led.

Step 12: Observe Your Sensor Values Using Sensorflare

  • You can check your sensor values on Sensorflare. The variables that you have been published on your code core will be observed here. The PIR, temperature and light sensor values are available in this example.
  • You can select on each sensor for seeing the day values statistics.