Basic RS232 Communication (#780)

* Added ZGateway RS232
This commit is contained in:
DieKatzchen authored and GitHub committed 2020-10-19 13:48:58 -05:00
1 parent 546b27773f
commit 3df16d2d52
11 files changed
+242

No files matched your search

+1
View File
@@ -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
+87
View File
@@ -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 <http://www.gnu.org/licenses/>.
*/
#include "User_config.h"
#ifdef ZgatewayRS232
# include <SoftwareSerial.h>
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
+72
View File
@@ -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 <http://www.gnu.org/licenses/>.
*/
#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
+15
View File
@@ -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);