diff --git a/docs/.vuepress/config.js b/docs/.vuepress/config.js index de983e4..41e170c 100644 --- a/docs/.vuepress/config.js +++ b/docs/.vuepress/config.js @@ -53,6 +53,7 @@ module.exports = { 'setitup/ir', 'setitup/lora', 'setitup/gsm', + 'setitup/rs232', 'setitup/sensors', 'setitup/actuators' ] @@ -78,6 +79,7 @@ module.exports = { 'use/ir', 'use/lora', 'use/gsm', + 'use/rs232', 'use/rfm69', 'use/sensors', 'use/actuators', diff --git a/docs/img/OpenMQTTgateway_Arduino_Addon_RS232.png b/docs/img/OpenMQTTgateway_Arduino_Addon_RS232.png new file mode 100644 index 0000000..64765d1 Binary files /dev/null and b/docs/img/OpenMQTTgateway_Arduino_Addon_RS232.png differ diff --git a/docs/img/OpenMQTTgateway_ESP32_Addon_RS232.png b/docs/img/OpenMQTTgateway_ESP32_Addon_RS232.png new file mode 100644 index 0000000..345e94f Binary files /dev/null and b/docs/img/OpenMQTTgateway_ESP32_Addon_RS232.png differ diff --git a/docs/img/OpenMQTTgateway_ESP8266_Addon_RS232.png b/docs/img/OpenMQTTgateway_ESP8266_Addon_RS232.png new file mode 100644 index 0000000..84c95b3 Binary files /dev/null and b/docs/img/OpenMQTTgateway_ESP8266_Addon_RS232.png differ diff --git a/docs/setitup/rs232.md b/docs/setitup/rs232.md new file mode 100644 index 0000000..2ac6862 --- /dev/null +++ b/docs/setitup/rs232.md @@ -0,0 +1,29 @@ +# RS232 gateway +## Compatible parts +|Module|Purpose|Where to Buy| +|-|-|-| +|TTL to RS232 module|Transcieving|-| + + +Any module using a MAX232 chip will work. Don't pay extra to get all the signals, we're only using RX and TX. + +## Pinout +|Board| Receiver Pin| Emitter Pin| +|-|:-:|:-:| +|Arduino UNO|D2|D9| +|ESP8266|D2|D4| +|ESP32|27/**26**|14| + +Connect VCC on the module to 5V of your board, and the ground on the module to the ground of your board. your Transmit pin goes to TX and your Receive pin goes to RX. This may seem backwards, but the module is labeled as where it will be connected on the RS232 side of the module. + +## Arduino Hardware setup +![RS232](../img/OpenMQTTgateway_Arduino_Addon_RS232.png) + +## ESP8266 Hardware setup +![RS232](../img/OpenMQTTgateway_ESP8266_Addon_RS232.png) + +## ESP32 Hardware setup +![RS232](../img/OpenMQTTgateway_ESP32_Addon_RS232.png) + +## Credits +RS232 Gateway Developed by DieKatzchen \ No newline at end of file diff --git a/docs/use/rs232.md b/docs/use/rs232.md new file mode 100644 index 0000000..6175f15 --- /dev/null +++ b/docs/use/rs232.md @@ -0,0 +1,23 @@ +# RS232 gateway + +## Sending an RS232 message + +Simply publish the message you wish to transmit, minus the prefix and postfix. For example, to send the "Turn On" signal for a Mitsubishi XD221U projector, the code is simply '!' so you would use the command + +`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoRS232 -m '{"value": "!"}'` + +It will automatically add the prefix and postfix you set in [config_RS232.h](https://github.com/1technophile/OpenMQTTGateway/blob/master/main/config_RS232.h). + +## Receiving an RS232 message +To receive a message, subscribe to all with `mosquitto_sub -t +/# -v` +and perform an action that should get a response from the device. For example, If I were to send the "Turn On" signal from earlier, I would receive back + +```json +home/OpenMQTTGateway/RS232toMQTT {"value":"1"} +``` + +Because this projector echoes back a received command to acknowledge. Some devices will send a NACK, or Negative Acknowledge, to confirm that they received your message but could not comply. That would look like + +```json +home/OpenMQTTGateway/RStoMQTT {"value":"!:N"} +``` diff --git a/main/User_config.h b/main/User_config.h index 85809c0..20d3f94 100755 --- a/main/User_config.h +++ b/main/User_config.h @@ -232,6 +232,7 @@ uint8_t wifiProtocol = 0; // default mode, automatic selection //#define ZboardM5STACK "ZboardM5STACK" //#define ZradioCC1101 "CC1101" //ESP8266, ESP32 //#define ZactuatorPWM "PWM" //ESP8266, ESP32 +//#define ZgatewayRS232 "RS232" //ESP8266, Arduino, ESP32 /*-------------DEFINE YOUR MQTT ADVANCED PARAMETERS BELOW----------------*/ #ifndef version_Topic diff --git a/main/ZgatewayRS232.ino b/main/ZgatewayRS232.ino new file mode 100644 index 0000000..c0cb730 --- /dev/null +++ b/main/ZgatewayRS232.ino @@ -0,0 +1,87 @@ +/* + OpenMQTTGateway - ESP8266 or Arduino program for home automation + + Act as a wifi or ethernet gateway between your RS232 device and a MQTT broker + Send and receiving command by MQTT + + This gateway enables to: + - receive MQTT data from a topic and send RS232 signal corresponding to the received MQTT data + - publish MQTT data to a different topic related to received RS232 signal + + Copyright: (c)Florian ROBERT + + This file is part of OpenMQTTGateway. + + OpenMQTTGateway is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenMQTTGateway is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#include "User_config.h" + +#ifdef ZgatewayRS232 + +# include + +SoftwareSerial RS232Serial(RS232_RX_GPIO, RS232_TX_GPIO); // RX, TX + +void setupRS232() { + // define pin modes for RX, TX: + pinMode(RS232_RX_GPIO, INPUT); + pinMode(RS232_TX_GPIO, OUTPUT); + // set the data rate for the SoftwareSerial port + RS232Serial.begin(RS232Baud); + + Log.notice(F("RS232_RX_GPIO: %d " CR), RS232_RX_GPIO); + Log.notice(F("RS232_TX_GPIO: %d " CR), RS232_TX_GPIO); + Log.notice(F("RS232Baud: %d " CR), RS232Baud); + Log.trace(F("ZgatewayRS232 setup done " CR)); +} + +void RS232toMQTT() { + //This function is Blocking, but there should only ever be a few bytes, usually an ACK or a NACK. + if (RS232Serial.available()) { + Log.trace(F("RS232toMQTT" CR)); + static char RS232data[MAX_INPUT]; + static unsigned int input_pos = 0; + static char inChar; + do { + if (RS232Serial.available()) { + inChar = RS232Serial.read(); + RS232data[input_pos] = inChar; + input_pos++; + Log.trace(F("Received %c" CR), inChar); + } + } while (inChar != RS232InPost && input_pos < MAX_INPUT); + RS232data[input_pos] = 0; + input_pos = 0; + Log.trace(F("Publish %s" CR), RS232data); + char* output = RS232data + sizeof(RS232Pre) - 1; + pub(subjectRS232toMQTT, output); + } +} + +void MQTTtoRS232(char* topicOri, JsonObject& RS232data) { + Log.trace(F("json" CR)); + if (cmpToMainTopic(topicOri, subjectMQTTtoRS232)) { + Log.trace(F("MQTTtoRS232 json" CR)); + const char* data = RS232data["value"]; + const char* prefix = RS232data["prefix"] | RS232Pre; + const char* postfix = RS232data["postfix"] | RS232Post; + Log.trace(F("Value set: %s" CR), data); + Log.trace(F("Prefix set: %s" CR), prefix); + Log.trace(F("Postfix set: %s" CR), postfix); + RS232Serial.print(prefix); + RS232Serial.print(data); + RS232Serial.print(postfix); + } +} +#endif \ No newline at end of file diff --git a/main/config_RS232.h b/main/config_RS232.h new file mode 100644 index 0000000..68b2d80 --- /dev/null +++ b/main/config_RS232.h @@ -0,0 +1,72 @@ +/* + OpenMQTTGateway - ESP8266 or Arduino program for home automation + + Act as a wifi or ethernet gateway between your RS232 device and a MQTT broker + Send and receiving command by MQTT + + This files enables to set your parameter for the RS232 gateway + + Copyright: (c)Florian ROBERT + + This file is part of OpenMQTTGateway. + + OpenMQTTGateway is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenMQTTGateway is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#ifndef config_RS232_h +#define config_RS232_h + +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" + +//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 + +/*-------------------PIN DEFINITIONS----------------------*/ +#ifndef RS232_RX_GPIO +# ifdef ESP8266 +# define RS232_RX_GPIO 4 //D2 +# elif ESP32 +# define RS232_RX_GPIO 26 +# elif __AVR_ATmega2560__ +# define RS232_RX_GPIO 2 // 2 = D2 on arduino mega +# else +# define RS232_RX_GPIO 0 // 0 = D2 on arduino UNO +# endif +#endif + +#ifndef RS232_TX_GPIO +# ifdef ESP8266 +# define RS232_TX_GPIO 2 //D4 +# elif ESP32 +# define RS232_TX_GPIO 14 +# elif __AVR_ATmega2560__ +# define RS232_TX_GPIO 9 +# else +# define RS232_TX_GPIO 9 +# endif +#endif + +#endif diff --git a/main/main.ino b/main/main.ino index e174f51..0be59fc 100644 --- a/main/main.ino +++ b/main/main.ino @@ -131,6 +131,9 @@ unsigned long timer_sys_measures = 0; #if defined(ZboardM5STICKC) || defined(ZboardM5STACK) # include "config_M5.h" #endif +#if defined(ZgatewayRS232) +# include "config_RS232.h" +#endif /*------------------------------------------------------------------------*/ //adding this to bypass the problem of the arduino builder issue 50 @@ -661,6 +664,9 @@ void setup() { #endif #ifdef ZsensorDHT setupDHT(); +#endif +#ifdef ZgatewayRS232 + setupRS232(); #endif Log.trace(F("mqtt_max_packet_size: %d" CR), mqtt_max_packet_size); Log.notice(F("Setup OpenMQTTGateway end" CR)); @@ -1272,6 +1278,9 @@ void loop() { if (RFM69toMQTT()) Log.trace(F("RFM69toMQTT OK" CR)); #endif +#ifdef ZgatewayRS232 + RS232toMQTT(); +#endif #ifdef ZactuatorFASTLED FASTLEDLoop(); #endif @@ -1434,6 +1443,9 @@ void stateMeasures() { SYSdata["m5-bat-power"] = (float)M5.Axp.GetBatPower(); SYSdata["m5-bat-chargecurrent"] = (float)M5.Axp.GetBatChargeCurrent(); SYSdata["m5-aps-voltage"] = (float)M5.Axp.GetAPSVoltage(); +# endif +# ifdef ZGatewayRS232 + modules = modules + ZGatewayRS232; # endif SYSdata["modules"] = modules; pub(subjectSYStoMQTT, SYSdata); @@ -1550,6 +1562,9 @@ void receivingMQTT(char* topicOri, char* datacallback) { # ifdef ZactuatorONOFF // outside the jsonpublishing macro due to the fact that we need to use simplepublishing with HA discovery MQTTtoONOFF(topicOri, jsondata); # endif +# ifdef ZgatewayRS232 + MQTTtoRS232(topicOri, jsondata); +# endif #endif digitalWrite(LED_SEND, LED_SEND_ON); diff --git a/platformio.ini b/platformio.ini index 4005ff6..d2b06ea 100644 --- a/platformio.ini +++ b/platformio.ini @@ -63,6 +63,7 @@ extra_configs = ;default_envs = manual-wifi-test ;default_envs = esp32dev-tls-test ;default_envs = nodemcuv2-tls-test +;default_envs = nodemcuv2-rs232 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ENVIRONMENTS PARAMETERS ; @@ -511,6 +512,18 @@ build_flags = '-DGateway_Name="OpenMQTTGateway_ESP8266_IR"' board_build.flash_mode = dout +[env:nodemcuv2-rs232] +platform = ${com.esp8266_platform} +board = nodemcuv2 +lib_deps = + ${com-esp.lib_deps} + ${libraries.esp8266_mdns} +build_flags = + ${com-esp.build_flags} + '-DZgatewayRS232="RS232"' + '-DGateway_Name="OpenMQTTGateway_ESP8266_RS232"' +board_build.flash_mode = dout + [env:avatto-bakeey-ir] platform = ${com.esp8266_platform} board = esp01_1m