diff --git a/code/espurna/config/arduino.h b/code/espurna/config/arduino.h index 649f2607..c6409bde 100644 --- a/code/espurna/config/arduino.h +++ b/code/espurna/config/arduino.h @@ -90,6 +90,7 @@ //#define TERMINAL_SUPPORT 0 //#define THINGSPEAK_SUPPORT 0 //#define WEB_SUPPORT 0 +//#define UART_MQTT_SUPPORT 1 //-------------------------------------------------------------------------------- // Sensors (values below are non-default values) diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index f4efdc1e..e5e73cd9 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -586,6 +586,8 @@ PROGMEM const char* const custom_reset_string[] = { #define MQTT_TOPIC_RFIN "rfin" #define MQTT_TOPIC_RFLEARN "rflearn" #define MQTT_TOPIC_RFRAW "rfraw" +#define MQTT_TOPIC_UARTIN "uartin" +#define MQTT_TOPIC_UARTOUT "uartout" // Light module #define MQTT_TOPIC_CHANNEL "channel" diff --git a/code/espurna/config/hardware.h b/code/espurna/config/hardware.h index 96a191bd..c430b1b6 100644 --- a/code/espurna/config/hardware.h +++ b/code/espurna/config/hardware.h @@ -1424,7 +1424,18 @@ #define RELAY8_PIN 15 #define RELAY8_TYPE RELAY_TYPE_NORMAL -// ----------------------------------------------------------------------------- +#endif + +#if defined(UART_MQTT_SUPPORT) + // Set baudrate on serial line + #define SERIAL_BAUDRATE 115200 + + // MQTT support is mandatory + #define MQTT_SUPPORT 1 + + // Remove UART noise on serial line + #define TERMINAL_SUPPORT 0 + #define DEBUG_SERIAL_SUPPORT 0 #elif defined(STM_RELAY) diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index 8d059b2c..116108f9 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -147,6 +147,10 @@ void setup() { #if SCHEDULER_SUPPORT schSetup(); #endif + #ifdef UART_MQTT_SUPPORT + uart_mqttSetup(); + #endif + // 3rd party code hook #if USE_EXTRA diff --git a/code/espurna/uart_mqtt b/code/espurna/uart_mqtt new file mode 100644 index 00000000..07825a4a --- /dev/null +++ b/code/espurna/uart_mqtt @@ -0,0 +1,86 @@ +#ifdef UART_MQTT_SUPPORT + +// ----------------------------------------------------------------------------- +// GLOBALS TO THE MODULE +// ----------------------------------------------------------------------------- +const byte numChars = 100; +char receivedChars[numChars]; // an array to store the received data +boolean newData = false; + +void _recvWithEndMarker() { + static byte ndx = 0; + char endMarker = '\n'; + char rc; + + while (Serial.available() > 0 && newData == false) { + rc = Serial.read(); + + if (rc != endMarker) { + receivedChars[ndx] = rc; + ndx++; + if (ndx >= numChars) { + ndx = numChars - 1; + } + } + else { + receivedChars[ndx] = '\0'; // terminate the string + ndx = 0; + newData = true; + } + } +} + +void _uartSendUART_MQTT() { + if (newData == true && MQTT_SUPPORT) { + DEBUG_MSG_P(PSTR("[UART_MQTT] Send data over MQTT: %s\n"), receivedChars); + mqttSend(MQTT_TOPIC_UARTIN, receivedChars); // publish: UART -> mqtt bus + newData = false; + } +} + +void _uartSendMQTT_UART(const char * message) { + DEBUG_MSG_P(PSTR("[UART_MQTT] Send data over UART: %s\n"), message); + Serial.print(message); + Serial.println(); +} + +#if MQTT_SUPPORT +void _UART_MQTTMqttCallback(unsigned int type, const char * topic, const char * payload) { + if (type == MQTT_CONNECT_EVENT) { + mqttSubscribe(MQTT_TOPIC_UARTOUT); + } + + if (type == MQTT_MESSAGE_EVENT) { + + // Match topic + String t = mqttTopicKey((char *) topic); + bool isUARTOut = t.equals(MQTT_TOPIC_UARTOUT); + + if (isUARTOut) { + _uartSendMQTT_UART(payload); + } + } +} +#endif + +// ----------------------------------------------------------------------------- +// SETUP & LOOP +// ----------------------------------------------------------------------------- + +void uart_mqttSetup() { + + #if MQTT_SUPPORT + mqttRegister(_UART_MQTTMqttCallback); + #endif + + // Register oop + espurnaRegisterLoop(UART_MQTTLoop); + +} + +void UART_MQTTLoop() { + _recvWithEndMarker(); + _uartSendUART_MQTT(); +} + +#endif