Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 77b0275a74
Fetching contributors…

Cannot retrieve contributors at this time

262 lines (205 sloc) 5.112 kb
/* Temperature-Humidity-Dewpoint Monitor
This sketch gathers temperature and humidity data via a DHT22 sensor,
and also calculates dew point based on those measurements.
*/
#include <DHT22.h>
#include <SoftwareSerial.h>
#include <stdlib.h>
#define DHT22_ERROR_VALUE -99.5
#define DHT22_PIN 4
// Setup a DHT22 instance
DHT22 myDHT22(DHT22_PIN);
#define SerialIn 2
#define SerialOut 3
#define 4Delay 900
byte thou=0;
byte hund=0;
byte tens=0;
byte ones=0;
SoftwareSerial mySerialPort(SerialIn, SerialOut);
void setup(void)
{
// start serial port
Serial.begin(9600);
Serial.println("DHT22 Library Demo");
pinMode(SerialOut, OUTPUT);
pinMode(SerialIn, INPUT);
mySerialPort.begin(9600);
mySerialPort.print("v");
mySerialPort.print("xxxx");
delay(4Delay);
mySerialPort.print("----");
delay(4Delay);
mySerialPort.print("8888");
delay(4Delay);
mySerialPort.print("xxxx");
delay(4Delay);
}
void loop(void)
{
float tempC;
float tempF;
float humid;
float dewPoint;
DHT22_ERROR_t errorCode;
delay(2000);
errorCode = myDHT22.readData();
switch(errorCode)
{
case DHT_ERROR_NONE:
Serial.print("Temperature: ");
tempC = myDHT22.getTemperatureC();
Serial.print(tempC);
Serial.print("C Humidity: ");
dispData((int)tempC, 'C');
tempF = (tempC*1.8)+32;
delay(4Delay);
dispData((int) tempF, 'F');
delay(4Delay);
humid = myDHT22.getHumidity();
Serial.print(humid);
Serial.println("%");
dispData((int)humid, 'h');
delay(4Delay);
dewPoint = calculateDewpoint(tempC, humid);
dispData((int) dewPoint, 'd');
Serial.print(dewPoint);
Serial.println("d");
delay(4Delay);
break;
case DHT_ERROR_CHECKSUM:
break;
case DHT_BUS_HUNG:
break;
case DHT_ERROR_NOT_PRESENT:
break;
case DHT_ERROR_ACK_TOO_LONG:
break;
case DHT_ERROR_SYNC_TIMEOUT:
break;
case DHT_ERROR_DATA_TIMEOUT:
break;
case DHT_ERROR_TOOQUICK:
break;
}
}
float calculateDewpoint(float T, float RH)
{
// approximate dewpoint using the formula from wikipedia's article on dewpoint
float dp = 0.0;
float gTRH = 0.0;
float a = 17.271;
float b = 237.7;
gTRH = ((a*T)/(b+T))+log(RH/100);
dp = (b*gTRH)/(a-gTRH);
return dp;
}
void dispData(int i, char c)
{
//char c = ' ';
ones=0;
tens=0;
hund=0;
thou=0;
if(c == 'k' || c=='K' || c=='m' || c=='l' || c == 'v' || c=='V' || c=='W' || c=='Z' || c=='w' || c=='z')
{
mySerialPort.print("bAdx");
return;
}
if((i<-999) || (i>9999))
{
mySerialPort.print("ERRx");
return;
}
int sign = i;
i = abs(i);
//float_toggle = 0;
ones = i%10;
if(i>=10) tens = ((i%100)-ones)/10;
if(i>=100) hund = ((i%1000)-(tens*10)-ones)/100;
if(i>=1000) thou = ((i%10000)-(hund*100)-(tens*10)-ones)/1000;
if (thou>0)
{
mySerialPort.print(thou);
mySerialPort.print(hund);
mySerialPort.print(tens);
mySerialPort.print(ones);
Serial.print((int)thou);
Serial.print((int)hund);
Serial.print((int)tens);
Serial.print((int)ones);
return;
}
if ((hund>0)&& (sign<0))
{
mySerialPort.print("-");
mySerialPort.print(hund);
mySerialPort.print(tens);
mySerialPort.print(ones);
Serial.print((int)hund);
Serial.print((int)tens);
Serial.print((int)ones);
Serial.print(c);
return;
}
if (hund>0)
{
mySerialPort.print(hund);
mySerialPort.print(tens);
mySerialPort.print(ones);
mySerialPort.print(c);
Serial.print((int)hund);
Serial.print((int)tens);
Serial.print((int)ones);
Serial.print(c);
return;
}
if ((tens>0) && (sign<0))
{
mySerialPort.print("-");
mySerialPort.print(tens);
mySerialPort.print(ones);
mySerialPort.print(c);
Serial.print("-");
Serial.print((int)tens);
Serial.print((int)ones);
Serial.print(c);
return;
}
if (tens>0)
{
mySerialPort.print(tens);
mySerialPort.print(ones);
mySerialPort.print("x");
mySerialPort.print(c);
Serial.print((int)tens);
Serial.print((int)ones);
Serial.print("x");
Serial.print(c);
return;
}
if((ones>0) && (sign<0))
{
mySerialPort.print("-");
mySerialPort.print(ones);
mySerialPort.print("x");
mySerialPort.print(c);
Serial.print("-");
Serial.print((int)ones);
Serial.print("x");
Serial.print(c);
return;
}
if(ones>0)
{
mySerialPort.print("x");
mySerialPort.print(ones);
mySerialPort.print("x");
mySerialPort.print(c);
Serial.print("x");
Serial.print((int)ones);
Serial.print("x");
Serial.print(c);
return;
}
}
Jump to Line
Something went wrong with that request. Please try again.