Skip to content

Commit 9636d23

Browse files
committed
Sending data every 15ms for test purpose
1 parent bda3cde commit 9636d23

File tree

2 files changed

+91
-15
lines changed

2 files changed

+91
-15
lines changed

lib/uart/uart.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include "ubidots.h"
2+
#include "uart.h"
3+
4+
#ifndef UART_NUMBER
5+
#warning "UART_NUMBER not defined, using default UART 2"
6+
#define UART_NUMBER 2
7+
#endif
8+
// Serial port pins
9+
#ifndef SERIAL_RX
10+
#warning "SERIAL_RX not defined, using default pin 16"
11+
#define SERIAL_RX 16
12+
#endif
13+
14+
#ifndef SERIAL_TX
15+
#warning "SERIAL_TX not defined, using default pin 17"
16+
#define SERIAL_TX 17
17+
#endif
18+
19+
// Message received from the UART
20+
String message;
21+
22+
// Hardware serial port to communicate with the EDU-CIAA
23+
HardwareSerial SerialPort(UART_NUMBER);
24+
25+
void uartInit() {
26+
Serial.begin(115200);
27+
// Initialize the serial port
28+
SerialPort.begin(115200, SERIAL_8N1, SERIAL_RX, SERIAL_TX);
29+
// Set the callback function to be called when a message is received
30+
SerialPort.onReceive(uartHandler);
31+
// Enable the serial port to receive data
32+
// SerialPort.listen();
33+
}
34+
35+
void uartHandler() {
36+
setStatus(ESP_READING_DATA);
37+
// Read the message from the UART
38+
int i = 0;
39+
Serial.println("Reading data from UART");
40+
while (SerialPort.available() && SerialPort.peek() != '\n') {
41+
message += (char) SerialPort.read();
42+
i++;
43+
}
44+
SerialPort.read();
45+
// Get the type of the variable, and convert it to an integer
46+
const int index = message[0] - '0';
47+
const int value = message.substring(1).toInt();
48+
// Publish the data to the Ubidots MQTT broker
49+
Serial.printf("Publishing data:\r\nIndex: %d, Value: %d\r\n", index, value);
50+
publishData(index, value);
51+
// Clear the message
52+
message = "";
53+
}
54+
55+
void sendToUart(String message) {
56+
SerialPort.println(message);
57+
}
58+
59+
void sendToUart(char *message) {
60+
SerialPort.println(message);
61+
}

src/main.cpp

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,29 @@
99

1010
typedef enum {
1111
ESP_INIT,
12-
ESP_NO_CONNECTED,
1312
ESP_CONNECTED,
13+
ESP_DISCONNECTED,
14+
ESP_READING_DATA,
1415
ESP_SENDING_DATA,
15-
ESP_UART_ERROR,
16+
ESP_DATA_ERROR,
1617
} ESP_Status_t;
1718

1819
ESP_Status_t ESP_STATUS = ESP_INIT;
1920

20-
String ESP_GetErrorAsString(ESP_Status_t status) {
21+
static String ESP_GetErrorAsString(ESP_Status_t status) {
2122
switch (status) {
2223
case ESP_INIT:
2324
return "ESP_INIT";
24-
case ESP_NO_CONNECTED:
25-
return "ESP_NO_CONNECTED";
2625
case ESP_CONNECTED:
2726
return "ESP_CONNECTED";
27+
case ESP_DISCONNECTED:
28+
return "ESP_DISCONNECTED";
29+
case ESP_READING_DATA:
30+
return "ESP_READING_DATA";
2831
case ESP_SENDING_DATA:
2932
return "ESP_SENDING_DATA";
30-
case ESP_UART_ERROR:
31-
return "ESP_UART_ERROR";
33+
case ESP_DATA_ERROR:
34+
return "ESP_DATA_ERROR";
3235
default:
3336
return "ESP_UNKNOWN_ERROR";
3437
}
@@ -43,14 +46,17 @@ const char *UBIDOTS_TOKEN = "BBFF-Pc8IvgnCXtKOaQ1lwUfq5oypeSU5AW";
4346
// Device label
4447
const char *DEVICE_LABEL = "esp32-edu-ciaa";
4548
// Variable labels
46-
const char *VARIABLE_LABELS[4] = {"temperature", "air-humidity", "light", "soil-humidity"};
49+
const char *VARIABLE_LABELS[4] = {"temperature", "air_humidity", "light", "soil_humidity"};
4750

4851
// Message received from the UART
4952
String message;
5053

5154
// Ubidots client
5255
Ubidots client(UBIDOTS_TOKEN);
5356

57+
unsigned long timer;
58+
const int PUBLISH_FREQUENCY = 15000;
59+
5460
// Hardware serial port to communicate with the EDU-CIAA
5561
HardwareSerial SerialPort(UART);
5662

@@ -76,9 +82,11 @@ void setup() {
7682
// Connecting to a WiFi network
7783
client.connectToWifi(WIFI_SSID, WIFI_PASSWORD);
7884
// Connecting to a mqtt broker
85+
client.setDebug(true);
7986
client.setCallback(callback);
8087
client.setup();
8188
client.reconnect();
89+
timer = millis();
8290
}
8391

8492
void callback(char *topic, byte *payload, unsigned int length) {
@@ -96,7 +104,7 @@ void callback(char *topic, byte *payload, unsigned int length) {
96104

97105
void loop() {
98106
if (!client.connected()) {
99-
ESP_STATUS = ESP_NO_CONNECTED;
107+
ESP_STATUS = ESP_DISCONNECTED;
100108
client.reconnect();
101109
}
102110

@@ -120,15 +128,22 @@ void uartHandler() {
120128
const int index = message[0] - '0';
121129
// Check if the type is valid, and if the index is in range
122130
if (index >= 0 && index < 4) {
131+
ESP_STATUS = ESP_SENDING_DATA;
132+
const char* variableLabel = VARIABLE_LABELS[index];
123133
const int value = message.substring(1).toInt();
124-
Serial.printf("Variable: %s, Value: %d\n", VARIABLE_LABELS[index], value);
125134
// Add the value to the Ubidots client
126-
client.add(VARIABLE_LABELS[index], value);
127-
// Publish the data to the Ubidots MQTT broker
128-
client.publish(DEVICE_LABEL);
135+
client.add(variableLabel, value);
136+
long diff = millis() - timer;
137+
diff = diff < 0 ? -diff : diff;
138+
if (diff > PUBLISH_FREQUENCY) {
139+
// Publish the data to the Ubidots MQTT broker
140+
client.publish(DEVICE_LABEL);
141+
timer = millis();
142+
}
143+
ESP_STATUS = ESP_CONNECTED;
129144
} else {
130-
Serial.println("Error: Invalid type, index out of range");
131-
ESP_STATUS = ESP_UART_ERROR;
145+
Serial.println("Error: Invalid type, index out of range\r\n");
146+
ESP_STATUS = ESP_DATA_ERROR;
132147
}
133148
message = "";
134149
}

0 commit comments

Comments
 (0)