I was asked to post the code that I am using to read a DHT22 and post to ThingSpeak.
My programming ability is at a Kindergarten level, please be kind.
I use ‘ThingSpeak’ because it has some very straightforward triggers, which I use to eventually get a text message on my phone if the readings stop for a certain time, or if the temperature gets outside of a specific range.
(Edit) This code is currently running on a Photon.
There is a ‘control’ pin that I am using to set the Photon to ‘deep sleep’ between 15 minute readings for battery operation and power savings. If you don’t have a way to bypass deep sleep, you can not reprogram the Photon because it won’t hear you.
// This #include statement was automatically added by the Spark IDE.
#include "PietteTech_DHT/PietteTech_DHT.h" //This is for DHT-22 Temperature and Humidity sensor.
// ThingSpeak is a simple but very powerful data collection service, with comprehensive
// 'triggers' that can be used to display and automate data responses.
String writeAPIKey = "abcdefghijklmnopo123456"; //The ThingSpeak feed ID
TCPClient client; // SPARK built in TCP/IP client.
float tempC = 0;
float tempF = 0;
float humidity = 0; // An example variable to send to ThingSpeak
float feelsLike; // The 'feels like' temperature adjusted for humidity.
int ledD = D7; // Use the built-in LED as a rudimentary status indicator
int controlPin = D6; // Ground for battery operation, high for continuous and reprogramming.
boolean control; // contains choice of power source, 0=battery, 1=line voltage
long sampleTime = 15; //Sample time in minutes
unsigned long lastMeasureTimer = 0; // How much time in milliseconds has passed since last measurement.
#define DHTPIN 4 // what pin we're connected
#define DHTTYPE DHT22 // Sensor type DHT11/21/22/AM2301/AM2302
void dht_wrapper(); // must be declared before the lib initialization
PietteTech_DHT dht(DHTPIN, DHTTYPE, dht_wrapper);
void setup()
{
pinMode(controlPin, INPUT); // Set the power source pin
pinMode(ledD, OUTPUT); // Set the status LED as an output so we can use it as a status indicator
Spark.variable("var1", &tempF, DOUBLE); //Store tempF as var1 on the Spark website
Spark.variable("var2", &humidity, DOUBLE);
lastMeasureTimer = millis() + (sampleTime*60*1000) + 1000; //Advance the timer to allow for an immediate initial measurement.
delay (3000); //Allow things to settle down with the Spark Core connection
}
// This wrapper is in charge of calling
// mus be defined like this for the lib work
void dht_wrapper() {
dht.isrCallback();
}
void loop()
{
control = digitalRead(controlPin);
if (control == HIGH ){
if (millis()-lastMeasureTimer>sampleTime*60*1000) {
postToThingspeak();
lastMeasureTimer = millis();
}
}
else if (control == LOW) {
postToThingspeak();
Spark.sleep(SLEEP_MODE_DEEP, sampleTime*60);
}
}
void ThingSpeak(String tsData) {
if (client.connect("api.thingspeak.com", 80)) // Try to connect to ThingSpeak
{
client.print("POST /update HTTP/1.1\n"); // Required info
client.print("Host: api.thingspeak.com\n"); // Required info
client.print("Connection: close\n"); // Required info
client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n"); // Required info
client.print("Content-Type: application/x-www-form-urlencoded\n"); // Required info
client.print("Content-Length: "); // Required info
client.print(tsData.length()); // Required info
client.print("\n\n"); // Required info
client.println(tsData); // Required info
delay(100); // Allow time for data to be processed before closing connection
ledStatus(3, 50); // Flash LED 3 times quickly to indicate success!
}
else
{
// Connection failed
ledStatus(3, 2000); // Flash LED 3 times slowly to indicate failure to connect.
}
client.flush(); //required TCP housekeeping
client.stop(); //required TCP housekeeping
}
void ledStatus(int x, int t)
{
for (int j = 0; j <= x-1; j++)
{
digitalWrite(ledD, HIGH); // LED ON
delay(t); //Wait some time
digitalWrite(ledD, LOW); //LED OFF
delay(t); //Wait some time
}
}
void postToThingspeak(){
dht.acquire();
while (dht.acquiring())
;
humidity = dht.getHumidity();
tempC = dht.getCelsius();
tempF = dht.getFahrenheit();
if (tempF > 80)
feelsLike = -42.379 +
2.04901523 * tempF +
10.14333127 * humidity +
-0.22475541 * tempF*humidity +
-0.00683783 * (tempF * tempF) +
-0.05481717 * (humidity * humidity) +
0.00122874 * (tempF * tempF) * humidity +
0.00085282 * tempF* (humidity * humidity) +
-0.00000199 * (tempF * tempF) * (humidity * humidity);
else feelsLike = tempF;
if (tempC != 0 ) ThingSpeak("field1="+String(tempF,1)+"&field2="+String(humidity,0)+"&field3="+String(feelsLike,1)); //Set a text value to send to ThingSpeak as data, and call the function tp send the data.
Spark.publish("temperature", String(tempF,1));
}
// End of code