homekit_office.tc¶
HomeKit Office — Color light + outlet + sensors via UDP
// HomeKit Office — Color light + outlet + sensors via UDP
// TinyC equivalent of the Scripter config:
// >h 111-11-111
// Licht,5,0,mh_pwr,mh_hue,mh_sat,mh_bri
// Ecklicht,7,0,elamp
// Bürotemperatur,10,0,btemp
// BüroFeuchtigkeit,10,1,bhumi
// BüroLuftdruck,10,2,bpress
//
// Sensors arrive via UDP multicast from a BME280 node.
// Light and outlet are controlled from Apple Home.
// All HomeKit globals are float — no x10 scaling needed.
// --- HomeKit-bound globals (native float values, auto-send via UDP) ---
global float mh_pwr; // light on/off (0.0 or 1.0)
global float mh_hue; // hue 0..360
global float mh_sat; // saturation 0..100
global float mh_bri; // brightness 0..100
global float elamp; // corner light on/off (0.0 or 1.0)
global float btemp; // temperature (e.g. 22.5)
global float bhumi; // humidity (e.g. 55.0)
global float bpress; // pressure (e.g. 1013.0)
// --- local state ---
int last_pwr;
int last_elamp;
// HomeKitWrite callback — only needed for local side effects (relay forwarding)
// Value is already written to the global by the firmware before this is called.
void HomeKitWrite(int dev, int var, float val) {
// Forward light power to relay
if (dev == 0 && var == 0) {
int pwr;
pwr = 0;
if (val > 0.0) { pwr = 1; }
if (pwr != last_pwr) {
tasm_power = pwr;
last_pwr = pwr;
}
}
}
void WebUI() {
webButton(mh_pwr, "Licht");
webSlider(mh_bri, 0, 100, "Helligkeit");
webButton(elamp, "Ecklicht");
}
void EverySecond() {
// Sensor values (btemp, bhumi, bpress) are auto-updated from UDP packets.
// Re-assign HomeKit-changed variables to trigger auto-send via UDP
// (hkReady = changed by Apple Home, self-assign triggers STORE_GLOBAL_UDP)
if (hkReady(mh_pwr)) { mh_pwr = mh_pwr; }
if (hkReady(mh_hue)) { mh_hue = mh_hue; }
if (hkReady(mh_sat)) { mh_sat = mh_sat; }
if (hkReady(mh_bri)) { mh_bri = mh_bri; }
if (hkReady(elamp)) { elamp = elamp; }
}
int main() {
mh_pwr = 0.0;
mh_hue = 0.0;
mh_sat = 0.0;
mh_bri = 50.0;
elamp = 0.0;
btemp = 22.0;
bhumi = 50.0;
bpress = 1013.0;
last_pwr = -1;
last_elamp = -1;
webPageLabel(0, "HomeKit Office");
webConsoleButton("/hk", "HomeKit Pairing");
hkSetCode("111-11-111");
hkAdd("Licht", HK_LIGHT); hkVar(mh_pwr); hkVar(mh_hue); hkVar(mh_sat); hkVar(mh_bri);
hkAdd("Ecklicht", HK_OUTLET); hkVar(elamp);
hkAdd("Bürotemperatur", HK_TEMPERATURE); hkVar(btemp);
hkAdd("BüroFeuchtigkeit", HK_HUMIDITY); hkVar(bhumi);
hkAdd("BüroLuftdruck", HK_LIGHT_SENSOR); hkVar(bpress);
hkStart();
return 0;
}