I am new to particle, but i have few panstam running with DHT22.
I have wired DHT22 to Particle on D1 and set up the following code (below), but I am getting only whole rounded number readings. Why is that? I know from my other project that the DHT22 can deliver also 2 decimals. Can you please help me with the required changes in the code?
My code so far:
// This #include statement was automatically added by the Particle IDE.
#include <PietteTech_DHT.h>
// DHT parameters
#define DHTTYPE DHT22 // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN D1 // Digital pin for communications
#define DHT_SAMPLE_INTERVAL 2000 // Sample every two seconds
// Variables
int temperature;
int humidity;
int blue = D7;
// Declaration
void dht_wrapper();
// Lib Initialize
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
UDP Udp;
// globals
unsigned int DHTnextSampleTime; // Next time we want to start sample
unsigned int localPort = 8888; //
IPAddress remoteIP(192,168,8,194);
int port_temp = 5555;
int port_hum = 6666;
bool bDHTstarted; // flag to indicate we started acquisition
int n; // counter
void setup() {
pinMode(blue, OUTPUT);
DHTnextSampleTime = 0;
Udp.begin(localPort);
Serial.begin(9600);
Serial.println(WiFi.localIP());
}
void dht_wrapper() {
DHT.isrCallback();
}
void loop()
{
// Check if we need to start the next sample
if (millis() > DHTnextSampleTime) {
if (!bDHTstarted) { // start the sample
DHT.acquire();
bDHTstarted = true;
}
if (!DHT.acquiring()) { // has sample completed?
// get DHT status
int result = DHT.getStatus();
int humidity = DHT.getHumidity();
int tempf = DHT.getFahrenheit();
int tempc = DHT.getCelsius();
int tempk = DHT.getKelvin();
int dp = DHT.getDewPoint();
int dpslow = DHT.getDewPointSlow();
//Turn the built in LED on to indicate publishing
digitalWrite(blue, HIGH);
//* Publish readings - uncomment/comment the values you'd like to publish
Particle.publish("Humidity", String(humidity) + "%");
//Particle.publish("Temperature", String(tempf) + " °F");
Particle.publish("Temperature", String(tempc) + " °C");
//Particle.publish("Temperature", String(tempk) + " °K");
//Particle.publish("Dew Point", String(dp) + " %");
//Particle.publish("Dew Point", String(dpslow) + " %");
// send the UDP Packet
Udp.beginPacket(remoteIP, port_temp);
Udp.write(String(tempc));
Udp.endPacket();
Udp.beginPacket(remoteIP, port_hum);
Udp.write(String(humidity));
Udp.endPacket();
//Turn the built in LED off indicate publishing finished
delay(250);
digitalWrite(blue, LOW);
n++; // increment counter
bDHTstarted = false; // reset the sample flag so we can take another
DHTnextSampleTime = millis() + DHT_SAMPLE_INTERVAL; // set the time for next sample
}
}
}
@peekay123 the issue was not in the code but is in the cloud flashing process… somehow the code is not being transferred from the cloud down to earth (particle). Particle blinks, that it is receiving something, restarts, but continues with the old firmware.
So I compiled the Fw, downloaded it and flashed with CLI, and now it works…
Thank you for the hint with the Floating. I am not that good in coding
Code :
// This #include statement was automatically added by the Particle IDE.
#include <PietteTech_DHT.h>
// DHT parameters
#define DHTTYPE DHT22 // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN D1 // Digital pin for communications
#define DHT_SAMPLE_INTERVAL 2000 // Sample every two seconds
// Variables
int blue = D7;
// Declaration
void dht_wrapper();
// Lib Initialize
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
UDP Udp;
// globals
unsigned int DHTnextSampleTime; // Next time we want to start sample
unsigned int localPort = 8888; //
IPAddress remoteIP(192,168,8,194);
int port_temp = 5000;
int port_hum = 5001;
bool bDHTstarted; // flag to indicate we started acquisition
int n; // counter
void setup() {
pinMode(blue, OUTPUT);
DHTnextSampleTime = 0;
Udp.begin(localPort);
Serial.begin(9600);
Serial.println(WiFi.localIP());
}
void dht_wrapper() {
DHT.isrCallback();
}
void loop()
{
// Check if we need to start the next sample
if (millis() > DHTnextSampleTime) {
if (!bDHTstarted) { // start the sample
DHT.acquire();
bDHTstarted = true;
}
if (!DHT.acquiring()) { // has sample completed?
// get DHT status
float result = DHT.getStatus();
float humidity = DHT.getHumidity();
float tempf = DHT.getFahrenheit();
float tempc = DHT.readTemperature();
float tempk = DHT.getKelvin();
float dp = DHT.getDewPoint();
float dpslow = DHT.getDewPointSlow();
//Turn the built in LED on to indicate publishing
digitalWrite(blue, HIGH);
//* Publish readings - uncomment/comment the values you'd like to publish
Particle.publish("Humidity", String(humidity) + "%");
//Particle.publish("Temperature", String(tempf) + " °F");
Particle.publish("Temperature", String(tempc) + " °C");
//Particle.publish("Temperature", String(tempk) + " °K");
//Particle.publish("Dew Point", String(dp) + " %");
//Particle.publish("Dew Point", String(dpslow) + " %");
// send the UDP Packet
Udp.beginPacket(remoteIP, port_temp);
Udp.write(String(tempc));
Udp.endPacket();
Udp.beginPacket(remoteIP, port_hum);
Udp.write(String(humidity));
Udp.endPacket();
//Turn the built in LED off indicate publishing finished
delay(250);
digitalWrite(blue, LOW);
n++; // increment counter
bDHTstarted = false; // reset the sample flag so we can take another
DHTnextSampleTime = millis() + DHT_SAMPLE_INTERVAL; // set the time for next sample
}
}
}