2
2
#include < HardwareSerial.h>
3
3
#include " wifi_credentials.h"
4
4
5
+ // Baud rate to communicate with the EDU-CIAA
6
+ #define BAUD_RATE 115200
5
7
// Serial port to communicate with the EDU-CIAA
6
- #define UART 2
8
+ #define DATA_UART 2
7
9
// Serial port pins
8
10
#define SERIAL_RX 16
9
11
#define SERIAL_TX 17
10
12
13
+ /*
14
+ * Frequency to publish the data to the Ubidots MQTT broker.
15
+ * The data is published every FREQUENCY_TO_PUBLISH minutes, after
16
+ * the last data was published or the last time the data was read from the UART.
17
+ * For testing purposes, you can set this value to 1 (or minor).
18
+ */
19
+ #define FREQUENCY_TO_PUBLISH_IN_M 10
20
+ #define FREQUENCY_TO_PUBLISH_IN_MS (FREQUENCY_TO_PUBLISH_IN_M * 60 * 1000 )
21
+
11
22
typedef enum {
12
23
ESP_INIT,
13
24
ESP_CONNECTED,
@@ -45,7 +56,7 @@ const char *WIFI_PASSWORD = WIFI_PASSWORD_SECRETS;
45
56
// Ubidots TOKEN
46
57
const char *UBIDOTS_TOKEN = UBIDOTS_TOKEN_SECRETS;
47
58
// Device label
48
- const char *DEVICE_LABEL = " esp32-edu-ciaa " ;
59
+ const char *DEVICE_LABEL = DEVICE_LABEL_SECRETS ;
49
60
// Variable labels
50
61
const char *VARIABLE_LABELS[4 ] = {" temperature" , " air_humidity" , " light" , " soil_humidity" };
51
62
@@ -56,10 +67,10 @@ String message;
56
67
Ubidots client (UBIDOTS_TOKEN);
57
68
58
69
unsigned long timer;
59
- const int PUBLISH_FREQUENCY = 15000 ;
70
+ const int PUBLISH_FREQUENCY_IN_MS = FREQUENCY_TO_PUBLISH_IN_MS ;
60
71
61
72
// Hardware serial port to communicate with the EDU-CIAA
62
- HardwareSerial SerialPort (UART );
73
+ HardwareSerial SerialPort (DATA_UART );
63
74
64
75
/* *
65
76
* @brief Callback function to handle the data received from the MQTT broker
@@ -70,16 +81,21 @@ HardwareSerial SerialPort(UART);
70
81
*/
71
82
void callback (char *topic, byte *payload, unsigned int length);
72
83
84
+ /* *
85
+ * @brief Function to publish the data to the Ubidots MQTT broker
86
+ */
87
+ void publishData (void *pvParameters);
88
+
73
89
/* *
74
90
* @brief Function to handle the data received from the UART
75
91
*/
76
92
void uartHandler ();
77
93
78
94
void setup () {
79
95
// Set software serial baud to 115200;
80
- Serial.begin (115200 );
96
+ Serial.begin (BAUD_RATE );
81
97
// Set hardware serial baud to 115200;
82
- SerialPort.begin (115200 , SERIAL_8N1, SERIAL_RX, SERIAL_TX);
98
+ SerialPort.begin (BAUD_RATE , SERIAL_8N1, SERIAL_RX, SERIAL_TX);
83
99
// Connecting to a WiFi network
84
100
client.connectToWifi (WIFI_SSID, WIFI_PASSWORD);
85
101
// Connecting to a mqtt broker
@@ -88,6 +104,25 @@ void setup() {
88
104
client.setup ();
89
105
client.reconnect ();
90
106
timer = millis ();
107
+
108
+ /* TaskHandle_t publishDataHandle = NULL;
109
+
110
+ xTaskCreatePinnedToCore(
111
+ publishData, // Task function.
112
+ "publishData", // name of task.
113
+ 10000, // Stack size of task
114
+ NULL, // parameter of the task
115
+ 1, // priority of the task
116
+ &publishDataHandle, // Task handle to keep track of created task
117
+ 0
118
+ ); // pin task to core 0
119
+
120
+ if (publishDataHandle == NULL) {
121
+ Serial.println("Error creating publishData task");
122
+ } else {
123
+ Serial.println("publishData task created");
124
+ vTaskDelete(publishDataHandle);
125
+ } */
91
126
}
92
127
93
128
void callback (char *topic, byte *payload, unsigned int length) {
@@ -136,7 +171,7 @@ void uartHandler() {
136
171
client.add (variableLabel, value);
137
172
long diff = millis () - timer;
138
173
diff = diff < 0 ? -diff : diff;
139
- if (diff > PUBLISH_FREQUENCY ) {
174
+ if (diff > PUBLISH_FREQUENCY_IN_MS ) {
140
175
// Publish the data to the Ubidots MQTT broker
141
176
client.publish (DEVICE_LABEL);
142
177
timer = millis ();
@@ -149,3 +184,13 @@ void uartHandler() {
149
184
message = " " ;
150
185
}
151
186
187
+ void publishData (void *params) {
188
+ const TickType_t xDelay = PUBLISH_FREQUENCY_IN_MS / portTICK_PERIOD_MS;
189
+ while (1 ) {
190
+ Serial.printf (" ESP_STATUS: %s\r\n " , ESP_GetErrorAsString (ESP_STATUS).c_str ());
191
+ // Publish the data to the Ubidots MQTT broker
192
+ client.publish (DEVICE_LABEL);
193
+ vTaskDelay (xDelay);
194
+ }
195
+ }
196
+
0 commit comments