-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgauge.js
203 lines (168 loc) · 5.81 KB
/
gauge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//window.onload = setInterval(AnalogRead('A0','Moisture'),2000)
var api_key = "19eb0377-6eee-470d-a011-a6adbe3a11fc"
var d_name = "BOLT10779429"
CanvasRenderingContext2D.prototype.roundRect =
function(x, y, width, height, radius, fill, stroke) {
if (typeof stroke == "undefined" ) {
stroke = true;
}
if (typeof radius === "undefined") {
radius = 5;
}
this.beginPath();
this.moveTo(x + radius, y);
this.lineTo(x + width - radius, y);
this.quadraticCurveTo(x + width, y, x + width, y + radius);
this.lineTo(x + width, y + height - radius);
this.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
this.lineTo(x + radius, y + height);
this.quadraticCurveTo(x, y + height, x, y + height - radius);
this.lineTo(x, y + radius);
this.quadraticCurveTo(x, y, x + radius, y);
this.closePath();
if (stroke) {
this.stroke();
}
if (fill) {
this.fill();
}
}
var xmlhttp = new XMLHttpRequest();
//get the body element and set background color
var body = document.getElementsByTagName("body")[0];
document.body.style.backgroundColor = "#346473";
while (body.firstChild) {
body.removeChild(body.firstChild);
}
function setup(cno,size,x=0,y=0){
//creating a canvas element for the html page
var canvas = document.createElement('canvas');
canvas.id = "canvas" + cno.toString();
canvas.width = size;
canvas.height = size;
canvas.style.left = x.toString() + "px"
canvas.style.top = x.toString() + "px"
body.appendChild(canvas);
//get the context variable
var ctx = canvas.getContext('2d');
ctx.canvas.addEventListener("click", function(e) { // use event argument
var rect = canvas.getBoundingClientRect(); // get element's abs. position
var x = e.clientX - rect.left; // get mouse x and adjust for el.
var y = e.clientY - rect.top;
var size = canvas.width;
x = x/size;
y = y/size;
if (x>0.15 && x<0.85 && y>0.8 && y<0.94){
switch(canvas.id){
case "canvas1":
console.log("recorded button1 press")
DigitalWrite("0", "HIGH");
setTimeout(function() {
DigitalWrite("0", "LOW");},5000);
break;
case "canvas2":
console.log("recorded button2 press")
DigitalWrite("1", "HIGH");
setTimeout(function() {
DigitalWrite("1", "LOW");},5000);
break;
}
}
});
}
function degtorad(degree) {
var factor = Math.PI/180;
return degree*factor;
}
function renderGauge (cno,axisText,btnText,size,reading,x=0,y=0) {
var canvas = document.getElementById("canvas" + cno.toString())
var ctx = canvas.getContext('2d');
if(typeof reading == "undefined"){
reading = 0;
}
else{
}
//set background color
ctx.fillStyle = '#346473';
ctx.fillRect(x,y,1*size,1*size);
//outline of the gauge
ctx.beginPath();
ctx.strokeStyle = '#9bd546';
ctx.lineWidth = 1;
ctx.arc(x+(0.5*size),y+(0.5*size),(0.4*size),degtorad(145),degtorad(35));
ctx.stroke();
//progress bar
ctx.beginPath();
ctx.lineWidth = 0.06*size;
ctx.arc(x+(0.5*size),y+(0.5*size),(0.37*size),degtorad(145),degtorad(145 + reading*0.244140625)); //instead of 270 the reading of sensor
ctx.stroke();
//percentage label
var percentage = Math.round(reading / 1024 * 100);
var percentText = percentage.toString() + "%";
ctx.font= (0.144*size).toString() + "px Nexa Light";
ctx.fillStyle = "#FFFFFF";
ctx.textAlign="center";
ctx.fillText(percentText,x+(0.5*size),y+(0.6*size));
//axis label
ctx.font= (0.12*size).toString() + "px Nexa Light";
ctx.fillStyle = "#FFFFFF";
ctx.textAlign="center";
ctx.fillText(axisText,x+(0.5*size),y+(0.45*size));
//button
ctx.lineWidth = 1;
ctx.fillStyle = "#9bd546";
ctx.roundRect(x+(0.15*size),y+(0.8*size),(0.7*size),(0.14*size),(0.02*size),1);
//button text
ctx.font= (0.096*size).toString() + "px Nexa Light";
ctx.fillStyle = "#FFFFFF";
ctx.textAlign="center";
ctx.fillText(btnText,x+(0.5*size),y+(0.9*size));
}
function AnalogRead(pin, sensortype) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
console.log(xmlhttp.status)
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var obj = JSON.parse(xmlhttp.responseText);
console.log(obj.success);
if(obj.success == "1") {
if (sensortype == "Moisture"){
renderGauge(1,sensortype,"POUR WATER",500,parseInt(obj.value));
}
else if(sensortype == "Luminosity"){
renderGauge(2,sensortype,"SHINE LIGHT",500,parseInt(obj.value));
}
}
else{
renderGauge(1,"Moisture","POUR WATER",500,0);
renderGauge(2,"Luminosity","SHINE LIGHT",500,0);
}
}
};
xmlhttp.open("GET","http://cloud.boltiot.com/remote/"+api_key+"/analogRead?pin="+pin+"&deviceName="+d_name,true);
xmlhttp.send();
}
function DigitalWrite(pin,state) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
console.log(xmlhttp.responseText.length)
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var obj = JSON.parse(xmlhttp.responseText);
if(obj.success == "1") {
console.log("GPIO state change succesfull")
}
}
};
xmlhttp.open("GET","http://cloud.boltiot.com/remote/" + api_key + "/digitalWrite?pin=" + pin + "&state=" + state + "&deviceName=" + d_name,true);
xmlhttp.send();
}
function onloadWindow(){
setup(1,500);
setup(2,500);
renderGauge(1,'Moisture',"POUR WATER",500,0);
renderGauge(2,'Luminosity',"SHINE LIGHT",500,0);
setInterval(function(){
AnalogRead('A0','Moisture');
AnalogRead('A0','Luminosity');
}, 1000);
}