Somewhat out of order, here are the functions I use to pull the data from the sensors using the libs. Keep going and you will see all the pieces you need to use the sensors and publish data in a non-blocking way. I pulled these functions out of a much larger firmware I have been working on but If you piece this all back together it should run, I haven’t done it myself but I try to keep functions fairly tight and serving a single purpose. I use Particle Dev exclusively so lib use in the WebIDE…I can’t help with but here are the links to the libs I am using:
PietteTech_DHT Lib
DS18B20 and OneWire Libs
void getairtemp(){
if (!bDHTstarted) { // start the sample
DHT.acquire();
bDHTstarted = true;
} //End of DHTstarted IF
if (!DHT.acquiring())
{ // has sample completed?
int result = DHT.getStatus();
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;
}
airtemp = (DHT.getFahrenheit());
airhumidity = (DHT.getHumidity());
n++; // increment DHT Sample counter
bDHTstarted = false; // reset the sample flag so we can take another
DHTnextSampleTime = millis() + DHT_SAMPLE_INTERVAL; // set the time for next sample
}
}
void getwatertemp(){
if(!ds18b20.search()){
ds18b20.resetsearch();
watercelsius = ds18b20.getTemperature();
waterfahrenheit = ds18b20.convertToFahrenheit(watercelsius);
DS18B20nextSampleTime = millis() + DS18B20_SAMPLE_INTERVAL;
Serial.print(waterfahrenheit);
}
}
Includes:
#include "PietteTech_DHT.h"
#include "DS18B20.h"
#include "OneWire.h"
Defines and vars:
#define DHTTYPE DHT11 // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN 3 // Digital pin for communications
#define DHT_SAMPLE_INTERVAL 4000 // Sample Air Temp every 4 seconds
#define Metric_Publish_Rate 60000 // Publish once per Minute
#define DS18B20_SAMPLE_INTERVAL 2000 //Sample Water Temp every two seconds
DS18B20 ds18b20 = DS18B20(D2); //Sets Pin D2 for Water Temp Sensor
//DHT Wrapper and Lib instantiate
void dht_wrapper();
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
//Global Vars
unsigned int DHTnextSampleTime; //Int Var to track last Air Temp/Humidity Sample
bool bDHTstarted; //True/False flag to indicate if we have started DHT acquisition
int n; //Int Var to track number of DHT11 Samples
unsigned int MetricnextPublishTime; //Int Var to track last Publish of Metrics
unsigned int DS18B20nextSampleTime; //Int Var to track last Water Temp Sample
float airtemp = 0;
float airhumidity = 0;
float watercelsius = 0; //Float Var for Water Temp in Celsius
float waterfahrenheit = 0; //Float Var for Water Temp in Fahrenheit
char metricdata[64]; //Char Var for Spark.Publish in metricpush function
Setup() stuff:
pinMode(D3, INPUT); //DHT11 Sensor
pinMode(D2, INPUT); //DS18B20 Sensor
DHTnextSampleTime = 0; //Set Sample Time for immediate Air Temp sampling
DS18B20nextSampleTime = 0; //Set Water Temp Sensor for immediate sampling
MetricnextPublishTime = 30000; //Set Metric Publishing to begin 30 Seconds after starting the main loop
DHT wrapper, mine is between the setup() and loop(), not sure if it matters where it is placed but I bet there is a wrong place.
// This wrapper is in charge of calling DHT Libs must be defined like this for the lib work
void dht_wrapper() {
DHT.isrCallback();
}
Function to take sample data from the sensors (if required time has passed as dictated by sensor_SAMPLE_INTERVAL values) then call another function to publish that data according to the Metric_Publish_Rate. NOTE: All time related values are in millis.
void SampleandPub(){
if (millis() > DS18B20nextSampleTime){
getwatertemp();
}
if (millis() > DHTnextSampleTime){
getairtemp();
}
if (millis() > MetricnextPublishTime){
metricpush();
}
}
Lastly the metricpush() function to send the sensor data to the Particle cloud.
void metricpush(){
Serial.print("Metric Push!");
sprintf(metricdata,"Air Temp: %2.2f, Humidity: %2.2f, Water Temperature: %2.2f", airtemp, airhumidity, waterfahrenheit);
Spark.publish("tempdata", metricdata, PRIVATE);
MetricnextPublishTime = millis() + Metric_Publish_Rate;
} //End of metricpush function
Not sure what your level of expertise is but if you cut paste everything as is above you will need to add one more thing in the loop() to actually use all of this.
void loop(){
SampleandPub();
}