-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbikebutton.ino
100 lines (78 loc) · 2.07 KB
/
bikebutton.ino
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
#include <ezButton.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
// NOTE: please create a local file with the following variables
// const char* ssid = "...";
// const char* password = "...";
#include "wifi_credentials.h"
#include "request_data.h"
//// Button
#define VALIDATION_TIME 2000
ezButton button(23); // create ezButton object that attach to pin GIOP21
unsigned long pressedTime = 0;
unsigned long releasedTime = 0;
unsigned long numberOfTimes = 0;
bool sendEvent = true;
String response = "";
DynamicJsonDocument doc(2048);
void setup() {
Serial.begin(9600);
button.setDebounceTime(50); // set debounce time to 50 milliseconds
//Initiate WiFi connection
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
// Wait for connection
Serial.print("\ntrying to connect to Wifi!\n");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("WiFi connected with IP: ");
Serial.println(WiFi.localIP());
}
void send() {
HTTPClient http;
Serial.println("Button was pressed: " + String(numberOfTimes));
String request = "https://fixms.webfoo.de/api/reports";
http.begin(request);
http.addHeader("Content-Type", "application/json");
if (numberOfTimes == 1) {
http.POST(jsonSendData1);
}
else if (numberOfTimes == 2) {
http.POST(jsonSendData2);
}
else {
http.POST(jsonSendData3);
}
sendEvent = true;
numberOfTimes = 0;
// TODO: fix bug
DeserializationError error = deserializeJson(doc, response);
if(error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
http.end();
return;
}
Serial.println(doc["value"].as<const char*>());
http.end();
}
void loop() {
button.loop(); // MUST call the loop() function first
if (button.isPressed()) {
pressedTime = millis();
}
else if (button.isReleased()) {
releasedTime = millis();
sendEvent = false;
numberOfTimes += 1;
if (numberOfTimes == 3) {
send();
}
}
if (millis() - releasedTime >= VALIDATION_TIME && !sendEvent) {
send();
}
}