Receiving a temperature reading.
The Sketch
To receive temperature data we created a simple sketch that reads the data from a DHT11 temp/humidity sensor that is connected on digital pin 2 of the Uno. The sketch imports a DHT library which allows for easy connectivity with the sensor. The values that are read from the sensor are then printed to the serial port so they can be read by the Raspberry Pi.
#include "DHT.h"//import library
#define DHTPIN 2 //sets pin for dht sensor
#define DHTTYPE DHT11 //sets type
DHT dht(DHTPIN, DHTTYPE);
int led_1 = 13; //pin for 1 LED
//int incomingByte = 0; //variable to hold incoming Serial data
void setup() {
// initialize the digital pin as an output:
pinMode(led_1, OUTPUT);
digitalWrite(led_1, HIGH); //turn on #1 indicator LED
digitalWrite(led_1, LOW); //turn off #1 LED
dht.begin();//begin dht sensor
Serial.begin(9600); //set baud rate for serial
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(t) || isnan(h))
{
Serial.println("Failed to read from DHT");
}
else
{
Serial.print(h);
Serial.print(",");
Serial.print(t);
delay(2000);
}
if(t > 23)
{
digitalWrite(led_1, HIGH); // set the LED on
delay(500); // wait for a second
}
else
{
digitalWrite(led_1, LOW); // set the LED on
delay(500); // wait for a second
}
}
In the above example there is an led on pin 13 that is set to turn on once the temperature goes above 23 degrees.

No comments:
Post a Comment