|
| 1 | +#include "UbidotsEsp32Mqtt.h" |
| 2 | +#include <HardwareSerial.h> |
| 3 | + |
| 4 | +// Serial port to communicate with the EDU-CIAA |
| 5 | +#define UART 2 |
| 6 | +// Serial port pins |
| 7 | +#define SERIAL_RX 16 |
| 8 | +#define SERIAL_TX 17 |
| 9 | + |
| 10 | +// WiFi credentials |
| 11 | +const char *WIFI_SSID = "Fibertel WiFi839 2.4Ghz"; // Enter your WiFi name |
| 12 | +const char *WIFI_PASSWORD = "00496026574"; // Enter WiFi password |
| 13 | + |
| 14 | +// Ubidots TOKEN |
| 15 | +const char *UBIDOTS_TOKEN = "BBFF-Pc8IvgnCXtKOaQ1lwUfq5oypeSU5AW"; |
| 16 | +// Device label |
| 17 | +const char *DEVICE_LABEL = "esp32-edu-ciaa"; |
| 18 | +// Variable labels |
| 19 | +const char *VARIABLE_LABELS[4] = {"temperature", "soil-humidity", "light", "air-humidity"}; |
| 20 | + |
| 21 | +// Message received from the UART |
| 22 | +String message; |
| 23 | + |
| 24 | +// Ubidots client |
| 25 | +Ubidots client(UBIDOTS_TOKEN); |
| 26 | + |
| 27 | +// Hardware serial port to communicate with the EDU-CIAA |
| 28 | +HardwareSerial SerialPort(UART); |
| 29 | + |
| 30 | +/** |
| 31 | + * @brief Callback function to handle the data received from the MQTT broker |
| 32 | + * |
| 33 | + * @param topic - Topic where the data was published |
| 34 | + * @param payload - Data received |
| 35 | + * @param length - Length of the data received |
| 36 | + */ |
| 37 | +void callback(char *topic, byte *payload, unsigned int length); |
| 38 | + |
| 39 | +/** |
| 40 | + * @brief Function to handle the data received from the UART |
| 41 | + */ |
| 42 | +void uartHandler(); |
| 43 | + |
| 44 | +void setup() { |
| 45 | + // Set software serial baud to 115200; |
| 46 | + Serial.begin(115200); |
| 47 | + // Set hardware serial baud to 115200; |
| 48 | + SerialPort.begin(115200, SERIAL_8N1, SERIAL_RX, SERIAL_TX); |
| 49 | + // Connecting to a WiFi network |
| 50 | + client.connectToWifi(WIFI_SSID, WIFI_PASSWORD); |
| 51 | + // Connecting to a mqtt broker |
| 52 | + client.setCallback(callback); |
| 53 | + client.setup(); |
| 54 | + client.reconnect(); |
| 55 | +} |
| 56 | + |
| 57 | +void callback(char *topic, byte *payload, unsigned int length) { |
| 58 | + Serial.print("Message arrived in topic: "); |
| 59 | + Serial.println(topic); |
| 60 | + int i; |
| 61 | + char data[length]; |
| 62 | + for (i = 0; i < length; i++) { |
| 63 | + data[i] = (char) payload[i]; |
| 64 | + } |
| 65 | + data[i] = '\0'; |
| 66 | + Serial.printf("Data: \n%s\n", data); |
| 67 | + Serial.println("-----------------------"); |
| 68 | +} |
| 69 | + |
| 70 | +void loop() { |
| 71 | + if (!client.connected()) { |
| 72 | + client.reconnect(); |
| 73 | + } |
| 74 | + |
| 75 | + while(SerialPort.available()) { |
| 76 | + uartHandler(); |
| 77 | + } |
| 78 | + |
| 79 | + client.loop(); |
| 80 | +} |
| 81 | + |
| 82 | +void uartHandler() { |
| 83 | + message = SerialPort.readString(); |
| 84 | + Serial.printf("Message received: %s"); |
| 85 | + // Get the type of the variable, and convert it to an integer |
| 86 | + const int index = message[0] - '0'; |
| 87 | + // Check if the type is valid, and if the index is in range |
| 88 | + if (index >= 0 && index < 4) { |
| 89 | + // Add the value to the Ubidots client |
| 90 | + client.add(VARIABLE_LABELS[index], message.substring(1).toFloat()); |
| 91 | + // Publish the data to the Ubidots MQTT broker |
| 92 | + client.publish(DEVICE_LABEL); |
| 93 | + } else { |
| 94 | + Serial.println("Error: Invalid type, index out of range"); |
| 95 | + } |
| 96 | +} |
| 97 | + |
0 commit comments