xdrv_08_serial_bridge.ino 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. xdrv_08_serial_bridge.ino - serial bridge support for Sonoff-Tasmota
  3. Copyright (C) 2018 Theo Arends and Dániel Zoltán Tolnai
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifdef USE_SERIAL_BRIDGE
  16. /*********************************************************************************************\
  17. * Serial Bridge using Software Serial library (TasmotaSerial)
  18. \*********************************************************************************************/
  19. #define XDRV_08 8
  20. #define SERIAL_BRIDGE_BUFFER_SIZE 130
  21. #include <TasmotaSerial.h>
  22. enum SerialBridgeCommands { CMND_SSERIALSEND, CMND_SBAUDRATE };
  23. const char kSerialBridgeCommands[] PROGMEM = D_CMND_SSERIALSEND "|" D_CMND_SBAUDRATE;
  24. TasmotaSerial *SerialBridgeSerial;
  25. uint8_t serial_bridge_active = 1;
  26. uint8_t serial_bridge_in_byte_counter = 0;
  27. unsigned long serial_bridge_polling_window = 0;
  28. char *serial_bridge_buffer = NULL;
  29. void SerialBridgeInput(void)
  30. {
  31. while (SerialBridgeSerial->available()) {
  32. yield();
  33. uint8_t serial_in_byte = SerialBridgeSerial->read();
  34. if (serial_in_byte > 127) { // binary data...
  35. serial_bridge_in_byte_counter = 0;
  36. SerialBridgeSerial->flush();
  37. return;
  38. }
  39. if (serial_in_byte) {
  40. if ((serial_in_byte_counter < SERIAL_BRIDGE_BUFFER_SIZE -1) && (serial_in_byte != Settings.serial_delimiter)) { // add char to string if it still fits
  41. serial_bridge_buffer[serial_bridge_in_byte_counter++] = serial_in_byte;
  42. serial_bridge_polling_window = millis(); // Wait for more data
  43. } else {
  44. serial_bridge_polling_window = 0; // Publish now
  45. break;
  46. }
  47. }
  48. }
  49. if (serial_bridge_in_byte_counter && (millis() > (serial_bridge_polling_window + SERIAL_POLLING))) {
  50. serial_bridge_buffer[serial_bridge_in_byte_counter] = 0; // serial data completed
  51. snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_JSON_SSERIALRECEIVED "\":\"%s\"}"), serial_bridge_buffer);
  52. MqttPublishPrefixTopic_P(RESULT_OR_TELE, PSTR(D_JSON_SSERIALRECEIVED));
  53. // XdrvRulesProcess();
  54. serial_bridge_in_byte_counter = 0;
  55. }
  56. }
  57. /********************************************************************************************/
  58. void SerialBridgeInit(void)
  59. {
  60. serial_bridge_active = 0;
  61. if ((pin[GPIO_SBR_RX] < 99) && (pin[GPIO_SBR_TX] < 99)) {
  62. serial_bridge_buffer = (char*)(malloc(SERIAL_BRIDGE_BUFFER_SIZE));
  63. if (serial_bridge_buffer != NULL) {
  64. SerialBridgeSerial = new TasmotaSerial(pin[GPIO_SBR_RX], pin[GPIO_SBR_TX]);
  65. if (SerialBridgeSerial->begin(Settings.sbaudrate * 1200)) { // Baud rate is stored div 1200 so it fits into one byte
  66. serial_bridge_active = 1;
  67. SerialBridgeSerial->flush();
  68. }
  69. }
  70. }
  71. }
  72. /*********************************************************************************************\
  73. * Commands
  74. \*********************************************************************************************/
  75. boolean SerialBridgeCommand(void)
  76. {
  77. char command [CMDSZ];
  78. boolean serviced = true;
  79. int command_code = GetCommandCode(command, sizeof(command), XdrvMailbox.topic, kSerialBridgeCommands);
  80. if (-1 == command_code) {
  81. serviced = false; // Unknown command
  82. }
  83. else if ((CMND_SSERIALSEND == command_code) && (XdrvMailbox.index > 0) && (XdrvMailbox.index <= 3)) {
  84. if (XdrvMailbox.data_len > 0) {
  85. if (1 == XdrvMailbox.index) {
  86. SerialBridgeSerial->write(XdrvMailbox.data, XdrvMailbox.data_len);
  87. SerialBridgeSerial->write("\n");
  88. }
  89. else if (2 == XdrvMailbox.index) {
  90. SerialBridgeSerial->write(XdrvMailbox.data, XdrvMailbox.data_len);
  91. }
  92. else if (3 == XdrvMailbox.index) {
  93. SerialBridgeSerial->write(Unescape(XdrvMailbox.data, &XdrvMailbox.data_len), XdrvMailbox.data_len);
  94. }
  95. snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, D_JSON_DONE);
  96. }
  97. }
  98. else if (CMND_SBAUDRATE == command_code) {
  99. char *p;
  100. int baud = strtol(XdrvMailbox.data, &p, 10);
  101. if (baud > 0) {
  102. baud /= 1200; // Make it a valid baudrate
  103. Settings.sbaudrate = (1 == XdrvMailbox.payload) ? SOFT_BAUDRATE / 1200 : baud;
  104. SerialBridgeSerial->begin(Settings.sbaudrate * 1200); // Reinitialize serial port with new baud rate
  105. }
  106. snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_LVALUE, command, Settings.sbaudrate * 1200);
  107. }
  108. else serviced = false; // Unknown command
  109. return serviced;
  110. }
  111. /*********************************************************************************************\
  112. * Interface
  113. \*********************************************************************************************/
  114. boolean Xdrv08(byte function)
  115. {
  116. boolean result = false;
  117. if (serial_bridge_active) {
  118. switch (function) {
  119. case FUNC_PRE_INIT:
  120. SerialBridgeInit();
  121. break;
  122. case FUNC_LOOP:
  123. SerialBridgeInput();
  124. break;
  125. case FUNC_COMMAND:
  126. result = SerialBridgeCommand();
  127. break;
  128. }
  129. }
  130. return result;
  131. }
  132. #endif // USE_SERIAL_BRIDGE