Created
July 10, 2014 11:56
-
-
Save yoggy/f472abb5f9d23f2dce43 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
Serial.println(buf); | |
// | |
// dht11_dht22_cds_pir_test.ino - a sample sketch that outputs the sensor data in JSON.. | |
// | |
// This program uses the following libraries.// | |
// https://github.com/adafruit/DHT-sensor-library | |
// | |
#include "DHT.h" | |
#define CDS_PIN 0 | |
#define PIR_INT 0 // Uno:D2, Leonard,Micro:D3 | |
DHT dht11(4, DHT11); | |
DHT dht22(5, DHT22); | |
char buf[256]; | |
int pir_val = LOW; | |
void setup() { | |
Serial.begin(9600); | |
while(!Serial) {} | |
dht11.begin(); | |
dht22.begin(); | |
attachInterrupt(PIR_INT, pir_rising_func, RISING); | |
} | |
void loop() { | |
create_dht_sensor_message(buf, 256, "home_dht11", dht11.readTemperature(), dht11.readHumidity()); | |
Serial.println(buf); | |
delay(1000); | |
create_dht_sensor_message(buf, 256, "home_dht22", dht22.readTemperature(), dht22.readHumidity()); | |
Serial.println(buf); | |
delay(1000); | |
int cds_val = analogRead(CDS_PIN); | |
create_cds_sensor_message(buf, 256, "home_cds", cds_val); | |
Serial.println(buf); | |
delay(3000); | |
// check PIR | |
create_pir_sensor_message(buf, 256, "home_pir", pir_val); | |
Serial.println(buf); | |
pir_val = LOW; | |
} | |
void pir_rising_func() { | |
pir_val = HIGH; | |
} | |
void create_pir_sensor_message(char *buf, int buf_len, char *name, int pir_val) | |
{ | |
snprintf( | |
buf, buf_len, | |
"{\"name\":\"%s\", \"pir\": %d, \"tick_count\":%d}", | |
name, pir_val, millis()); | |
} | |
void create_cds_sensor_message(char *buf, int buf_len, char *name, int cds_val) | |
{ | |
snprintf( | |
buf, buf_len, | |
"{\"name\":\"%s\", \"cds\": %d, \"tick_count\":%d}", | |
name, cds_val, millis()); | |
} | |
void create_dht_sensor_message(char *buf, int buf_len, char *name, float t, float h) | |
{ | |
char th_str[8], tl_str[8]; | |
char hh_str[8], hl_str[8]; | |
create_float_string(t, th_str, 8, tl_str, 8); | |
create_float_string(h, hh_str, 8, hl_str, 8); | |
snprintf( | |
buf, buf_len, | |
"{\"name\":\"%s\", \"temperature\": %s.%s, \"humidity\":%s.%s, \"tick_count\":%d}", | |
name, th_str, tl_str, hh_str, hl_str, millis()); | |
} | |
void create_float_string(float val, char *h_str, int h_str_len, char *l_str, int l_str_len) | |
{ | |
int val_h = (int)val; | |
int val_l = (int)(val * 100) % 100; | |
snprintf(h_str, h_str_len, "%d", val_h); | |
snprintf(l_str, l_str_len, "%02d", val_l); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment