Hello,
I am currently experiencing a weird problem.
I have a mega2560 with Ethernet shield and am trying to get a JSON reading of the values.
The weird thing is, it works but only for humidity. Temperature won’t display in JSON. Only in Serial.println().
(My code may not be negative temps proof, not tested yet)
Interesting parts are readDHT22Sensor function and end of loop().
#include <DHT.h>
#include <Ethernet.h>
#include <SPI.h>
DHT dht(7, DHT22);
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //ethernet shield mac address
byte ip[] = { 192, 168, 10, 102 }; // arduino IP in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port
String readString;
char *readDHT22Sensor(int pin){
DHT dht(pin, DHT22);
dht.begin();
char returnString[64];
char temperature[6];
char humidity[6];
dtostrf(dht.readTemperature(), 10, 2, temperature);
dtostrf(dht.readHumidity(), 10, 2, humidity);
sprintf(returnString, "{temperature : %s , humidity : %s}", temperature, humidity);
return returnString;
}
void setup() {
Ethernet.begin(mac, ip, gateway, gateway, subnet);
server.begin();
pinMode(7, INPUT);
dht.begin();
Serial.begin(9600);
Serial.println("go");
}
void loop() {
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}
//if HTTP request has ended
if (c == '\n') {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/json");
client.println();
client.println(readDHT22Sensor(7));
//Serial.println(readDHT22Sensor(7));
delay(1);
//stopping client
client.stop();
if(readString.indexOf("pin52on") >0)
{
Serial.println("52 high");
pinMode(53, OUTPUT);
digitalWrite(53, HIGH);
}
if(readString.indexOf("pin52off") >0)
{
Serial.println("52 low");
pinMode(53, OUTPUT);
digitalWrite(53, LOW);
}
//clearing string for next read
readString="";
}
}
}
}
}
The result :
TBH I don’t really understand why…
Thanks for your help