I'm facing a strange problem: I'm using a D18B20 plus a DHT11 with my Spark Core, sending data to my local EmonCMS. Everything works fine...but I can't figure out how to show at least one decimal point for values coming from DHT22, while temperature from D18B20 is correctly formatted.
I don't understand if the problem comes from my sketch on Spark Core, from MySQL database or from some hidden options in EmonCMS.
Anyone can help me? i've been struggling around for a week...
My code:
// This #include statement was automatically added by the Particle IDE.
#include "PietteTech_DHT/PietteTech_DHT.h"// This #include statement was automatically added by the Particle IDE.
#include "spark-dallas-temperature/spark-dallas-temperature.h"// This #include statement was automatically added by the Particle IDE.
#include "OneWire/OneWire.h"#include "application.h"
#include "HttpClient/HttpClient.h"#define DHTTYPE DHT11 // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN D3 // Digital pin for communications
#define DHT_SAMPLE_INTERVAL 2000 // Sample every two seconds
//declaration
void dht_wrapper(); // must be declared before the lib initialization// Lib instantiate
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);/**
Declaring the variables.
*/
#define TOKEN "7327a3859afda46ba2eb1d326b7b1d1e1251922f"
#define ONE_WIRE_BUS D2
double temperature = 0.0;
int light = 0;
float h = 0.0;
float t = 0.0;bool bDHTstarted; // flag to indicate we started acquisition
unsigned int nextTime = 0; // Next time to contact the server
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensor(&oneWire);
HttpClient http;// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
{ "Content-Type", "application/json" },
{ "X-Auth-Token" , TOKEN },
{ "Accept" , "*/*"},
{ NULL, NULL } // NOTE: Always terminate headers will NULL
};http_request_t request;
http_response_t response;void setup() {
Spark.variable("temp", &temperature, DOUBLE);
Spark.variable("light", &light, INT);
//Spark.variable("humidity", &h, DOUBLE);sensor.begin();
sensor.setResolution(12);
//dht.begin();
Serial.begin(9600);
}void dht_wrapper() {
DHT.isrCallback();
}void loop() {
if (nextTime < millis()) {if (!bDHTstarted) { // start the sample
Serial.println("Retrieving information from sensor: ");
DHT.acquire();
bDHTstarted = true;
}
if (!DHT.acquiring()) { // has sample completed?
// get DHT status
int result = DHT.getStatus();Serial.print("Read sensor: ");
switch (result) {
case DHTLIB_OK:
Serial.println("OK");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("Error\n\r\tChecksum error");
break;
case DHTLIB_ERROR_ISR_TIMEOUT:
Serial.println("Error\n\r\tISR time out error");
break;
case DHTLIB_ERROR_RESPONSE_TIMEOUT:
Serial.println("Error\n\r\tResponse time out error");
break;
case DHTLIB_ERROR_DATA_TIMEOUT:
Serial.println("Error\n\r\tData time out error");
break;
case DHTLIB_ERROR_ACQUIRING:
Serial.println("Error\n\r\tAcquiring");
break;
case DHTLIB_ERROR_DELTA:
Serial.println("Error\n\r\tDelta time to small");
break;
case DHTLIB_ERROR_NOTSTARTED:
Serial.println("Error\n\r\tNot started");
break;
default:
Serial.println("Unknown error");
break;
}
Serial.print("Humidity (%): ");
h = DHT.getHumidity();
Serial.println(h, 2);Serial.print("Temperature (oC): ");
t = DHT.getCelsius();
Serial.println(t, 2);Serial.print("Temperature (oF): ");
Serial.println(DHT.getFahrenheit(), 2);Serial.print("Temperature (K): ");
Serial.println(DHT.getKelvin(), 2);Serial.print("Dew Point (oC): ");
Serial.println(DHT.getDewPoint());Serial.print("Dew Point Slow (oC): ");
Serial.println(DHT.getDewPointSlow());bDHTstarted = false; // reset the sample flag so we can take another
/////}sensor.requestTemperatures();
temperature = sensor.getTempCByIndex(0);
light = analogRead(A0);request.hostname = "xxxxxxxxxxxx.noip.me";
request.port = 80;
request.path = "/emoncms/input/post.json?apikey=04ee49a2498f7eb7b1aab7a54a27257a&node=1&json={temp:" + String(temperature) + ",tempDHT11:" + String(t) + ",light:" + String(light) + ",humidity:" + String(h) + "}";http.post(request, response, headers);
nextTime = millis() + 30000;
}
}
}
Re: Floating point not shown
Assuming the Serial.prints are reporting correctly, have you tried using "String(t, 2)" and "String(h, 2)" in your request string.
Paul
Re: Floating point not shown
You are right about Serial.print.
Just had a try with String(t,1)...no joy!
...
request.path = "/emoncms/input/post.json?apikey=04ee49a2498f7eb7b1aab7a54a27257a&node=1&json={temp:" + String(temperature) + ",tempDHT11:" + String(t,1) + ",light:" + String(light) + ",humidity:" + String(h,1) + "}";
...
(where can I find infos about code formatting on this forum?)
Re: Floating point not shown
Can you add something like Serialln.print(request.path) to see the populated request string over serial?
I wouldn't know where to point you regards the code tips as we don't really send floats from a sketch that often, you could look at some NanodeRF sketches or search Arduino Ethernet shield, maybe.
I checked the arduino site for the correct String format https://www.arduino.cc/en/Reference/StringConstructor
Paul
Re: Floating point not shown
Serial is ok: I set 2 decimal values for Temp, one for Humidity, and that's it
Retrieving information from sensor:
Read sensor: OK
Humidity (%): 36.0
Temperature (oC): 21.00
Temperature (oF): 69.80
Temperature (K): 294.15
Dew Point (oC): 5.35
Dew Point Slow (oC): 5.38
/emoncms/input/post.json?apikey=04ee49a2498f7eb7b1aab7a54a27257a&node=1&json={temp:21.500000,tempDHT11:21.00,light:735,humidity:36.0}
Retrieving information from sensor:
Read sensor: OK
Humidity (%): 36.0
Temperature (oC): 21.00
Temperature (oF): 69.80
Temperature (K): 294.15
Dew Point (oC): 5.35
Dew Point Slow (oC): 5.38
/emoncms/input/post.json?apikey=04ee49a2498f7eb7b1aab7a54a27257a&node=1&json={temp:21.562500,tempDHT11:21.00,light:928,humidity:36.0}
Retrieving information from sensor:
Read sensor: OK
Humidity (%): 36.0
Temperature (oC): 21.00
Temperature (oF): 69.80
Temperature (K): 294.15
Dew Point (oC): 5.35
Dew Point Slow (oC): 5.38
/emoncms/input/post.json?apikey=04ee49a2498f7eb7b1aab7a54a27257a&node=1&json={temp:21.500000,tempDHT11:21.00,light:1023,humidity:36.0}
Also JSON string is ok...but:
hey, "...The DHT11 does not provide decimal parts. It outputs zero's. ...",
here the explanation: http://forum.arduino.cc/index.php?topic=57058.0
So...end of story!
thanks anyway!