Skip to content

Commit bda3cde

Browse files
committed
Getting data from UART
1 parent cbcfb28 commit bda3cde

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/main.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ typedef enum {
1212
ESP_NO_CONNECTED,
1313
ESP_CONNECTED,
1414
ESP_SENDING_DATA,
15-
ESP_NET_ERROR,
1615
ESP_UART_ERROR,
1716
} ESP_Status_t;
1817

@@ -28,8 +27,6 @@ String ESP_GetErrorAsString(ESP_Status_t status) {
2827
return "ESP_CONNECTED";
2928
case ESP_SENDING_DATA:
3029
return "ESP_SENDING_DATA";
31-
case ESP_NET_ERROR:
32-
return "ESP_NET_ERROR";
3330
case ESP_UART_ERROR:
3431
return "ESP_UART_ERROR";
3532
default:
@@ -46,7 +43,7 @@ const char *UBIDOTS_TOKEN = "BBFF-Pc8IvgnCXtKOaQ1lwUfq5oypeSU5AW";
4643
// Device label
4744
const char *DEVICE_LABEL = "esp32-edu-ciaa";
4845
// Variable labels
49-
const char *VARIABLE_LABELS[4] = {"temperature", "soil-humidity", "light", "air-humidity"};
46+
const char *VARIABLE_LABELS[4] = {"temperature", "air-humidity", "light", "soil-humidity"};
5047

5148
// Message received from the UART
5249
String message;
@@ -113,19 +110,26 @@ void loop() {
113110
void uartHandler() {
114111
ESP_STATUS = ESP_SENDING_DATA;
115112
// Read the message from the UART
116-
message = SerialPort.readString();
117-
Serial.printf("Message received: %s");
113+
int i = 0;
114+
while (SerialPort.available() && SerialPort.peek() != '\n') {
115+
message += (char) SerialPort.read();
116+
i++;
117+
}
118+
SerialPort.read();
118119
// Get the type of the variable, and convert it to an integer
119120
const int index = message[0] - '0';
120121
// Check if the type is valid, and if the index is in range
121122
if (index >= 0 && index < 4) {
123+
const int value = message.substring(1).toInt();
124+
Serial.printf("Variable: %s, Value: %d\n", VARIABLE_LABELS[index], value);
122125
// Add the value to the Ubidots client
123-
client.add(VARIABLE_LABELS[index], message.substring(1).toFloat());
126+
client.add(VARIABLE_LABELS[index], value);
124127
// Publish the data to the Ubidots MQTT broker
125128
client.publish(DEVICE_LABEL);
126129
} else {
127130
Serial.println("Error: Invalid type, index out of range");
128131
ESP_STATUS = ESP_UART_ERROR;
129132
}
133+
message = "";
130134
}
131135

0 commit comments

Comments
 (0)