Go Down

Topic: DHT22 Webserver (Read 8224 times) previous topic - next topic

DancesWithWords

I'm trying to get the following code to work for me:

Code: [Select]
#include <SPI.h> 
#include <Wire.h> 
#include <Ethernet.h> 
//#include <UIPEthernet.h> 
#include <DHT.h> 
#include <stdio.h> 
dht DHT; 
#define DHT22_PIN 2 
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("<BR/>"); 
    myClient.print("Temperature="); 
    myClient.print(DHT.temperature, 0); 
    myClient.write(" "); 
    myClient.print("C"); 
    myClient.print("<BR/>"); 
    myClient.print("Dewpoint="); 
    myClient.print(dewPoint(DHT.temperature,DHT.humidity), 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; 




However I'm getting a number of errors:

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


If you can help I'd appreciate it.  Thanks

====
DWW

Code was taken from:

http://goo.gl/lvxR3Y

Peter_n

#1
Nov 27, 2014, 09:15 am Last Edit: Nov 27, 2014, 09:17 am by Peter_n
It is the same error, something wrong with DHT and dht.something.
Start with the first error, it means that the complier doesn't know what "dht" is.
Perhaps the DHT.h file does not exist, or is in the wrong location, or you forgot to install the DHT library.

Are you sure that the string will become one long string when split over multiple lines that way ? Or was that in JavaScript, or maybe I'm confused with another language.

DancesWithWords

Right now I can confirm that DHT.h is installed and working as I've tested it with other sketches.  Why it kicks up an error for dht I'm not sure.

The other error are produce on separate lines, which to my inexperience eyes suggest they are all separate errors on different lines.

How to fix all this I've no clue hence my post.

Thanks for taking the time to reply.


=====
DWW

Peter_n

#3
Nov 27, 2014, 05:52 pm Last Edit: Nov 27, 2014, 05:55 pm by Peter_n
The other error are also dht errors.
I can tell you for sure, the compiler doesn't know what 'dht' is. Perhaps you need to include <dht.h> instead of <DHT.h> ?

About the long string over multiple lines: the way you have it is perfectly okay. I was confusing it with the #define over multiple lines.

robtillaart

#include <DHT.h> 

should be

#include <dht.h> 
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

DancesWithWords

#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"

DancesWithWords

FYI

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

DancesWithWords

#7
Nov 27, 2014, 09:07 pm Last Edit: Nov 27, 2014, 09:10 pm by DancesWithWords
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

DancesWithWords

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

Code: [Select]
// 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");
}

DancesWithWords

#9
Nov 27, 2014, 09:36 pm Last Edit: Nov 27, 2014, 09:55 pm by DancesWithWords
So I've changed the DHT22 part of the sketch.  It now says:

Code: [Select]
#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:

Code: [Select]

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

 

robtillaart

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)
Code: [Select]

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

    myClient.print("Temperature="); 
    myClient.print(DHT.temperature(), 0); 
    myClient.write(" "); 
    myClient.print("C"); 
    myClient.print("<BR/>");
 
    myClient.print("Dewpoint="); 
    myClient.print(dewPoint(DHT.temperature(), DHT.humidity() ), 0); 
    myClient.write(" "); 
    myClient.print("C"); 
    myClient.print("<BR/><BR/>"); 
 } 


myClient.write(" "); 

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

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

DancesWithWords

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

robtillaart

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.
Code: [Select]


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

    myClient.print("Temperature="); 
    myClient.print(DHT.temperature(), 0); 
    myClient.print(" "); 
    myClient.print("C"); 
    myClient.print("<BR/>");
 
    myClient.print("Dewpoint="); 
    myClient.print(dewPoint(DHT.temperature(), DHT.humidity() ), 0); 
    myClient.print(" "); 
    myClient.print("C"); 
    myClient.print("<BR/><BR/>"); 
 }

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

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

DancesWithWords

#13
Nov 28, 2014, 03:49 pm Last Edit: Nov 28, 2014, 03:51 pm by DancesWithWords
Gave your updated code a try.  No joy.  :smiley-sad:

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:
Code: [Select]
#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("<BR/>"); 

    myClient.print("Temperature="); 
    myClient.print(DHT.temperature(), 0); 
    myClient.print(" "); 
    myClient.print("C"); 
    myClient.print("<BR/>");
 
    myClient.print("Dewpoint="); 
    myClient.print(dewPoint(DHT.temperature(), DHT.humidity() ), 0); 
    myClient.print(" "); 
    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; 
}   

robtillaart

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.

Code: [Select]

#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("<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; 
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up