Anthony Garofalo
Published

IoT Command Center

A device that's jam packed with sensors and buttons and can do whatever you want it to.

AdvancedFull instructions provided8,293
IoT Command Center

Things used in this project

Hardware components

Spark Core
Particle Spark Core
Or any other dev board from the Spark family. For information on setting your dev board up, visit their Getting Started page: http://docs.spark.io/start/
×1
Perf board
For soldering the Spark Core to. You could solder directly to the board if you wanted to. You also need some perf board to make a 3.3V power rail and a 5V power rail. I use power rails from a bread board so that I can still easily change or add sensors if
×1
Momentary push button
×6
Temperature/Humidity sensor
You can use a DHT11 or DHT22. I ended up using a DHT22 because it is more accurate. The DHT22 is also a bit larger than the DHT11.
×1
Vibration Sensor
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
Piezo Buzzer or Speaker
×1
Gas/Smoke sensor
MQ5 is good for natural gas. There are so many MQ sensors to choose. From Air Quality sensors to alcohol and smoke(MQ3), you can find one to fit your needs.
×1
Adafruit NeoPixel
×1
Resistor 10k ohm
Resistor 10k ohm
×8
Power Button
×1
4-40 screws
×3
Enclosure
See below to download
×1

Story

Read more

Custom parts and enclosures

Enclosure.stl

Base.stl

Led Tunnel.stl

Schematics

Fritzting of full circuit

Logic level shifter diagram

Code

Iot Command Center

C/C++
/* 
01010000011100100110111101110100011011110010000001000111

IoT Command Center

Created March 31, 2015
Modified April 12, 2015
by Anthony Garofalo (Proto G)

Visit my YouTube channel here: https://www.youtube.com/channel/UCpTuKJrXFwybnpOG7HpTpZw
Visit my Instructables page here: http://www.instructables.com/member/Proto+G/

  _____   ______  _____  _______  _____        ______
 |_____] |_____/ |     |    |    |     |      |  ____
 |       |    \_ |_____|    |    |_____|      |_____|
  
01010000011100100110111101110100011011110010000001000111
*/

#include "Adafruit_DHT/Adafruit_DHT.h"
#include "application.h"
//#include "spark_disable_wlan.h" // For faster local debugging only
#include "neopixel__spark_internet_button/neopixel__spark_internet_button.h"

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN A7
#define PIXEL_COUNT 1
#define PIXEL_TYPE WS2812B

#define DHTPIN 1//Pin that the temperature/humidity sensor is connected to.

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11		// DHT 11 
#define DHTTYPE DHT22		// DHT 22 (AM2302)
//#define DHTTYPE DHT21		// DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +3.3V rail
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht(DHTPIN, DHTTYPE);


Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

double f;//variable for the temperature in fahrenheit.
double t;//variable for the temperature in celcius.
double h;//variable for the humidity percentage from 0-100

int SensorA0;//I did not use A0 but if you want to add another sensor on A0 you can.
int SensorA1;//
int SensorA2;//
int SensorA3;//
int SensorA4;//
int SensorA5;//
int SensorA6;//
int SensorA7;//

int button1;
int button2;
int button3;
int button4;
int button5;
int button6;

//melody1 function.  You could create multiple melody funcitons to play when certain circumstances are met
int tinkerDigitalRead(String pin);
int tinkerDigitalWrite(String command);
int tinkerAnalogRead(String pin);
int tinkerAnalogWrite(String command);

// Plays a melody - Connect small speaker to analog pin A0

int speakerPin = D0;
int AlarmTrigger = 0;


void setup() 
{
    pinMode(0, OUTPUT);//Speaker
    pinMode(2, INPUT);//button1
    pinMode(3, INPUT);//button2
    pinMode(4, INPUT);//button1
    pinMode(5, INPUT);//button1
    pinMode(6, INPUT);//button1
    pinMode(7, INPUT);//button1

    dht.begin();
  
    strip.begin();
    strip.show(); // Initialize all pixels to 'off'
    rainbow(5);
    rainbow(5);
    rainbow(5);
    
    Spark.variable("SensorA0", &SensorA0, INT);//I did not use A0 but if you want to add another sensor on A0 you can.
    Spark.variable("SensorA1", &SensorA1, INT);//
    Spark.variable("SensorA2", &SensorA2, INT);//
    Spark.variable("SensorA3", &SensorA3, INT);//
    Spark.variable("SensorA4", &SensorA4, INT);//
    Spark.variable("SensorA5", &SensorA5, INT);//
    Spark.variable("SensorA6", &SensorA6, INT);//
    Spark.variable("SensorA7", &SensorA7, INT);//I did not use A7 but if you want to add another sensor on A7 you can.
    
    Spark.variable("Temp", &f, DOUBLE);//temperature in fahrenheit
    Spark.variable("Humidity", &h, DOUBLE);//humidity percentage from 0-100
    //Spark.variable("Temp", &t, DOUBLE);//temperature in fahrenheit 
    
    /*
    The limit on Spark Variables is 10 so if you want to monitor fahrenheit 
    and celcius, just comment out an analog variable above that you are not using
    and uncomment the celcius Spark Variable
    */
    
    
    Spark.function("digitalread", tinkerDigitalRead);
    Spark.function("digitalwrite", tinkerDigitalWrite);
    Spark.function("analogread", tinkerAnalogRead);
    Spark.function("analogwrite", tinkerAnalogWrite);
}

void loop() 
{
    
   button1 = digitalRead(2);
   button2 = digitalRead(3);
   button3 = digitalRead(4);
   button4 = digitalRead(5);
   button5 = digitalRead(6);
   button6 = digitalRead(7);

  if(button1 == HIGH){
   Spark.publish("button1");
   melody1();
  }
  if(button2 == HIGH){
   Spark.publish("button2");   
  }
  if(button3 == HIGH){
   Spark.publish("button3");   
  }
  if(button4 == HIGH){
   Spark.publish("button4");   
  }
  if(button5 == HIGH){
   Spark.publish("button5");   
  }
  if(button6 == HIGH){
   Spark.publish("button6");   
  }
  
  
  
// Wait a few seconds between measurements.
	delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a 
// very slow sensor)
	float h = dht.getHumidity();
// Read temperature as Celsius
	t = dht.getTempCelcius();
// Read temperature as Farenheit
	f = dht.getTempFarenheit();
  
// Check if any reads failed and exit early (to try again).
	if (isnan(h) || isnan(t) || isnan(f)) {
		Serial.println("Failed to read from DHT sensor!");
		//rainbow(5);
		//rainbow(5);
		strip.setPixelColor(0, 0, 255, 255);
		strip.show();
		delay(1500);
		strip.setPixelColor(0, 0, 128, 255);
		strip.show();
		delay(1500);
		strip.setPixelColor(0, 255, 255, 0);
		strip.show();
		delay(1500);
		strip.setPixelColor(0, 255, 0, 0);
		strip.show();
		delay(1500);
		
		return;
	}

// Compute heat index
// Must send in temp in Fahrenheit!
	float hi = dht.getHeatIndex();
	float dp = dht.getDewPoint();
	float k = dht.getTempKelvin();

	Serial.print("Humid: "); 
	Serial.print(h);
	Serial.print("% - ");
	Serial.print("Temp: "); 
	//Serial.print(t);
	//Serial.print("*C ");
	Serial.print(f);
	Serial.print("*F ");
	//Serial.print(k);
	//Serial.print("*K - ");
	//Serial.print("DewP: ");
	//Serial.print(dp);
	//Serial.print("*C - ");
	//Serial.print("HeatI: ");
	//Serial.print(hi);
	//Serial.println("*C");
	//Serial.println(Time.timeStr());
	

  if(f < 69){
      strip.setPixelColor(0, 0, 255, 255);
	  strip.show();
	  AlarmTrigger = 0;
  }	
  
  if(f< 75 && f>69){
      strip.setPixelColor(0, 0, 128, 255);
	  strip.show();
	  AlarmTrigger = 0;
  }
  
  if(f< 80 && f>75){
      strip.setPixelColor(0, 255, 255, 0);
	  strip.show();
	  AlarmTrigger = 0;
  }
  
  if(f > 80){
      strip.setPixelColor(0, 255, 0, 0);
	  strip.show();
	  
	  if(AlarmTrigger == 0){//This is used so that the melody does not repeat continuously.
	      melody1();
	      AlarmTrigger = 1;
	  }
  }
  
  /*
  Syntax for tone
  tone(pin, frequency, duration)
  */
  
  /*
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; thisNote++) {

    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000/noteDurations[thisNote];
    tone(speakerPin, melody[thisNote],noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(speakerPin);
  }
  */
    
}


void melody1(){
    
    // notes in the melody:
    int melody[] = {1908,2551,2551,2273,2551,0,2024,1908}; //C4,G3,G3,A3,G3,0,B3,C4
    // note durations: 4 = quarter note, 8 = eighth note, etc.:
    int noteDurations[] = {4,8,8,4,4,4,4,4 };
    
    // iterate over the notes of the melody:
    for (int thisNote = 0; thisNote < 8; thisNote++) {
    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000/noteDurations[thisNote];
    tone(speakerPin, melody[thisNote],noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(speakerPin);
  }
    
    
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

int tinkerDigitalRead(String pin) {
    int pinNumber = pin.charAt(1) - '0';
    if (pinNumber< 0 || pinNumber >7) return -1;
    if(pin.startsWith("D")) {
        pinMode(pinNumber, INPUT_PULLDOWN);
        return digitalRead(pinNumber);}
    else if (pin.startsWith("A")){
        pinMode(pinNumber+10, INPUT_PULLDOWN);
        return digitalRead(pinNumber+10);}
    return -2;}

int tinkerDigitalWrite(String command){
    bool value = 0;
    int pinNumber = command.charAt(1) - '0';
    if (pinNumber< 0 || pinNumber >7) return -1;
    if(command.substring(3,7) == "HIGH") value = 1;
    else if(command.substring(3,6) == "LOW") value = 0;
    else return -2;
    if(command.startsWith("D")){
        pinMode(pinNumber, OUTPUT);
        digitalWrite(pinNumber, value);
        return 1;}
    else if(command.startsWith("A")){
        pinMode(pinNumber+10, OUTPUT);
        digitalWrite(pinNumber+10, value);
        return 1;}
    else return -3;}

int tinkerAnalogRead(String pin){
    int pinNumber = pin.charAt(1) - '0';
    if (pinNumber< 0 || pinNumber >7) return -1;
    if(pin.startsWith("D")){
        pinMode(pinNumber, INPUT);
        return analogRead(pinNumber);}
    else if (pin.startsWith("A")){
        pinMode(pinNumber+10, INPUT);
        return analogRead(pinNumber+10);}
    return -2;}

int tinkerAnalogWrite(String command){
    int pinNumber = command.charAt(1) - '0';
    if (pinNumber< 0 || pinNumber >7) return -1;
    String value = command.substring(3);
    if(command.startsWith("D")){
        pinMode(pinNumber, OUTPUT);
        analogWrite(pinNumber, value.toInt());
        return 1;}
    else if(command.startsWith("A")){
        pinMode(pinNumber+10, OUTPUT);
        analogWrite(pinNumber+10, value.toInt());
        return 1;}
    else return -2;}

Credits

Anthony Garofalo

Anthony Garofalo

2 projects • 27 followers

Comments