diff --git a/code/espurna/config/arduino.h b/code/espurna/config/arduino.h index c6409bde..e173fe1f 100644 --- a/code/espurna/config/arduino.h +++ b/code/espurna/config/arduino.h @@ -89,8 +89,8 @@ //#define TELNET_SUPPORT 0 //#define TERMINAL_SUPPORT 0 //#define THINGSPEAK_SUPPORT 0 -//#define WEB_SUPPORT 0 //#define UART_MQTT_SUPPORT 1 +//#define WEB_SUPPORT 0 //-------------------------------------------------------------------------------- // Sensors (values below are non-default values) diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index e5e73cd9..0a9b2507 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -489,6 +489,26 @@ PROGMEM const char* const custom_reset_string[] = { #define NOFUSS_SERVER "" // Default NoFuss Server #define NOFUSS_INTERVAL 3600000 // Check for updates every hour +// ----------------------------------------------------------------------------- +// UART <-> MQTT +// ----------------------------------------------------------------------------- + +#ifndef UART_MQTT_SUPPORT +#define UART_MQTT_SUPPORT 0 // No support by default +#endif + +#define UART_MQTT_PORT Serial // Serial port +#define UART_MQTT_BAUDRATE 115200 // Serial speed +#define UART_MQTT_BUFFER_SIZE 100 // UART buffer size + +#if UART_MQTT_SUPPORT +#define MQTT_SUPPORT 1 +#undef TERMINAL_SUPPORT +#define TERMINAL_SUPPORT 0 +#undef DEBUG_SERIAL_SUPPORT +#define DEBUG_SERIAL_SUPPORT 0 +#endif + // ----------------------------------------------------------------------------- // MQTT // ----------------------------------------------------------------------------- diff --git a/code/espurna/config/hardware.h b/code/espurna/config/hardware.h index c430b1b6..2ea44670 100644 --- a/code/espurna/config/hardware.h +++ b/code/espurna/config/hardware.h @@ -1424,19 +1424,6 @@ #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) // Info diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index 116108f9..0f8e7d4c 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -148,7 +148,7 @@ void setup() { schSetup(); #endif #ifdef UART_MQTT_SUPPORT - uart_mqttSetup(); + uartmqttSetup(); #endif diff --git a/code/espurna/uart_mqtt b/code/espurna/uart_mqtt deleted file mode 100644 index 07825a4a..00000000 --- a/code/espurna/uart_mqtt +++ /dev/null @@ -1,86 +0,0 @@ -#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 diff --git a/code/espurna/uartmqtt.ino b/code/espurna/uartmqtt.ino new file mode 100644 index 00000000..07808af6 --- /dev/null +++ b/code/espurna/uartmqtt.ino @@ -0,0 +1,95 @@ +/* + +UART_MQTT MODULE + +Copyright (C) 2018 by Albert Weterings +Adapted by Xose PĂ©rez + +*/ + +#ifdef UART_MQTT_SUPPORT + +char _uartmqttBuffer[UART_MQTT_BUFFER_SIZE]; +bool _uartmqttNewData = false; + +// ----------------------------------------------------------------------------- +// Private +// ----------------------------------------------------------------------------- + +void _uartmqttReceiveUART() { + + static unsigned char ndx = 0; + + while (UART_MQTT_PORT.available() > 0 && _uartmqttNewData == false) { + + char rc = UART_MQTT_PORT.read(); + + if (rc != '\n') { + + _uartmqttBuffer[ndx] = rc; + if (ndx < UART_MQTT_BUFFER_SIZE - 1) ndx++; + + } else { + _uartmqttBuffer[ndx] = '\0'; + _uartmqttNewData = true; + ndx = 0; + } + + } + +} + +void _uartmqttSendMQTT() { + if (_uartmqttNewData == true && MQTT_SUPPORT) { + DEBUG_MSG_P(PSTR("[UART_MQTT] Send data over MQTT: %s\n"), _uartmqttBuffer); + mqttSend(MQTT_TOPIC_UARTIN, _uartmqttBuffer); + _uartmqttNewData = false; + } +} + +void _uartmqttSendUART(const char * message) { + DEBUG_MSG_P(PSTR("[UART_MQTT] Send data over UART: %s\n"), message); + UART_MQTT_PORT.println(message); +} + +void _uartmqttMQTTCallback(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 = mqttMagnitude((char *) topic); + if (t.equals(MQTT_TOPIC_UARTOUT)) { + _uartmqttSendUART(payload); + } + + } + +} + +// ----------------------------------------------------------------------------- +// SETUP & LOOP +// ----------------------------------------------------------------------------- + +void _uartmqttLoop() { + _uartmqttReceiveUART(); + _uartmqttSendMQTT(); +} + +void uartmqttSetup() { + + // Init port + UART_MQTT_PORT.begin(UART_MQTT_BAUDRATE); + + // Register MQTT callbackj + mqttRegister(_uartmqttMQTTCallback); + + // Register loop + espurnaRegisterLoop(_uartmqttLoop); + +} + +#endif