#!/usr/bin/env php setCredentials(getenv("MQTT_USER"), getenv("MQTT_PASS")); } $c->connect(MQTT_HOST, 1883, 60); $c->subscribe(ENERGY_METER_TOPIC, 2); $c->subscribe(DOOR_TOPIC, 2); $c->onMessage(function (Message $message) { l("got message", $message->topic, $message->payload); switch ($message->topic) { case ENERGY_METER_TOPIC: energy($message->payload); break; case DOOR_TOPIC: door($message->payload); break; default: l("unknown topic", $message->topic); } }); while (true) { while ($lastOutput > time() - 10) { $c->loop(1000); } output(); } function door($newStatus) { global $doorStatus, $energyCounter, $doorOpenEnergy, $doorOpenTime; $newStatus = (bool) $newStatus; if ($newStatus === $doorStatus) { return; } $doorStatus = $newStatus; if (!$newStatus) { $doorOpenEnergy = $energyCounter; $doorOpenTime = time(); } output(); } function energy($value) { global $energyCounter,$doorOpenEnergy; $energyCounter = (float) $value; if(is_null($doorOpenEnergy)){ $doorOpenEnergy = $energyCounter; } output(); } function output() { global $doorStatus, $energyCounter, $doorOpenEnergy, $doorOpenTime, $lastOutput, $c; if ($doorStatus) { $myUsage = $energyCounter - $doorOpenEnergy; $myCost = $myUsage * ENERGY_PRICE; $c->publish( OUTPUT_TOPIC_PREFIX . "/used_energy_float", strval($myUsage), 2 ); l("cost", $myCost); $c->publish(OUTPUT_TOPIC_PREFIX . "/price_float", strval($myCost), 2); $c->publish( OUTPUT_TOPIC_PREFIX . "/price_string", number_format($myCost, 2, ",", "."), 2 ); } else { l("nope"); $c->publish(OUTPUT_TOPIC_PREFIX . "/used_energy_float", "", 2); $c->publish(OUTPUT_TOPIC_PREFIX . "/price_float", "", 2); $c->publish(OUTPUT_TOPIC_PREFIX . "/price_string", "", 2); } $lastOutput = time(); }