-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.js
More file actions
77 lines (62 loc) · 2.35 KB
/
backend.js
File metadata and controls
77 lines (62 loc) · 2.35 KB
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
var apiKey = "D3E2BF871590A363DAC3E9B7BADA82A0"
var onPin = 7
var offPin = 6
var servoPin = 9
var mainString = "http://us01.proxy.teleduino.org/api/1.0/328.php?k={" + apiKey + "}&r="
var stringRequest = ""
function onLoad() {
document.getElementById("apiTxt").textContent = apiKey
document.getElementById("statusTxT").textContent = "Offline"
document.getElementById("onTxt").textContent = onPin
document.getElementById("offTxt").textContent = offPin
document.getElementById("servoTxt").textContent = servoPin
}
//funciones botones
function setup() {
stringRequest = mainString + "defineServo&servo=0&pin=" + servoPin
llamadaAPI(stringRequest)
stringRequest = mainString + "definePinMode&pin=" + onPin + "&mode=1"
llamadaAPI(stringRequest)
stringRequest = mainString + "definePinMode&pin=" + offPin + "&mode=1"
llamadaAPI(stringRequest)
}
function prenderLedOn() {
servo90()
stringRequest = mainString + "setDigitalOutput&pin=" + onPin + "&output=" + "1"
llamadaAPI(stringRequest)
stringRequest = mainString + "setDigitalOutput&pin=" + offPin + "&output=" + "0"
llamadaAPI(stringRequest)
}
function prenderLedOff() {
servo0()
stringRequest = mainString + "setDigitalOutput&pin=" + offPin + "&output=" + "1"
llamadaAPI(stringRequest)
stringRequest = mainString + "setDigitalOutput&pin=" + onPin + "&output=" + "0"
llamadaAPI(stringRequest)
}
function servo0() {
stringRequest = mainString + "setServo&servo=0&position=0"
llamadaAPI(stringRequest)
}
function servo90() {
stringRequest = mainString + "setServo&servo=0&position=90"
llamadaAPI(stringRequest)
}
//funcion que reliza llamadas a la API
function llamadaAPI() {
var request = new XMLHttpRequest()
// Open a new connection, using the GET request on the URL endpoint
request.open('GET', stringRequest, true)
request.onload = function () {
var data = JSON.parse(this.response)
if (request.status >= 200 && request.status < 400) {
console.log(data.status);
document.getElementById("statusTxT").textContent = "Online"
} else {
console.log('error');
document.getElementById("statusTxT").textContent = "Error"
}
}
// Send request
request.send()
}