Temp via Json

I am trying to get the temperature from my DHT22 across Json so a webage can display the values. I am having an issue where the values are intermittent and show arduinoEthernetComCallback('{"DHT_Temp": -1766.20,"DHT_Hmuid": -999.00}') one second then
arduinoEthernetComCallback('{"DHT_Temp": 25.6,"DHT_Hmuid": 40.5}').

I cant figure out why it's not staying on the correct values only. I'm sure its a code issue, I'm still new and learning!

Thanks for any help :slight_smile:

#include <SPI.h>
#include <Ethernet.h>
#include <dht.h>

dht DHT;

#define DHT22_PIN 7

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10,50,50,227);
char callback[27] = "arduinoEthernetComCallback";
 
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup()
{
  // start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();

}
 
void loop()
{
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line

boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();

// 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
DHT.read22(DHT22_PIN);

client.println("HTTP/1.1 200 OK");
client.println("Content-Type: application/json");
client.println();

// output the value of each analog input pin as a json-p object
float temperatureDegF = Fahrenheit(DHT.temperature);
float humidity = DHT.humidity;
client.print(callback);
client.print("('{");
client.print("\"DHT_Temp");
client.print("\": ");
client.print(temperatureDegF);
client.print(",");
client.print("\"DHT_Hmuid");
client.print("\": ");
client.print(humidity);
client.println("}')");

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

} 


//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
        return 1.8 * celsius + 32;
}

Seems that DHT.temperature is giving you an unexpected value.

What DHT library are you using? Have you tested basic reading of your sensor with this library to confirm it produces consistent data when you use it like this?

I'm using the DHT library from Arduino Playground - DHTLib

When using their DHT22_test Serial prints the correct values all the time.

When I use client.print() it looks like I am getting one good value per scan then the next is bad and then good again.

The original code I created this from had Analog pins and that read correctly.

I feel like I don't have something in the correct order and the values get reset at the end of a scan then set again once it loops.

You aren't checking the returned status from dht.read(). You need to do that to know whether the read succeeded. In general, aim to call the DHT library in exactly the same way in your project sketch as the way you used it when you were testing the sensor - and that should be consistent with the DHT library examples.

Are you referring to this for checking the returned status?

switch (chk)
    {
    case DHTLIB_OK:
        Serial.print("OK,\t");
        del -= 10;
        break;
    case DHTLIB_ERROR_CHECKSUM:
        Serial.print("Checksum error,\t");
        break;
    case DHTLIB_ERROR_TIMEOUT:
        Serial.print("Time out error,\t");
        del += 10;
        break;
    default:
        Serial.print("Unknown error,\t");
        break;
    }

This is the whole file from the DHT22_Test

//
//    FILE: dht22_test.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.01
// PURPOSE: DHT library test sketch for DHT22 && Arduino
//     URL:
//
// Released to the public domain
//

#include <dht.h>

dht DHT;

#define DHT22_PIN 5

void setup()
{
    Serial.begin(115200);
    Serial.println("DHT TEST PROGRAM ");
    Serial.print("LIBRARY VERSION: ");
    Serial.println(DHT_LIB_VERSION);
    Serial.println();
    Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)\tTime (us)");
}

void loop()
{
    // READ DATA
    Serial.print("DHT22, \t");

    uint32_t start = micros();
    int chk = DHT.read22(DHT22_PIN);
    uint32_t stop = micros();

    switch (chk)
    {
    case DHTLIB_OK:
        Serial.print("OK,\t");
        break;
    case DHTLIB_ERROR_CHECKSUM:
        Serial.print("Checksum error,\t");
        break;
    case DHTLIB_ERROR_TIMEOUT:
        Serial.print("Time out error,\t");
        break;
    default:
        Serial.print("Unknown error,\t");
        break;
    }
    // DISPLAY DATA
    Serial.print(DHT.humidity, 1);
    Serial.print(",\t");
    Serial.print(DHT.temperature, 1);
    Serial.print(",\t");
    Serial.print(stop - start);
    Serial.println();

    delay(2000);
}
//
// END OF FILE
//

dancinj:
Are you referring to this for checking the returned status?

That will print out a description of the status. If you want to do that, that's fine. The main thing is that you need to check what the return value from dht.read22() is - the value that is assigned to chk in the DHT22_Test sketch, but which you discard in your one.

Would this be the correct fix?

void loop()
{

  float temperatureDegF;
  float humidity;
  
  uint32_t start = micros();  
  int chk = DHT.read22(DHT22_PIN);
  uint32_t stop = micros();
  
  switch (chk)
    {
    case DHTLIB_OK:
        break;
    }

I'm not exactly sure what line of code is needed to check the return value from dht.read22()