22#include < HardwareSerial.h>
33#include " wifi_credentials.h"
44
5+ // Baud rate to communicate with the EDU-CIAA
6+ #define BAUD_RATE 115200
57// Serial port to communicate with the EDU-CIAA
6- #define UART 2
8+ #define DATA_UART 2
79// Serial port pins
810#define SERIAL_RX 16
911#define SERIAL_TX 17
1012
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+
1122typedef enum {
1223 ESP_INIT,
1324 ESP_CONNECTED,
@@ -45,7 +56,7 @@ const char *WIFI_PASSWORD = WIFI_PASSWORD_SECRETS;
4556// Ubidots TOKEN
4657const char *UBIDOTS_TOKEN = UBIDOTS_TOKEN_SECRETS;
4758// Device label
48- const char *DEVICE_LABEL = " esp32-edu-ciaa " ;
59+ const char *DEVICE_LABEL = DEVICE_LABEL_SECRETS ;
4960// Variable labels
5061const char *VARIABLE_LABELS[4 ] = {" temperature" , " air_humidity" , " light" , " soil_humidity" };
5162
@@ -56,10 +67,10 @@ String message;
5667Ubidots client (UBIDOTS_TOKEN);
5768
5869unsigned long timer;
59- const int PUBLISH_FREQUENCY = 15000 ;
70+ const int PUBLISH_FREQUENCY_IN_MS = FREQUENCY_TO_PUBLISH_IN_MS ;
6071
6172// Hardware serial port to communicate with the EDU-CIAA
62- HardwareSerial SerialPort (UART );
73+ HardwareSerial SerialPort (DATA_UART );
6374
6475/* *
6576 * @brief Callback function to handle the data received from the MQTT broker
@@ -70,16 +81,21 @@ HardwareSerial SerialPort(UART);
7081 */
7182void callback (char *topic, byte *payload, unsigned int length);
7283
84+ /* *
85+ * @brief Function to publish the data to the Ubidots MQTT broker
86+ */
87+ void publishData (void *pvParameters);
88+
7389/* *
7490 * @brief Function to handle the data received from the UART
7591 */
7692void uartHandler ();
7793
7894void setup () {
7995 // Set software serial baud to 115200;
80- Serial.begin (115200 );
96+ Serial.begin (BAUD_RATE );
8197 // 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);
8399 // Connecting to a WiFi network
84100 client.connectToWifi (WIFI_SSID, WIFI_PASSWORD);
85101 // Connecting to a mqtt broker
@@ -88,6 +104,25 @@ void setup() {
88104 client.setup ();
89105 client.reconnect ();
90106 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+ } */
91126}
92127
93128void callback (char *topic, byte *payload, unsigned int length) {
@@ -136,7 +171,7 @@ void uartHandler() {
136171 client.add (variableLabel, value);
137172 long diff = millis () - timer;
138173 diff = diff < 0 ? -diff : diff;
139- if (diff > PUBLISH_FREQUENCY ) {
174+ if (diff > PUBLISH_FREQUENCY_IN_MS ) {
140175 // Publish the data to the Ubidots MQTT broker
141176 client.publish (DEVICE_LABEL);
142177 timer = millis ();
@@ -149,3 +184,13 @@ void uartHandler() {
149184 message = " " ;
150185}
151186
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