Skip to content

Commit 9a9bcd4

Browse files
Atualizando readme
1 parent c5987fb commit 9a9bcd4

File tree

8 files changed

+332
-1
lines changed

8 files changed

+332
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,14 @@ void setup() {
4747

4848
makeCache();
4949
showConfig();
50-
50+
51+
//
52+
API_AUTH = API_URL + "/auth";
53+
API_SEND_DATA = API_URL + "/device/" + device_id;
54+
//
55+
56+
requestAccess();
57+
5158
ticker.detach();
5259
digitalWrite(LED, LOW);
5360

@@ -62,6 +69,7 @@ void setup() {
6269
void loop() {
6370
delay(2000);
6471
sendData();
72+
yield();
6573
}
6674
```
6775

firmware/firmware.ino

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/************************** Inclusão das Bibliotecas **************************/
2+
3+
#include <WiFi.h>
4+
#include <WebServer.h>
5+
#include <Ticker.h>
6+
#include <HTTPClient.h>
7+
#include <Preferences.h>
8+
9+
#include <WiFiManager.h>
10+
11+
/************************* Variaveis globais **********************/
12+
13+
#define DEBUG true
14+
#define DEEP_SLEEP false
15+
16+
#define LED 2
17+
18+
String API_URL = "http://127.0.0.1:5000/api";
19+
String API_AUTH = "";
20+
String API_SEND_DATA = "";
21+
22+
char device_id[40];
23+
char device_token[70];
24+
char client_id[40];
25+
26+
char token[500];
27+
28+
bool shouldSaveConfig = false;
29+
30+
uint32_t sleep_time = 60 * 1000000; // intervalo de 1 minuto
31+
32+
33+
/**************************** DEBUG *******************************/
34+
35+
#if DEBUG
36+
#define DEBUG_PRINTLN(m) Serial.println(m)
37+
#define DEBUG_PRINT(m) Serial.print(m)
38+
39+
#else
40+
#define DEBUG_PRINTLN(m)
41+
#define DEBUG_PRINT(m)
42+
43+
#endif
44+
45+
/************************* Instanciação dos objetos **************************/
46+
47+
Ticker ticker;
48+
WiFiClient client;
49+
HTTPClient http;
50+
Preferences preferences;
51+
52+
/************************* Declaração dos Prototypes **************************/
53+
54+
void initSerial();
55+
void openStorage();
56+
void setupWifiManager();
57+
void requestAccess();
58+
bool sendData();
59+
void handleError(int httpCode , String message);
60+
61+
/********************************** Sketch ************************************/
62+
63+
void setup() {
64+
pinMode(LED, OUTPUT);
65+
ticker.attach(0.3, tick);
66+
67+
initSerial();
68+
openStorage();
69+
70+
setupWifiManager();
71+
72+
makeCache();
73+
showConfig();
74+
75+
//
76+
API_AUTH = API_URL + "/auth";
77+
API_SEND_DATA = API_URL + "/device/" + device_id;
78+
//
79+
80+
requestAccess();
81+
82+
ticker.detach();
83+
digitalWrite(LED, LOW);
84+
85+
#if DEEP_SLEEP
86+
sendData();
87+
closeStorage();
88+
DEBUG_PRINTLN("[ESP] Sleeping...");
89+
ESP.deepSleep(sleep_time);
90+
#endif
91+
}
92+
93+
void loop() {
94+
delay(2000);
95+
sendData();
96+
yield();
97+
}
98+

firmware/http.ino

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2+
void handleError(int httpCode , String message ) {
3+
DEBUG_PRINT("[HTTP] Code: " + String(httpCode));
4+
DEBUG_PRINTLN(" | Message: " + message);
5+
6+
if (message == "token_expired") {
7+
DEBUG_PRINTLN("[HTTP] requesting new token");
8+
requestAccess();
9+
}
10+
}
11+
12+
void requestAccess() {
13+
String p;
14+
15+
p += "{";
16+
p += "\"id\":";
17+
p += "\"37e73144-af71-4fde-93ab-98d1d6a6f83f\"";
18+
p += ",\"password\":";
19+
p += "\"f7a7329f391bc2a6d3141fb2b867cbdb637fed874aea8ae9c658418817a67ea4\"";
20+
p += "}";
21+
22+
http.begin(API_AUTH);
23+
24+
http.addHeader("content-type", "application/json");
25+
26+
int httpCode = http.POST(p);
27+
28+
String response = http.getString();
29+
http.end();
30+
31+
if (httpCode != HTTP_CODE_OK) {
32+
handleError(httpCode, response);
33+
return;
34+
}
35+
36+
setJWTToken(response);
37+
}
38+
39+
bool sendData() {
40+
ticker.attach(0.1, tick);
41+
42+
String p;
43+
p += "{";
44+
p += "\"snr_1\":";
45+
p += "\"0\"";
46+
p += ",\"snr_2\":";
47+
p += "\"0\"";
48+
p += ",\"snr_3\":";
49+
p += "\"0\"";
50+
p += ",\"snr_4\":";
51+
p += "\"0\"";
52+
p += ",\"snr_5\":";
53+
p += "\"0\"";
54+
p += ",\"snr_6\":";
55+
p += "\"0\"";
56+
p += ",\"snr_7\":";
57+
p += "\"0\"";
58+
p += ",\"snr_8\":";
59+
p += "\"0\"";
60+
p += "}";
61+
62+
http.begin(API_SEND_DATA);
63+
64+
http.addHeader("Authorization", token);
65+
http.addHeader("ClientID", client_id);
66+
67+
int httpCode = http.POST(p);
68+
69+
String response = http.getString();
70+
http.end();
71+
72+
ticker.detach();
73+
digitalWrite(LED, LOW);
74+
75+
if (httpCode != HTTP_CODE_OK) {
76+
handleError(httpCode, response);
77+
return false;
78+
}
79+
80+
DEBUG_PRINTLN("[HTTP] " + response);
81+
return true;
82+
}
83+

firmware/serial.ino

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
void initSerial() {
3+
#if DEBUG
4+
Serial.begin(115200);
5+
#endif
6+
}

firmware/storage.ino

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
void openStorage() {
3+
preferences.begin("esp-32", false);
4+
}
5+
6+
void closeStorage() {
7+
preferences.end();
8+
}
9+
10+
/************************* GET **********************/
11+
12+
void setDeviceToken(String str) {
13+
preferences.putString("device_token", str);
14+
}
15+
16+
void setDeviceID(String str) {
17+
preferences.putString("device_id", str);
18+
}
19+
20+
void setClientID(String str) {
21+
preferences.putString("client_id", str);
22+
}
23+
24+
void setJWTToken(String str) {
25+
preferences.putString("jwt_token", str);
26+
27+
strcpy(token, str.c_str());
28+
29+
DEBUG_PRINTLN("[HTTP] Token: " + String(token));
30+
}
31+
32+
/************************* SET **********************/
33+
34+
String getDeviceID() {
35+
String str = preferences.getString("device_id");
36+
37+
return str;
38+
}
39+
40+
String getDeviceToken() {
41+
String str = preferences.getString("device_token");
42+
43+
return str;
44+
}
45+
46+
String getClientID() {
47+
String str = preferences.getString("client_id");
48+
49+
return str;
50+
}
51+
52+
String getJWTToken() {
53+
String str = preferences.getString("jwt_token");
54+
55+
return str;
56+
}
57+
58+
void loadJWTToken() {
59+
strcpy(token, getJWTToken().c_str());
60+
}
61+
62+
void makeCache() {
63+
strcpy(device_id, getDeviceID().c_str());
64+
strcpy(device_token, getDeviceToken().c_str());
65+
strcpy(client_id, getClientID().c_str());
66+
67+
loadJWTToken();
68+
}
69+
70+
void showConfig() {
71+
DEBUG_PRINTLN("[CONFIG] API: " + API_URL);
72+
DEBUG_PRINTLN("[CONFIG] DeviceID: " + String(device_id));
73+
DEBUG_PRINTLN("[CONFIG] DeviceToken: " + String(device_token));
74+
DEBUG_PRINTLN("[CONFIG] ClientID: " + String(client_id));
75+
}
76+

firmware/wifi.ino

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
void tick()
2+
{
3+
int state = digitalRead(LED);
4+
digitalWrite(LED, !state);
5+
}

firmware/wifimanager.ino

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
void configModeCallback (WiFiManager *myWiFiManager) {
3+
ticker.attach(0.2, tick);
4+
}
5+
6+
void saveConfigCallback () {
7+
DEBUG_PRINTLN("Should save config");
8+
shouldSaveConfig = true;
9+
}
10+
11+
void setupWifiManager () {
12+
WiFiManagerParameter custom_device_id("device_id", "DeviceID", device_id, 40);
13+
WiFiManagerParameter custom_device_token("device_token", "DeviceToken", device_token, 70);
14+
WiFiManagerParameter custom_client_id("client_id", "ClientID", client_id, 40);
15+
16+
WiFiManager wifiManager;
17+
18+
wifiManager.setSaveConfigCallback(saveConfigCallback);
19+
wifiManager.setAPCallback(configModeCallback);
20+
21+
wifiManager.addParameter(&custom_device_id);
22+
wifiManager.addParameter(&custom_device_token);
23+
wifiManager.addParameter(&custom_client_id);
24+
25+
if (!wifiManager.autoConnect("ESP-32")) {
26+
DEBUG_PRINTLN("failed to connect and hit timeout");
27+
delay(3000);
28+
29+
ESP.restart();
30+
delay(5000);
31+
}
32+
33+
DEBUG_PRINTLN("[WIFI] Connected");
34+
35+
strcpy(device_id, custom_device_id.getValue());
36+
strcpy(device_token, custom_device_token.getValue());
37+
strcpy(client_id, custom_client_id.getValue());
38+
39+
if (shouldSaveConfig) {
40+
DEBUG_PRINTLN("saving config");
41+
42+
DEBUG_PRINTLN("DeviceID" + String(device_id));
43+
DEBUG_PRINTLN("DeviceToken" + String(device_token));
44+
DEBUG_PRINTLN("ClientID" + String(client_id));
45+
46+
setDeviceID(device_id);
47+
setDeviceToken(device_token);
48+
setClientID(client_id);
49+
}
50+
51+
DEBUG_PRINT("[WIFI] IP: ");
52+
DEBUG_PRINTLN(WiFi.localIP());
53+
}
54+

0 commit comments

Comments
 (0)