Go Down

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

DancesWithWords

#15
Nov 28, 2014, 05:43 pm Last Edit: Nov 28, 2014, 05:51 pm by DancesWithWords
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

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

robtillaart

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.



Rob Tillaart

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

robtillaart

Can you try this simplified loop?
Code: [Select]
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);
}
[/]
Rob Tillaart

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

DancesWithWords

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

robtillaart


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

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

DancesWithWords

Code is uploaded and live: faintfuzzies.ca:8080

DancesWithWords

#22
Nov 28, 2014, 07:48 pm Last Edit: Nov 28, 2014, 07:52 pm by DancesWithWords
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<br />
analog input 1 is 340<br />
analog input 2 is 316<br />
analog input 3 is 300<br />
analog input 4 is 286<br />
analog input 5 is 297<br />
</html>
t 3 is 303<br />
analog input 4 is 284<br />
analog input 5 is 294<br />
</html>
E HHTTP/1.1 200 OK
Content-Type: text/html
Connection: close
Refresh: 5

<!DOCTYPE HTML><Title>Office Enviroment Server</Title><html><h1>00:28:38</h1>Humidity=23%<BR/>Temperature=24 C<BR/>HTTP/1.1 200 OK
Content-Type: text/html
Connection: close
RefreHTTP/1.1 200 OK
Content-Type: text/html
Connection: close
Refresh: 5

<!DOCTYPE HTML><Title>Office Enviroment Server</Title><html><h1>00:28:48</h1>Humidity=23%<BR/>Temperature=24 C<BR/>Dewpoint=2 C<BR/><BR/>analog input 0 is 298<br />
HTTP/1.1 200 OK
Content-Type: text/html
Connection: close
Refresh: 5

<!DOCTYPE HTML><Title>Office Enviroment Server</Title><html>288<br />
</html>
fresh: 5

<!DOCTYPE HTML><Title>Office Enviroment Server</Title><html><h1>00:28:25</h1>Humidity=23%<BR/>Temperature=24 C<BR/>Dewpoint=2 C<BR/><BR/>analog input 0 is 299<br />
analog input 1 is 339<br />
analog input 2 is 315<br />
analog input 3 is 303<br />
analog input 4 is 287<br />
analog input 5 is 297<br />
</html>
><html><h1>00:28:06</h1>Humidity=23%<BR/>Temperature=24 C<BR/>Dewpoint=2 C<BR/><BR/>analog input 0 is 305<br />
analog input 1 is 343<br />
analog input 2 is 316<br />
analog input 3 is 301<br />
analog input 4 is 283<br />
analog input 5 is 296<br />
</html>
/>
analog input 2 is 317<br />
analog input 3 is 299<br />
anaHTTP/1.1 200 OK
Content-Type: text/html
Connection: close
Refresh: 5

robtillaart

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

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

robtillaart

#24
Nov 28, 2014, 08:31 pm Last Edit: Nov 28, 2014, 08:34 pm by robtillaart
- http://www.w3schools.com/tags/tag_br.asp -

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

In XHTML, the <br> tag must be properly closed, like this: <br />.

  myClient.print("<BR/>"); usesnospaceintag
....
Rob Tillaart

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

robtillaart

compacter code sometimes helps
Code: [Select]
void sendTempToNetwork(EthernetClient myClient)
{
  myClient.print("Humidity=");
  myClient.print(dht.readHumidity(), 0);
  myClient.print("%<BR />Temperature=");
  myClient.print(dht.readTemperature(), 0);
  myClient.print(" C<BR/>Dewpoint=");
  myClient.print(dewPoint(dht.readTemperature(), dht.readHumidity() ), 0);
  myClient.println(" C<BR /><BR />"); // <<<<< note the println ...
}
Rob Tillaart

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

DancesWithWords

Running with this code snippet right now.

====
DWW

DancesWithWords

FYI

This is the code I'm currently running.


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, 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)
{
  myClient.print("Humidity=");
  myClient.print(dht.readHumidity(), 0);
  myClient.print("%<BR />Temperature=");
  myClient.print(dht.readTemperature(), 0);
  myClient.print(" C<BR/>Dewpoint=");
  myClient.print(dewPoint(dht.readTemperature(), dht.readHumidity() ), 0);
  myClient.println(" C<BR /><BR />"); // <<<<< note the println ...
}
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");

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

    // give the web browser time to receive the data
    delay(10);
    // 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

What kind of ethernet do you have?
standard ethernet shield?

it looks like if <ethernetshield> is getting the data too fast, buffer overruns or so.  Can't explain it yet.

Rob Tillaart

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

DancesWithWords

Using a Ardiuno Ethernet Shield w/ MicroSD slot Rev. 3

Go Up