Added configuration settings for MQTT in config.php. Finished screen box sizes for the fermenters monitor. Select beername and code from the current brew products and send it to the fermenter. Added switch commands to the fermenter. Delay data get from the fermenter after sending any command so there is time to process the commands. Turn switches off when the mode changes. Removed 0.2 degrees setpoint diffs for low and high, the fermenter must deal with it. Prevent turning the heater and cooler together on.

Thu, 10 Jan 2019 20:22:06 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 10 Jan 2019 20:22:06 +0100
changeset 185
4c25db9e8102
parent 184
3dbe1d2265ed
child 186
a7c2c61a01ad

Added configuration settings for MQTT in config.php. Finished screen box sizes for the fermenters monitor. Select beername and code from the current brew products and send it to the fermenter. Added switch commands to the fermenter. Delay data get from the fermenter after sending any command so there is time to process the commands. Turn switches off when the mode changes. Removed 0.2 degrees setpoint diffs for low and high, the fermenter must deal with it. Prevent turning the heater and cooler together on.

www/cmd_fermenter.php file | annotate | diff | comparison | revisions
www/config.php.dist file | annotate | diff | comparison | revisions
www/css/style.css file | annotate | diff | comparison | revisions
www/includes/db_product.php file | annotate | diff | comparison | revisions
www/js/mon_fermenter.js file | annotate | diff | comparison | revisions
www/mon_fermenter.php file | annotate | diff | comparison | revisions
--- a/www/cmd_fermenter.php	Wed Jan 09 16:19:26 2019 +0100
+++ b/www/cmd_fermenter.php	Thu Jan 10 20:22:06 2019 +0100
@@ -1,4 +1,5 @@
 <?php
+require_once('config.php');
 
 /*
  * Sequence number file. Must be mode 666 in directory mode 777
@@ -39,7 +40,7 @@
  * payload:  {"timestamp":1546956819,"seq":688,"metric":$payload}
  * $payload: {"product":{"code":"HUP001","name":"Hop Hup"}}
  *           {"stage":"PRIMARY","mode":"FRIDGE","setpoint":{"low":17.8,"high":18.2}}
- *           {"heater":{"state":0},"cooler":{"state":0},"fan":{"state":0}}
+ *           {"heater":{"state":0},"cooler":{"state":0},"fan":{"state":0},"light":{"state":0}}
  *           {"profile":{"uuid":"...","name":"profielnaam","inittemp":{"low":18.2,"high":20.5},"fridgemode":0,
  *              "steps":[{"resttime":12,"steptime":36,"target":{"low":20.8,"high":23.1},"fridgemode":0},
  *              	 {"resttime":48,"steptime":48,"target":{"low":27.5,"high":28.0},"fridgemode":0}]}}
@@ -48,6 +49,6 @@
 $payload = '\'{"timestamp":' . time() . ',"seq":' . $nr . ',"metric":' . $payload . '}\'';
 
 syslog(LOG_NOTICE, $topic . ' ' . $payload);
-shell_exec('mosquitto_pub -h seaport -q 0 -t '.$topic.' -m '.$payload);
+shell_exec('mosquitto_pub -h '.MQTT_HOST.' -q 0 -t '.$topic.' -m '.$payload);
 
 ?>
--- a/www/config.php.dist	Wed Jan 09 16:19:26 2019 +0100
+++ b/www/config.php.dist	Thu Jan 10 20:22:06 2019 +0100
@@ -3,7 +3,7 @@
 /*
  * Check for development server
  */
-if ( strcmp($_SERVER['SERVER_NAME'], "seaport.mbse.ym") == 0)  {
+if (isset($_SERVER['SERVER_NAME']) && ( strcmp($_SERVER['SERVER_NAME'], "seaport.mbse.ym") == 0))  {
 
 	define ('DBASE_NAME', 'bms_dev');
 	define ('DBASE_HOST', 'lx02');
@@ -13,6 +13,11 @@
 
 	define ('EMAIL_FROM', 'noreply@mbse.ym');
 
+	define ('MQTT_HOST', 'localhost');
+	define ('MQTT_PORT', 1883 );
+	define ('MQTT_USER', 'mqtt_user' );
+	define ('MQTT_PASS', 'mqtt_pass' );
+
 	setlocale ( LC_TIME , 'nl_NL' );
 
 } else {
@@ -25,6 +30,11 @@
 
 	define ('EMAIL_FROM', 'noreply@mbse.ym');
 
+	define ('MQTT_HOST', 'localhost');
+        define ('MQTT_PORT', 1883 );
+        define ('MQTT_USER', 'mqtt_user' );
+        define ('MQTT_PASS', 'mqtt_pass' );
+
 	setlocale ( LC_TIME , 'nl_NL' );
 
 }
--- a/www/css/style.css	Wed Jan 09 16:19:26 2019 +0100
+++ b/www/css/style.css	Thu Jan 10 20:22:06 2019 +0100
@@ -128,7 +128,7 @@
  * +-------------------------------+
  */
 #fermenter_panel_top {
-  width: 280px;
+  width: 290px;
   height: 200px;
   float: right;
   margin: 5px;
@@ -165,7 +165,7 @@
 
 
 #fermenter_panel_display {
-  width: 280px;
+  width: 290px;
   height: 70px;
   float: right;
   margin: 5px;
@@ -176,7 +176,7 @@
   margin-bottom: 3px;
 }
 #fermenter_display {
-  width: 140px;
+  width: 145px;
   height: 70px;
   float: left;
   text-align: center;
@@ -184,7 +184,7 @@
 
 
 #fermenter_panel_control {
-  width: 280px;
+  width: 290px;
   height: 140px;
   float: right;
   margin: 5px;
@@ -198,7 +198,7 @@
 #fermenter_led1,
 #fermenter_led2,
 #fermenter_led3 {
-  width: 93px;
+  width: 96px;
   height: 30px;
   float: left;
   text-align: center;
@@ -214,21 +214,20 @@
 #fermenter_toggle2,
 #fermenter_toggle3 {
   float: left;
-  margin-left: 57px;
+  margin-left: 60px;
   margin-top: 20px;
 }
 
 
 #fermenter_panel_buttons {
-  width: 280px;
-  height: 130px;
+  width: 290px;
+  height: 155px;
   float: right;
   margin: 5px;
   background-color: #252526;
   border: 2px solid;
   border-color: #59b4d4;
   border-radius: 5px 5px 5px 5px;
-  margin-bottom: 3px;
 }
 
 
--- a/www/includes/db_product.php	Wed Jan 09 16:19:26 2019 +0100
+++ b/www/includes/db_product.php	Thu Jan 10 20:22:06 2019 +0100
@@ -335,6 +335,24 @@
 	}
 
 	/*
+	 * SELECT, produce a list of products that can be fermented.
+	 */
+	if (isset($_GET['select']) && ($_GET['select'] == "ferment")) {
+		$query  = "SELECT code,name,stage FROM products WHERE ";
+		$query .= "stage='Wait' OR stage='Brew' OR stage='Primary' OR stage='Secondary' OR stage='Tertiary' OR stage='Carbonation' ORDER BY code;";
+		$result = mysqli_query($connect, $query) or die("SQL Error 1: " . mysqli_error($connect));
+		while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
+			$brews[] = array(
+				'code' => $row['code'],
+				'name' => $row['name'],
+				'stage' => $row['stage']
+			);
+		}
+		echo json_encode($brews);
+		return;
+	}
+
+	/*
 	 * Default, select all or a given record.
 	 */
 	if (isset($_GET['record'])) {
--- a/www/js/mon_fermenter.js	Wed Jan 09 16:19:26 2019 +0100
+++ b/www/js/mon_fermenter.js	Thu Jan 10 20:22:06 2019 +0100
@@ -31,6 +31,33 @@
 	var	yl = 12;	// Normal yeast temp range
 	var	yh = 24;
 
+	var productSource = {
+		datatype: "json",
+		cache: false,
+		datafields: [
+			{ name: 'code', type: 'string' },
+			{ name: 'name', type: 'string' },
+			{ name: 'stage', type: 'string' },
+		],
+		id: 'code',
+		url: "includes/db_product.php?select=ferment"
+	};
+	var productlist = new $.jqx.dataAdapter(productSource);
+	$("#select_beer").jqxDropDownList({
+		placeHolder: "Kies bier:",
+		theme: theme,
+		source: productlist,
+		displayMember: "code",
+		width: 150,
+		height: 23,
+		dropDownWidth: 500,
+		autoDropDownHeight: true,
+		renderer: function (index, label, value) {
+			var datarecord = productlist.records[index];
+			return datarecord.code + " - " + datarecord.name;
+		}
+	});
+
 	var gaugeoptions = {
 		min: 0, max: 40, width: 375, height: 375,
 		ranges: [{ startValue:  0, endValue: yl, style: { fill: '#3399FF', stroke: '#3399FF' }, endWidth: 10, startWidth: 10 },
@@ -97,14 +124,50 @@
 	function sendBase(stage, mode, tlo, thi) {
 
 		console.log("sendBase("+stage+", "+mode+", "+tlo+", "+thi+")");
-		var data  = 'node=rpi01&alias=unit0&payload={"stage":"'+stage;
+		var data  = 'node='+record.node+'&alias='+record.alias+'&payload={"stage":"'+stage;
 		    data += '","mode":"'+mode+'","setpoint":{"low":'+tlo+',"high":'+thi+'}}';
 		$.ajax({
 			url: "cmd_fermenter.php",
 			data: data,
 			type: "POST",
 			success: function(data) {
-				//do something after something is recieved from php
+				//do something after something is received from php
+			},
+			error: function(jqXHR, textStatus, errorThrown) {
+				console.log("sendBase() error");
+			}
+		});
+	}
+
+	function sendSwitch(sw1, sw2, sw3, sw4) {
+
+		console.log("sendSwitch("+sw1+", "+sw2+", "+sw3+", "+sw4+")");
+		var data  = 'node='+record.node+'&alias='+record.alias+'&payload=';
+		    data += '{"heater":{"state":'+sw1+'},"cooler":{"state":'+sw2+'},"fan":{"state":'+sw3+'},"light":{"state":'+sw4+'}}';
+		$.ajax({
+			url: "cmd_fermenter.php",
+			data: data,
+			type: "POST",
+			success: function(data) {
+				//do something after something is received from php
+			},
+			error: function(jqXHR, textStatus, errorThrown) {
+				console.log("sendBase() error");
+			}
+		});
+	}
+
+	function sendProduct(code, name) {
+
+		console.log("sendProduct("+code+", "+name+")");
+		var data  = 'node='+record.node+'&alias='+record.alias+'&payload=';
+		    data += '{"product":{"code":"'+code+'","name":"'+name+'"}}';
+		$.ajax({
+			url: "cmd_fermenter.php",
+			data: data,
+			type: "POST",
+			success: function(data) {
+				//do something after something is received from php
 			},
 			error: function(jqXHR, textStatus, errorThrown) {
 				console.log("sendBase() error");
@@ -165,6 +228,11 @@
 			$("#info_beer").html(record.beercode + " - " + record.beername);
 			$("#info_mode").jqxDropDownList('selectItem', record.mode);
 			$("#info_stage").jqxDropDownList('selectItem', record.stage);
+			$("#info_profile").html(record.profile_name);
+			if (record.profile_name == "")
+				$("#info_mode").jqxDropDownList('disableItem', "PROFILE");
+			else
+				$("#info_mode").jqxDropDownList('enableItem', "PROFILE");
 			$("#target_lo").val(record.setpoint_low);
 			$("#target_hi").val(record.setpoint_high);
 			if (record.online && ((record.mode == "FRIDGE") || (record.mode == "BEER"))) {
@@ -177,8 +245,11 @@
 
 			if (record.online && (record.mode != "OFF")) {
 				$("#fermenter_powerled").html('<div class="LEDblue_on"></div>Power');
+				$("#select_beer").jqxDropDownList({ disabled: true });
+				$("#select_beer").jqxDropDownList('clearSelection');
 			} else {
 				$("#fermenter_powerled").html('<div class="LEDblue_off"></div>Power');
+				$("#select_beer").jqxDropDownList({ disabled: false });
 			}
 			if (record.online && (record.alarm != "0")) {
 				$("#fermenter_alarmled").html('<div class="LEDred_on"></div>Alarm');
@@ -240,26 +311,55 @@
 	// Get the data immediatly and then at regular intervals to refresh.
 	dataAdapter.dataBind();
 	setInterval(function() {
+		var skip = false;
 		if (newBase) {
 			sendBase(record.stage, record.mode, record.setpoint_low, record.setpoint_high);
 			newBase = false;
+			skip = true;
 		}
-		dataAdapter.dataBind();
+		if (newSwitch) {
+			sendSwitch(record.heater_state, record.cooler_state, record.fan_state, record.light_state);
+			newSwitch = false;
+			skip = true;
+		}
+		if (newProduct) {
+			sendProduct(record.code, record.name);
+			newProduct = false;
+			skip = true;
+		}
+
+		if (! skip) {
+			// Only if we didn't send a command so that a command can be processed.
+			dataAdapter.dataBind();
+		}
 	}, 10000);
 
 	$('#info_mode').on('change', function (event) {
 		record.mode = args.item.value;
+		$("#fermenter_toggle1").val(0);
+		$("#fermenter_toggle2").val(0);
+		$("#fermenter_toggle3").val(0);
 		newBase = true;
 	});
-	$('#info_stage').on('change', function (event) {
+	$('#info_stage').on('select', function (event) {
 		record.stage = args.item.value;
 		newBase = true;
 	});
+	$("#select_beer").on('select', function (event) {
+		if (event.args) {
+			var index = event.args.index;
+			var datarecord = productlist.records[index];
+			record.code = datarecord.code;
+			record.name = datarecord.name;
+			newProduct = true;
+		}
+	});
+
 	$('#target_lo').on('change', function (event) {
 		record.setpoint_low = parseFloat(event.args.value);
 		// Keep the high target above the low.
-		if (record.setpoint_low > (record.setpoint_high - 0.2)) {
-			record.setpoint_high = record.setpoint_low + 0.2;
+		if (record.setpoint_low > record.setpoint_high) {
+			record.setpoint_high = record.setpoint_low;
 			$("#target_hi").val(record.setpoint_high);
 		}
 		newBase = true;
@@ -267,13 +367,54 @@
 	$('#target_hi').on('change', function (event) {
 		record.setpoint_high = parseFloat(event.args.value);
 		// Keep the low target below the high.
-		if (record.setpoint_high < (record.setpoint_low + 0.2)) {
-			record.setpoint_low = record.setpoint_high - 0.2;
+		if (record.setpoint_high < record.setpoint_low) {
+			record.setpoint_low = record.setpoint_high;
 			$("#target_lo").val(record.setpoint_low);
 		}
 		newBase = true;
 	});
 
+	$("#fermenter_toggle1").on('checked', function (event) {
+		if (record.mode == "NONE") {
+			record.heater_state = 0;
+			newSwitch = true;
+		}
+	});
+	$("#fermenter_toggle1").on('unchecked', function (event) {
+		if (record.mode == "NONE") {
+			record.heater_state = 100;
+			record.cooler_state = 0;
+			$("#fermenter_toggle2").val(0);
+			newSwitch = true;
+		}
+	});
+	$("#fermenter_toggle2").on('checked', function (event) {
+		if (record.mode == "NONE") {
+			record.cooler_state = 0;
+			newSwitch = true;
+		}
+	});
+	$("#fermenter_toggle2").on('unchecked', function (event) {
+		if (record.mode == "NONE") {
+			record.cooler_state = 100;
+			record.heater_state = 0;
+			$("#fermenter_toggle1").val(0);
+			newSwitch = true;
+		}
+	});
+	$("#fermenter_toggle3").on('checked', function (event) {
+		if (record.mode == "NONE") {
+			record.fan_state = 0;
+			newSwitch = true;
+		}
+	});
+	$("#fermenter_toggle3").on('unchecked', function (event) {
+		if (record.mode == "NONE") {
+			record.fan_state = 100;
+			newSwitch = true;
+		}
+	});
+
 
 	// The chart button.
    	$("#FLog").jqxButton({ template: "info", width: '150px', theme: theme });
--- a/www/mon_fermenter.php	Wed Jan 09 16:19:26 2019 +0100
+++ b/www/mon_fermenter.php	Thu Jan 10 20:22:06 2019 +0100
@@ -9,12 +9,13 @@
      <div id="fermenter_table">
       <div id="fermenter_info">
        <table style='width: 100%; padding: 10px;'>
-        <tr><th colspan=2>Klimaatkast overzicht</th></tr>
-        <tr><td>Uuid</td><td><div id="info_uuid"></div></td></tr>
-        <tr><td>Systeem</td><td><div id="info_system"></div></td></tr>
-        <tr><td>Bier</td><td><div id="info_beer"></div></td></tr>
-        <tr><td>Werking</td><td><div id="info_mode"></div></td></tr>
-        <tr><td>Fase</td><td><div id="info_stage"></div></td></tr>
+        <tr><th colspan=3>Klimaatkast overzicht</th></tr>
+        <tr><td>Uuid</td><td colspan="2"><div id="info_uuid"></div></td></tr>
+        <tr><td>Systeem</td><td colspan="2"><div id="info_system"></div></td></tr>
+        <tr><td>Code en bier</td><td><div id="info_beer"></div></td><td><div id="select_beer"></div></td></tr>
+        <tr><td>Werking</td><td colspan="2"><div id="info_mode"></div></td></tr>
+	<tr><td>Vergisting fase</td><td colspan="2"><div id="info_stage"></div></td></tr>
+        <tr><td>Vergisting profiel</td><td><div id="info_profile"></div></td><td><div id="select_profile"></div></td></tr>
        </table>
       </div>
      </div>
@@ -54,7 +55,7 @@
      </div> <!-- fermenter_panel_control -->
 
      <div id="fermenter_panel_buttons">
-      <input type="button" id="FLog" value="Vergisting log" />
+      <input style="margin-top: 20px; margin-left: 70px;" type="button" id="FLog" value="Vergisting log" />
      </div> <!-- fermenter_panel_buttons -->
     </div> <!-- End right side -->
    </div>

mercurial