I promised to post up my GUI example in some form here, so here goes …
I am still in progress of making this thread. Bear with me
http://lurks.kapsi.fi/seka/GUI-example.zip
This package has everything you need to make your own external UI that looks whatever you want it to.
I included the bootstrap layout (bootstrap is the templating model), javascript that calls for socket.io actions that pimatic responds to.
Here’s one screenshot that gives the general idea what it can look like.
http://lurks.kapsi.fi/seka/ui2.PNG
The essential part, however is the javascript and socket io. If you want, you can take those to any GUI template you want to.
Starting up
I am assuming you know how to setup an apache/other website server on your Raspberry Pi. You can also host this website in any other server, even outside of your home network, but I don’t recommend this, as the password to your pimatic is in the javascript file.
If you do this, you should at very least password protect the site.
Here’s one link for getting started with Apache, if you don’t know how to.
https://www.raspberrypi.org/documentation/remote-access/web-server/apache.md
However, you don’t need to set up apache just yet. You can develop this on your desktop PC, and then push it to wherever you want.
First, we need to set up the connection with Pimatic on the main javascript file, pihlaja-script.js. Its named by me…
Replace IP-ADDRESS, PORT, USERNAME and PASSWORD with your values.
//DEFINE THE CONNECTION
var socket = io.connect('http://IP-ADDRESS:PORT/?username=USERNAME&password=PASSWORD', {
reconnection: true,
reconnectionDelay: 1000,
reconnectionDelayMax: 3000,
timeout: 20000,
forceNew: true
});
Save and open up the webpage (index.html).
When you have the webpage open, turn on your developer tools of the web browser (F12 on Chrome and Firefox)
From there, open “Console”. This is where the socket IO Messages appear.
What you want to do now, is to write down every device/variable you want to get in this GUI.
You should see something like this: http://lurks.kapsi.fi/seka/tutorial/1.PNG
We need to bind the IDs and values to the javascript file. see the highlighted sections.
http://lurks.kapsi.fi/seka/tutorial/2.PNG
My device id in the image is swi-1, but pretend that its switch-1 as in the code sample below.
And the code in javascript.
//EXAMPLE FOR A BUTTON TOGGLE (SWITCH):
//(This uses pimatic-homeduino switch)
//
//TOGGLE THE CLASS 'ACTIVE' WHEN BUTTON IS PRESSED
//SCRIPT CHECKS THAT THE DEVICE IS NAMED LIKE YOU WANTED
//SO NO CHANGES ON YOUR CONFIGURATION CAUSE HAZARDOUS THINGS..
//(This is checked when you connect, there's the active checking below)
socket.on('devices', function(devices){
console.log(devices);
if (devices[0].id == "switch-1"){
if (devices[0]["attributes"][0].value == true){
$("#button-1").addClass('active');
}else{
$("#button-1").removeClass('active');}
}
Adding more buttons
<Content>
Wrapping up
<Content>