-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathco.ino
More file actions
153 lines (123 loc) · 3.99 KB
/
Copy pathco.ino
File metadata and controls
153 lines (123 loc) · 3.99 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
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
#include <WiFi.h>
#include <HTTPClient.h>
#include <Base64.h>
#include <ArduinoJson.h>
#include <time.h>
const char* ssid = "xxxxxxxxx";
const char* password = "xxxxxxxx";
// Konfigurasi GitHub
const char* serverUrl = "https://api.github.com/repos/your-github-username/your-repo/contents/data.json";
const char* token = "Bearer Fine-grained personal access tokens";
// Konfigurasi waktu
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 25200; // GMT+7 untuk Indonesia
const int daylightOffset_sec = 0;
// Pin sensor
const int mq7Pin = 32;
// Variabel untuk tracking perubahan data
float lastPpm = 0;
String lastSHA = "";
unsigned long lastUpdateTime = 0;
const unsigned long UPDATE_INTERVAL = 5000; // Update setiap 5 detik
String getCurrentSHA() {
HTTPClient http;
String sha = "";
http.begin(serverUrl);
http.addHeader("Authorization", token);
http.addHeader("Cache-Control", "no-cache");
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
StaticJsonDocument<1024> doc;
DeserializationError error = deserializeJson(doc, http.getString());
if (!error) {
sha = doc["sha"].as<String>();
Serial.println("Current SHA: " + sha);
}
}
http.end();
return sha;
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
int wifiTimeout = 0;
while (WiFi.status() != WL_CONNECTED && wifiTimeout < 20) {
delay(500);
Serial.print(".");
wifiTimeout++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("\nConnected to WiFi");
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
} else {
Serial.println("\nFailed to connect WiFi");
ESP.restart();
}
}
String getFormattedTime() {
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
return "Failed to obtain time";
}
char timeStringBuff[50];
strftime(timeStringBuff, sizeof(timeStringBuff), "%Y-%m-%d %H:%M:%S", &timeinfo);
return String(timeStringBuff);
}
void updateGitHub(float ppm, int sensorValue, String timestamp) {
HTTPClient http;
http.begin(serverUrl);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", token);
http.addHeader("Cache-Control", "no-cache");
// Buat JSON data
DynamicJsonDocument doc(512);
doc["timestamp"] = timestamp;
JsonObject data = doc.createNestedObject("data");
data["ppm"] = ppm;
data["raw_value"] = sensorValue;
String jsonStr;
serializeJsonPretty(doc, jsonStr);
String encodedData = base64::encode(jsonStr);
String payload = "{\n";
payload += " \"message\": \"Update data.json - " + timestamp + "\",\n";
payload += " \"content\": \"" + encodedData + "\",\n";
payload += " \"sha\": \"" + lastSHA + "\"\n";
payload += "}";
int httpResponseCode = http.PUT(payload);
if (httpResponseCode == HTTP_CODE_OK || httpResponseCode == HTTP_CODE_CREATED) {
String response = http.getString();
StaticJsonDocument<1024> respDoc;
deserializeJson(respDoc, response);
lastSHA = respDoc["content"]["sha"].as<String>();
Serial.println("Data updated successfully!");
Serial.println("Time: " + timestamp);
Serial.println("PPM: " + String(ppm));
Serial.println("Raw Value: " + String(sensorValue));
} else {
Serial.printf("Update failed: %d\n", httpResponseCode);
}
http.end();
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi disconnected. Reconnecting...");
WiFi.reconnect();
delay(5000);
return;
}
unsigned long currentTime = millis();
if (currentTime - lastUpdateTime >= UPDATE_INTERVAL) {
int sensorValue = analogRead(mq7Pin);
float ppm = sensorValue / 1023.0 * 100;
// Update hanya jika ada perubahan signifikan atau belum ada SHA
if (abs(ppm - lastPpm) >= 0.1 || lastSHA == "") {
String timestamp = getFormattedTime();
if (lastSHA == "") {
lastSHA = getCurrentSHA();
}
updateGitHub(ppm, sensorValue, timestamp);
lastPpm = ppm;
}
lastUpdateTime = currentTime;
}
}