Dateien nach "/" hochladen
Regular update
This commit is contained in:
@@ -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
|
||||
|
||||
This gateway enables to:
|
||||
@@ -25,6 +25,8 @@
|
||||
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 ZgatewayRF_RHASK
|
||||
|
||||
#include <RH_ASK.h>
|
||||
@@ -32,316 +34,131 @@
|
||||
#include <SPI.h> // Not actually used but needed to compile
|
||||
#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);
|
||||
RH_ASK driver(500,4,0); // Initialize RH_ASK driver
|
||||
|
||||
void setupRF(){
|
||||
//RF init parameters
|
||||
Serial.println("Setup RF");
|
||||
Log.notice("Setup RF");
|
||||
if (!driver.init())
|
||||
{
|
||||
|
||||
Log.notice("Success");
|
||||
}
|
||||
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 RFtoMQTT(){
|
||||
void RFtoMQTTdiscovery(int deviceID, String sensorName, String sensorUnit) {
|
||||
// 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 buflen = sizeof(buf);
|
||||
|
||||
if (driver.recv(buf, &buflen)) // Non-blocking
|
||||
{
|
||||
// Debug
|
||||
String RFTopicDebug;
|
||||
RFTopicDebug = "rf/sensors/";
|
||||
char RFTopicCharDebug[50];
|
||||
RFTopicDebug.toCharArray(RFTopicCharDebug,50);
|
||||
// pubMQTT(RFTopicCharDebug,"Received Sth");
|
||||
// Serial.println("Received sth");
|
||||
// Debug End
|
||||
|
||||
if (driver.recv(buf, &buflen)) {
|
||||
Log.trace(F("Rcv. RF" CR));
|
||||
# ifdef ESP32
|
||||
Log.trace(F("RF Task running on core :%d" CR), xPortGetCoreID());
|
||||
# endif
|
||||
|
||||
// Decrypt RF message
|
||||
int adressIn, parameterIn, RCin;
|
||||
float payloadIn;
|
||||
MessageDecrypt(buf, &adressIn, ¶meterIn, &payloadIn, &RCin);
|
||||
|
||||
// debug
|
||||
Serial.print("RC_in: ");Serial.println(RCin);
|
||||
// Serial.print("Buf: ");Serial.println(buf);
|
||||
Serial.print("Payload: ");Serial.println(payloadIn);
|
||||
// if (!isAduplicateSignal(MQTTvalue) && MQTTvalue != 0) { // conditions to avoid duplications of RF -->MQTT
|
||||
|
||||
// debug
|
||||
// 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);
|
||||
if (((RCin != RC_old) || (adressIn != adressIn_old)) && (unsigned long)buf != 0) { // new message (no duplicate) if rolling counter is changed
|
||||
RC_old = RCin;
|
||||
adressIn_old = adressIn;
|
||||
|
||||
if(newValue)
|
||||
{
|
||||
// match Sensor number in the array with sensor ID
|
||||
// Detect device in the device list
|
||||
int SensorNo;
|
||||
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])
|
||||
{
|
||||
FoundSensor = true;
|
||||
sensorName = RFSensorNames[SensorNo];
|
||||
sensorUnit = RFSensorUnit[SensorNo];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(FoundSensor)
|
||||
{
|
||||
// trc(SensorNo);
|
||||
// Publish topic
|
||||
// Define topic name
|
||||
String RFTopic;
|
||||
RFTopic = "rf/sensors/" + RFSensorType[SensorNo] + "/" + RFSensorNames[SensorNo];
|
||||
char RFTopicChar[50];
|
||||
RFTopic.toCharArray(RFTopicChar,50);
|
||||
if(!FoundSensor) {
|
||||
Log.notice("Sensor not identified");
|
||||
return;
|
||||
}
|
||||
|
||||
// Publish matrix
|
||||
pubMQTT(RFTopicChar,payloadIn);
|
||||
// pub(subjectRFtoMQTT, RFdata);
|
||||
// Define JSON message
|
||||
StaticJsonDocument<JSON_MSG_BUFFER> RFdataBuffer;
|
||||
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
|
||||
// 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, ¶meterIn, &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);
|
||||
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();
|
||||
Log.notice("duplicate, no publishing\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool MessageDecrypt(byte ByteArray[], int *adress, int *parameter, float *payload, int *RCin)
|
||||
{
|
||||
|
||||
// Serial.print("BytAr: ");Serial.println(ByteArray[0]);
|
||||
|
||||
*adress = (int)ByteArray[0];
|
||||
// *parameter = (int)ByteArray[1];
|
||||
|
||||
// To be replaced by memcopy // todo
|
||||
byte arrayFloat[4];
|
||||
// arrayFloat[0] = ByteArray[2];
|
||||
// arrayFloat[1] = ByteArray[3];
|
||||
// arrayFloat[2] = ByteArray[4];
|
||||
// arrayFloat[3] = ByteArray[5];
|
||||
|
||||
arrayFloat[0] = ByteArray[1];
|
||||
arrayFloat[1] = ByteArray[2];
|
||||
arrayFloat[2] = ByteArray[3];
|
||||
arrayFloat[3] = ByteArray[4];
|
||||
|
||||
// int i = arrayFloat[0] | (arrayFloat[1] << 8) | (arrayFloat[2] << 16) | (arrayFloat[3] << 24);
|
||||
float testfloat;
|
||||
testfloat = *((float*) (arrayFloat));
|
||||
*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];
|
||||
// Serial.print("RC in: ");Serial.println(RCin);
|
||||
|
||||
// Serial.println(messageInput[0]);
|
||||
|
||||
// return CRCCheckOK;
|
||||
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
|
||||
|
||||
1412
ZmqttDiscovery.ino
Normal file
1412
ZmqttDiscovery.ino
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,24 +1,31 @@
|
||||
/*-------------------RF sensor + hardware settings----------------------*/
|
||||
const int RFSensorIDs[] = {1,2,3,4,5,6,7};
|
||||
const String RFSensorNames[] = {"RainSensor", "Humidity1", "RainSensor1", "LuxSensor1","MoistureSensor1","MoistureSensor_Test","Doorbell"};
|
||||
const String RFSensorType[] = {"RainMass", "Humidity", "Rain", "Light","Moisture","Moisture","Door"};
|
||||
const String RFSensorBaseName = "rf/sensors/";
|
||||
const String RFTargetBaseName = "rf/target/";
|
||||
const String RFSensorNames[] = {"RainSensor", "Humidity1", "RainSensor1", "LuxSensor1","notUsed","MoistureSensor_1","Doorbell"};
|
||||
const String RFSensorUnit[] = {"Impuse", "Humidity", "Rain", "Light","Moisture","%","Door"};
|
||||
//const String RFSensorBaseName = "rf/sensors/";
|
||||
//const String RFTargetBaseName = "rf/target/";
|
||||
|
||||
/*
|
||||
const int RFTargetIDs[] = {5,6,7};
|
||||
const String RFTargetNames[] = {"Light1","Light2","LED1"};
|
||||
const String RFTargetType[] = {"Light", "Light","LED"};
|
||||
const String ParameterIds[] = {"red","green","blue","brightness", "effect", "effectparameter"};
|
||||
+/
|
||||
|
||||
/*-------------------RF topics & parameters----------------------*/
|
||||
//433Mhz MQTT Subjects and keys
|
||||
#define subjectMQTTtoRF Base_Topic Gateway_Name "/commands/MQTTto433"
|
||||
#define subjectRFtoMQTT Base_Topic Gateway_Name "/433toMQTT"
|
||||
#define subjectMQTTtoRF "/commands/MQTTto433"
|
||||
//#define subjectRFtoMQTT Base_Topic Gateway_Name "/433toMQTT"
|
||||
#define subjectRFtoMQTT "/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 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 RFmqttDiscovery true //uncomment this line so as to create a discovery switch for each RF signal received
|
||||
|
||||
|
||||
/*
|
||||
RF supported protocols
|
||||
433_1
|
||||
|
||||
Reference in New Issue
Block a user