Humidity and Temperature sensor

Made by vagisha singh

I am using a DHT22 humidity and temperature sensor to publish temperature and humidity for a room.

0
// This #include statement was automatically added by the Particle IDE.
//#include "https://dweet/for/humidity?temp=" + sf + "&humidity=" + sh;"

// This #include statement was automatically added by the Particle IDE.
#include "HttpClient.h"
#include "Adafruit_DHT.h"

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#define DHTPIN D2     // what pin we're 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 +5V
// 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);

/**
* Declaring the variables.
*/
unsigned int nextTime = 0;    // Next time to contact the server
HttpClient http;

// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
    //  { "Content-Type", "application/json" },
    //  { "Accept" , "application/json" },
    { "Accept" , "*/*"},
    { NULL, NULL } // NOTE: Always terminate headers will NULL
};

http_request_t request;
http_response_t response;

void setup() {
	//Serial.begin(9600);
	Particle.publish("DEBUG","DHT22 test!");

	dht.begin();
}

void loop() {
// Wait a few seconds between measurements.
	delay(1000);

// 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
	float t = dht.getTempCelcius();
// Read temperature as Farenheit
	float f = dht.getTempFarenheit();

// Check if any reads failed and exit early (to try again).
	if (isnan(h) || isnan(t) || isnan(f)) {
		Particle.publish("DEBUG","Failed to read from DHT sensor!");
		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());

	//cast floats to string
	String sf(f, 2);
	String sh(h, 2);

	//pblish tests
	//Spark.publish("faranheight",f,60,PRIVATE);
	Particle.publish("TEMP",sf);
	Particle.publish("HUMIDITY",sh);

	//post to dweet
	request.hostname = "dweet.io";
    request.port = 80;
    request.path = "/dweet/for/humiditysensor?temp=" + sf + "&humidity=" + sh;

	//json body doesn't work
	//request.body = "{\"humidity\":\"43%\",\"temp\":\"45f\"}"; //"{\"key\":\"value\"}";

	Particle.publish("DEBUG",request.body);

    // Get request
    http.get(request, response, headers);
    //Spark.publish("DEBUG","Application Response status: ");
    //Particle.publish("DEBUG",response.status);

    //Spark.publish("DEBUG","Application HTTP Response Body: ");
    Particle.publish("DEBUG",response.body);
}
Click to Expand
0
// This #include statement was automatically added by the Particle IDE.
//#include "https://dweet/for/humidity?temp=" + sf + "&humidity=" + sh;"

// This #include statement was automatically added by the Particle IDE.
#include "HttpClient.h"
#include "Adafruit_DHT.h"

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#define DHTPIN D2     // what pin we're 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 +5V
// 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);

/**
* Declaring the variables.
*/
unsigned int nextTime = 0;    // Next time to contact the server
HttpClient http;

// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
    //  { "Content-Type", "application/json" },
    //  { "Accept" , "application/json" },
    { "Accept" , "*/*"},
    { NULL, NULL } // NOTE: Always terminate headers will NULL
};

http_request_t request;
http_response_t response;

int led = D0; // LED is connected to D0
int pushButton = D4; // Push button is connected to D2

void setup() {
	//Serial.begin(9600);
	Particle.publish("DEBUG","DHT22 test!");

	dht.begin();
}

void loop() {
// Wait a few seconds between measurements.
	delay(1000);

// 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
	float t = dht.getTempCelcius();
// Read temperature as Farenheit
	float f = dht.getTempFarenheit();

// Check if any reads failed and exit early (to try again).
	if (isnan(h) || isnan(t) || isnan(f)) {
		Particle.publish("DEBUG","Failed to read from DHT sensor!");
		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());

	//cast floats to string
	String sf(f, 2);
	String sh(h, 2);

	//pblish tests
	//Spark.publish("faranheight",f,60,PRIVATE);
	Particle.publish("TEMP",sf);
	Particle.publish("HUMIDITY",sh);

	//post to dweet
	request.hostname = "dweet.io";
    request.port = 80;
    request.path = "/dweet/for/humiditysensor?temp=" + sf + "&humidity=" + sh;

	//json body doesn't work
	//request.body = "{\"humidity\":\"43%\",\"temp\":\"45f\"}"; //"{\"key\":\"value\"}";

	Particle.publish("DEBUG",request.body);

    // Get request
    http.get(request, response, headers);
    //Spark.publish("DEBUG","Application Response status: ");
    //Particle.publish("DEBUG",response.status);

    //Spark.publish("DEBUG","Application HTTP Response Body: ");
    Particle.publish("DEBUG",response.body);
}




// This routine runs only once upon reset
void setup()
{
 pinMode(led, OUTPUT); // Initialize D0 pin as output
 pinMode(pushButton, INPUT_PULLUP);
 // Initialize D2 pin as input with an internal pull-up resistor
}

// This routine loops forever
void loop()
{
 int pushButtonState;

 pushButtonState = digitalRead(pushButton);

 if(pushButtonState == LOW)
 { // If we push down on the push button
   digitalWrite(led, HIGH);  // Turn ON the LED
 }
 else
 {
   digitalWrite(led, LOW);   // Turn OFF the LED
 }

}
Click to Expand
0

The code above is taken and edited from https://www.hackster.io/shaiss/temperature-humidity-monitor-with-dweet-freeboard-3f6fba  and edited to make the led work everytime the sensor gets a reading. There is a button added to turn the sensor on/off

0

The particle uses dweet to connect to freeboard which provides for a dashboard. 

0
0
0
Video showing the breadboard and the readings
(2017)
x
Share this Project

This project is only accessible by signed in users. Be considerate and think twice before sharing.



Focused on
About

I am using a DHT22 humidity and temperature sensor to publish temperature and humidity for a room.

Created

February 2nd, 2017