I’m having a rather odd issue with the temperature sensor bing used in a timer callback. If the temperature callback function is executed in the loop everything works perfectly. However, if the the callback is executed by the timer, the sensor always reads -127. I have no idea why this could happen?
You can see the results on the OLED screen. The full code is quite large, so i’ll start basic and add anything that may be required, but i believe all the relevant bits of code are below the images.
In particle loop everything works
As timer callback (all i did was comment out function in loop and enable checkTempTimer.start() in setup)
It makes no sense to me that a sensor would behave differently as a timer call back vs in the loop function. Any ideas appreciated…
CONFIG
// CONFIG OLED
#define OLED_CLK D0
#define OLED_MOSI D1
#define OLED_RESET D2
#define OLED_DC D3
#define OLED_CS D4
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
// CONFIG TEMPERATURE SENSOR
int tempSensorPin = A0;
OneWire oneWire(tempSensorPin);
DallasTemperature temp(&oneWire);
// APP GLOBALS
double tempC = 0.0;
double tempF = 0.0;
String lastTempTime = "";
String lastTempCheck = "";
// TIMERS
Timer publishTempTimer(5000, publishTemp);
Timer checkTempTimer(2000, checkTemp); // Timer fires callback, but temp data is -127
FUNCTIONS (other functions omitted for brevity)
void checkTemp () {
temp.requestTemperatures();
double tempCheck = temp.getTempCByIndex(0);
lastTempCheck = String(tempCheck);
lastTempTime = getTime();
if ( tempCheck > -50 && tempCheck < 50)
{
tempC = tempCheck;
tempF = tempC * 9.0 / 5.0 + 32.0;
}
}
PARTICLE SETUP
void setup() {
Serial.begin(9600);
// OLED
display.begin(SSD1306_SWITCHCAPVCC);
showSplash("loading...", 0); // custom splash screen
// Temp
temp.begin();
checkTempTimer.start(); //disable for loop checkTemp
publishTempTimer.start();
}
PARTICLE LOOP
void loop() {
# Check for button class HIGH state and show splash screen
if (btnSplash.isOn()){
showSplash("button pressed", 0);
}else{
displayTemp(lastTempTime+" = "+lastTempCheck);
}
// Comment out checkTempTimer.start() and uncomment line below end everything works
//checkTemp();
}