-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBotão
137 lines (108 loc) · 3.31 KB
/
Botão
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
#define LARGE_JSON_BUFFERS 1
#include <Arduino.h>
#include <ESPDateTime.h>
#include <Thing.h>
#include <WebThingAdapter.h>
char *ssid="insere_nome_roteador";
char *password="insere_senha";
const uint8_t buttonPin = 4;
WebThingAdapter *adapter;
//https://webthings.io/schemas/#PushButton
const char *remoteTypes[] = {"PushButton", nullptr};
ThingDevice remote("urn:dev:ops:mypushbutton", "Push Button I", remoteTypes);
ThingEvent pressed("pressed",
"PressedEvent",
BOOLEAN, "PressedEvent");
ThingProperty button("button", "PushButton", BOOLEAN, "PushedProperty");
int ledAzul=13;
int ledVermelho=15;
void setup(void)
{
pinMode(buttonPin, INPUT);
pinMode(ledVermelho, OUTPUT);
pinMode(ledAzul, OUTPUT);
Serial.begin(115200);
Serial.setDebugOutput(true);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
digitalWrite(ledVermelho, HIGH); // ACENDE LED VERMELHO ENQUANTO ESTIVER PROCURANDO REDE
}
digitalWrite(ledVermelho, LOW); // APAGA LED VERMELHO
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
adapter = new WebThingAdapter("push-button", WiFi.localIP());
remote.description = "A Webthings PushButton";
button.title = "PushButton";
remote.addProperty(&button);
remote.addEvent(&pressed);
adapter->addDevice(&remote);
adapter->begin();
Serial.println("HTTP server started");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.print("/things/");
Serial.println(remote.id);
randomSeed(analogRead(0));
DateTime.begin();
if (!DateTime.isTimeValid()) {
Serial.println("Failed to get time from server.");
}
}
unsigned long oldMillis = 0;
bool lastTest = false;
void loop(void)
{
if (digitalRead(4)==LOW) {
// botão apertado
digitalWrite(ledAzul, HIGH);
if (lastTest == false) {
// mudança de estado de não apertado para apertado
lastTest=true;
if (!DateTime.isTimeValid()) {
Serial.println("Failed to get time from server, retry.");
DateTime.begin();
} else {
// Serial.println("Force Event!");
oldMillis = millis();
button.setValue({.boolean = lastTest});
ThingEventObject *ev = new ThingEventObject(
"pressed",
BOOLEAN,
{.boolean = true},
DateTime.formatUTC(DateFormatter::ISO8601)
);
remote.queueEventObject(ev);
Serial.println("Mudou para pressionado.");
}
}
} else {
// botão solto
digitalWrite(ledAzul, LOW);
if (lastTest == true) {
// mudança de estado de apertado para não apertado
lastTest=false;
if (!DateTime.isTimeValid()) {
Serial.println("Failed to get time from server, retry.");
DateTime.begin();
} else {
// Serial.println("Force Event!");
oldMillis = millis();
button.setValue({.boolean = lastTest});
ThingEventObject *ev = new ThingEventObject(
"pressed",
BOOLEAN,
{.boolean = true},
DateTime.formatUTC(DateFormatter::ISO8601)
);
remote.queueEventObject(ev);
Serial.println("Mudou para solto");
}
}
}
adapter->update();
}