I have been trying to work this out, however I am fairly new at this so I have not had much luck. I am able to get values to post to thingspeak, but they are 4,294,967,296 for both temp and rh. Any help would be appreciated.
// This #include statement was automatically added by the Particle IDE.
#include "thingspeak/thingspeak.h"
#include "PietteTech_DHT/PietteTech_DHT.h"
#define DHTTYPE AM2301 // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN D3 // Digital pin for communications
ThingSpeakLibrary::ThingSpeak thingspeak ("XXXXXXXXXX");
//declaration
void dht_wrapper(); // must be declared before the lib initialization
// Lib instantiate
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
/*
*****************************************************************************************
**** Visit https://www.thingspeak.com to sign up for a free account and create
**** a channel. The video tutorial http://community.thingspeak.com/tutorials/thingspeak-channels/
**** has more information. You need to change this to your channel, and your write API key
**** IF YOU SHARE YOUR CODE WITH OTHERS, MAKE SURE YOU REMOVE YOUR WRITE API KEY!!
*****************************************************************************************/
void setup() {
}
void dht_wrapper() {
DHT.isrCallback();
}
void loop() {
int result = DHT.acquireAndWait();
double temp = (DHT.getFahrenheit(), 2);
double rh = (DHT.getHumidity(), 2);
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
// pieces of information in a channel. Here, we write to field 1.
bool valSet = thingspeak.recordValue(1, String(temp, DEC));
bool valSet1 = thingspeak.recordValue(2, String(rh, DEC));
bool valsSent= thingspeak.sendValues();
bool valsSent1= thingspeak.sendValues();
delay(20000); // ThingSpeak will only accept updates every 15 seconds.
}
Glad you got it working! As a side note, There are two ThingSpeak libraries available for the Particle Core and Photon. I recently created a new ThingSpeak library that supports reading and writing to ThingSpeak, and has a number of examples.
You can get it from within Particle Build, but info is available here
Just adding a follow up that we’ve contributed a library for Photon and Core to to write or read data to or from ThingSpeak, which has MATLAB analytics and visualization.
I am hoping to get this to work with a DHT11. I tried to use your code but am not getting any readings on my ThingSpeak channel. Any help would be appreciated.
// This #include statement was automatically added by the Particle IDE. #include “thingspeak/thingspeak.h”
// This #include statement was automatically added by the Particle IDE. #include “PietteTech_DHT/PietteTech_DHT.h”
#define DHTTYPE DHT11 // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN D5 // Digital pin for communications
//declaration
void dht_wrapper(); // must be declared before the lib initialization
// Lib instantiate
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
String writeAPIKey = "WRITE API KEY (removed)";
String channelID = "Removed";
// TCP socket initialize
TCPClient client;
float temp = 0;
float rh = 0;
void setup() {
}
void dht_wrapper() {
DHT.isrCallback();
}
void loop() {
DHT.acquire();
delay (2000);
temp = DHT.getFahrenheit();
rh = DHT.getHumidity();
ThingSpeakUpdate("field1="+String(temp)+"&field2="+String(rh));
delay(15000);
}
void ThingSpeakUpdate(String tsData)
{
Serial.println("Date string: " + tsData);
Serial.println("...Connecting to Thingspeak"); // Connecting and sending data to Thingspeak if(client.connect("api.thingspeak.com", 80)) { Serial.println("...Connection succesful, updating datastreams"); client.print("POST /update HTTP/1.1\n"); client.print("Host: api.thingspeak.com\n"); client.print("Connection: close\n"); client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n"); client.print("Content-Type: application/x-www-form-urlencoded\n"); client.print("Content-Length: "); client.print(tsData.length()); client.print("\n\n"); client.println(tsData); //the ""ln" is important here. // This delay is pivitol without it the TCP client will often close before the data is fully sent delay(200); Serial.println("Thingspeak update sent."); } else{ // Failed to connect to Thingspeak Serial.println("Unable to connect to Thingspeak."); } if(!client.connected()){ client.stop(); } client.flush(); client.stop();
}