-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_client.cpp
More file actions
80 lines (60 loc) · 1.68 KB
/
http_client.cpp
File metadata and controls
80 lines (60 loc) · 1.68 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
78
79
80
#include "http_client.h"
#include "config.h"
#include "crypto.h"
#include <WiFi.h>
#include <HTTPClient.h>
#include <vector>
struct OfflineData {
String uid;
String waktu;
};
static std::vector<OfflineData> offlineQueue;
bool sendData(const String& uid, const String& waktu) {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("[HTTP] FAIL: WiFi disconnected");
return false;
}
HTTPClient http;
String payload =
"{\"uid\":\"" + uid +
"\",\"device_id\":\"" + DEVICE_ID +
"\",\"waktu\":\"" + waktu + "\"}";
String sig = hmacSign(payload);
// 🔍 DEBUG HMAC
Serial.println("[HMAC] Payload:");
Serial.println(payload);
Serial.println("[HMAC] Signature:");
Serial.println(sig);
http.begin(API_URL);
http.addHeader("Content-Type", "application/json");
http.addHeader("X-Signature", sig);
int code = http.POST(payload);
http.end();
Serial.printf("[HTTP] Response Code = %d\n", code);
return code == 200;
}
void queueOffline(const String& uid, const String& waktu) {
offlineQueue.push_back({uid, waktu});
}
void syncOfflineQueue() {
if (WiFi.status() != WL_CONNECTED || offlineQueue.empty()) return;
for (auto &d : offlineQueue) {
sendData(d.uid, d.waktu);
}
offlineQueue.clear();
}
void testHttpBasic() {
HTTPClient http;
http.begin("http://192.168.1.100:8000/health");
int code = http.GET();
http.end();
Serial.printf("[TEST] HTTP GET /health => %d\n", code);
}
void testPostNoHmac() {
HTTPClient http;
http.begin("http://192.168.1.100:8000/test");
http.addHeader("Content-Type", "application/json");
int code = http.POST("{\"ping\":\"ok\"}");
http.end();
Serial.printf("[TEST] POST no HMAC => %d\n", code);
}