Created
June 23, 2014 09:23
-
-
Save yoggy/919d382b5341b88ae8de 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
// | |
// dht11_dht22_cds_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" | |
DHT dht11(2, DHT11); | |
DHT dht22(3, DHT22); | |
#define CDS_PIN 0 | |
char buf[256]; | |
void setup() { | |
Serial.begin(9600); | |
while(!Serial) {} | |
dht11.begin(); | |
dht22.begin(); | |
} | |
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); | |
} | |
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