Arduino, DHT22, Webserver

Posted by geeko.boy on November 30th, 2014

This was so far the most difficult Arduino project I’ve had to do so far. The goal was simple display some sensor data from a DHT22 temperature and humidity sensor. The code presented a number of problems. You can read the full run down how we got the working code here. The original code for this project was taken from Peter Oakes at this site.


You will find the code here:

#include <SPI.h>  
#include <Wire.h>  
#include <Ethernet.h>  
//#include <UIPEthernet.h>  
#include <DHT.h>  
#include <stdio.h>

#define DHTTYPE DHT22
#define DHTPIN 2

DHT dht(DHTPIN, DHT22);

float combined_temp_C;  
float combined_temp_F;  //create a new variable  
byte TempHi;              // Variable hold data high byte  
byte TempLo;              // Variable hold data low byte  
boolean P_N;              // Bit flag for Positive and Negative  
boolean P_N112;  
boolean P_N212;  
unsigned int Decimal;     // Variable hold decimal value  
char * strTempC = NULL;  
char * strTempF = NULL;  
byte mac[] = { 
  0x90, 0xA2, 0xDA, 0x00, 0x4E, 0x10 };  // MAC address 84.85.88.16.0.36  
byte ip[]  = { 
  192, 168, 1, 20 };                     // ip-address, please change to fit your network  
byte mydns[] = { 
  192, 168, 1, 1 };              
byte gateway[] = { 
  192, 168, 1, 1 };               
byte subnet[] = { 
  255,255,255,0 };   
EthernetServer server(8080);  
static char output[300] = "";  



char headerHTML[] = "HTTP/1.1 200 OK\r\n"  
"Content-Type: text/html\r\n"  
"Connection: close\r\n"  
"Refresh: 5\r\n"  
"\r\n"  
"<!DOCTYPE HTML>"  
"<Title>Office Environmental Server</Title>"  
"<html>";  

char footerHTML[] = "</html>" ;  


void setup() 
{  
  Serial.begin(9600);  
  //ethernet  
  Ethernet.begin(mac, ip);  
  server.begin();  
  Serial.print("server is at ");  
  Serial.println(Ethernet.localIP());  
}  

char * TimeElapsed() 
{ // Was Home Page  
  long t = millis() / 1000;  
  word h = t / 3600;  
  byte m = (t / 60) % 60;  
  byte s = t % 60;  
  sprintf(output, "<h1>%d%d:%d%d:%d%d</h1>" , h/10, h%10, m/10, m%10, s/10, s%10);  
  return output;  
} 

void sendTempToNetwork(EthernetClient myClient)  
{  
  // now humidity / temp sensor  
  // int chk = DHT.read22(DHTPIN);  
  myClient.print("Humidity=");  
  myClient.print(dht.readHumidity(), 0);  
  myClient.print("%");  
  myClient.print("<BR/>");  
  myClient.print("Temperature=");  
  myClient.print(dht.readTemperature(), 0);  
  myClient.write(" ");  
  myClient.print("C");  
  myClient.print("<BR/>");  
  myClient.print("Dewpoint=");  
  myClient.print(dewPoint(dht.readTemperature(), dht.readHumidity() ), 0);  
  myClient.write(" ");  
  myClient.print("C");  
  myClient.print("<BR/><BR/>");  
}  
void sendAnalogToNetwork(EthernetClient myClient)  
{  
  // output the value of each analog input pin for good measure  
  for (int analogChannel = 0; analogChannel < 6; analogChannel++) {  
    int sensorReading = analogRead(analogChannel);  
    myClient.print("analog input ");  
    myClient.print(analogChannel);  
    myClient.print(" is ");  
    myClient.print(sensorReading);  
    myClient.println("<br />");         
  }  
}  
/******************************************************************************* 
 * Main Loop 
 *******************************************************************************/
void loop()   
{  
  // listen for incoming clients  
  EthernetClient client = server.available();  
  if (client) {  
    Serial.println("new client");  
    // an http request ends with a blank line  
    boolean currentLineIsBlank = true;  
    while (client.connected()) {  
      if (client.available()) {  
        char c = client.read();  
        Serial.write(c);  
        // if you've gotten to the end of the line (received a newline  
        // character) and the line is blank, the http request has ended,  
        // so you can send a reply  
        if (c == '\n' && currentLineIsBlank) {  
          // send a standard http response header  
          client.print(headerHTML);  
          // now send the stuff we want  
          client.print(TimeElapsed()); // your old code here  
          // do some more stuff  
          sendTempToNetwork(client);  
          sendAnalogToNetwork(client);  
          // were done sending so now send the footer to close the page  
          client.println(footerHTML);  
          break;  
        }  
        if (c == '\n') {  
          // you're starting a new line  
          currentLineIsBlank = true;  
        }   
        else if (c != '\r') {  
          // you've gotten a character on the current line  
          currentLineIsBlank = false;  
        }  
      }  
    }  
    // give the web browser time to receive the data  
    delay(1);  
    // close the connection:  
    client.stop();  
    Serial.println("client disconnected");  
  }  
  delay(2000);  
}  
void Cal_Temp()  
{  
  if (TempHi&0x80)     // If bit7 of the TempHi is HIGH then the temperature is negative  
    P_N = 0;  
  else       // Else the temperature is positive  
  P_N = 1;  
  TempHi = TempHi & 0x7F;   // Remove sign  
  TempLo = TempLo & 0xF0;   // Filter out last nibble  
  TempLo = TempLo >>4; // Shift right 4 times  
  Decimal = TempLo;  
  Decimal = Decimal * 625;  // Each bit = 0.0625 degree C  
  combined_temp_C= TempHi + TempLo*.0625;  
  combined_temp_F = combined_temp_C*1.8+32;  
  sprintf(strTempC, "%f", combined_temp_C);  
  sprintf(strTempF, "%f", combined_temp_F);  
}  
////Celsius to Kelvin conversion  
//double Kelvin(double celsius)  
//{  
//        return celsius + 273.15;  
//}  
// dewPoint function NOAA  
// reference: http://wahiduddin.net/calc/density_algorithms.htm   
double dewPoint(double celsius, double humidity)  
{  
  double A0= 373.15/(273.15 + celsius);  
  double SUM = -7.90298 * (A0-1);  
  SUM += 5.02808 * log10(A0);  
  SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;  
  SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;  
  SUM += log10(1013.246);  
  double VP = pow(10, SUM-3) * humidity;  
  double T = log(VP/0.61078);   // temp var  
  return (241.88 * T) / (17.558-T);  
}  
// delta max = 0.6544 wrt dewPoint()  
// 5x faster than dewPoint()  
// reference: http://en.wikipedia.org/wiki/Dew_point  
double dewPointFast(double celsius, double humidity)  
{  
  double a = 17.271;  
  double b = 237.7;  
  double temp = (a * celsius) / (b + celsius) + log(humidity/100);  
  double Td = (b * temp) / (a - temp);  
  return Td;  
}

Posted in Arduino Projects | No Comments »

Arduino, DHT22, and a LCD: Colour to Temperature

Posted by geeko.boy on November 30th, 2014

In this project we do an adaptation of rotating colour sketch and instead we use range of temperatures and assign each range a colour. So my idea is to do 15 Celsius or less would be blue, 15 to 25 degrees would be assigned the colour green and above 25 Celsius would be red.
I’d like to thank the folks at the Arduino and adafruits forums for all there help giving these sketches up and running.

There is the code:

#include <LiquidCrystal.h> //lcd library
#include "DHT.h"  //sensor library
#define DHTPIN 2   //  pin 32 (TQFP-32)
#define DHTTYPE DHT22   // type of sensor DHT 22  (AM2302)
#define REDLITE 3
#define GREENLITE 5
#define BLUELITE 6
byte degreeChar[8] = 
{ B01100,
  B10010,
  B10010,
  B01100,
  B00000,
  B00000,
  B00000,
  B00000, };
DHT dht(DHTPIN, DHTTYPE);

// LCD
// LCD RS=7, EN=8, DS4=9, DS5=10, DS6=11, DS7=12
LiquidCrystal lcd( 7, 8, 9, 10, 11, 12 );

// you can change the overall brightness by range 0 -> 255
int brightness = 255;



void setup() {
  lcd.begin(20, 4);
  analogReference(INTERNAL);
  pinMode(0, OUTPUT);
  digitalWrite(0, HIGH); // power on
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH); // backlight on
  dht.begin(); //  start sensor 
    lcd.setCursor(0,0);


  pinMode(REDLITE, OUTPUT);
  pinMode(GREENLITE, OUTPUT);
  pinMode(BLUELITE, OUTPUT);
 
  brightness = 100;  
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(t) || isnan(h)) {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("SENSOR");    
    lcd.setCursor(0,1);
    lcd.print("ERROR");
  } 
  else {
    lcd.clear();
    lcd.setCursor(0,1);
    lcd.print("TEMPERATURE ");
    lcd.print(t);
    lcd.createChar(0, degreeChar);
    lcd.setCursor(18,1);
    lcd.write(byte(0));
    lcd.print("C");
    lcd.setCursor(0,2);
    lcd.print("HUMIDITY    ");
    lcd.print(h);
    lcd.print(" %");
 }
if (t < 15)
{
    setBacklight(0, 0, 255);  // set to blue
}
else if (t < 25)
{
    setBacklight(0, 255, 0);  // set to green
}
else 
{
    setBacklight(255, 0, 0);  // set to red
}
{
  digitalWrite(13, HIGH);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(13, LOW);    // sets the LED off
  delay(1000);                  // waits for a second
}
}
  
void setBacklight(uint8_t r, uint8_t g, uint8_t b) {
  // normalize the red LED - its brighter than the rest!
  r = map(r, 0, 255, 0, 100);
  g = map(g, 0, 255, 0, 150);
 
  r = map(r, 0, 255, 0, brightness);
  g = map(g, 0, 255, 0, brightness);
  b = map(b, 0, 255, 0, brightness);
 
  // common anode so invert!
  r = map(r, 0, 255, 255, 0);
  g = map(g, 0, 255, 255, 0);
  b = map(b, 0, 255, 255, 0);
  Serial.print("R = "); Serial.print(r, DEC);
  Serial.print(" G = "); Serial.print(g, DEC);
  Serial.print(" B = "); Serial.println(b, DEC);
  analogWrite(REDLITE, r);
  analogWrite(GREENLITE, g);
  analogWrite(BLUELITE, b);
}

Posted in Uncategorized | No Comments »

Arduino, DHT22, and a LCD: Rotate That Colour

Posted by geeko.boy on November 18th, 2014

Welcome to my latest Arduino project. This project is based on the DHT22 temperature and humidity sensor. I’m using a 20×4 RGB LCD screen to output the data to. I got the idea that since the LCD can do RGB why not make it rotate through these colours while dislaying the data from the sensor. All parts used here are or where available from . the full sketch is below and if you have any comments leave them in the comments section below.

// include the library code:
#include <LiquidCrystal.h>
#include <Wire.h>
#include "DHT.h"  //sensor library
#define DHTPIN 2   //  pin 32 (TQFP-32)
#define DHTTYPE DHT22   // type of sensor DHT 22  (AM2302) 

#define REDLITE 3
#define GREENLITE 5
#define BLUELITE 6

byte degreeChar[8] = 
{ B01100,
  B10010,
  B10010,
  B01100,
  B00000,
  B00000,
  B00000,
  B00000, };
DHT dht(DHTPIN, DHTTYPE);


 
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
 
// you can change the overall brightness by range 0 -> 255
int brightness = 255;
 
void setup() {
  lcd.begin(20, 4);
  analogReference(INTERNAL);
  pinMode(0, OUTPUT);
  digitalWrite(0, HIGH); // power on
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH); // backlight on
  dht.begin(); //  start sensor 
  lcd.setCursor(0,0);
    
    
  pinMode(REDLITE, OUTPUT);
  pinMode(GREENLITE, OUTPUT);
  pinMode(BLUELITE, OUTPUT); 
    
  brightness = 100;
}


void loop() {
  
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(t) || isnan(h)) {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("SENSOR");    
    lcd.setCursor(0,1);
    lcd.print("ERROR");
  } 
  else {
    lcd.clear();
    lcd.setCursor(0,1);
    lcd.print("TEMPERATURE ");
    lcd.print(t);
    lcd.createChar(0, degreeChar);
    lcd.setCursor(18,1);
    lcd.write(byte(0));
    lcd.print("C");
    lcd.setCursor(0,2);
    lcd.print("HUMIDTY     ");
    lcd.print(h);
    lcd.print(" %");
}  
  for (int i = 0; i < 255; i++) {
    setBacklight(i, 0, 255-i);
    delay(15);
  }
  for (int i = 0; i < 255; i++) {
    setBacklight(255-i, i, 0);
    delay(15);
  }
  for (int i = 0; i < 255; i++) {
    setBacklight(0, 255-i, i);
    delay(15);
  }
}
 
 
 
void setBacklight(uint8_t r, uint8_t g, uint8_t b) {
  // normalize the red LED - its brighter than the rest!
  r = map(r, 0, 255, 0, 100);
  g = map(g, 0, 255, 0, 150);
 
  r = map(r, 0, 255, 0, brightness);
  g = map(g, 0, 255, 0, brightness);
  b = map(b, 0, 255, 0, brightness);
 
  // common anode so invert!
  r = map(r, 0, 255, 255, 0);
  g = map(g, 0, 255, 255, 0);
  b = map(b, 0, 255, 255, 0);
  Serial.print(" R = "); Serial.print(r, DEC);
  Serial.print(" G = "); Serial.print(g, DEC);
  Serial.print(" B = "); Serial.println(b, DEC);
  analogWrite(REDLITE, r);
  analogWrite(GREENLITE, g);
  analogWrite(BLUELITE, b);
}

Posted in Arduino Projects | No Comments »

plant health monitor

Posted by DWW on June 30th, 2014

http://m.instructables.com/id/plant-health-monitor/

Posted in Uncategorized | No Comments »

How to See Pluto at Opposition as New Horizons Crosses the One Year Out Mark

Posted by DWW on June 30th, 2014

http://www.universetoday.com/112856/how-to-see-pluto-at-opposition-as-new-horizons-crosses-the-one-year-out-mark/

Posted in Uncategorized | No Comments »