So you think you want to be an IOT Geek – Adventures with Photon and Grove – IOT Fan

a picture showing temperature sensor and fan connected to grove shield

 

IOT is the next technological wave and we all would be riding it weather we want or not. As a technologist by hobby and profession I like idea of everything I interact with being smarter and it is quickly turning into reality. You are right, we are just getting started and we have a long way to go use the Internet of Things to make everyone’s life’s easier, more comfortable and save money as we still do not fully understand how we would manage a lot of things like interactions, interconnections and most importantly security of these millions of devices across the internet and constantly communicating.

 

However we will explore more about all of these as we go along this adventure. In this series we will be playing with Photon and Grove components and see where this journey takes us.

 

Here is what I ordered and will be using in this series

A Particle Photon – $19 (Photon)

Grove Kit for Photon – $49 (Grove Shield for Photon)

Grove Mixer Pack V1 – $29 (Grove Mixer Pack V2)

 

This would give us enough devices to play with. This is all we need for the entire tutorial however you do not need to order all of this, you could also order individual parts for the adventure you like as well.

 

If you already have knowledge of electronics than you might get the photon Starter kit and components that you need and use soldering and or jumper wires to make the circuit for your self.

 

If you are not good with electronics and or just lazy like me then here is the minimum of what you need to play along with the recipes in this series

1 Particle Photon

1 Grove Photon Shield

Individual Grove Components

 

Enough about starter information, let’s get started with our First Adventure

 

Fan with button

 

Requirement

  • Photon
  • Grove Shield for Photon
  • Mini Fan
  • Button
  • Cardboard

 

Let’s Get Cracking

  • Place the Photon on the Grove shield if you have not already done so and connect the USB port to the power.
a picture showing placement of photon on the grove shield

Photon Grove Shield

 

  • Now lets adds the connector wires to port A4 and D2
a picture showing connector wires to Grove shield

Connector Wire Photon

  • Connect the Button to the connector wire on D2
a picture showing the grove button

Grove Button

  • Connect the Mini Fan converter to connector on A4 and connect the Mini Fan to the 2 wire port on Fan connector
a picture showing grove mini fan assembly

Grove Mini Fan Assembly

  • This is how the final assembly should look like.
a picture showing a fan and button connected with photon

Mini Fan with Button

  • And now we would need to write some code to make this work. To start coding you would either need to login to your cloud IDE (Particle Build) or create a new project in your local particle Dev IDE (Particle Local IDE)

 

// -------------------------------------
// Adventure : Start a Fan with a button
// -------------------------------------

#include "application.h"

// name the pins
#define BUTTONPIN D2
#define FANPIN A4

// This routine runs only once upon reset
void setup()
{
  pinMode(BUTTONPIN, INPUT);
  pinMode(FANPIN, OUTPUT);
}

// This routine loops forever
void loop()
{
  int val = digitalRead(BUTTONPIN);                // read the Button Input
  if(val == 0) {                                   // if button is pressed
    digitalWrite(FANPIN, LOW);                   // let the fan run
  }
  else {
    digitalWrite(FANPIN, HIGH);                 // stop the fan
  }
  delay(500);
}
  • Now when you press the button the Fan should start spinning.
a picture showing running IOT fan through button

IOT Fan

Smart Fan – Runs when it’s hot

Requirement

  • Photon
  • Grove Shield for Photon
  • Mini Fan
  • Cardboard
  • Temperature Sensor

Lets make this fan smarter. We would use the temperature sensor to tell us the temperature and when its hot the fan will start spinning automatically.

  • Remove the connection of the button and add the temperature sensor at A0 in the Grove Shield
a picture showing connectors at A0 and A4 in Grove Shield

A4 and A0

  • This is how the final assembly would look like
a picture showing temperature sensor and fan connected to grove shield

Run fan when its hot

// ------------------------------------------------
// Adventure: Start a Fan when it’s hot
// ------------------------------------------------
#include "application.h"
#include <math.h>

// name the pins
#define MOTORPIN A4
#define TEMPPIN A0

int reading = 0;
double volts = 0.0;
int getrand(int min,int max);
double tempC = 0.0;
double tempF = 0.0;

// This routine runs only once upon reset
void setup()
{
  pinMode(MOTORPIN, OUTPUT);
  pinMode(TEMPPIN, INPUT);
}

// This routine loops forever
void loop()
{
  int B = 3975;
  int analogValue = analogRead(TEMPPIN);                                      // read temprature sensor pin
  float resistance=(float)(4095-analogValue)*10000/analogValue;               // get the resistance of the sensor
  tempC=1/(log(resistance/10000)/B+1/298.15)-273.15;                          // temprature in celcius
  tempF = tempC * 9.0 / 5.0 + 32.0;                                           // temprature in farenhiet
  Serial.println(tempF);

  if(tempF > 75) {                                 // if it's hot
    digitalWrite(MOTORPIN, HIGH);                   // let the fan run
  }
  else {
    digitalWrite(MOTORPIN, LOW);                 // stop the fan
  }
  delay(5000);
}

 

Run Fan through internet or when it’s hot

In the this adventure we will make this fan even smarter by able to receive commands through the internet so that we could start it through a browser.

Lets change our code to include a local variable and also expose a cloud function which would internally call the function to control the fan. Also we will alter the condition for turning on and off the fan to include the command given tough internet.

 

// ------------------------------------------------
// Adventure: Start a Fan when it’s hot
// ------------------------------------------------
#include "application.h"
#include <math.h>
// name the pins
#define MOTORPIN A4
#define TEMPPIN A0

int reading = 0;
double volts = 0.0;
double tempC = 0.0;
double tempF = 0.0;
String fanCommand = "";
int fanState = -1;


// This routine runs only once upon reset
void setup()
{
  pinMode(MOTORPIN, OUTPUT);
  pinMode(TEMPPIN, INPUT);
  Particle.function("fan", fanController);
}

// This routine loops forever
void loop()
{
  int B = 3975;
  int analogValue = analogRead(TEMPPIN);                                      // read temprature sensor pin
  float resistance=(float)(4095-analogValue)*10000/analogValue;               // get the resistance of the sensor
  tempC=1/(log(resistance/10000)/B+1/298.15)-273.15;                          // temprature in celcius
  tempF = tempC * 9.0 / 5.0 + 32.0;                                           // temprature in farenhiet
  Serial.println(tempF);

  if(tempF > 80 || fanCommand.equalsIgnoreCase("on")) {                                 // if it's hot
    digitalWrite(MOTORPIN, HIGH);                   // let the fan run
  }
  else {
    digitalWrite(MOTORPIN, LOW);                 // stop the fan
  }
  delay(5000);
}

int fanController(String command) {
    Serial.println(command);
    fanCommand = command;

    if (command.equalsIgnoreCase("on")) {
        digitalWrite(MOTORPIN, HIGH);                 // let the fan run
        fanState = 1;
    }
    else if (command.equalsIgnoreCase("off")) {
        digitalWrite(MOTORPIN, LOW);                 // stop the fan
        fanState = 0;
    } else {
        digitalWrite(MOTORPIN, LOW);                 // stop the fan
    }
    fanState = -1;
    return fanState;
}

 

Now let us create a simple html page to turn on and off our Fan

 

<html>
  <head>
    <title>Fan Controller</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
    <script>
    var accessToken = "Your device acess token";
    var deviceID = "You device Id";
    var urlLED = "https://api.spark.io/v1/devices/" + deviceID + "/fan";

    function callbackFanOn(data, status) {
        if (status == "success") {
            $('#fan-gif').attr('src','images/FanRunning.gif');
        } else {
            alert("There was a problem turning on the fan.");
        }
    }

    function callbackFanOff(data, status) {
        if (status == "success") {
            $('#fan-gif').attr('src','images/FanStopped.gif');
        } else {
            alert("There was a problem turning off the fan.");
        }
    }

    function switchOn(){
        $.post(urlLED, {params: "on", access_token: accessToken }, callbackFanOn);

    }

    function switchOff(){
        $.post(urlLED, {params: "off", access_token: accessToken }, callbackFanOff);
    }
    </script>
  </head>

  <body>
    <div style="align:center;">
      <input type="button" onClick="switchOn()" value="ON"/>
      <input type="button" onClick="switchOff()" value="OFF"/>
      <br>
      <img id="fan-gif" src="" >
    </div>
  </body>
</html>

 

a picture showing controlling fan from internet

Control Fan Through Internet

 

Hope you had fun doing the project. Any questions, comments and feedback are most welcome.