Dateien nach "/" hochladen

Regular update
This commit is contained in:
2025-02-10 22:13:31 +01:00
parent 947ca66acc
commit 422f6c4d97
4 changed files with 1512 additions and 276 deletions

View File

@@ -1,7 +1,7 @@
/* /*
OpenMQTTGateway - ESP8266 or Arduino program for home automation Theengs OpenMQTTGateway - We Unite Sensors in One Open-Source Interface
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker Act as a gateway between your 433mhz, infrared IR, BLE, LoRa signal and one interface like an MQTT broker
Send and receiving command by MQTT Send and receiving command by MQTT
This gateway enables to: This gateway enables to:
@@ -25,6 +25,8 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "User_config.h"
#ifdef ZgatewayRF_RHASK #ifdef ZgatewayRF_RHASK
#include <RH_ASK.h> #include <RH_ASK.h>
@@ -32,316 +34,131 @@
#include <SPI.h> // Not actually used but needed to compile #include <SPI.h> // Not actually used but needed to compile
#endif #endif
int RollingCounter = 0; //int RollingCounter = 0; // Initialize rolling counter to avoid duplicate messages (messages a repeated multiple times
int RC_old = 99, adressIn_old = 99; // initialize values to an unused value to ensure that the first value is not missed
//RH_ASK driver(2000,RF_RECEIVER_PIN,RF_EMITTER_PIN); RH_ASK driver(500,4,0); // Initialize RH_ASK driver
RH_ASK driver(500,4,0);
void setupRF(){ void setupRF(){
//RF init parameters //RF init parameters
Serial.println("Setup RF"); Log.notice("Setup RF");
if (!driver.init()) if (!driver.init())
{ {
Log.notice("Success");
} }
else else
{ {
Log.notice("RF failed");
} }
// #ifdef RH_HAVE_SERIAL
// trc(F("init failed"));
// #else
// ;
// trc(F("RF_EMITTER_PIN "));
// trc(RF_EMITTER_PIN);
// trc(F("RF_RECEIVER_PIN "));
// trc(RF_RECEIVER_PIN);
// trc(F("RF setup ok"));
// #endif
} }
//uint8_t buf_Stored[RH_ASK_MAX_MESSAGE_LEN]; # if defined(ZmqttDiscovery) && !defined(RF_DISABLE_TRANSMIT) && defined(RFmqttDiscovery)
int RC_old; void RFtoMQTTdiscovery(int deviceID, String sensorName, String sensorUnit) {
void RFtoMQTT(){ // Function creates autodiscovery message depending on the sensor ID
Log.trace(F("RF Entity Discovered, create HA Discovery CFG" CR));
String discovery_topic = String(subjectRFtoMQTT);
String theUniqueId = getUniqueId(String(deviceID), "-RH_ASK");
String devID = String(deviceID); // Device ID (adress of RF sender)
announceDeviceTrigger(
false,
(char*)discovery_topic.c_str(),
"received",
"",
(char*)theUniqueId.c_str(),
(char*)sensorName.c_str(), "", "", "",
(char*)sensorUnit.c_str(),
(char*)devID.c_str());
}
# endif
void RFtoX() {
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN]; uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf); uint8_t buflen = sizeof(buf);
if (driver.recv(buf, &buflen)) // Non-blocking if (driver.recv(buf, &buflen)) {
{ Log.trace(F("Rcv. RF" CR));
// Debug # ifdef ESP32
String RFTopicDebug; Log.trace(F("RF Task running on core :%d" CR), xPortGetCoreID());
RFTopicDebug = "rf/sensors/"; # endif
char RFTopicCharDebug[50];
RFTopicDebug.toCharArray(RFTopicCharDebug,50);
// pubMQTT(RFTopicCharDebug,"Received Sth");
// Serial.println("Received sth");
// Debug End
// Decrypt RF message
int adressIn, parameterIn, RCin; int adressIn, parameterIn, RCin;
float payloadIn; float payloadIn;
MessageDecrypt(buf, &adressIn, &parameterIn, &payloadIn, &RCin); MessageDecrypt(buf, &adressIn, &parameterIn, &payloadIn, &RCin);
// debug // if (!isAduplicateSignal(MQTTvalue) && MQTTvalue != 0) { // conditions to avoid duplications of RF -->MQTT
Serial.print("RC_in: ");Serial.println(RCin);
// Serial.print("Buf: ");Serial.println(buf);
Serial.print("Payload: ");Serial.println(payloadIn);
// debug if (((RCin != RC_old) || (adressIn != adressIn_old)) && (unsigned long)buf != 0) { // new message (no duplicate) if rolling counter is changed
// trc("Adresse: ");trc(adressIn);
// trc("Parameter: ");trc(parameterIn);
// trc("Payload: ");trc(payloadIn);
// end debug
Log.notice(F("Message: %i" CR), adressIn);
Log.notice(F("Message: %i" CR), parameterIn);
Log.notice(F("Message: %i" CR), RCin);
// Compare old vs new value
bool newValue = false;
if ((!isAduplicateSignal((uint64_t)buf) || RCin != RC_old) && (unsigned long)buf != 0)
{
newValue = true;
}
else
{
// trc("Don't publish");
}
storeSignalValue((uint64_t)buf); // Store value to compare with new values
// Serial.print("RC in: ");Serial.print(RCin);Serial.print("RC old: ");Serial.println(RC_old);
RC_old = RCin; RC_old = RCin;
adressIn_old = adressIn;
if(newValue) // Detect device in the device list
{
// match Sensor number in the array with sensor ID
int SensorNo; int SensorNo;
bool FoundSensor = false; bool FoundSensor = false;
for(SensorNo = 0;SensorNo<sizeof(RFSensorNames)/sizeof(RFSensorNames[0]);SensorNo++) String sensorName, sensorUnit;
for(SensorNo = 0;SensorNo<sizeof(RFSensorNames)/sizeof(RFSensorNames[0]);SensorNo++) // Compare received sensor ID with sensor ID array in configuration file
{ {
if(adressIn == RFSensorIDs[SensorNo]) if(adressIn == RFSensorIDs[SensorNo])
{ {
FoundSensor = true; FoundSensor = true;
sensorName = RFSensorNames[SensorNo];
sensorUnit = RFSensorUnit[SensorNo];
break; break;
} }
} }
if(FoundSensor) if(!FoundSensor) {
{ Log.notice("Sensor not identified");
// trc(SensorNo); return;
// Publish topic }
// Define topic name
String RFTopic;
RFTopic = "rf/sensors/" + RFSensorType[SensorNo] + "/" + RFSensorNames[SensorNo];
char RFTopicChar[50];
RFTopic.toCharArray(RFTopicChar,50);
// Publish matrix // Define JSON message
pubMQTT(RFTopicChar,payloadIn); StaticJsonDocument<JSON_MSG_BUFFER> RFdataBuffer;
// pub(subjectRFtoMQTT, RFdata); JsonObject RFdata = RFdataBuffer.to<JsonObject>();
//String theUniqueId = getUniqueId(String(adressIn), "-RH_ASK"); // get unique ID for this sensor
String template_Name = String(sensorName) + "_" + String(adressIn);
RFdata[template_Name] = (float)payloadIn;
# if defined(ZmqttDiscovery) && !defined(RF_DISABLE_TRANSMIT) && defined(RFmqttDiscovery) //component creation for HA
if (SYSConfig.discovery)
RFtoMQTTdiscovery(adressIn, sensorName, sensorUnit);
# endif
RFdata["origin"] = subjectRFtoMQTT;
enqueueJsonObject(RFdata);
// Casting "receivedSignal[o].value" to (unsigned long) because ArduinoLog doesn't support uint64_t for ESP's
// Log.trace(F("Store val: %u" CR), (unsigned long)MQTTvalue);
// storeSignalValue(MQTTvalue);
} }
else else
// trc(F("Unkown sensor"));
;
}
}
}
// if (mySwitchBen.available()){
// unsigned long MQTTvalue = (unsigned long)mySwitchBen.getReceivedValue();
// mySwitchBen.resetAvailable();
// trc(F("Received sth"));
//
// if (!isAduplicate(MQTTvalue) && MQTTvalue!=0) {// conditions to avoid duplications of RF -->MQTT
// int adressIn, payloadIn, parameterIn;
// MessageDecrypt(MQTTvalue, &adressIn, &parameterIn, &payloadIn);
// // match Sensor number in the array with sensor ID
// int SensorNo;
// bool FoundSensor = false;
// for(SensorNo = 0;SensorNo<sizeof(RFSensorNames)/sizeof(RFSensorNames[0]);SensorNo++)
// {
// if(adressIn == RFSensorIDs[SensorNo])
// {
// FoundSensor = true;
// break;
// }
// }
//
//// trc(SensorNo);
//
// // Define topic name
// String RFTopic;
// RFTopic = "rf/sensors/" + RFSensorType[SensorNo] + "/" + RFSensorNames[SensorNo];
//// trc(RFTopic);
// char RFTopicChar[50];
// RFTopic.toCharArray(RFTopicChar,50);
//
// // Publish matrix
// pub(RFTopicChar,payloadIn);
// trc(F("Store to avoid duplicate"));
// storeValue(MQTTvalue);
// if (repeatRFwMQTT){
// trc(F("Pub RF for rpt"));
// pub(subjectMQTTtoRF,MQTTvalue);
// }
// }
// }
//}
void MQTTtoRF(char * topicOri, char * datacallback) {
unsigned long data = strtoul(datacallback, NULL, 10); // we will not be able to pass values > 4294967295
String topic = topicOri;
//trc(F("Test"));
// Get the name of the target
String TargetName;
String ParameterName;
int posDivider = topic.lastIndexOf("/");
if(posDivider != -1)
{ {
ParameterName = topic.substring(posDivider+1); Log.notice("duplicate, no publishing\n");
String topicSubstring = topic.substring(0,posDivider);
posDivider = topicSubstring.lastIndexOf("/");
if(posDivider != -1)
TargetName = topicSubstring.substring(posDivider+1);
}
// trc(F("Target Name: "));
// trc(TargetName);
// trc(ParameterName);
// Get target number in array by sensor name
int TargetNo;
bool TargetFound = false;
for(TargetNo = 0;TargetNo<(sizeof(RFTargetNames)/sizeof(RFTargetNames[0]));TargetNo++)
{
if(RFTargetNames[TargetNo] == TargetName)
{
TargetFound = true;
break;
}
}
// trc(TargetNo);
if(TargetFound)
{
// find parameter ID
int ParId;
for(ParId=0;ParId<(sizeof(ParameterIds)/sizeof(ParameterIds[0]));ParId++)
{
if(ParameterIds[ParId] == ParameterName)
break;
}
// Search for target adress
byte MessageOut[8];
MessageEncrypt(RFTargetIDs[TargetNo], ParId, data, MessageOut);
for(int i=0;i<=RF_EMITTER_REPEAT;i++)
{
driver.send(MessageOut,8);
driver.waitPacketSent();
} }
} }
} }
bool MessageDecrypt(byte ByteArray[], int *adress, int *parameter, float *payload, int *RCin) bool MessageDecrypt(byte ByteArray[], int *adress, int *parameter, float *payload, int *RCin)
{ {
// Serial.print("BytAr: ");Serial.println(ByteArray[0]);
*adress = (int)ByteArray[0]; *adress = (int)ByteArray[0];
// *parameter = (int)ByteArray[1];
// To be replaced by memcopy // todo
byte arrayFloat[4]; byte arrayFloat[4];
// arrayFloat[0] = ByteArray[2];
// arrayFloat[1] = ByteArray[3];
// arrayFloat[2] = ByteArray[4];
// arrayFloat[3] = ByteArray[5];
arrayFloat[0] = ByteArray[1]; arrayFloat[0] = ByteArray[1];
arrayFloat[1] = ByteArray[2]; arrayFloat[1] = ByteArray[2];
arrayFloat[2] = ByteArray[3]; arrayFloat[2] = ByteArray[3];
arrayFloat[3] = ByteArray[4]; arrayFloat[3] = ByteArray[4];
// int i = arrayFloat[0] | (arrayFloat[1] << 8) | (arrayFloat[2] << 16) | (arrayFloat[3] << 24);
float testfloat; float testfloat;
testfloat = *((float*) (arrayFloat)); testfloat = *((float*) (arrayFloat));
*payload = testfloat; *payload = testfloat;
//testfloat = *((float*)(arrayFloat));
// Serial.print("Number: ");Serial.println(testfloat);
// Serial.print("Byte: ");Serial.println(arrayFloat[1]);
// byte CRCReceive = ByteArray[6];
// CRC Check
// byte CRCValue = CRC8(ByteArray, 6);
// bool CRCCheckOK = false;
// if(CRCValue == CRCReceive)
// {
// trc(F("CRC OK"));
// CRCCheckOK = true;
// }
// *RCin = (int)ByteArray[7];
*RCin = (int)ByteArray[5]; *RCin = (int)ByteArray[5];
// Serial.print("RC in: ");Serial.println(RCin);
// Serial.println(messageInput[0]);
// return CRCCheckOK;
return true; return true;
} }
void MessageEncrypt(int adressIn, int parameterIn, float payloadIn, byte pByteArray[])
{
// byte ByteArray[6];
pByteArray[0] = (byte)adressIn;
pByteArray[1] = (byte)parameterIn;
byte * payloadBArray = (byte *)&payloadIn;
pByteArray[2] = payloadBArray[0];
pByteArray[3] = payloadBArray[1];
pByteArray[4] = payloadBArray[2];
pByteArray[5] = payloadBArray[3];
// long l = *(long*) &payloadIn;
byte CRCValue = CRC8(pByteArray, 6);
pByteArray[6]= CRCValue;
RollingCounter = RollingCounter + 1;
if(RollingCounter > 2)
RollingCounter = 0;
pByteArray[7] = RollingCounter;
}
//CRC-8 - based on the CRC8 formulas by Dallas/Maxim
//code released under the therms of the GNU GPL 3.0 license
byte CRC8(const byte *data, byte len) {
byte crc = 0x00;
while (len--) {
byte extract = *data++;
for (byte tempI = 8; tempI; tempI--) {
byte sum = (crc ^ extract) & 0x01;
crc >>= 1;
if (sum) {
crc ^= 0x8C;
}
extract >>= 1;
}
}
return crc;
}
#endif #endif

1412
ZmqttDiscovery.ino Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,24 +1,31 @@
/*-------------------RF sensor + hardware settings----------------------*/ /*-------------------RF sensor + hardware settings----------------------*/
const int RFSensorIDs[] = {1,2,3,4,5,6,7}; const int RFSensorIDs[] = {1,2,3,4,5,6,7};
const String RFSensorNames[] = {"RainSensor", "Humidity1", "RainSensor1", "LuxSensor1","MoistureSensor1","MoistureSensor_Test","Doorbell"}; const String RFSensorNames[] = {"RainSensor", "Humidity1", "RainSensor1", "LuxSensor1","notUsed","MoistureSensor_1","Doorbell"};
const String RFSensorType[] = {"RainMass", "Humidity", "Rain", "Light","Moisture","Moisture","Door"}; const String RFSensorUnit[] = {"Impuse", "Humidity", "Rain", "Light","Moisture","%","Door"};
const String RFSensorBaseName = "rf/sensors/"; //const String RFSensorBaseName = "rf/sensors/";
const String RFTargetBaseName = "rf/target/"; //const String RFTargetBaseName = "rf/target/";
/*
const int RFTargetIDs[] = {5,6,7}; const int RFTargetIDs[] = {5,6,7};
const String RFTargetNames[] = {"Light1","Light2","LED1"}; const String RFTargetNames[] = {"Light1","Light2","LED1"};
const String RFTargetType[] = {"Light", "Light","LED"}; const String RFTargetType[] = {"Light", "Light","LED"};
const String ParameterIds[] = {"red","green","blue","brightness", "effect", "effectparameter"}; const String ParameterIds[] = {"red","green","blue","brightness", "effect", "effectparameter"};
+/
/*-------------------RF topics & parameters----------------------*/ /*-------------------RF topics & parameters----------------------*/
//433Mhz MQTT Subjects and keys //433Mhz MQTT Subjects and keys
#define subjectMQTTtoRF Base_Topic Gateway_Name "/commands/MQTTto433" #define subjectMQTTtoRF "/commands/MQTTto433"
#define subjectRFtoMQTT Base_Topic Gateway_Name "/433toMQTT" //#define subjectRFtoMQTT Base_Topic Gateway_Name "/433toMQTT"
#define subjectRFtoMQTT "/433toMQTT"
#define subjectGTWRFtoMQTT Base_Topic Gateway_Name "/433toMQTT" #define subjectGTWRFtoMQTT Base_Topic Gateway_Name "/433toMQTT"
#define RFprotocolKey "433_" // protocol will be defined if a subject contains RFprotocolKey followed by a value of 1 digit #define RFprotocolKey "433_" // protocol will be defined if a subject contains RFprotocolKey followed by a value of 1 digit
#define RFbitsKey "RFBITS_" // bits will be defined if a subject contains RFbitsKey followed by a value of 2 digits #define RFbitsKey "RFBITS_" // bits will be defined if a subject contains RFbitsKey followed by a value of 2 digits
#define repeatRFwMQTT false // do we repeat a received signal by using mqtt with RF gateway #define repeatRFwMQTT false // do we repeat a received signal by using mqtt with RF gateway
#define RFmqttDiscovery true //uncomment this line so as to create a discovery switch for each RF signal received
/* /*
RF supported protocols RF supported protocols
433_1 433_1

View File

@@ -2663,7 +2663,7 @@ void loop() {
RFtoX(); RFtoX();
#endif #endif
#ifdef ZgatewayRF_RHASK #ifdef ZgatewayRF_RHASK
RFtoMQTT(); RFtoX();
#endif #endif
#ifdef ZgatewayRF2 #ifdef ZgatewayRF2
RF2toX(); RF2toX();