Created
December 30, 2015 23:29
-
-
Save clarity99/aefa1b25651411083785 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This #include statement was automatically added by the Particle IDE. | |
#include "PietteTech_DHT/PietteTech_DHT.h" | |
// This #include statement was automatically added by the Particle IDE. | |
#include "RCSwitch/RCSwitch.h" | |
// This #include statement was automatically added by the Particle IDE. | |
#include "OneWire/OneWire.h" | |
/* | |
* Connected sensor | |
* Spark.publish() + PIR motion sensor = awesome | |
* Thanks to Adafruit for the reference and inspiration | |
*/ | |
int inputPin = D4; // choose the input pin (for PIR sensor) | |
int pirState = LOW; // we start, assuming no motion detected | |
int val = 0; // variable for reading the pin status | |
int calibrateTime = 10000; // wait for the thingy to calibrate | |
int lightPin = A0; | |
int lightDPin = D1; | |
int temp = 0; | |
int prevTemp = 0; | |
int light = 0; | |
int motion=0; | |
double tempDS = 0; | |
int ow = D5; // put the onewire bus on D0 | |
OneWire ds(ow); // a 1 - 4.7K resistor to 3.3v is necessary | |
RCSwitch mySwitch = RCSwitch(); | |
int dht = D0; | |
int rh = 0; | |
int ms = 0; | |
#define DHTTYPE DHT11 // Sensor type DHT11/21/22/AM2301/AM2302 | |
#define DHTPIN 0 // Digital pin for communications | |
#define DHT_SAMPLE_INTERVAL 200000 // Sample every two seconds | |
//declaration | |
void dht_wrapper(); // must be declared before the lib initialization | |
// Lib instantiate | |
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper); | |
// globals | |
unsigned int DHTnextSampleTime; // Next time we want to start sample | |
bool bDHTstarted; // flag to indicate we started acquisition | |
int n; | |
int switchOn(String args) | |
{ | |
mySwitch.switchOn(1, args.toInt()); | |
mySwitch.switchOn(1, args.toInt()); | |
mySwitch.switchOn(1, args.toInt()); | |
delay(250); | |
mySwitch.switchOn(1, args.toInt()); | |
mySwitch.switchOn(1, args.toInt()); | |
Spark.publish("rob/switch", "on"+args); | |
} | |
int switchOff(String args) | |
{ | |
mySwitch.switchOff(1, args.toInt()); | |
mySwitch.switchOff(1, args.toInt()); | |
mySwitch.switchOff(1, args.toInt()); | |
delay(250); | |
mySwitch.switchOff(1, args.toInt()); | |
mySwitch.switchOff(1, args.toInt()); | |
Spark.publish("rob/switch", "off"+args); | |
} | |
int getDHTHumidity(String args){ | |
return (int)rh; | |
} | |
int getDHTTemperature(String args){ | |
return (int)temp; | |
} | |
int lightD = 0; | |
void setup() { | |
pinMode(inputPin, INPUT); // declare sensor as input | |
pinMode(lightPin, INPUT); // declare sensor as input | |
pinMode(lightDPin, INPUT); // declare sensor as input | |
// pinMode(tempPin, INPUT); | |
mySwitch.enableTransmit(D3); | |
Spark.function("switchOn", switchOn); | |
Spark.function("switchOff", switchOff); | |
Spark.variable("rh", &rh, INT); | |
Spark.variable("temp", &temp, INT); | |
Spark.variable("lighta", &light, INT); | |
Spark.variable("lightd", &lightD, INT); | |
Spark.variable("tempDS", &tempDS, DOUBLE); | |
Spark.variable("motion", &motion, INT); | |
Spark.function("getTemp", getDHTTemperature); | |
Spark.function("getHum", getDHTHumidity); | |
Serial.begin(9600); | |
DHTnextSampleTime = 0; // Start the first sample immediately | |
Spark.publish("rob/debug", "core setup completed"); | |
} | |
// This wrapper is in charge of calling | |
// mus be defined like this for the lib work | |
void dht_wrapper() { | |
DHT.isrCallback(); | |
} | |
bool first = true; | |
void debugprint() | |
{ | |
Spark.publish("rob/debug", "from timer"); | |
} | |
void loop() | |
{ | |
} | |
void loop2() | |
{ | |
if (first) | |
{ | |
first = false; | |
// Timer timer(10000, debugprint); | |
Spark.publish("rob/debug", "core setup completed - in loop"); | |
} | |
if (millis() > DHTnextSampleTime) { | |
if (calibrated()) { | |
readTheSensor(); | |
reportTheData(); | |
// tempDS = (double)getDSTemp(); | |
} | |
if (!bDHTstarted) { // start the sample | |
Serial.print("\n"); | |
Serial.print(n); | |
Serial.print(": Retrieving information from sensor: "); | |
DHT.acquire(); | |
bDHTstarted = true; | |
} | |
if (!DHT.acquiring()) { // has sample completed? | |
// get DHT status | |
int result = DHT.getStatus(); | |
Serial.print("Read sensor: "); | |
switch (result) { | |
case DHTLIB_OK: | |
Serial.println("OK"); | |
break; | |
case DHTLIB_ERROR_CHECKSUM: | |
Serial.println("Error\n\r\tChecksum error"); | |
break; | |
case DHTLIB_ERROR_ISR_TIMEOUT: | |
Serial.println("Error\n\r\tISR time out error"); | |
break; | |
case DHTLIB_ERROR_RESPONSE_TIMEOUT: | |
Serial.println("Error\n\r\tResponse time out error"); | |
break; | |
case DHTLIB_ERROR_DATA_TIMEOUT: | |
Serial.println("Error\n\r\tData time out error"); | |
break; | |
case DHTLIB_ERROR_ACQUIRING: | |
Serial.println("Error\n\r\tAcquiring"); | |
break; | |
case DHTLIB_ERROR_DELTA: | |
Serial.println("Error\n\r\tDelta time to small"); | |
break; | |
case DHTLIB_ERROR_NOTSTARTED: | |
Serial.println("Error\n\r\tNot started"); | |
break; | |
default: | |
Serial.println("Unknown error"); | |
break; | |
} | |
Serial.print("Humidity (%): "); | |
Serial.println(DHT.getHumidity(), 2); | |
Serial.print("Temperature (oC): "); | |
Serial.println(DHT.getCelsius(), 2); | |
temp = DHT.getCelsius()-2; | |
rh = DHT.getHumidity(); | |
n++; // increment counter | |
bDHTstarted = false; // reset the sample flag so we can take another | |
DHTnextSampleTime = millis() + DHT_SAMPLE_INTERVAL; // set the time for next sample | |
Spark.publish("rob/tempsrednja", String(temp)); | |
Spark.publish("rob/humsrednja", String(rh)); | |
} | |
} | |
//reportTemp(); | |
delay(1000); | |
} | |
bool calibrated() { | |
return millis() - calibrateTime > 0; | |
} | |
void readTheSensor() { | |
val = digitalRead(inputPin); | |
Serial.print(val); | |
light = analogRead(lightPin); | |
int ld = digitalRead(lightDPin); | |
if (ld != lightD) | |
{ | |
Spark.publish("rob/lightd", String(ld)); | |
lightD = ld; | |
} | |
} | |
void reportTemp() | |
{ | |
if (temp != prevTemp) | |
{ | |
double temp2 = (temp/4095)*3.3*100; | |
Spark.publish("rob/temp", String(temp2)); | |
Spark.publish("rob/temp2", String(temp2-8)); | |
prevTemp = temp; | |
} | |
} | |
void reportTheData() { | |
if (val == HIGH) { | |
if (pirState == LOW) { | |
// we have just turned on | |
Serial.println("Motion detected!"); | |
Spark.publish("rob/motion", "started"); | |
// We only want to print on the output change, not state | |
motion = 1; | |
pirState = HIGH; | |
} | |
} else { | |
if (pirState == HIGH) { | |
// we have just turned of | |
Serial.println("Motion ended!"); | |
// We only want to print on the output change, not state | |
Spark.publish("rob/motion", "ended"); | |
pirState = LOW; | |
motion = 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment