At first I thought this issue was the same as muskie “Connected but ‘variable not found’ after a while.” I have almost the same application. I am testing 2 different temperature and relative humidity sensors on a development board.
After about 9 - 15 minutes I can no longer get my spark variable (curl command returns “error: Timed out”). The spark core is still breathing cyan. Also I cannot reprogram the board through the we bide until I cycle power.
After reading Muskie’s post, I converted from Adafruit_DHT to PietteTechDHT library, but the issue is still occurring. My only other thought is that I am not using the interval timer library appropriately.
I think the issue is in my code, as it has been working in this wifi network for long periods of time before, so any advice would be very welcomed.
BTW the interval timer is used to drive a sensor that requires an AC signal. I sample the A2D right before I toggle the ac signal.
// This #include statement was automatically added by the Spark IDE.
#include "PietteTech_DHT/PietteTech_DHT.h"
// This #include statement was automatically added by the Spark IDE.
#include "SparkIntervalTimer/SparkIntervalTimer.h"
#include "math.h"
//////Sensor data variables///////////////////////////////////////
//define for DHT22
#define MS_PER_SEC 1000
#define DHT_PIN 2
#define DHTTYPE DHT22
void dht_wrapper();
PietteTech_DHT rht_sensor(DHT_PIN, DHTTYPE, dht_wrapper); //initialize DHT22
int result = 0;
float dht22_temp_f = 0;
float dht22_rh = 0;
bool bDHTStarted = false;
//defond variables
float defond_temp = 0;
int defond_temp_a2d = 0;
float defond_rh = 0;
int defond_rh_a2d = 0;
//si labs sensor variables
int si_rh_raw = 0;
int si_temp_raw = 0;
float si_rh = 0;
float si_temp_c = 0;
float si_temp_f = 0;
#define SI_ADDY 0x40
#define GET_RH 0xE5
#define GET_TEMP 0xE0
//ac sensor drive
//these variables are for the option labeled option 1 in the
// schematic.
IntervalTimer Opt1Timer;
const uint8_t sns_drv1_pin = A4;
const uint8_t sns_drv2_pin = A5;
int sns_drv_state = LOW;
//////event and timing variables//////////////////////////////////
unsigned long last_millis = 0; //last recorded milli second
unsigned long rht_sensor_last_poll = 0; //last poll of DHT22
unsigned int rht_sensor_min_poll_time = 3;
char result_str[400]; //for web postings
char settings_str[100]; // for web postings
//prototypes
unsigned long elapse_seconds(unsigned long);
float c_to_f(float);
float f_to_c(float);
float dew_point(float, float);
void toggle_sns_drv(void);
float si7013_getTemp(void);
float si7013_getRH(void);
void setup() {
Serial.begin(9600);
Serial.println("rht_sensor started");
Spark.variable("data", &result_str, STRING);
Spark.variable("settings", &settings_str, STRING);
//defond sensor set-up
pinMode(A3, INPUT); //defond temperature sensor
pinMode(A2, INPUT); //defond rh sensor
pinMode(sns_drv1_pin, OUTPUT); //for option 1
pinMode(sns_drv2_pin, OUTPUT); //for option 1
Opt1Timer.begin(toggle_sns_drv, 5, hmSec, TIMER4);
//si labs sensor set-up
Wire.setSpeed(CLOCK_SPEED_100KHZ);
Wire.stretchClock(true);
Wire.begin();
}
void dht_wrapper(void)
{
rht_sensor.isrCallback();
}
void loop() {
if(last_millis > millis()) last_millis = millis(); //roll-over condition
//poll the sensor every 3 seconds and report the temp and humidity
if(elapse_seconds(rht_sensor_last_poll) > rht_sensor_min_poll_time)
{
if(!rht_sensor.acquiring())
{
if(result != DHTLIB_OK)
{
Serial.println("onboard sensor read failed");
dht22_temp_f = -32000.0;
dht22_rh = -32000.0;
}
else
{
dht22_temp_f = rht_sensor.getFahrenheit();
dht22_rh = rht_sensor.getHumidity();
}
bDHTStarted = false;
}
defond_temp_a2d = analogRead(A3);
si_rh = si7013_getRH();
si_temp_c = si7013_getTemp();
si_temp_f = c_to_f(si_temp_c);
if(!bDHTStarted)
{
rht_sensor.acquire();
bDHTStarted = true;
}
rht_sensor_last_poll = millis();
////////////////////////////////////////////////////////////////////////
//data string
////////////////////////////////////////////////////////////////////////
//
// notes: "data" is a Spark variable.
// this string is a Spark variable and available through the
// api using the GET command.
////////////////////////////////////////////////////////////////////////
sprintf(result_str, "{\"dht22_temp\":%f"
",\"dht22_rh\":%f"
",\"defond_temp_a2d\":%d"
",\"defond_rh_a2d\":%d"
",\"si_temp_f\":%f"
",\"si_rh\":%f}"
,dht22_temp_f
,dht22_rh
,(int)defond_temp_a2d
,(int)defond_rh_a2d
,si_temp_f
,si_rh);
Serial.println(result_str);
}
}
////////////////////////////////////////////////////////////////////////
//name: elapse_seconds
//author: jerome verhoeven
//date: 2014.09.26
//desc: used to calculate the elapse time from an event
//arg: unsigned long (millis() recorded when event happened
//return: unsigned long elapsed time in seconds
//notes: must define MS_PER_SEC globally
////////////////////////////////////////////////////////////////////////
unsigned long elapse_seconds(unsigned long event_time)
{
unsigned long elapse_time;
elapse_time = (millis() - event_time + MS_PER_SEC/2) / MS_PER_SEC;
return elapse_time;
}
////////////////////////////////////////////////////////////////////////
//name: c_to_f
//author: jerome verhoeven
//date: 2014.10.14
//desc: converts degrees celsius to fahrenheit
//arg: (float) degrees f
//return: (float) degrees c
//notes:
////////////////////////////////////////////////////////////////////////
float c_to_f(float temp_c)
{
float temp_f = 0;
temp_f = (temp_c * 9.0 / 5.0) + 32;
return temp_f;
}
////////////////////////////////////////////////////////////////////////
//name: f_to_c
//author: jerome verhoeven
//date: 2014.10.14
//desc: convert degrees farhenheit to celsius
//arg: (float) degrees c
//return: (float) degrees f
//notes:
////////////////////////////////////////////////////////////////////////
float f_to_c(float temp_f)
{
temp_f = constrain(temp_f, -40, 212);
int itemp_f = (int)((temp_f) * 10);
int itemp_c = map(itemp_f, -400, 2120, -400, 1000);
float temp_c = ((float)itemp_c) / 10.0;
return temp_c;
}
////////////////////////////////////////////////////////////////////////
//name: dew_point
//author: jerome verhoeven
//date: 2014.10.14
//desc: calculates the dew point in degrees f
//arg: (float) temperature in farenheit, (float) relative humidity in %
//return: (float) dew point degrees farenheit
//notes: assumes sea level //uses vaisala humidity conversion formula
// guide
////////////////////////////////////////////////////////////////////////
float dew_point(float temp_f, float rh)
{
//constrain the inputs
temp_f = constrain(temp_f, -40, 212);
rh = constrain(rh, 0, 100);
//define constants
const float a = 6.116441;
const float m = 7.591386;
const float tn = 240.7263;
float temp_c = f_to_c(temp_f);
float pws = a * powf(10,((m * temp_c) / (temp_c + tn)));
//Serial.print("pws: ");
//Serial.println(pws);
float pw = pws * (rh / 100);
//Serial.print("pw: ");
//Serial.println(pw);
float dp = tn / ((m / log10f(pw / a)) - 1);
//Serial.print("dp_C: ");
//Serial.println(dp);
dp = c_to_f(dp);
return dp;
}
////////////////////////////////////////////////////////////////////////
//name: toggle_sns_drv
//author: jerome verhoeven
//date: 2014.12.22
//desc: toggle the sns_drv lines to provide an ac signal to a sensor
//arg: void
//return: void
//notes:
////////////////////////////////////////////////////////////////////////
void toggle_sns_drv(void)
{
if(sns_drv_state == LOW)
{
defond_rh_a2d = analogRead(A2);
sns_drv_state = HIGH;
//sns_drv1 high
PIN_MAP[sns_drv1_pin].gpio_peripheral->BSRR = PIN_MAP[sns_drv1_pin].gpio_pin;
//sns_drv2 low
PIN_MAP[sns_drv2_pin].gpio_peripheral->BRR = PIN_MAP[sns_drv2_pin].gpio_pin;
}
else
{
sns_drv_state = LOW;
//sns_drv1 low
PIN_MAP[sns_drv1_pin].gpio_peripheral->BRR = PIN_MAP[sns_drv1_pin].gpio_pin;
//sns_drv2 high
PIN_MAP[sns_drv2_pin].gpio_peripheral->BSRR = PIN_MAP[sns_drv2_pin].gpio_pin;
}
}
////////////////////////////////////////////////////////////////////////
//name: si7013_getTempC()
//author: jerome verhoeven
//date: 2014.12.23
//desc: returns the Temperature in degrees Celsius
//arg: void
//return: float degrees in C
//notes: must be called after a getRH() call
////////////////////////////////////////////////////////////////////////
float si7013_getTemp()
{
int ret_val = 0;
char c = 0;
Wire.beginTransmission(SI_ADDY);
Wire.write(GET_TEMP);
if(Wire.endTransmission())
{
Serial.println("si labs transmission failed temp");
return 0;
}
Wire.requestFrom(SI_ADDY, 2);
while(Wire.available())
{
ret_val = ret_val << 8;
c = Wire.read();
ret_val |= c;
}
return ((175.72 * (float)ret_val) / 65536) - 46.85;
}
////////////////////////////////////////////////////////////////////////
//name: si7013_getRH()
//author: jerome verhoeven
//date: 2014.12.23
//desc: returns rh
//arg: void
//return: float for rh
//notes:
////////////////////////////////////////////////////////////////////////
float si7013_getRH()
{
int ret_val = 0;
char c = 0;
Wire.beginTransmission(SI_ADDY);
Wire.write(GET_RH);
if(Wire.endTransmission())
{
Serial.println("si labs transmission failed rh");
return 0;
}
Wire.requestFrom(SI_ADDY, 2);
while(Wire.available())
{
ret_val = ret_val << 8;
c = Wire.read();
ret_val |= c;
}
return ((125 * (float)ret_val) / 65536) - 6;
}
////////////////////////////////////////////////////////////////////////
//name: si7013_getTempRH
//author: jerome verhoeven
//date: 2014.12.26
//desc: retreive rh and temperature from the si7013 sensor
//arg: pointer to float (temp variable),
// pointer to float (rh variable)
//return: 0 for success; other indicates error
//notes:
////////////////////////////////////////////////////////////////////////
int si7013_getTempRH(float *temp, float *rh)
{
int ret_val = -1;
unsigned char c = 0;
unsigned int buffer = 0;
Wire.beginTransmission(SI_ADDY);
Wire.write(GET_RH);
ret_val = Wire.endTransmission();
if(ret_val) //error
{
return ret_val;
}
Wire.requestFrom(SI_ADDY, 3);
while(Wire.available())
{
buffer = buffer << 8;
c = Wire.read();
}
}
////////////////////////////////////////////////////////////////////////
//name:
//author: jerome verhoeven
//date: 2014.
//desc:
//arg:
//return:
//notes:
////////////////////////////////////////////////////////////////////////
//terminal commands
// curl https://api.spark.io/v1/devices/53ff6e066667574845390967/settings -d access_token=43f5e84b1359c8cfd3d75c9a0fce0df13802c22e -d"args={\"temp_offset\":0,\"rh_offset\":0,\"water_thres\":800}"
// curl https://api.spark.io/v1/devices/53ff6e066667574845390967/settings?access_token=43f5e84b1359c8cfd3d75c9a0fce0df13802c22e
// curl https://api.spark.io/v1/devices/53ff6e066667574845390967/data?access_token=43f5e84b1359c8cfd3d75c9a0fce0df13802c22e