Jon Hobbs
Published © GPL3+

Home Automation Laboratory (HAL) Part 4 - Custom Interfaces

The final installment. We install and setup a web-server that will serve up custom pages to monitor and control our home automation system.

BeginnerFull instructions provided1 hour2,749
Home Automation Laboratory (HAL) Part 4 - Custom Interfaces

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
Raspberry Pi Pi Case
×1

Software apps and online services

Apache

Story

Read more

Code

Web Worker

JavaScript
This is the javascrtipt code for a web worker that our web page uses to poll the status of an OpenHAB item
function poll_zone_1_status() {
    var theUrl = "http://192.168.1.169:8080/rest/items/sprinkler_z1_switch/state";
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
    xmlHttp.send( null );
    postMessage(xmlHttp.responseText);
    setTimeout("poll_zone_1_status()",1000);
}

Web Page

JavaScript
This is the snippet of our web page that launches the web worker and uses it's return values to update a control on the web page.
	var ws1 = new Worker("workers/poll_zone_1_status.js");
        ws1.onmessage = function(event)
        {
            if (event.data == 'ON')
            {
                document.getElementById("zone_1_status").style.color = 'green';
            }
            else
            {
                document.getElementById("zone_1_status").style.color = 'red';
            }
            document.getElementById("zone_1_status").textContent = event.data;
        };

Send Command

JavaScript
This is a javascript function that was extracted from a custom web page. It shows how to send a command to an OpenHAB item.
	function ZoneSwitch(zone, command)
	{
		var req = new XMLHttpRequest();
		req.open("POST", "http://192.168.1.169:8080/rest/items/sprinkler_z" + zone + "_switch", true);
		req.setRequestHeader("Content-Type", "text/plain");
		req.setRequestHeader("Accept", "application/json");
		req.send(command);
	}

Credits

Jon Hobbs

Jon Hobbs

7 projects • 24 followers
Just a Minnesota kid hanging out in Kansas

Comments