www/ispindel/index.php

Mon, 18 May 2020 11:00:59 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 18 May 2020 11:00:59 +0200
changeset 679
48f8f3fce7c0
parent 585
fa6e702d0d24
child 847
dc9281501ca6
permissions
-rw-r--r--

Added reconnecting-websocket.js to automatic reconnect the websocket if the connection is lost. Usefull for mobile devices that go to sleep after a while. Changed mon_fermenters to use websockets instead of polling. Fixed wrong temperature color ranges on the fermenter monior. Increased the websocket receive buffer to 2048. In cannot overflow, but larger messages are chunked and the application does not handle these split messages. Needs termferm 0.9.9 or newer.

<?php

require($_SERVER['DOCUMENT_ROOT']."/config.php");

if ($_SERVER['HTTP_USER_AGENT'] == 'ESP8266HTTPClient') {
	//            /usr/bin/mosquitto_pub -h host] [-p port] [-u username [-P password]] -t topic -m message
	$publisher = '/usr/bin/mosquitto_pub -h '.MQTT_HOST.' -t ';

	$input = file_get_contents('php://input');
	//syslog(LOG_NOTICE, "ispindel " . $input);
	$data = json_decode($input, true );
	$node = 'ispindel-' . sprintf("%06x", floatval($data['ID']));

	$alarm = 0;
	$interval = $data['interval'];
        if (floatval($data['battery']) < 3.3) {
                $alarm += 4;
		if (($interval * 3) < 3600)
			$interval = 3600; // at least 3600 seconds when the battery is low.
		else
			$interval = $interval * 3;
	}

	$uuid = 'c0ffeeee-cafe-dead-beef-' . sprintf("%06x", 0xffffff - floatval($data['ID'])) . sprintf("%06x", floatval($data['ID']));
	$topic = 'mbv1.0/ispindels/NBIRTH/' . $node;
	$payload  = '{"metric":{"uuid":"' . $uuid . '","interval":' . $interval . ',';
	$payload .= '"properties":{"hardwaremake":"Wemos","hardwaremodel":"D1 mini","os":"Unknown","os_version":"Unknown","FW":"Unknown"},';
	$payload .= '"net":{"address":"' . $_SERVER['REMOTE_ADDR'] . '","ifname":"sta","rssi":' . $data['RSSI'] . '}}}';
	$pub = $publisher . $topic . ' -m \'' . $payload . '\'';
	exec($pub);

	$alarm = 0;
	if (floatval($data['battery']) < 3.3)
		$alarm += 4;

	if ($data['temp_units'] == 'K') {
		$tempC = floatval($data['temperature']) - 273.15;
	} else if ($data['temp_units'] == 'F') {
		$tempC = floatval($data['temperature']) / 1.8 - 32;
	} else {
		$tempC = floatval($data['temperature']);
	}

	$uuid = 'c0ffeeee-cafe-dead-bee0-' . sprintf("%06x", 0xffffff - floatval($data['ID'])) . sprintf("%06x", floatval($data['ID']));
	$topic = 'mbv1.0/ispindels/DBIRTH/' . $node;
	$payload  = '{"unit":{"uuid":"' . $uuid .'","alias":"' . $data['name'] . '","alarm":' . $alarm . ',"interval":' . $interval;
	$payload .= ',"angle":' . $data['angle'] . ',"temperature":' . $tempC . ',"battery":' . $data['battery'] . ',"gravity":' . $data['gravity'] . '}}';
	$pub = $publisher . $topic . ' -m \'' .$payload . '\'';
	exec($pub);
}

?>

mercurial