I have had a few Particle Photons that have been happily pulling DHT-type sensors (a DHT11, DHT22, and an AM2301) for a couple years even. But I wanted to make a change. Previously they output results to a simple webpage and posted them to a cloud service. I changed the webpage to be XML so it could more easily be parsed. Nothing dramatic. No errors on verification either. But… the Photons were on VERY old firmware (0.6.2 I think) so I let them go to the new default 1.2.1.
Long story short… they proceeded to have constant panic attacks. SOS signal with 14 red flashes (Heap Error). I had no idea what caused it so I went back to the exact code that was working… and it panicked as well.
So here I am, having rebuilt my code almost from scratch, copying the VERY simple examples for the PietteTech_DHT library and a simple web server. If I leave off the “DHT.acquireAndWait();” the Photon runs and does not panic, but I NEVER get a good result from any sensor. They all display sensor values of -7 for Celsius, Dew Point, and Humidity on the XML… If someone can take a look at my code (simplified version below) I would appreciate any pointers on what I must have done wrong. The hardware has not changed at all. The PIN (6) is correct… 10K Ohm resistor in place… I must be missing something REALLY simple for it not to be working.
As a note, I try to avoid delay in most places, so you can see I have checks against the millis() instead, so it is giving the sensor enough time between checks.
// This sample made possible due to the PietteTech_DHT library and examples from Particle.
// It is NOT meant to be final code but merely a "should be working" proof of concept.
// This #include statement was automatically added by the Particle IDE.
#include <PietteTech_DHT.h>
const int Photon = 1;
#define DHTPIN 6 // what pin we're connected to
// Sensor types DHT11/21/22/AM2301/AM2302
#define DHTTYPE AM2301 // Photon 1
//#define DHTTYPE DHT11 // Photon 3
//#define DHTTYPE DHT22 // Photon 4
PietteTech_DHT dht(DHTPIN, DHTTYPE);
TCPClient WebClient;
TCPClient WeatherClient;
TCPServer WebServer = TCPServer( 80 );
const int MaxSensors = 6;
float SensorReadingsF[ MaxSensors ];
String SensorNames[ MaxSensors ] = { "tempin", "temp", "dewin", "dew", "humin", "hum" }; // Fill this array with the keywords
unsigned long SensorCheck = 5000; // Set the initial time before a sensor reading occurs. Default is 5 seconds (5000 milliseconds)
unsigned int SensorDelay = 60000; // Set the delay between temperature readings. Default is 60 seconds (60000 milliseconds). Must be more than 2500
String StartTime; // String showing the time when the Photon was started
String LastTime; // String showing the last time the Photon updated the sensors
void setup() {
dht.begin();
delay( 1000 );
WebServer.begin();
delay( 1000 );
// Set all the sensor readings to -1000, my placeholder to indicate no real value assigned
for( int x = 0; x < MaxSensors; x++ ) {
SensorReadingsF[ x ] = -1000;
}
StartTime = Time.timeStr();
}
void loop() {
// Has enough time passed? Then run a sensor check.
if( millis() > SensorCheck ) {
CheckSensors();
SensorCheck = millis() + SensorDelay;
}
// If something connects to the Photon, provide the XML, otherwise make it available for connection
if( WebClient.connected() && WebClient.available() ) {
serveXML();
} else {
WebClient = WebServer.available();
}
}
// CheckSensors is meant to go through each sensor attached to the Photon
void CheckSensors() {
//int result = dht.acquireAndWait(); // DOES NOT WORK... Causes a panic state (Heap Error)
SensorReadingsF[ 0 ] = dht.getCelsius();
SensorReadingsF[ 2 ] = dht.getDewPoint();
SensorReadingsF[ 4 ] = dht.getHumidity();
LastTime = Time.timeStr();
}
void serveXML() {
WebClient.println( "HTTP/1.1 200 OK" );
WebClient.println( "Content-Type: text/xml" );
WebClient.println();
WebClient.println( "<?xml version='1.0'?>" );
WebClient.println( "<status software='07/17/2019' hardware='Photon'>" );
WebClient.print( "<device>" );
WebClient.print( Photon );
WebClient.println( "</device>" );
WebClient.print( "<dateUTC>" );
WebClient.print( Time.timeStr() );
WebClient.println( "</dateUTC>" );
WebClient.print( "<startdateUTC>" );
WebClient.print( StartTime );
WebClient.println( "</startdateUTC>" );
WebClient.print( "<lastdateUTC>" );
WebClient.print( LastTime );
WebClient.println( "</lastdateUTC>" );
WebClient.println( "<sensors>" );
for( int x = 0; x < MaxSensors; x++ ) {
if( SensorReadingsF[ x ] != -1000 ) {
WebClient.println( "<sensor>" );
WebClient.print( "<name>" );
WebClient.print( SensorNames[ x ] );
WebClient.print( "</name>" );
WebClient.print( "<value>" );
WebClient.print( SensorReadingsF[ x ] );
WebClient.println( "</value>" );
WebClient.println( "</sensor>" );
}
}
WebClient.println( "</sensors>" );
WebClient.println( "</status>" );
WebClient.flush();
WebClient.stop();
delay( 100 );
}
XML Output is:
<status software="07/18/2019" hardware="Photon">
<device>1</device>
<dateUTC>Thu Jul 18 22:49:29 2019</dateUTC>
<startdateUTC>Thu Jul 18 22:46:05 2019</startdateUTC>
<lastdateUTC>Thu Jul 18 22:49:05 2019</lastdateUTC>
<sensors>
<sensor>
<name>tempin</name>
<value>-7.00</value>
</sensor>
<sensor>
<name>dewin</name>
<value>-7.00</value>
</sensor>
<sensor>
<name>humin</name>
<value>-7.00</value>
</sensor>
</sensors>
</status>