Skip to content

Commit cbcfb28

Browse files
committed
Adding state to ESP
1 parent c009a9b commit cbcfb28

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/main.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,36 @@
77
#define SERIAL_RX 16
88
#define SERIAL_TX 17
99

10+
typedef enum {
11+
ESP_INIT,
12+
ESP_NO_CONNECTED,
13+
ESP_CONNECTED,
14+
ESP_SENDING_DATA,
15+
ESP_NET_ERROR,
16+
ESP_UART_ERROR,
17+
} ESP_Status_t;
18+
19+
ESP_Status_t ESP_STATUS = ESP_INIT;
20+
21+
String ESP_GetErrorAsString(ESP_Status_t status) {
22+
switch (status) {
23+
case ESP_INIT:
24+
return "ESP_INIT";
25+
case ESP_NO_CONNECTED:
26+
return "ESP_NO_CONNECTED";
27+
case ESP_CONNECTED:
28+
return "ESP_CONNECTED";
29+
case ESP_SENDING_DATA:
30+
return "ESP_SENDING_DATA";
31+
case ESP_NET_ERROR:
32+
return "ESP_NET_ERROR";
33+
case ESP_UART_ERROR:
34+
return "ESP_UART_ERROR";
35+
default:
36+
return "ESP_UNKNOWN_ERROR";
37+
}
38+
}
39+
1040
// WiFi credentials
1141
const char *WIFI_SSID = "Fibertel WiFi839 2.4Ghz"; // Enter your WiFi name
1242
const char *WIFI_PASSWORD = "00496026574"; // Enter WiFi password
@@ -69,6 +99,7 @@ void callback(char *topic, byte *payload, unsigned int length) {
6999

70100
void loop() {
71101
if (!client.connected()) {
102+
ESP_STATUS = ESP_NO_CONNECTED;
72103
client.reconnect();
73104
}
74105

@@ -80,6 +111,8 @@ void loop() {
80111
}
81112

82113
void uartHandler() {
114+
ESP_STATUS = ESP_SENDING_DATA;
115+
// Read the message from the UART
83116
message = SerialPort.readString();
84117
Serial.printf("Message received: %s");
85118
// Get the type of the variable, and convert it to an integer
@@ -92,6 +125,7 @@ void uartHandler() {
92125
client.publish(DEVICE_LABEL);
93126
} else {
94127
Serial.println("Error: Invalid type, index out of range");
128+
ESP_STATUS = ESP_UART_ERROR;
95129
}
96130
}
97131

0 commit comments

Comments
 (0)