Create an LCD screen with the HTML Canvas
This example shows how to create an LCD screen on a dashboard by using the HTML Canvas widget.
HTML:
<div id="image_background">
<p>Last value:</p>
<span id="image_background_text">No data</span>
</div>
CSS:
@import url('https://fonts.googleapis.com/css?family=VT323');
#image_background {
background: url('https://i.imgur.com/uuYygjz.png') 0 / contain no-repeat;
font-family: "VT323";
font-size: 25px;
letter-spacing: 10.5px;
height: 220px;
width: 450px;
}
#image_background p, #image_background_text {
margin: 0;
left: 75px;
position: absolute;
}
#image_background p {
top: 88px;
font-size: 25px !important;
}
#image_background_text {
top: 121px;
}
JavaScript:
In the following code, the VAR_ID variable must be set according to the variable that should be displayed:
var bg = document.getElementById("image_background");
var text = document.getElementById("image_background_text");
var lastValue = null;
var TOKEN = ubidots.token;
var VAR_ID = "variable-id";
setInterval(function () { getLastValue(VAR_ID, TOKEN); }, 2000);
function makeHttpRequest(method, url, headers, payload) {
return new Promise(function (resolve, reject) {
var options = {
method: method,
headers: headers,
};
if (payload) {
options.body = JSON.stringify(payload);
}
fetch(url, options)
.then(function (response) {
if (!response.ok) {
throw new Error(response.statusText);
}
return response.json();
})
.then(function (data) {
resolve(data);
})
.catch(function (error) {
reject(error);
});
});
}
function changeImage(value) {
var choose = "normal";
var background = {
blue: "https://i.imgur.com/ZE0W7Yx.png",
normal: "https://i.imgur.com/uuYygjz.png",
yellow: "https://i.imgur.com/RHvDkUE.png",
};
if (value <= 30) {
choose = "blue";
bg.style.color = "white";
} else if (value > 30 && value <= 70) {
choose = "yellow";
bg.style.color = "white";
}
bg.style.background = "url(" + background[choose] + ") 0 / contain no-repeat";
}
function getLastValue(variableId, token) {
var url =
"https://industrial.api.ubidots.com/api/v1.6/variables/" +
variableId +
"/values";
var headers = {
"X-Auth-Token": token,
"Content-Type": "application/json",
};
makeHttpRequest("GET", url + "?page_size=1", headers)
.then(function (res) {
if (lastValue === null || res.results[0].value !== lastValue) {
text.textContent = res.results[0].value;
lastValue = res.results[0].value;
changeImage(lastValue);
}
})
.catch(function (error) {
console.error(error);
});
}
Once the settings are saved, it will look like this:
Adjusting the screen's contrast
Modify the HTML to:
<div id="image_background">
<img id="display_background" src="https://i.imgur.com/meoKhMG.png" />
<p>Last value:</p>
<span id="image_background_text">No data</span>
</div>
Modify the CSS code to:
@import url('https://fonts.googleapis.com/css?family=VT323');
#image_background {
background: url('https://i.imgur.com/ehNdUxY.png') 0 / contain no-repeat;
font-family: "VT323";
font-size: 25px;
letter-spacing: 10.5px;
height: 220px;
width: 450px;
}
#image_background p, #image_background_text {
margin: 0;
left: 75px;
position: absolute;
}
#image_background p {
top: 88px;
font-size: 25px !important;
}
#image_background_text {
top: 121px;
}
#display_background {
height: 94px;
position: absolute;
top: 70px;
left: 52px;
pointer-events: none;
}
Modify the JavaScript code to:
var bg = document.getElementById("display_background");
var text = document.getElementById("image_background_text");
var lastValue = null;
var lastContrast = null;
var TOKEN = ubidots.token;
var VAR_ID_LV = "id-of-variable-to-be-displayed";
var VAR_ID_CONTRAST = "id-of-contrast-variable";
var scale = d3.scaleLinear().range([0, 4]).domain([0, 1023]);
setInterval(function () {
// Get LCD Screen last value
getLastValue(VAR_ID_LV, TOKEN, function (res) {
var value = null;
try {
value = res.results[0].value;
} catch (e) {
console.log("No data");
}
if (
(lastValue === null || value !== lastValue) &&
value !== null
) {
text.textContent = value;
lastValue = value;
}
});
// Get LCD Screen contrast
getLastValue(VAR_ID_CONTRAST, TOKEN, function (res) {
var value = null;
try {
value = res.results[0].value;
} catch (e) {
console.log("No data");
}
if (
(lastContrast === null || value !== lastContrast) &&
value !== null
) {
bg.style.filter = "contrast(" + scale(value) + ")";
lastContrast = value;
}
});
}, 2000);
function makeHttpRequest(method, url, headers, payload) {
return new Promise(function (resolve, reject) {
var options = {
method: method,
headers: headers,
};
if (payload) {
options.body = JSON.stringify(payload);
}
fetch(url, options)
.then(function (response) {
if (!response.ok) {
throw new Error(response.statusText);
}
return response.json();
})
.then(function (data) {
resolve(data);
})
.catch(function (error) {
reject(error);
});
});
}
function getLastValue(variableId, token, cb) {
var url =
"https://industrial.api.ubidots.com/api/v1.6/variables/" +
variableId +
"/values";
var headers = {
"X-Auth-Token": token,
"Content-Type": "application/json",
};
makeHttpRequest("GET", url + "?page_size=1", headers)
.then(function (res) {
if (typeof cb === "function") cb(res);
})
.catch(function (error) {
console.error(error);
});
}
Lastly, add another 3rd party library needed for this implementation:
https://cdnjs.cloudflare.com/ajax/libs/d3/4.9.0/d3.min.js
This is the result:
Last updated