/* 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; i7) 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;}