[RS232] add JSON parsing and hardware serial support to RS232 gateway (#1409)

* add JSON parse option to RS232 gateway

* add hardware UART support to RS232 gateway

* changed default RS232toMQTTmode to raw (instead of JSON)

* removed reference to Arduino Mega in RS232 gateway config

* update RS232 gateway doc
This commit is contained in:
Merijn authored and GitHub committed 2023-01-25 16:43:53 -06:00
1 parent c1afc02ffe
commit 6e3e8c3330
3 files changed
+266 -31

No files matched your search

+97 -17
View File
@@ -30,27 +30,106 @@ extern void setupRS232();
extern void RS232toMQTT();
extern void MQTTtoRS232(char* topicOri, JsonObject& RS232data);
/*-------------------IR topics & parameters----------------------*/
//RS232 MQTT Subjects
#define subjectGTWRS232toMQTT "/RS232toMQTT"
#define subjectRS232toMQTT "/RS232toMQTT"
#define subjectMQTTtoRS232 "/commands/MQTTtoRS232"
#define subjectForwardMQTTtoRS232 "home/gateway2/commands/MQTTtoRS232"
/*-------------------RS232 topics & parameters----------------------*/
// Settings RS232 to MQTT topic
#define subjectRS232toMQTT "/RS232toMQTT"
// setting to specify mode used for sending MQTT messages:
// 0: RAW: all recieved input at RS232 interface is collected in single MQTT message
// defined by RS232toMQTTsubject
//
// 1: JSON: Assumes input at RS232 interface to be valid JSON. JSON keys are used to
// split the JSON data in separate MQTT sub-topics. This will be repeated for
// sub-keys up to the specified nesting level (RS232maxJSONlevel).
//
// EXAMPLE: "{temperature: {sens1: 22, sens2: 23}, humidity: {sens1: 80, sens2: 60}}"
// - with RS232maxJSONlevel=1:
// ./RS232toMQTT/temperature ==> "{sens1: 22, sens2: 23}"
// ./RS232toMQTT/humidity ==> "{sens1: 80, sens2: 60}"
//
// - with RS232maxJSONlevel=2 (or higher):
// ./RS232toMQTT/temperature/sens1 ==> 22
// ./RS232toMQTT/temperature/sens2 ==> 23
// ./RS232toMQTT/humidity/sens1 ==> 80
// ./RS232toMQTT/humidity/sens2 ==> 60
//
#ifndef RS232toMQTTmode
# define RS232toMQTTmode 0
#endif
// settings for RS232TopicMode 0 (RAW)
#define RS232InPost '\n' // Hacky way to get last character of postfix for incoming
#define MAX_INPUT 200 // how much serial data we expect
// settings for RS232TopicMode 1 (JSON)
#define RS232maxJSONlevel 2 // Max nested level in which JSON keys are converted to seperate sub-topics
#define RS232JSONDocSize 1024 // bytes to reserve for the JSON doc
// settings for MQTT to RS232
#define subjectMQTTtoRS232 "/commands/MQTTtoRS232"
#define RS232Pre "00" // The prefix for the RS232 message
#define RS232Post "\r" // The postfix for the RS232 message
//Setup for RS232
#define MAX_INPUT 50 // how much serial data we expect
#define RS232Baud 9600 // The serial connection Baud
#define RS232Pre "00" // The prefix for the RS232 message
#define RS232Post "\r" // The postfix for the RS232 message
#define RS232InPost '\r' // Hacky way to get last character of postfix for incoming
#ifndef RS232Baud
# define RS232Baud 9600 // The serial connection Baud
#endif
/*-------------------PIN DEFINITIONS----------------------*/
#ifndef RS232_UART
// set hardware serial UART to be used for device communication or
// use software emulaton if not defined
//
// VALUES:
// - not defined: use software emulation (pins set by RS232_RX_GPIO & RS232_TX_GPIO)
//
// - 0: use HW UART0 (serial), warning: default used for logging & usb
// ESP8266: (TX0 GPIO1, RX0 GPIO3)
// (TX0 GPIO15, RX0 GPIO13) with UART0 swap enabled
// ESP32: (TX0 by RS232_TX_GPIO, RX0 by RS232_RX_GPIO)
// ATmega2560: (TX0 pin1, RX0 pin0)
// Arduino Uno: (TX0 pin1, RX0 pin0)
//
// - 1: use HW UART1 (serial1)
// ESP8266: (TX1 GPIO2, RX none) only transmit available
// ESP32: (TX1 by RS232_TX_GPIO, RX1 by RS232_RX_GPIO)
// ATmega2560: (TX1 pin18, RX1 pin19)
// Arduino Uno: N/A
//
// - 2: use HW UART2 (serial2)
// ESP8266: N/A
// ESP32: (TX2 by RS232_TX_GPIO, RX2 by RS232_RX_GPIO)
// ATmega2560: (TX2 pin16, RX1 pin17)
// Arduino Uno: N/A
//
// - 3: use HW UART3 (serial3)
// ESP8266: N/A
// ESP32: N/A
// ATmega2560: (TX2 pin14, RX1 pin15)
// Arduino Uno: N/A
//
// defaults
# ifdef ESP32
# define RS232_UART 1 // use HW UART ESP32
# else
# undef RS232_UART // default use software serial
//# define RS232_UART 1 // define to use HW UART
# endif
#endif
#ifndef RS232_UART0_SWAP
// option for ESP8266 only to swap UART0 ports from (GPIO1,GPIO3) to (GPIO15,GPIO13)
# define RS232_UART0_SWAP
#endif
#ifndef RS232_RX_GPIO
# ifdef ESP8266
// define receive pin (for software serial or ESP32 with configurable HW UART)
# if defined(ESP8266) && !defined(RS232_UART)
# define RS232_RX_GPIO 4 //D2
# elif ESP32
# elif defined(ESP32)
# define RS232_RX_GPIO 26
# elif __AVR_ATmega2560__
# elif defined(__AVR_ATmega2560__) && !defined(RS232_UART)
# define RS232_RX_GPIO 2 // 2 = D2 on arduino mega
# else
# define RS232_RX_GPIO 0 // 0 = D2 on arduino UNO
@@ -58,15 +137,16 @@ extern void MQTTtoRS232(char* topicOri, JsonObject& RS232data);
#endif
#ifndef RS232_TX_GPIO
# ifdef ESP8266
// define transmit pin (for software serial and/or ESP32)
# if defined(ESP8266) && !defined(RS232_UART)
# define RS232_TX_GPIO 2 //D4
# elif ESP32
# define RS232_TX_GPIO 14
# elif __AVR_ATmega2560__
# elif defined(__AVR_ATmega2560__) && !defined(RS232_UART)
# define RS232_TX_GPIO 9
# else
# define RS232_TX_GPIO 9
# endif
#endif
#endif
#endif