DHT22 Webserver

robtillaart:
#include <DHT.h>

should be

#include <dht.h>

Tried that, I get the following error.

sketch_nov26b.ino:5:19: fatal error: dht.h: No such file or directory
compilation terminated.

I got sketches to work with the "DHT.h"

FYI

The source of the library is Adafruit. It has been very reliable up to now.

So I tried to rename the ".h" file in the library to "dht.h" restart the Arduino IDE and got the following error.

working2DHT.ino:7:1: error: ‘dht’ does not name a type
working2DHT.ino: In function ‘void sendTempToNetwork(EthernetClient)’:
working2DHT.ino:56:18: error: expected primary-expression before ‘.’ token
working2DHT.ino:58:23: error: expected primary-expression before ‘.’ token
working2DHT.ino:62:23: error: expected primary-expression before ‘.’ token
working2DHT.ino:67:32: error: expected primary-expression before ‘.’ token
working2DHT.ino:67:48: error: expected primary-expression before ‘.’ token

Here is a DHT test sketch that I've just ran and uploaded. Everything is working fine.

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

#include "DHT.h"

#define DHTPIN 2     // 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
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 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

// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
// NOTE: For working with a faster chip, like an Arduino Due or Teensy, you
// might need to increase the threshold for cycle counts considered a 1 or 0.
// You can do this by passing a 3rd parameter for this threshold.  It's a bit
// of fiddling to find the right value, but in general the faster the CPU the
// higher the value.  The default for a 16mhz AVR is a value of 6.  For an
// Arduino Due that runs at 84mhz a value of 30 works.
// Example to initialize DHT sensor for Arduino Due:
//DHT dht(DHTPIN, DHTTYPE, 30);

void setup() {
  Serial.begin(9600); 
  Serial.println("DHTxx test!");
 
  dht.begin();
}

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

  // 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.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit
  float f = dht.readTemperature(true);
  
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index
  // Must send in temp in Fahrenheit!
  float hi = dht.computeHeatIndex(f, h);

  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hi);
  Serial.println(" *F");
}

So I've changed the DHT22 part of the sketch. It now says:

#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, DHTTYPE);

It looks like line 55 is where my next set of errors start from. Do you think I might be right? That lines says:

 }  
void sendTempToNetwork(EthernetClient myClient)
 {

And I think this is producing the first of my error messages.

working2_1DHT.ino: In function ‘void sendTempToNetwork(EthernetClient)’:
working2_1DHT.ino:58:18: error: expected primary-expression before ‘.’ token
working2_1DHT.ino:60:23: error: expected primary-expression before ‘.’ token
working2_1DHT.ino:64:23: error: expected primary-expression before ‘.’ token
working2_1DHT.ino:69:32: error: expected primary-expression before ‘.’ token
working2_1DHT.ino:69:48: error: expected primary-expression before ‘.’ token

So suggestions?

====
DWW

You are combining calls from my DHT library and the Adafruits one and the interfaces are not compatible.

Some simple changes will fix this.
(as I do not have the Adafruit lib nearby I cannot verify)

try replace this function
(and please put some empty lines in your code to split logical blocks visually e.g. between functions)

 void sendTempToNetwork(EthernetClient myClient)  
 {  
    // now humidity / temp sensor  
    // int chk = DHT.read22(DHT22_PIN);  
    myClient.print("Humidity=");  
    myClient.print(DHT.humidity(), 0);  
    myClient.print("%");  
    myClient.print("
");  

    myClient.print("Temperature=");  
    myClient.print(DHT.temperature(), 0);  
    myClient.write(" ");  
    myClient.print("C");  
    myClient.print("
");
  
    myClient.print("Dewpoint=");  
    myClient.print(dewPoint(DHT.temperature(), DHT.humidity() ), 0);  
    myClient.write(" ");  
    myClient.print("C");  
    myClient.print("

");  
 }

myClient.write(" ");

do you know the difference between single quotes and double quotes in C/C++ ?

Tried your suggestion. No joy, got the same errors.

As to your last question about C/C++ quotes. No, I do not know the difference.

Sadly when I first bought my Arduino Uno I thought this programming thing was going to be easy. Now I just feel swamped with all the things I don't know. I'm realizing as I go that I don't know even enough to know where to put spaces, tabs or line spaces. Things I know would make debug for-instance so much easier. I can't even tell the difference between an array and a function etc. Makes this whole Arduino thing very frustrating, but I'm not giving up yet. ;D

Programming is more difficult than you think, but it becomes easier when you think.

It can take several weeks to really get a grip on it. And there will always be stuff that is new or unseen before.

OK back to your code:

myClient.write() only does single characters, indicated by single quotes e.g. 'a'
myClient.print() does also multiple characters, aka string or char array, indicated by double quotes e.g. "hello"

rewrote the function below to fix the single/double quote error.

 void sendTempToNetwork(EthernetClient myClient)  
 {  
    // now humidity / temp sensor  
    // int chk = DHT.read22(DHT22_PIN);  
    myClient.print("Humidity=");  
    myClient.print(DHT.humidity(), 0);  
    myClient.print("%");  
    myClient.print("
");  

    myClient.print("Temperature=");  
    myClient.print(DHT.temperature(), 0);  
    myClient.print(" ");  
    myClient.print("C");  
    myClient.print("
");
  
    myClient.print("Dewpoint=");  
    myClient.print(dewPoint(DHT.temperature(), DHT.humidity() ), 0);  
    myClient.print(" ");  
    myClient.print("C");  
    myClient.print("

");  
 }

Now the compiler should complain less ..
if it does not work, post the code again + output you get.

Gave your updated code a try. No joy. :slightly_frowning_face:

Errors are:
working2_3DHT.ino: In function ‘void sendTempToNetwork(EthernetClient)’:
working2_3DHT.ino:61:23: error: expected primary-expression before ‘.’ token
working2_3DHT.ino:66:23: error: expected primary-expression before ‘.’ token
working2_3DHT.ino:72:32: error: expected primary-expression before ‘.’ token
working2_3DHT.ino:72:51: error: expected primary-expression before ‘.’ token

The code:

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

#define DHTTYPE DHT22
#define DHTPIN 2
DHT dht(DHTPIN, DHTTYPE);
  
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, 203 };                     // 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(80);  
static char output[300] = "";  
void setup() {  
 Serial.begin(9600);  
 //ethernet  
   Ethernet.begin(mac, ip);  
   server.begin();  
   Serial.print("server is at ");  
   Serial.println(Ethernet.localIP());  
}  
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>RBBS Server</Title>"  
          "<html>";  
            
 char footerHTML[] = "</html>" ;  
   
 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(DHT22_PIN);  
    myClient.print("Humidity=");  
    myClient.print(DHT.humidity(), 0);  
    myClient.print("%");  
    myClient.print("
");  

    myClient.print("Temperature=");  
    myClient.print(DHT.temperature(), 0);  
    myClient.print(" ");  
    myClient.print("C");  
    myClient.print("
");
  
    myClient.print("Dewpoint=");  
    myClient.print(dewPoint(DHT.temperature(), DHT.humidity() ), 0);  
    myClient.print(" ");  
    myClient.print("C");  
    myClient.print("

");  
 } 
 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("
");         
    }  
 }  
/******************************************************************************* 
 * 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;  
}

I downloaded the ADAFRUIT dht library and fixed your program so it compiles. Read the DHT.h for the EXACT interface of the library and library calls to understand how the library must be called. If you copy code from another library (for DHT there exists at least 6 or so) you must adapt the calling code to the interface used.

#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, 203 };                     // 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(80);  
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>RBBS 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("
");  
  myClient.print("Temperature=");  
  myClient.print(dht.readTemperature(), 0);  
  myClient.write(" ");  
  myClient.print("C");  
  myClient.print("
");  
  myClient.print("Dewpoint=");  
  myClient.print(dewPoint(dht.readTemperature(), dht.readHumidity() ), 0);  
  myClient.write(" ");  
  myClient.print("C");  
  myClient.print("

");  
}  
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("
");         
  }  
}  
/******************************************************************************* 
 * 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;  
}

Thanks for making that work. I appreciate all the help I've received from the Arduino forums support team. ;D

So I'm going to make this my next job to review this code so I full understand your changes and how this all works. Not much good to me if I don't put the work in to learn something from this.

Working temperature server; faintfuzzies.ca:8080

==
DancesWithWords

Well I spoke too soon. If you let faintfuzzies.ca:8080 run a couple of minutes it starts misbehaving. Take a look and let me know what you think. I've tested it on Chrome and Firefox the rests are the same. The first few iterations are fine then things go wonky. Finally this webserver just freezes up and requires a reboot.

====
DWW

The code now compiles so it is syntactically correct.
Apparently the semantics and behaviour is still not right.

It looks to me as if the Arduino does not wait for a new connection but just spits snippets of data.

Can you try this simplified loop?

void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) 
  {
    Serial.println("new client");

    // 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;

    // give the web browser time to receive the data
    delay(10);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
  delay(2000);
}
[/]

That breaks a few things:

working2_4DHT.ino: In function ‘void loop()’:
working2_4DHT.ino:122:5: error: break statement not within loop or switch
working2_4DHT.ino: At global scope:
working2_4DHT.ino:132:1: error: expected declaration before ‘}’ token

remove the line with break. (I cannot test the code only compile it, sorry :slight_smile:

Code is uploaded and live: faintfuzzies.ca:8080

Still having issues, there was an improvement it lasted 28 minutes before crapping out. This is the junk I get just before it crashes.

analog input 0 is 300

analog input 1 is 340

analog input 2 is 316

analog input 3 is 300

analog input 4 is 286

analog input 5 is 297

t 3 is 303

analog input 4 is 284

analog input 5 is 294

E HHTTP/1.1 200 OK Content-Type: text/html Connection: close Refresh: 5 Office Enviroment Server

00:28:38

Humidity=23%

Temperature=24 C
HTTP/1.1 200 OK
Content-Type: text/html
Connection: close
RefreHTTP/1.1 200 OK
Content-Type: text/html
Connection: close
Refresh: 5

Office Enviroment Server

00:28:48

Humidity=23%

Temperature=24 C
Dewpoint=2 C

analog input 0 is 298

HTTP/1.1 200 OK
Content-Type: text/html
Connection: close
Refresh: 5

Office Enviroment Server288 fresh: 5 Office Enviroment Server

00:28:25

Humidity=23%

Temperature=24 C
Dewpoint=2 C

analog input 0 is 299

analog input 1 is 339

analog input 2 is 315

analog input 3 is 303

analog input 4 is 287

analog input 5 is 297

>

00:28:06

Humidity=23% Temperature=24 C Dewpoint=2 C

analog input 0 is 305

analog input 1 is 343

analog input 2 is 316

analog input 3 is 301

analog input 4 is 283

analog input 5 is 296

/> analog input 2 is 317

analog input 3 is 299

anaHTTP/1.1 200 OK
Content-Type: text/html
Connection: close
Refresh: 5

do you still have myClient.write(" "); in your code?

Differences Between HTML and XHTML
In HTML, the
tag has no end tag.

In XHTML, the
tag must be properly closed, like this:
.

myClient.print("
"); usesnospaceintag
....