The following code works great as a data field on my Edge 820 to fire off a background process to retrieve the internal temperature. It runs every 5 minutes, which is a limitation of the frequency of background processes. That's ok. Thanks JIM for the pointers!
The only problem is that it often waits several minutes before it gets the first reading... I'm sure there is a simple fix to get it to retrieve the sensor data immediately the first time before waiting 5 minutes before the next retrieval.
The "paintScreen" code simply writes the current time of data as well as the temperature and the time the sensor was retrieved.
The rest of the code is the mechanisms to manage the background process and associated data.
If anyone can suggest the change I need to prevent the screen from saying: "Waiting...." for several minutes before the first TEMP retrieval, that would be awesome.
=======================================================================================
using Toybox.Application as App;
using Toybox.Graphics as Gfx;
using Toybox.WatchUi as Ui;
using Toybox.Background;
using Toybox.System;
using Toybox.Sensor;
using Toybox.Graphics as Gfx;
using Toybox.Lang;
using Toybox.Time;
var ToD, ToT; // time of day and time that temp was retrieved
// Grab Time of Day and Internal Temp Sensor via Background Process and Paint Screen
class paintScreen extends Toybox.WatchUi.DataField {
function initialize() { DataField.initialize(); }
function onUpdate(dc) {
var w = dc.getWidth() / 2, h = dc.getHeight() / 2;
ToD = Lang.format("$1$:$2$", [System.getClockTime().hour, System.getClockTime().min.format("%02d")]);
var message = App.getApp().backgroundMessage;
dc.setColor(Gfx.COLOR_BLACK, Gfx.COLOR_WHITE); dc.clear();
dc.drawText(w, h, Gfx.FONT_MEDIUM, "ToD: " + ToD, Gfx.TEXT_JUSTIFY_CENTER | Gfx.TEXT_JUSTIFY_VCENTER);
dc.drawText(w, h + 40, Gfx.FONT_MEDIUM, message, Gfx.TEXT_JUSTIFY_CENTER | Gfx.TEXT_JUSTIFY_VCENTER);
}
}
(:background)
class BackgroundApp extends App.AppBase {
var backgroundMessage = "Waiting...";
function initialize() { AppBase.initialize(); }
function getInitialView() { Background.registerForTemporalEvent(new Time.Duration(5 * 60)); return [ new paintScreen() ]; }
function onBackgroundData(data) { backgroundMessage = data; Ui.requestUpdate(); }
function getServiceDelegate() { return [new BackgroundServiceDelegate()]; }
}
// The Service Delegate is the main entry point for background processes
(:background)
class BackgroundServiceDelegate extends System.ServiceDelegate {
var intTEMP = new[2];
function initialize() { System.ServiceDelegate.initialize(); }
function onTemporalEvent() {
Sensor.setEnabledSensors( [Sensor.SENSOR_TEMPERATURE] );
Sensor.enableSensorEvents( method(:onSnsr) );
}
function onSnsr(sensor_info)
{
if( sensor_info.temperature != null ) { intTEMP[1] = (sensor_info.temperature * 9.0/5.0 + 32.0).format("%.1f") + " F"; }
else { intTEMP[1] = "--- F"; }
ToT = Lang.format("$1$:$2$", [System.getClockTime().hour, System.getClockTime().min.format("%02d")]);
intTEMP[0] = ToT;
Background.exit(intTEMP);
}
}