Platformio integration (#424)
* adaptation of structure to platformio * also remove zgatewayRF315 (not usefull) * replace boolean by bool #155
This commit is contained in:
1 parent
811054c904
commit
8471585d3d
845 files changed
+1200
-623793
No files matched your search
@@ -0,0 +1,193 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This program enables to:
|
||||
- receive MQTT data from a topic and send signals corresponding to the received MQTT data
|
||||
- publish MQTT data to a different topic related to received signals
|
||||
|
||||
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 user_config_h
|
||||
#define user_config_h
|
||||
/*-------------------VERSION----------------------*/
|
||||
#define OMG_VERSION "0.9.2beta"
|
||||
|
||||
/*-------------CONFIGURE WIFIMANAGER-------------(only ESP8266 & SONOFF RFBridge)*/
|
||||
/*
|
||||
* The following parameters are set during the WifiManager setup process:
|
||||
* - wifi_ssid
|
||||
* - wifi_password
|
||||
* - mqtt_user
|
||||
* - mqtt_pass
|
||||
* - mqtt_server
|
||||
* - mqtt_port
|
||||
*
|
||||
* To completely disable WifiManager, define ESPWifiManualSetup.
|
||||
* If you do so, please don't forget to set these variables before compiling
|
||||
*
|
||||
* Otherwise you can provide these credentials on the web interface after connecting
|
||||
* to the access point with your password (SSID: WifiManager_ssid, password: WifiManager_password)
|
||||
*/
|
||||
|
||||
/*-------------DEFINE YOUR NETWORK PARAMETERS BELOW----------------*/
|
||||
#if defined(ESP8266)||defined(ESP32) // for nodemcu, weemos and esp8266
|
||||
//#define ESPWifiManualSetup true //uncomment you don't want to use wifimanager for your credential settings on ESP
|
||||
#endif
|
||||
|
||||
#if defined(ESPWifiManualSetup) // for nodemcu, weemos and esp8266
|
||||
#define wifi_ssid "wifi ssid"
|
||||
#define wifi_password "wifi password"
|
||||
#else // for arduino + W5100
|
||||
const byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0x54, 0x95 }; //W5100 ethernet shield mac adress
|
||||
#endif
|
||||
|
||||
#define WifiManager_password "your_password" //this is going to be the WPA2-PSK password for the initial setup access point
|
||||
#define WifiManager_ssid "OpenMQTTGateway" //this is the network name of the initial setup access point
|
||||
#define WifiManager_ConfigPortalTimeOut 120
|
||||
|
||||
/*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/
|
||||
//MQTT Parameters definition
|
||||
//#define mqtt_server_name "www.mqtt_broker.com" // instead of defining the server by its IP you can define it by its name, uncomment this line and set the correct MQTT server host name
|
||||
char mqtt_user[20] = "your_username"; // not compulsory only if your broker needs authentication
|
||||
char mqtt_pass[30] = "your_password"; // not compulsory only if your broker needs authentication
|
||||
char mqtt_server[40] = "192.168.1.17";
|
||||
char mqtt_port[6] = "1883";
|
||||
|
||||
#define Gateway_Name "OpenMQTTGateway"
|
||||
|
||||
//uncomment the line below to integrate msg value into the subject when receiving
|
||||
//#define valueAsASubject true
|
||||
|
||||
/*-------------DEFINE THE MODULES YOU WANT BELOW----------------*/
|
||||
//Addons and module management, comment the Z line
|
||||
|
||||
#define ZgatewayRF "RF" //ESP8266, Arduino, ESP32
|
||||
#define ZgatewayIR "IR" //ESP8266, Arduino, Sonoff RF Bridge
|
||||
//#define ZgatewayLORA "LORA" //ESP8266, Arduino, ESP32
|
||||
//#define ZgatewayPilight "Pilight" //ESP8266, Arduino, ESP32
|
||||
#define ZgatewayBT "BT" //ESP8266, ESP32
|
||||
//#define ZgatewayRF2 "RF2" //ESP8266, Arduino, ESP32
|
||||
//#define ZgatewaySRFB "SRFB" // Sonoff RF Bridge
|
||||
//#define Zgateway2G "2G" //ESP8266, Arduino, ESP32
|
||||
//#define ZgatewayRFM69 "RFM69" //ESP8266, Arduino, ESP32
|
||||
//#define ZactuatorONOFF "ONOFF" //ESP8266, Arduino, ESP32, Sonoff RF Bridge
|
||||
//#define ZsensorINA226 "INA226" //ESP8266, Arduino, ESP32
|
||||
//#define ZsensorHCSR501 "HCSR501" //ESP8266, Arduino, ESP32, Sonoff RF Bridge
|
||||
//#define ZsensorADC "ADC" //ESP8266, Arduino, ESP32
|
||||
//#define ZsensorBH1750 "BH1750" //ESP8266, Arduino, ESP32
|
||||
//#define ZsensorTSL2561 "TSL2561" //ESP8266, Arduino, ESP32
|
||||
//#define ZsensorBME280 "BME280" //ESP8266, Arduino, ESP32
|
||||
//#define ZsensorDHT "DHT" //ESP8266, Arduino, ESP32, Sonoff RF Bridge
|
||||
//#define ZsensorGPIOKeyCode "GPIOKeyCode" //ESP8266, Arduino, ESP32
|
||||
//#define ZsensorGPIOInput "GPIOInput" //ESP8266, Arduino, ESP32
|
||||
//#define ZmqttDiscovery "HADiscovery"//ESP8266, Arduino, ESP32, Sonoff RF Bridge
|
||||
|
||||
/*-------------DEFINE YOUR ADVANCED NETWORK PARAMETERS BELOW----------------*/
|
||||
//#define MDNS_SD //uncomment if you want to use mdns for discovering automatically your ip server, please note that MDNS with ESP32 can cause the BLE to not work
|
||||
#define maxMQTTretry 4 //maximum MQTT connection attempts before going to wifi setup
|
||||
|
||||
//set minimum quality of signal so it ignores AP's under that quality
|
||||
#define MinimumWifiSignalQuality 8
|
||||
|
||||
// these values are only used if no dhcp configuration is available
|
||||
const byte ip[] = { 192, 168, 1, 99 }; //ip adress
|
||||
const byte gateway[] = { 0, 0, 0, 0 }; //ip adress, if first value is different from 0 advanced config network will be used and you should fill gateway & dns
|
||||
const byte Dns[] = { 0, 0, 0, 0 }; //ip adress, if first value is different from 0 advanced config network will be used and you should fill gateway & dns
|
||||
const byte subnet[] = { 255, 255, 255, 0 }; //ip adress
|
||||
|
||||
/*-------------DEFINE YOUR MQTT ADVANCED PARAMETERS BELOW----------------*/
|
||||
#define Base_Topic "home/"
|
||||
#define version_Topic Base_Topic Gateway_Name "/version"
|
||||
#define will_Topic Base_Topic Gateway_Name "/LWT"
|
||||
#define will_QoS 0
|
||||
#define will_Retain true
|
||||
#define will_Message "offline"
|
||||
#define Gateway_AnnouncementMsg "online"
|
||||
|
||||
#define jsonPublishing true //comment if you don't want to use Json publishing (one topic for all the parameters)
|
||||
//example home/OpenMQTTGateway_ESP32_DEVKIT/BTtoMQTT/4XXXXXXXXXX4 {"rssi":-63,"servicedata":"fe0000000000000000000000000000000000000000"}
|
||||
#define jsonReceiving true //comment if you don't want to use Json reception analysis
|
||||
|
||||
//#define simplePublishing true //comment if you don't want to use simple publishing (one topic for one parameter)
|
||||
//example
|
||||
// home/OpenMQTTGateway_ESP32_DEVKIT/BTtoMQTT/4XXXXXXXXXX4/rssi -63.0
|
||||
// home/OpenMQTTGateway_ESP32_DEVKIT/BTtoMQTT/4XXXXXXXXXX4/servicedata fe0000000000000000000000000000000000000000
|
||||
#define simpleReceiving true //comment if you don't want to use old way reception analysis
|
||||
|
||||
/*-------------DEFINE YOUR OTA PARAMETERS BELOW----------------*/
|
||||
#define ota_hostname Gateway_Name
|
||||
#define ota_password "OTAPASSWORD"
|
||||
#define ota_port 8266
|
||||
|
||||
/*-------------DEFINE PINs FOR STATUS LEDs----------------*/
|
||||
#ifdef ESP8266
|
||||
#define led_receive 40
|
||||
#define led_send 42
|
||||
#define led_info 44
|
||||
#elif ESP32
|
||||
#define led_receive 40
|
||||
#define led_send 42
|
||||
#define led_info 44
|
||||
#elif __AVR_ATmega2560__ //arduino mega
|
||||
#define led_receive 40
|
||||
#define led_send 42
|
||||
#define led_info 44
|
||||
#else //arduino uno/nano
|
||||
#define led_receive 40
|
||||
#define led_send 42
|
||||
#define led_info 44
|
||||
#endif
|
||||
|
||||
// VCC ------------D|-----------/\/\/\/\ ----------------- Arduino PIN
|
||||
// LED Resistor 270-510R
|
||||
|
||||
/*----------------------------OTHER PARAMETERS-----------------------------*/
|
||||
/*-------------------CHANGING THEM IS NOT COMPULSORY-----------------------*/
|
||||
/*----------------------------USER PARAMETERS-----------------------------*/
|
||||
#ifdef ZgatewaySRFB
|
||||
#define SERIAL_BAUD 19200
|
||||
#else
|
||||
#define SERIAL_BAUD 115200
|
||||
#endif
|
||||
/*--------------MQTT general topics-----------------*/
|
||||
// global MQTT subject listened by the gateway to execute commands (send RF, IR or others)
|
||||
#define subjectMQTTtoX Base_Topic Gateway_Name "/commands/#"
|
||||
#define subjectMultiGTWKey "toMQTT"
|
||||
#define subjectGTWSendKey "MQTTto"
|
||||
|
||||
//variables to avoid duplicates
|
||||
#define time_avoid_duplicate 3000 // if you want to avoid duplicate mqtt message received set this to > 0, the value is the time in milliseconds during which we don't publish duplicates
|
||||
|
||||
#if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
|
||||
#define JSON_MSG_BUFFER 512 // Json message max buffer size, don't put 1024 or higher it is causing unexpected behaviour on ESP8266
|
||||
#else // boards with smaller memory
|
||||
#define JSON_MSG_BUFFER 64 // Json message max buffer size, don't put 1024 or higher it is causing unexpected behaviour on ESP8266
|
||||
#endif
|
||||
|
||||
#define TimeBetweenReadingSYS 120000 // time between system readings (like memory)
|
||||
#define subjectSYStoMQTT Base_Topic Gateway_Name "/SYStoMQTT"
|
||||
|
||||
//#define subjectTRACEtoMQTT Base_Topic Gateway_Name "/log" //uncomment if you want to see traces on /log topic
|
||||
|
||||
/*-------------------ACTIVATE TRACES----------------------*/
|
||||
#define TRACE 1 // commented = trace off, uncommented = trace on
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
OpenMQTTGateway Addon - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
Output pin High or Low
|
||||
|
||||
Copyright: (c)Florian ROBERT
|
||||
|
||||
Contributors:
|
||||
- 1technophile
|
||||
|
||||
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 ZactuatorONOFF
|
||||
|
||||
void setupONOFF(){
|
||||
trc(F("ACTUATOR_ONOFF_PIN"));
|
||||
trc(ACTUATOR_ONOFF_PIN);
|
||||
//init
|
||||
pinMode(ACTUATOR_ONOFF_PIN, OUTPUT);
|
||||
trc(F("Set to OFF"));
|
||||
digitalWrite(ACTUATOR_ONOFF_PIN, LOW);
|
||||
trc(F("ZactuatorONOFF setup done "));
|
||||
}
|
||||
|
||||
void MQTTtoONOFF(char * topicOri, char * datacallback){
|
||||
|
||||
int boolSWITCHTYPE;
|
||||
if (strcmp(datacallback, "ON") == 0) boolSWITCHTYPE = 1;
|
||||
if (strcmp(datacallback, "OFF") == 0) boolSWITCHTYPE = 0;
|
||||
|
||||
if (strcmp(topicOri,subjectMQTTtoONOFF) == 0){
|
||||
trc(F("MQTTtoONOFF data analysis"));
|
||||
trc(boolSWITCHTYPE);
|
||||
digitalWrite(ACTUATOR_ONOFF_PIN, boolSWITCHTYPE);
|
||||
// we acknowledge the sending by publishing the value to an acknowledgement topic
|
||||
pub(subjectGTWONOFFtoMQTT, datacallback);
|
||||
}
|
||||
}
|
||||
void MQTTtoONOFF(char * topicOri, JsonObject& ONOFFdata){
|
||||
|
||||
if (strcmp(topicOri,subjectMQTTtoONOFF) == 0){
|
||||
trc(F("MQTTtoONOFF json data analysis"));
|
||||
int boolSWITCHTYPE = ONOFFdata["switchType"] | 99;
|
||||
if (boolSWITCHTYPE != 99) {
|
||||
trc(F("MQTTtoONOFF boolSWITCHTYPE ok"));
|
||||
trc(boolSWITCHTYPE);
|
||||
digitalWrite(ACTUATOR_ONOFF_PIN, boolSWITCHTYPE);
|
||||
// we acknowledge the sending by publishing the value to an acknowledgement topic
|
||||
pub(subjectGTWONOFFtoMQTT, ONOFFdata);
|
||||
}else{
|
||||
trc(F("MQTTtoONOFF Fail reading from json"));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,163 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This gateway enables to:
|
||||
- receive MQTT data from a topic and send SMS corresponding to the received MQTT data
|
||||
- publish MQTT data to a different topic related to received SMS
|
||||
|
||||
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 Zgateway2G
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <A6lib.h> // library for controling A6 or A7 module
|
||||
|
||||
// Instantiate the library with TxPin, RxPin.
|
||||
A6lib A6l(_2G_TX_PIN, _2G_RX_PIN); //D6 to A6 RX, D7 to A6 TX
|
||||
|
||||
int unreadSMSLocs[50] = {0};
|
||||
int unreadSMSNum = 0;
|
||||
SMSmessage sms;
|
||||
|
||||
void setup2G(){
|
||||
|
||||
trc(F("_2G_TX_PIN "));
|
||||
trc(_2G_TX_PIN);
|
||||
trc(F("_2G_RX_PIN "));
|
||||
trc(_2G_RX_PIN);
|
||||
setupGSM(false);
|
||||
trc(F("Zgateway2G setup done "));
|
||||
}
|
||||
|
||||
void setupGSM(bool deleteSMS){
|
||||
trc(F("Init 2G module: "));
|
||||
trc(_2G_PWR_PIN);
|
||||
delay(1000);
|
||||
// Power-cycle the module to reset it.
|
||||
A6l.powerCycle(_2G_PWR_PIN);
|
||||
trc(F("waiting for network connection"));
|
||||
A6l.blockUntilReady(_2G_MODULE_BAUDRATE);
|
||||
trc(F("A6/A7 gsm ready"));
|
||||
signalStrengthAnalysis();
|
||||
delay(1000);
|
||||
// deleting all sms
|
||||
if (deleteSMS){
|
||||
if (A6l.deleteSMS(1,4) == A6_OK ) {
|
||||
trc(F("delete SMS OK"));
|
||||
}else{
|
||||
trc(F("delete SMS KO"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void signalStrengthAnalysis(){
|
||||
int signalStrength = 0;
|
||||
signalStrength = A6l.getSignalStrength();
|
||||
trc(F("Signal strength: "));
|
||||
trc(signalStrength);
|
||||
if (signalStrength < _2G_MIN_SIGNAL || signalStrength > _2G_MAX_SIGNAL ) {
|
||||
trc(F("Signal too low restart the module"));
|
||||
setupGSM(false); // if we are below or above a threshold signal we relaunch the setup of GSM module
|
||||
}
|
||||
}
|
||||
|
||||
bool _2GtoMQTT(){
|
||||
// Get the memory locations of unread SMS messages.
|
||||
unreadSMSNum = A6l.getUnreadSMSLocs(unreadSMSLocs, 512);
|
||||
trc(F("Creating SMS buffer"));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject& SMSdata = jsonBuffer.createObject();
|
||||
for (int i = 0; i < unreadSMSNum; i++) {
|
||||
trc(F("New message at index: "));
|
||||
trc(unreadSMSNum);
|
||||
sms = A6l.readSMS(unreadSMSLocs[i]);
|
||||
SMSdata.set("message", (char *)sms.message.c_str());
|
||||
SMSdata.set("date", (char *)sms.date.c_str());
|
||||
SMSdata.set("phone", (char *)sms.number.c_str());
|
||||
A6l.deleteSMS(unreadSMSLocs[i]); // we delete the SMS received
|
||||
trc(F("Adv data 2GtoMQTT"));
|
||||
pub(subject2GtoMQTT,SMSdata);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
#ifdef simpleReceiving
|
||||
void MQTTto2G(char * topicOri, char * datacallback) {
|
||||
|
||||
String data = datacallback;
|
||||
String topic = topicOri;
|
||||
|
||||
if (topic == subjectMQTTto2G) {
|
||||
trc(F("MQTTto2G data analysis"));
|
||||
// 2G DATA ANALYSIS
|
||||
String phone_number = "";
|
||||
int pos0 = topic.lastIndexOf(_2GPhoneKey);
|
||||
if (pos0 != -1) {
|
||||
pos0 = pos0 + strlen(_2GPhoneKey);
|
||||
phone_number = topic.substring(pos0);
|
||||
trc(F("MQTTto2G phone ok"));
|
||||
trc(phone_number);
|
||||
trc(F("MQTTto2G sms"));
|
||||
trc(data);
|
||||
if (A6l.sendSMS(phone_number,data) == A6_OK ) {
|
||||
trc(F("SMS OK"));
|
||||
// Acknowledgement to the GTW2G topic
|
||||
pub(subjectGTW2GtoMQTT, datacallback);// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
}else{
|
||||
trc(F("SMS KO"));
|
||||
// Acknowledgement to the GTW2G topic
|
||||
pub(subjectGTW2GtoMQTT, "SMS KO");// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
}
|
||||
}else{
|
||||
trc(F("MQTTto2G Fail reading phone number"));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef jsonReceiving
|
||||
void MQTTto2G(char * topicOri, JsonObject& SMSdata) {
|
||||
|
||||
if (strcmp(topicOri,subjectMQTTto2G) == 0){
|
||||
const char * sms = SMSdata["message"];
|
||||
const char * phone = SMSdata["phone"];
|
||||
trc(F("MQTTto2G json data analysis"));
|
||||
if (sms && phone) {
|
||||
trc(F("MQTTto2G sms & phone ok"));
|
||||
trc(sms);
|
||||
if (A6l.sendSMS(String(phone),String(sms)) == A6_OK ) {
|
||||
trc(F("SMS OK"));
|
||||
// Acknowledgement to the GTW2G topic
|
||||
pub(subjectGTW2GtoMQTT, SMSdata);// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
}else{
|
||||
trc(F("SMS KO"));
|
||||
pub(subjectGTW2GtoMQTT, "SMS KO");// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
}
|
||||
}else{
|
||||
trc(F("MQTTto2G Fail reading from json"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,694 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal/BLE and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This gateway enables to:
|
||||
- publish MQTT data to a different topic related to BLE devices rssi signal
|
||||
- publish MQTT data related to mi flora temperature, moisture, fertility and lux
|
||||
- publish MQTT data related to mi jia indoor temperature & humidity sensor
|
||||
|
||||
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/>.
|
||||
|
||||
Thanks to wolass https://github.com/wolass for suggesting me HM 10 and dinosd https://github.com/dinosd/BLE_PROXIMITY for inspiring me how to implement the gateway
|
||||
*/
|
||||
#include "User_config.h"
|
||||
|
||||
#ifdef ZgatewayBT
|
||||
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
vector<BLEdevice> devices;
|
||||
|
||||
void setWorBMac(char * mac , bool isWhite){
|
||||
bool foundMac = false;
|
||||
for(vector<BLEdevice>::iterator p = devices.begin(); p != devices.end(); ++p){
|
||||
if((strcmp(p->macAdr,mac) == 0)){
|
||||
p->isWhtL = isWhite;
|
||||
p->isBlkL = !isWhite;
|
||||
foundMac = true;
|
||||
}
|
||||
}
|
||||
if (!foundMac) {
|
||||
BLEdevice device;
|
||||
strcpy( device.macAdr,mac );
|
||||
device.isDisc = false;
|
||||
device.isWhtL = isWhite;
|
||||
device.isBlkL = !isWhite;
|
||||
devices.push_back(device);
|
||||
}
|
||||
}
|
||||
|
||||
bool oneWhite(){
|
||||
for(vector<BLEdevice>::iterator p = devices.begin(); p != devices.end(); ++p){
|
||||
if(p->isWhtL) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isWhite(char * mac){
|
||||
for(vector<BLEdevice>::iterator p = devices.begin(); p != devices.end(); ++p){
|
||||
if((strcmp(p->macAdr,mac) == 0)){
|
||||
return p->isWhtL;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isBlack(char * mac){
|
||||
for(vector<BLEdevice>::iterator p = devices.begin(); p != devices.end(); ++p){
|
||||
if((strcmp(p->macAdr,mac) == 0)){
|
||||
return p->isBlkL;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isDiscovered(char * mac){
|
||||
for(vector<BLEdevice>::iterator p = devices.begin(); p != devices.end(); ++p){
|
||||
if((strcmp(p->macAdr,mac) == 0)){
|
||||
return p->isDisc;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void dumpDevices(){
|
||||
for(vector<BLEdevice>::iterator p = devices.begin(); p != devices.end(); ++p){
|
||||
trc(p->macAdr);
|
||||
trc(p->isDisc);
|
||||
trc(p->isWhtL);
|
||||
trc(p->isBlkL);
|
||||
}
|
||||
}
|
||||
|
||||
void strupp(char* beg)
|
||||
{
|
||||
while (*beg = toupper(*beg))
|
||||
++beg;
|
||||
}
|
||||
|
||||
#ifdef ZmqttDiscovery
|
||||
void MiFloraDiscovery(char * mac){
|
||||
#define MiFloraparametersCount 4
|
||||
trc(F("MiFloraDiscovery"));
|
||||
char * MiFlorasensor[MiFloraparametersCount][8] = {
|
||||
{"sensor", "MiFlora-lux", mac, "illuminance","{{ value_json.lux | is_defined }}","", "", "lu"} ,
|
||||
{"sensor", "MiFlora-tem", mac,"temperature","{{ value_json.tem | is_defined }}","", "", "°C"} ,
|
||||
{"sensor", "MiFlora-fer", mac,"","{{ value_json.fer | is_defined }}","", "", ""} ,
|
||||
{"sensor", "MiFlora-moi", mac,"","{{ value_json.moi | is_defined }}","", "", "%"}
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
};
|
||||
|
||||
for (int i=0;i<MiFloraparametersCount;i++){
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(MiFlorasensor[i][1]);
|
||||
String discovery_topic = String(subjectBTtoMQTT) + String(mac);
|
||||
String unique_id = String(mac) + "-" + MiFlorasensor[i][1];
|
||||
createDiscovery(MiFlorasensor[i][0],
|
||||
(char *)discovery_topic.c_str(), MiFlorasensor[i][1], (char *)unique_id.c_str(),
|
||||
will_Topic, MiFlorasensor[i][3], MiFlorasensor[i][4],
|
||||
MiFlorasensor[i][5], MiFlorasensor[i][6], MiFlorasensor[i][7],
|
||||
0,"","",true,"");
|
||||
}
|
||||
BLEdevice device;
|
||||
strcpy( device.macAdr, mac );
|
||||
device.isDisc = true;
|
||||
device.isWhtL = false;
|
||||
device.isBlkL = false;
|
||||
devices.push_back(device);
|
||||
}
|
||||
|
||||
void MiJiaDiscovery(char * mac){
|
||||
#define MiJiaparametersCount 3
|
||||
trc(F("MiJiaDiscovery"));
|
||||
char * MiJiasensor[MiJiaparametersCount][8] = {
|
||||
{"sensor", "MiJia-batt", mac, "battery","{{ value_json.batt | is_defined }}","", "", "V"} ,
|
||||
{"sensor", "MiJia-tem", mac,"temperature","{{ value_json.tem | is_defined }}","", "", "°C"} ,
|
||||
{"sensor", "MiJia-hum", mac,"humidity","{{ value_json.hum | is_defined }}","", "", "%"}
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
};
|
||||
|
||||
for (int i=0;i<MiJiaparametersCount;i++){
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(MiJiasensor[i][1]);
|
||||
String discovery_topic = String(subjectBTtoMQTT) + String(mac);
|
||||
String unique_id = String(mac) + "-" + MiJiasensor[i][1];
|
||||
createDiscovery(MiJiasensor[i][0],
|
||||
(char *)discovery_topic.c_str(), MiJiasensor[i][1], (char *)unique_id.c_str(),
|
||||
will_Topic, MiJiasensor[i][3], MiJiasensor[i][4],
|
||||
MiJiasensor[i][5], MiJiasensor[i][6], MiJiasensor[i][7],
|
||||
0,"","",true,"");
|
||||
}
|
||||
BLEdevice device;
|
||||
strcpy( device.macAdr, mac );
|
||||
device.isDisc = true;
|
||||
device.isWhtL = false;
|
||||
device.isBlkL = false;
|
||||
devices.push_back(device);
|
||||
}
|
||||
|
||||
void LYWSD02Discovery(char * mac){
|
||||
#define LYWSD02parametersCount 3
|
||||
trc(F("LYWSD02Discovery"));
|
||||
char * LYWSD02sensor[LYWSD02parametersCount][8] = {
|
||||
{"sensor", "LYWSD02-batt", mac, "battery","{{ value_json.batt | is_defined }}","", "", "V"} ,
|
||||
{"sensor", "LYWSD02-tem", mac,"temperature","{{ value_json.tem | is_defined }}","", "", "°C"} ,
|
||||
{"sensor", "LYWSD02-hum", mac,"humidity","{{ value_json.hum | is_defined }}","", "", "%"}
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
};
|
||||
|
||||
for (int i=0;i<LYWSD02parametersCount;i++){
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(LYWSD02sensor[i][1]);
|
||||
String discovery_topic = String(subjectBTtoMQTT) + String(mac);
|
||||
String unique_id = String(mac) + "-" + LYWSD02sensor[i][1];
|
||||
createDiscovery(LYWSD02sensor[i][0],
|
||||
(char *)discovery_topic.c_str(), LYWSD02sensor[i][1], (char *)unique_id.c_str(),
|
||||
will_Topic, LYWSD02sensor[i][3], LYWSD02sensor[i][4],
|
||||
LYWSD02sensor[i][5], LYWSD02sensor[i][6], LYWSD02sensor[i][7],
|
||||
0,"","",true,"");
|
||||
}
|
||||
BLEdevice device;
|
||||
strcpy( device.macAdr, mac );
|
||||
device.isDisc = true;
|
||||
device.isWhtL = false;
|
||||
device.isBlkL = false;
|
||||
devices.push_back(device);
|
||||
}
|
||||
|
||||
void CLEARGRASSTRHDiscovery(char * mac){
|
||||
#define CLEARGRASSTRHparametersCount 3
|
||||
trc(F("CLEARGRASSTRHDiscovery"));
|
||||
char * CLEARGRASSTRHsensor[CLEARGRASSTRHparametersCount][8] = {
|
||||
{"sensor", "CLEARGRASSTRH-batt", mac, "battery","{{ value_json.batt | is_defined }}","", "", "V"} ,
|
||||
{"sensor", "CLEARGRASSTRH-tem", mac,"temperature","{{ value_json.tem | is_defined }}","", "", "°C"} ,
|
||||
{"sensor", "CLEARGRASSTRH-hum", mac,"humidity","{{ value_json.hum | is_defined }}","", "", "%"}
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
};
|
||||
|
||||
for (int i=0;i<CLEARGRASSTRHparametersCount;i++){
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(CLEARGRASSTRHsensor[i][1]);
|
||||
String discovery_topic = String(subjectBTtoMQTT) + String(mac);
|
||||
String unique_id = String(mac) + "-" + CLEARGRASSTRHsensor[i][1];
|
||||
createDiscovery(CLEARGRASSTRHsensor[i][0],
|
||||
(char *)discovery_topic.c_str(), CLEARGRASSTRHsensor[i][1], (char *)unique_id.c_str(),
|
||||
will_Topic, CLEARGRASSTRHsensor[i][3], CLEARGRASSTRHsensor[i][4],
|
||||
CLEARGRASSTRHsensor[i][5], CLEARGRASSTRHsensor[i][6], CLEARGRASSTRHsensor[i][7],
|
||||
0,"","",true,"");
|
||||
}
|
||||
BLEdevice device;
|
||||
strcpy( device.macAdr, mac );
|
||||
device.isDisc = true;
|
||||
device.isWhtL = false;
|
||||
device.isBlkL = false;
|
||||
devices.push_back(device);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef ESP32
|
||||
/*
|
||||
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
|
||||
Ported to Arduino ESP32 by Evandro Copercini
|
||||
*/
|
||||
// core task implementation thanks to https://techtutorialsx.com/2017/05/09/esp32-running-code-on-a-specific-core/
|
||||
|
||||
#include <BLEDevice.h>
|
||||
#include <BLEUtils.h>
|
||||
#include <BLEScan.h>
|
||||
#include <BLEAdvertisedDevice.h>
|
||||
#include "soc/timer_group_struct.h"
|
||||
#include "soc/timer_group_reg.h"
|
||||
|
||||
//Time used to wait for an interval before resending BLE infos
|
||||
unsigned long timeBLE= 0;
|
||||
|
||||
//core on which the BLE detection task will run
|
||||
static int taskCore = 0;
|
||||
|
||||
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
|
||||
void onResult(BLEAdvertisedDevice advertisedDevice) {
|
||||
trc(F("Creating BLE buffer"));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject& BLEdata = jsonBuffer.createObject();
|
||||
String mac_adress = advertisedDevice.getAddress().toString().c_str();
|
||||
BLEdata.set("id", (char *)mac_adress.c_str());
|
||||
mac_adress.replace(":","");
|
||||
mac_adress.toUpperCase();
|
||||
String mactopic = subjectBTtoMQTT + mac_adress;
|
||||
char mac[mac_adress.length()+1];
|
||||
mac_adress.toCharArray(mac,mac_adress.length()+1);
|
||||
trc("device detected");
|
||||
trc(mac);
|
||||
if((!oneWhite() || isWhite(mac)) && !isBlack(mac)){ //if not black listed mac we go AND if we have no white mac or this mac is white we go out
|
||||
if (advertisedDevice.haveName()) BLEdata.set("name", (char *)advertisedDevice.getName().c_str());
|
||||
if (advertisedDevice.haveManufacturerData()) BLEdata.set("manufacturerdata", (char *)advertisedDevice.getManufacturerData().c_str());
|
||||
if (advertisedDevice.haveRSSI()) BLEdata.set("rssi", (int) advertisedDevice.getRSSI());
|
||||
if (advertisedDevice.haveTXPower()) BLEdata.set("txpower", (int8_t) advertisedDevice.getTXPower());
|
||||
#ifdef subjectHomePresence
|
||||
if (advertisedDevice.haveRSSI()) haRoomPresence(BLEdata);// this device has an rssi in consequence we can use it for home assistant room presence component
|
||||
#endif
|
||||
if (advertisedDevice.haveServiceData()){
|
||||
trc(F("Get services data :"));
|
||||
int serviceDataCount = advertisedDevice.getServiceDataCount();
|
||||
trc(serviceDataCount);
|
||||
for (int j = 0; j < serviceDataCount;j++){
|
||||
std::string serviceData = advertisedDevice.getServiceData(j);
|
||||
int serviceDataLength = serviceData.length();
|
||||
String returnedString = "";
|
||||
for (int i=0; i<serviceDataLength; i++)
|
||||
{
|
||||
int a = serviceData[i];
|
||||
if (a < 16) {
|
||||
returnedString = returnedString + "0";
|
||||
}
|
||||
returnedString = returnedString + String(a,HEX);
|
||||
}
|
||||
char service_data[returnedString.length()+1];
|
||||
returnedString.toCharArray(service_data,returnedString.length()+1);
|
||||
service_data[returnedString.length()] = '\0';
|
||||
#ifdef pubBLEServiceData
|
||||
BLEdata.set("servicedata", service_data);
|
||||
BLEdata.set("servicedatauuid", (char *)advertisedDevice.getServiceDataUUID(j).toString().c_str());
|
||||
#endif
|
||||
pub((char *)mactopic.c_str(),BLEdata);
|
||||
if (strstr(BLEdata["servicedatauuid"].as<char*>(),"fe95") != NULL){
|
||||
trc("Processing BLE device data");
|
||||
int pos = -1;
|
||||
pos = strpos(service_data,"209800");
|
||||
if (pos != -1){
|
||||
trc(F("mi flora data reading"));
|
||||
//example "servicedata":"71209800bc63b6658d7cc40d0910023200"
|
||||
#ifdef ZmqttDiscovery
|
||||
if(!isDiscovered(mac)) MiFloraDiscovery(mac);
|
||||
#endif
|
||||
process_data(pos - 24,service_data,mac);
|
||||
}
|
||||
pos = -1;
|
||||
pos = strpos(service_data,"20aa01");
|
||||
if (pos != -1){
|
||||
trc(F("mi jia data reading"));
|
||||
#ifdef ZmqttDiscovery
|
||||
if(!isDiscovered(mac)) MiJiaDiscovery(mac);
|
||||
#endif
|
||||
process_data(pos - 26,service_data,mac);
|
||||
}
|
||||
pos = -1;
|
||||
pos = strpos(service_data,"205b04");
|
||||
if (pos != -1){
|
||||
trc(F("LYWSD02 data reading"));
|
||||
//example "servicedata":"70205b04b96ab883c8593f09041002e000"
|
||||
#ifdef ZmqttDiscovery
|
||||
if(!isDiscovered(mac)) LYWSD02Discovery(mac);
|
||||
#endif
|
||||
process_data(pos - 24,service_data,mac);
|
||||
}
|
||||
pos = -1;
|
||||
pos = strpos(service_data,"304703");
|
||||
if (pos != -1){
|
||||
trc(F("ClearGrass T RH data reading"));
|
||||
//example "servicedata":"5030470340743e10342d58041002d6000a100164"
|
||||
#ifdef ZmqttDiscovery
|
||||
if(!isDiscovered(mac)) CLEARGRASSTRHDiscovery(mac);
|
||||
#endif
|
||||
process_data(pos - 26,service_data,mac);
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
pub((char *)mactopic.c_str(),BLEdata); // publish device even if there is no service data
|
||||
}
|
||||
}else{
|
||||
trc(F("Filtered mac device"));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void BLEscan(){
|
||||
|
||||
TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
|
||||
TIMERG0.wdt_feed=1;
|
||||
TIMERG0.wdt_wprotect=0;
|
||||
|
||||
BLEDevice::init("");
|
||||
BLEScan* pBLEScan = BLEDevice::getScan(); //create new scan
|
||||
MyAdvertisedDeviceCallbacks myCallbacks;
|
||||
pBLEScan->setAdvertisedDeviceCallbacks(&myCallbacks);
|
||||
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
|
||||
BLEScanResults foundDevices = pBLEScan->start(Scan_duration);
|
||||
}
|
||||
|
||||
void coreTask( void * pvParameters ){
|
||||
|
||||
String taskMessage = "BT Task running on core ";
|
||||
taskMessage = taskMessage + xPortGetCoreID();
|
||||
|
||||
while(true){
|
||||
trc(taskMessage);
|
||||
delay(BLEinterval);
|
||||
BLEscan();
|
||||
}
|
||||
}
|
||||
|
||||
void setupBT(){
|
||||
BLEinterval = TimeBtw_Read;
|
||||
trc(F("BLEinterval btw scans"));
|
||||
trc(BLEinterval);
|
||||
// we setup a task with priority one to avoid conflict with other gateways
|
||||
xTaskCreatePinnedToCore(
|
||||
coreTask, /* Function to implement the task */
|
||||
"coreTask", /* Name of the task */
|
||||
10000, /* Stack size in words */
|
||||
NULL, /* Task input parameter */
|
||||
1, /* Priority of the task */
|
||||
NULL, /* Task handle. */
|
||||
taskCore); /* Core where the task should run */
|
||||
trc(F("ZgatewayBT multicore ESP32 setup done "));
|
||||
}
|
||||
|
||||
bool BTtoMQTT(){ // for on demand BLE scans
|
||||
BLEscan();
|
||||
}
|
||||
#else // arduino or ESP8266 working with HM10/11
|
||||
|
||||
#include <SoftwareSerial.h>
|
||||
|
||||
#define STRING_MSG "OK+DISC:"
|
||||
#define QUESTION_MSG "AT+DISA?"
|
||||
#define RESPONSE_MSG "OK+DISIS"
|
||||
#define RESP_END_MSG "OK+DISCE"
|
||||
#define SETUP_MSG "OK+RESET"
|
||||
|
||||
SoftwareSerial softserial(BT_RX, BT_TX);
|
||||
|
||||
String returnedString = "";
|
||||
unsigned long timebt = 0;
|
||||
|
||||
// this struct define which parts of the hexadecimal chain we extract and what to do with these parts
|
||||
struct decompose d[6] = {{"mac",16,12,true},{"typ",28,2,false},{"rsi",30,2,false},{"rdl",32,2,false},{"sty",44,4,true},{"rda",34,60,false}};
|
||||
|
||||
void setupBT() {
|
||||
BLEinterval = TimeBtw_Read;
|
||||
trc(F("BLEinterval btw scans"));
|
||||
trc(BLEinterval);
|
||||
softserial.begin(9600);
|
||||
softserial.print(F("AT+ROLE1"));
|
||||
delay(100);
|
||||
softserial.print(F("AT+IMME1"));
|
||||
delay(100);
|
||||
softserial.print(F("AT+RESET"));
|
||||
delay(100);
|
||||
#ifdef HM_BLUE_LED_STOP
|
||||
softserial.print(F("AT+PIO11")); // When not connected (as in BLE mode) the LED is off. When connected the LED is solid on.
|
||||
#endif
|
||||
delay(100);
|
||||
trc(F("ZgatewayBT HM1X setup done "));
|
||||
}
|
||||
|
||||
bool BTtoMQTT() {
|
||||
|
||||
//extract serial data from module in hexa format
|
||||
while (softserial.available() > 0) {
|
||||
int a = softserial.read();
|
||||
if (a < 16) {
|
||||
returnedString = returnedString + "0";
|
||||
}
|
||||
returnedString = returnedString + String(a,HEX);
|
||||
}
|
||||
|
||||
if (millis() > (timebt + BLEinterval)) {//retriving data
|
||||
timebt = millis();
|
||||
#if defined(ESP8266)
|
||||
yield();
|
||||
#endif
|
||||
if (returnedString != "") {
|
||||
size_t pos = 0;
|
||||
while ((pos = returnedString.lastIndexOf(BLEdelimiter)) != -1) {
|
||||
#if defined(ESP8266)
|
||||
yield();
|
||||
#endif
|
||||
String token = returnedString.substring(pos);
|
||||
returnedString.remove(pos,returnedString.length() );
|
||||
char token_char[token.length()+1];
|
||||
token.toCharArray(token_char, token.length()+1);
|
||||
trc(token);
|
||||
if ( token.length() > 60){// we extract data only if we have detailled infos
|
||||
for(int i =0; i<6;i++)
|
||||
{
|
||||
extract_char(token_char,d[i].extract,d[i].start, d[i].len ,d[i].reverse, false);
|
||||
if (i == 3) d[5].len = (int)strtol(d[i].extract, NULL, 16) * 2; // extracting the length of the rest data
|
||||
}
|
||||
|
||||
if((strlen(d[0].extract)) == 12) // if a mac adress is detected we publish it
|
||||
{
|
||||
trc(F("Creating BLE buffer"));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject& BLEdata = jsonBuffer.createObject();
|
||||
strupp(d[0].extract);
|
||||
if(isBlack(d[0].extract)) return false; //if black listed mac we go out
|
||||
if(oneWhite() && !isWhite(d[0].extract)) return false; //if we have at least one white mac and this mac is not white we go out
|
||||
#ifdef subjectHomePresence
|
||||
String HomePresenceId;
|
||||
for (int i = 0; i<12; i++){
|
||||
HomePresenceId += String(d[0].extract[i]);
|
||||
if(((i-1) % 2 == 0) && (i!=11)) HomePresenceId += ":";
|
||||
}
|
||||
trc(F("HomePresenceId"));
|
||||
trc(HomePresenceId);
|
||||
BLEdata.set("id", (char *)HomePresenceId.c_str());
|
||||
#endif
|
||||
String topic = subjectBTtoMQTT + String(d[0].extract);
|
||||
int rssi = (int)strtol(d[2].extract, NULL, 16) - 256;
|
||||
BLEdata.set("rssi", (int)rssi);
|
||||
#ifdef subjectHomePresence
|
||||
haRoomPresence(BLEdata);// this device has an rssi in consequence we can use it for home assistant room presence component
|
||||
#endif
|
||||
String Service_data(d[5].extract);
|
||||
Service_data = Service_data.substring(14);
|
||||
#ifdef pubBLEServiceData
|
||||
BLEdata.set("servicedata", (char *)Service_data.c_str());
|
||||
#endif
|
||||
pub((char *)topic.c_str(),BLEdata);
|
||||
int pos = -1;
|
||||
pos = strpos(d[5].extract,"209800");
|
||||
if (pos != -1) {
|
||||
trc("mi flora data reading");
|
||||
#ifdef ZmqttDiscovery
|
||||
if(!isDiscovered(d[0].extract)) MiFloraDiscovery(d[0].extract);
|
||||
#endif
|
||||
bool result = process_data(pos - 38,(char *)Service_data.c_str(),d[0].extract);
|
||||
}
|
||||
pos = -1;
|
||||
pos = strpos(d[5].extract,"20aa01");
|
||||
//example "servicedata":"5020aa0194dfaa33342d580d1004e3002c02"
|
||||
if (pos != -1){
|
||||
trc("mi jia data reading");
|
||||
#ifdef ZmqttDiscovery
|
||||
if(!isDiscovered(d[0].extract)) MiJiaDiscovery(d[0].extract);
|
||||
#endif
|
||||
bool result = process_data(pos - 40,(char *)Service_data.c_str(),d[0].extract);
|
||||
}
|
||||
pos = -1;
|
||||
pos = strpos(d[5].extract,"205b04");
|
||||
//example "servicedata":"141695fe70205b0461298882c8593f09061002d002"
|
||||
if (pos != -1){
|
||||
trc("LYWSD02 data reading");
|
||||
#ifdef ZmqttDiscovery
|
||||
if(!isDiscovered(d[0].extract)) LYWSD02Discovery(d[0].extract);
|
||||
#endif
|
||||
bool result = process_data(pos - 38,(char *)Service_data.c_str(),d[0].extract);
|
||||
}
|
||||
pos = -1;
|
||||
pos = strpos(d[5].extract,"304703");
|
||||
if (pos != -1){
|
||||
trc("CLEARGRASSTRH data reading");
|
||||
#ifdef ZmqttDiscovery
|
||||
if(!isDiscovered(d[0].extract)) CLEARGRASSTRHDiscovery(d[0].extract);
|
||||
#endif
|
||||
bool result = process_data(pos - 40,(char *)Service_data.c_str(),d[0].extract);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
returnedString = ""; //init data string
|
||||
return false;
|
||||
}
|
||||
softserial.print(F(QUESTION_MSG));
|
||||
return false;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
bool process_data(int offset, char * rest_data, char * mac_adress){
|
||||
|
||||
trc(F("Creating BLE buffer"));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject& BLEdata = jsonBuffer.createObject();
|
||||
trc("rest_data");
|
||||
trc(rest_data);
|
||||
int data_length = 0;
|
||||
switch (rest_data[51 + offset]) {
|
||||
case '1' :
|
||||
case '2' :
|
||||
case '3' :
|
||||
case '4' :
|
||||
data_length = ((rest_data[51 + offset] - '0') * 2)+1;
|
||||
trc("data_length");
|
||||
trc(data_length);
|
||||
break;
|
||||
default:
|
||||
trc("can't read data_length");
|
||||
return false;
|
||||
}
|
||||
|
||||
char rev_data[data_length];
|
||||
char data[data_length];
|
||||
memcpy( rev_data, &rest_data[52 + offset], data_length );
|
||||
rev_data[data_length] = '\0';
|
||||
|
||||
// reverse data order
|
||||
revert_hex_data(rev_data, data, data_length);
|
||||
double value = strtol(data, NULL, 16);
|
||||
trc(value);
|
||||
char val[12];
|
||||
String mactopic(mac_adress);
|
||||
mactopic = subjectBTtoMQTT + mactopic;
|
||||
|
||||
// second value
|
||||
char val2[12];
|
||||
trc("rest_data");
|
||||
trc(rest_data);
|
||||
// Mi flora provides tem(perature), (earth) moi(sture), fer(tility) and lux (illuminance)
|
||||
// Mi Jia provides tem(perature), batt(erry) and hum(idity)
|
||||
// following the value of digit 47 we determine the type of data we get from the sensor
|
||||
switch (rest_data[47 + offset]) {
|
||||
case '9' :
|
||||
BLEdata.set("fer", (double)value);
|
||||
break;
|
||||
case '4' :
|
||||
if (value > 65000) value = value - 65535;
|
||||
BLEdata.set("tem", (double)value/10);
|
||||
break;
|
||||
case '6' :
|
||||
if (value > 65000) value = value - 65535;
|
||||
BLEdata.set("hum", (double)value/10);
|
||||
break;
|
||||
case '7' :
|
||||
BLEdata.set("lux", (double)value);
|
||||
break;
|
||||
case '8' :
|
||||
BLEdata.set("moi", (double)value);
|
||||
break;
|
||||
|
||||
case 'a' : // batteryLevel
|
||||
BLEdata.set("batt", (double)value);
|
||||
break;
|
||||
|
||||
case 'd' : // temp+hum
|
||||
char tempAr[8];
|
||||
// humidity
|
||||
memcpy(tempAr, data, 4);
|
||||
tempAr[4] = '\0';
|
||||
value = strtol(tempAr, NULL, 16);
|
||||
if (value > 65000) value = value - 65535;
|
||||
BLEdata.set("hum", (double)value/10);
|
||||
// temperature
|
||||
memcpy(tempAr, &data[4], 4);
|
||||
tempAr[4] = '\0';
|
||||
value = strtol(tempAr, NULL, 16);
|
||||
if (value > 65000) value = value - 65535;
|
||||
BLEdata.set("tem", (double)value/10);
|
||||
break;
|
||||
default:
|
||||
trc("can't read values");
|
||||
return false;
|
||||
}
|
||||
pub((char *)mactopic.c_str(),BLEdata);
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef subjectHomePresence
|
||||
void haRoomPresence(JsonObject& HomePresence){
|
||||
int BLErssi = HomePresence["rssi"];
|
||||
trc(F("BLErssi"));
|
||||
trc(BLErssi);
|
||||
int txPower = HomePresence["txpower"]|0;
|
||||
if (txPower == 0) txPower = -59; //if tx power is not found we set a default calibration value
|
||||
trc(F("txPower"));
|
||||
trc(txPower);
|
||||
double ratio = BLErssi*1.0/txPower;
|
||||
double distance;
|
||||
if (ratio < 1.0) {
|
||||
distance = pow(ratio,10);
|
||||
}else{
|
||||
distance = (0.89976)* pow(ratio,7.7095) + 0.111;
|
||||
}
|
||||
HomePresence["distance"] = distance;
|
||||
trc(F("BLE DISTANCE :"));
|
||||
trc(distance);
|
||||
pub(subjectHomePresence,HomePresence);
|
||||
}
|
||||
#endif
|
||||
|
||||
void MQTTtoBT(char * topicOri, JsonObject& BTdata) { // json object decoding
|
||||
if (strcmp(topicOri,subjectMQTTtoBTset) == 0){
|
||||
trc(F("MQTTtoBT json set"));
|
||||
|
||||
// Black list & white list set
|
||||
int WLsize = BTdata["white-list"].size();
|
||||
if(WLsize > 0){
|
||||
trc(F("WL set"));
|
||||
for (int i = 0; i < WLsize; i++){
|
||||
const char * whiteMac = BTdata["white-list"][i];
|
||||
setWorBMac((char *)whiteMac, true);
|
||||
}
|
||||
}
|
||||
int BLsize = BTdata["black-list"].size();
|
||||
if(BLsize > 0){
|
||||
trc(F("BL set"));
|
||||
for (int i = 0; i < BLsize; i++){
|
||||
const char * blackMac = BTdata["black-list"][i];
|
||||
setWorBMac((char *)blackMac,false);
|
||||
}
|
||||
}
|
||||
if (BLsize > 0 || WLsize > 0) dumpDevices();
|
||||
|
||||
// Scan interval set
|
||||
if (BTdata.containsKey("interval")){
|
||||
trc(F("BLE interval set"));
|
||||
// storing BLE interval for further use if needed
|
||||
unsigned int prevBLEinterval = BLEinterval;
|
||||
// set BLE interval if present if not setting default value
|
||||
BLEinterval = (unsigned int)BTdata["interval"]|prevBLEinterval;
|
||||
trc(BLEinterval);
|
||||
if (BLEinterval == 0) {
|
||||
if(BTtoMQTT())// as BLEinterval is = to 0 we can launch the loop and the scan will execute immediately
|
||||
trc(F("Scan done"));
|
||||
BLEinterval = prevBLEinterval; // as 0 was just used as a command we recover previous scan duration
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,812 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This gateway enables to:
|
||||
- receive MQTT data from a topic and send IR signal corresponding to the received MQTT data
|
||||
- publish MQTT data to a different topic related to received IR 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 ZgatewayIR
|
||||
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
#include <IRremoteESP8266.h>
|
||||
#include <IRsend.h> // Needed if you want to send IR commands.
|
||||
#include <IRrecv.h> // Needed if you want to receive IR commands.
|
||||
#include <IRutils.h>
|
||||
#ifdef DumpMode // in dump mode we increase the size of the buffer to catch big codes
|
||||
IRrecv irrecv(IR_RECEIVER_PIN, 1024, 15U, true);
|
||||
#else
|
||||
IRrecv irrecv(IR_RECEIVER_PIN);
|
||||
#endif
|
||||
IRsend irsend(IR_EMITTER_PIN);
|
||||
#else
|
||||
#include <IRremote.h>
|
||||
IRrecv irrecv(IR_RECEIVER_PIN);
|
||||
IRsend irsend; //connect IR emitter pin to D9 on arduino, you need to comment #define IR_USE_TIMER2 and uncomment #define IR_USE_TIMER1 on library IRremote.h so as to free pin D3 for RF RECEIVER PIN
|
||||
#endif
|
||||
|
||||
// IR protocol bits definition for Arduino (for ESP9266 they are defined in IRRemoteESP8266.h)
|
||||
#ifndef NEC_BITS
|
||||
#define NEC_BITS 32U
|
||||
#endif
|
||||
#ifndef SAMSUNG_BITS
|
||||
#define SAMSUNG_BITS 32U
|
||||
#endif
|
||||
#ifndef SHARP_BITS
|
||||
#define SHARP_ADDRESS_BITS 5U
|
||||
#define SHARP_COMMAND_BITS 8U
|
||||
#define SHARP_BITS (SHARP_ADDRESS_BITS + SHARP_COMMAND_BITS + 2) // 15U
|
||||
#endif
|
||||
#ifndef RC5_BITS
|
||||
#define RC5_RAW_BITS 14U
|
||||
#define RC5_BITS RC5_RAW_BITS - 2U
|
||||
#endif
|
||||
#ifndef DISH_BITS
|
||||
#define DISH_BITS 16U
|
||||
#endif
|
||||
#ifndef SONY_12_BITS
|
||||
#define SONY_12_BITS 12U
|
||||
#endif
|
||||
#ifndef LG_BITS
|
||||
#define LG_BITS 28U
|
||||
#endif
|
||||
#ifndef WHYNTER_BITS
|
||||
#define WHYNTER_BITS 32U
|
||||
#endif
|
||||
|
||||
void setupIR()
|
||||
{
|
||||
|
||||
//IR init parameters
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
irsend.begin();
|
||||
#endif
|
||||
|
||||
irrecv.enableIRIn(); // Start the receiver
|
||||
|
||||
trc(F("IR_EMITTER_PIN "));
|
||||
trc(IR_EMITTER_PIN);
|
||||
trc(F("IR_RECEIVER_PIN "));
|
||||
trc(IR_RECEIVER_PIN);
|
||||
trc(F("ZgatewayIR setup done "));
|
||||
|
||||
}
|
||||
void IRtoMQTT(){
|
||||
decode_results results;
|
||||
|
||||
if (irrecv.decode(&results)){
|
||||
trc(F("Creating IR buffer"));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject& IRdata = jsonBuffer.createObject();
|
||||
|
||||
trc(F("Rcv. IR"));
|
||||
#ifdef ESP32
|
||||
String taskMessage = "Task running on core ";
|
||||
taskMessage = taskMessage + xPortGetCoreID();
|
||||
trc(taskMessage);
|
||||
#endif
|
||||
IRdata.set("value", (unsigned long)(results.value));
|
||||
IRdata.set("protocol", (int)(results.decode_type));
|
||||
IRdata.set("bits",(int)(results.bits));
|
||||
#if defined(ESP8266) || defined(ESP32) //resultToHexidecimal is only available with IRremoteESP8266
|
||||
String hex = resultToHexidecimal(&results);
|
||||
IRdata.set("hex", (char *)hex.c_str());
|
||||
#endif
|
||||
String rawCode = "";
|
||||
// Dump data
|
||||
for (uint16_t i = 1; i < results.rawlen; i++) {
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
if (i % 100 == 0) yield(); // Preemptive yield every 100th entry to feed the WDT.
|
||||
rawCode = rawCode + (results.rawbuf[i] * RAWTICK);
|
||||
#else
|
||||
rawCode = rawCode + (results.rawbuf[i] * USECPERTICK);
|
||||
#endif
|
||||
if ( i < results.rawlen-1 ) rawCode = rawCode + ","; // ',' not needed on last one
|
||||
}
|
||||
trc(rawCode);
|
||||
IRdata.set("raw", rawCode);
|
||||
// if needed we directly resend the raw code
|
||||
if (RawDirectForward){
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
uint16_t rawsend[results.rawlen];
|
||||
for (uint16_t i = 1; i < results.rawlen; i++) {
|
||||
if (i % 100 == 0) yield(); // Preemptive yield every 100th entry to feed the WDT.
|
||||
#else
|
||||
unsigned int rawsend[results.rawlen];
|
||||
for (int i = 1; i < results.rawlen; i++) {
|
||||
#endif
|
||||
rawsend[i] = results.rawbuf[i];
|
||||
}
|
||||
irsend.sendRaw(rawsend, results.rawlen, RawFrequency);
|
||||
trc(F("raw signal redirected"));
|
||||
}
|
||||
irrecv.resume(); // Receive the next value
|
||||
unsigned long MQTTvalue = IRdata.get<unsigned long>("value");
|
||||
if (pubIRunknownPrtcl == false && IRdata.get<int>("protocol") == -1){ // don't publish unknown IR protocol
|
||||
trc(F("--no pub. unknown protocol--"));
|
||||
} else if (!isAduplicate(MQTTvalue) && MQTTvalue!=0) {// conditions to avoid duplications of RF -->MQTT
|
||||
trc(F("Adv data IRtoMQTT"));
|
||||
pub(subjectIRtoMQTT,IRdata);
|
||||
trc(F("Store to avoid duplicate"));
|
||||
storeValue(MQTTvalue);
|
||||
if (repeatIRwMQTT){
|
||||
trc(F("Pub. IR for repeat"));
|
||||
pub(subjectMQTTtoIR,MQTTvalue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef simpleReceiving
|
||||
void MQTTtoIR(char * topicOri, char * datacallback) {
|
||||
|
||||
// IR DATA ANALYSIS
|
||||
//send received MQTT value by IR signal
|
||||
bool signalSent = false;
|
||||
uint64_t data = 0;
|
||||
String strcallback = String(datacallback);
|
||||
trc(datacallback);
|
||||
unsigned int s = strcallback.length();
|
||||
//number of "," value count
|
||||
int count = 0;
|
||||
for(int i = 0; i < s; i++)
|
||||
{
|
||||
if (datacallback[i] == ',') {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
if(count == 0){
|
||||
data = strtoul(datacallback, NULL, 10); // standard sending with unsigned long, we will not be able to pass values > 4294967295
|
||||
}
|
||||
#ifdef IR_GC
|
||||
else if(strstr(topicOri, "IR_GC") != NULL){ // sending GC data from https://irdb.globalcache.com
|
||||
trc(F("IR_GC"));
|
||||
//buffer allocation from char datacallback
|
||||
uint16_t GC[count+1];
|
||||
String value = "";
|
||||
int j = 0;
|
||||
for(int i = 0; i < s; i++)
|
||||
{
|
||||
if (datacallback[i] != ',') {
|
||||
value = value + String(datacallback[i]);
|
||||
}
|
||||
if ((datacallback[i] == ',') || (i == s - 1))
|
||||
{
|
||||
GC[j]= value.toInt();
|
||||
value = "";
|
||||
j++;
|
||||
}
|
||||
}
|
||||
irsend.sendGC(GC, j);
|
||||
signalSent = true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_Raw
|
||||
else if(strstr(topicOri, "IR_Raw") != NULL){ // sending Raw data
|
||||
trc(F("IR_Raw"));
|
||||
//buffer allocation from char datacallback
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
uint16_t Raw[count+1];
|
||||
#else
|
||||
unsigned int Raw[count+1];
|
||||
#endif
|
||||
String value = "";
|
||||
int j = 0;
|
||||
for(int i = 0; i < s; i++)
|
||||
{
|
||||
if (datacallback[i] != ',') {
|
||||
value = value + String(datacallback[i]);
|
||||
}
|
||||
if ((datacallback[i] == ',') || (i == s - 1))
|
||||
{
|
||||
Raw[j]= value.toInt();
|
||||
value = "";
|
||||
j++;
|
||||
}
|
||||
}
|
||||
irsend.sendRaw(Raw, j, RawFrequency);
|
||||
signalSent = true;
|
||||
}
|
||||
#endif
|
||||
|
||||
//We look into the subject to see if a special Bits number is defined
|
||||
String topic = topicOri;
|
||||
unsigned int valueBITS = 0;
|
||||
int pos = topic.lastIndexOf(IRbitsKey);
|
||||
if (pos != -1){
|
||||
pos = pos + +strlen(IRbitsKey);
|
||||
valueBITS = (topic.substring(pos,pos + 2)).toInt();
|
||||
trc(F("Bits nb:"));
|
||||
trc(valueBITS);
|
||||
}
|
||||
//We look into the subject to see if a special repeat number is defined
|
||||
uint16_t valueRPT = 0;
|
||||
int pos2 = topic.lastIndexOf(IRRptKey);
|
||||
if (pos2 != -1) {
|
||||
pos2 = pos2 + strlen(IRRptKey);
|
||||
valueRPT = (topic.substring(pos2,pos2 + 1)).toInt();
|
||||
trc(F("IR repeat:"));
|
||||
trc(valueRPT);
|
||||
}
|
||||
|
||||
if(topicOri && (strstr(topicOri, "IR_NEC") == NULL)){
|
||||
signalSent = sendIdentifiedProtocol(topicOri, data, datacallback, valueBITS, valueRPT);
|
||||
}else{
|
||||
trc(F("Using NEC protocol"));
|
||||
if (valueBITS == 0) valueBITS = NEC_BITS;
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
irsend.sendNEC(data, valueBITS, valueRPT);
|
||||
#else
|
||||
for (int i=0; i <= valueRPT; i++) irsend.sendNEC(data, valueBITS);
|
||||
#endif
|
||||
signalSent = true;
|
||||
}
|
||||
|
||||
if (signalSent){ // we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
pub(subjectGTWIRtoMQTT, datacallback);
|
||||
}
|
||||
irrecv.enableIRIn(); // ReStart the IR receiver (if not restarted it is not able to receive data)
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef jsonReceiving
|
||||
void MQTTtoIR(char * topicOri, JsonObject& IRdata) {
|
||||
|
||||
if (strcmp(topicOri,subjectMQTTtoIR) == 0){
|
||||
trc(F("MQTTtoIR json"));
|
||||
unsigned long data = IRdata["value"];
|
||||
const char * raw = IRdata["raw"];
|
||||
const char * datastring = IRdata["datastring"];
|
||||
if (data != 0||raw||datastring) {
|
||||
trc(F("MQTTtoIR data || raw ok"));
|
||||
bool signalSent = false;
|
||||
trc(F("value"));
|
||||
trc(data);
|
||||
trc(F("raw"));
|
||||
trc(raw);
|
||||
trc(F("datastring"));
|
||||
trc(datastring);
|
||||
const char * protocol_name = IRdata["protocol_name"];
|
||||
unsigned int valueBITS = IRdata["bits"];
|
||||
uint16_t valueRPT = IRdata["repeat"]|repeatIRwNumber;
|
||||
|
||||
if(raw){
|
||||
unsigned int s = strlen(raw);
|
||||
//number of "," value count
|
||||
int count = 0;
|
||||
for(int i = 0; i < s; i++)
|
||||
{
|
||||
if (raw[i] == ',') {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
#ifdef IR_GC
|
||||
if(strstr(protocol_name, "IR_GC") != NULL){ // sending GC data from https://irdb.globalcache.com
|
||||
trc(F("IR_GC"));
|
||||
//buffer allocation from char datacallback
|
||||
uint16_t GC[count+1];
|
||||
String value = "";
|
||||
int j = 0;
|
||||
for(int i = 0; i < s; i++)
|
||||
{
|
||||
if (raw[i] != ',') {
|
||||
value = value + String(raw[i]);
|
||||
}
|
||||
if ((raw[i] == ',') || (i == s - 1))
|
||||
{
|
||||
GC[j]= value.toInt();
|
||||
value = "";
|
||||
j++;
|
||||
}
|
||||
}
|
||||
irsend.sendGC(GC, j);
|
||||
signalSent = true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_Raw
|
||||
if(strstr(protocol_name, "IR_Raw") != NULL){ // sending Raw data
|
||||
trc(F("IR_Raw"));
|
||||
//buffer allocation from char datacallback
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
uint16_t Raw[count+1];
|
||||
#else
|
||||
unsigned int Raw[count+1];
|
||||
#endif
|
||||
String value = "";
|
||||
int j = 0;
|
||||
for(int i = 0; i < s; i++)
|
||||
{
|
||||
if (raw[i] != ',') {
|
||||
value = value + String(raw[i]);
|
||||
}
|
||||
if ((raw[i] == ',') || (i == s - 1))
|
||||
{
|
||||
Raw[j]= value.toInt();
|
||||
value = "";
|
||||
j++;
|
||||
}
|
||||
}
|
||||
irsend.sendRaw(Raw, j, RawFrequency);
|
||||
signalSent = true;
|
||||
}
|
||||
#endif
|
||||
}else if(protocol_name && (strstr(protocol_name, "IR_NEC") == NULL)){
|
||||
signalSent = sendIdentifiedProtocol(protocol_name, data, datastring, valueBITS, valueRPT);
|
||||
}else{
|
||||
trc(F("Using NEC protocol"));
|
||||
if (valueBITS == 0) valueBITS = NEC_BITS;
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
irsend.sendNEC(data, valueBITS, valueRPT);
|
||||
#else
|
||||
for (int i=0; i <= valueRPT; i++) irsend.sendNEC(data, valueBITS);
|
||||
#endif
|
||||
signalSent = true;
|
||||
}
|
||||
if (signalSent){ // we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
trc(F("MQTTtoIR OK"));
|
||||
pub(subjectGTWIRtoMQTT, IRdata);
|
||||
}
|
||||
irrecv.enableIRIn(); // ReStart the IR receiver (if not restarted it is not able to receive data)
|
||||
}else{
|
||||
trc(F("MQTTtoIR Fail reading from json"));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
bool sendIdentifiedProtocol(const char * protocol_name, unsigned long data, const char * datastring, unsigned int valueBITS, uint16_t valueRPT){
|
||||
unsigned char dataarray[valueBITS];
|
||||
const char* pointer = datastring;
|
||||
int i = 0;
|
||||
while (strlen(pointer) > 0 && i < valueBITS) {
|
||||
if (pointer[0] == ',') {
|
||||
pointer++;
|
||||
}
|
||||
else
|
||||
{
|
||||
dataarray[i] = strtol(pointer, (char**)& pointer, 16);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef IR_Whynter
|
||||
if (strstr(protocol_name, "IR_Whynter") != NULL){
|
||||
if (valueBITS == 0) valueBITS = WHYNTER_BITS;
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
irsend.sendWhynter(data, valueBITS, valueRPT);
|
||||
#else
|
||||
for (int i=0; i <= valueRPT; i++) irsend.sendWhynter(data, valueBITS);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_LG
|
||||
if (strstr(protocol_name, "IR_LG") != NULL){
|
||||
if (valueBITS == 0) valueBITS = LG_BITS;
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
irsend.sendLG(data, valueBITS, valueRPT);
|
||||
#else
|
||||
for (int i=0; i <= valueRPT; i++) irsend.sendLG(data, valueBITS);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_Sony
|
||||
if (strstr(protocol_name, "IR_Sony") != NULL){
|
||||
if (valueBITS == 0) valueBITS = SONY_12_BITS;
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
irsend.sendSony(data, valueBITS, valueRPT);
|
||||
#else
|
||||
for (int i=0; i <= valueRPT; i++) irsend.sendSony(data, valueBITS);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_DISH
|
||||
if (strstr(protocol_name, "IR_DISH") != NULL){
|
||||
if (valueBITS == 0) valueBITS = DISH_BITS;
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
irsend.sendDISH(data, valueBITS, valueRPT);
|
||||
#else
|
||||
for (int i=0; i <= valueRPT; i++) irsend.sendDISH(data, valueBITS);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_RC5
|
||||
if (strstr(protocol_name, "IR_RC5") != NULL){
|
||||
if (valueBITS == 0) valueBITS = RC5_BITS;
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
irsend.sendRC5(data, valueBITS, valueRPT);
|
||||
#else
|
||||
for (int i=0; i <= valueRPT; i++) irsend.sendRC5(data, valueBITS);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_RC6
|
||||
if (strstr(protocol_name, "IR_RC6") != NULL){
|
||||
if (valueBITS == 0) valueBITS = RC6_MODE0_BITS;
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
irsend.sendRC6(data, valueBITS, valueRPT);
|
||||
#else
|
||||
for (int i=0; i <= valueRPT; i++) irsend.sendRC6(data, valueBITS);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_Sharp
|
||||
if (strstr(protocol_name, "IR_Sharp") != NULL){
|
||||
if (valueBITS == 0) valueBITS = SHARP_BITS;
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
irsend.sendSharpRaw(data, valueBITS, valueRPT);
|
||||
#else
|
||||
for (int i=0; i <= valueRPT; i++) irsend.sendSharpRaw(data, valueBITS);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_SAMSUNG
|
||||
if (strstr(protocol_name, "IR_SAMSUNG") != NULL){
|
||||
if (valueBITS == 0) valueBITS = SAMSUNG_BITS;
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
irsend.sendSAMSUNG(data, valueBITS, valueRPT);
|
||||
#else
|
||||
for (int i=0; i <= valueRPT; i++) irsend.sendSAMSUNG(data, valueBITS);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_JVC
|
||||
if (strstr(protocol_name, "IR_JVC") != NULL){
|
||||
if (valueBITS == 0) valueBITS = JVC_BITS;
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
irsend.sendJVC(data, valueBITS, valueRPT);
|
||||
#else
|
||||
for (int i=0; i <= valueRPT; i++) irsend.sendJVC(data, valueBITS);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_PANASONIC
|
||||
if (strstr(protocol_name, "IR_PANASONIC") != NULL){
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
if (valueBITS == 0) valueBITS = PANASONIC_BITS;
|
||||
irsend.sendPanasonic(PanasonicAddress, data, valueBITS, valueRPT);
|
||||
#else
|
||||
for (int i=0; i <= valueRPT; i++) irsend.sendPanasonic(PanasonicAddress, data);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(ESP8266) || defined(ESP32) // sendings not available on arduino
|
||||
#ifdef IR_COOLIX
|
||||
if (strstr(protocol_name, "IR_COOLIX") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kCoolixBits;
|
||||
irsend.sendCOOLIX(data, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_RCMM
|
||||
if (strstr(protocol_name, "IR_RCMM") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kRCMMBits;
|
||||
irsend.sendRCMM(data, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_DENON
|
||||
if (strstr(protocol_name, "IR_DENON") != NULL){
|
||||
if (valueBITS == 0) valueBITS = DENON_BITS;
|
||||
irsend.sendDenon(data, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_GICABLE
|
||||
if (strstr(protocol_name, "IR_GICABLE") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kGicableBits;
|
||||
if (valueRPT == repeatIRwNumber) valueRPT = std::max(valueRPT, kGicableMinRepeat);
|
||||
irsend.sendGICable(data, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_SHERWOOD
|
||||
if (strstr(protocol_name, "IR_SHERWOOD") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kSherwoodBits;
|
||||
if (valueRPT == repeatIRwNumber) valueRPT = std::max(valueRPT, kSherwoodMinRepeat);
|
||||
irsend.sendSherwood(data, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_MITSUBISHI
|
||||
if (strstr(protocol_name, "IR_MITSUBISHI") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kMitsubishiBits;
|
||||
if (valueRPT == repeatIRwNumber) valueRPT = std::max(valueRPT, kMitsubishiMinRepeat);
|
||||
irsend.sendMitsubishi(data, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_NIKAI
|
||||
if (strstr(protocol_name, "IR_NIKAI") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kNikaiBits;
|
||||
irsend.sendNikai(data, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_MIDEA
|
||||
if (strstr(protocol_name, "IR_MIDEA") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kMideaBits;
|
||||
irsend.sendMidea(data, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_MAGIQUEST
|
||||
if (strstr(protocol_name, "IR_MAGIQUEST") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kMagiquestBits;
|
||||
irsend.sendMagiQuest(data, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_LASERTAG
|
||||
if (strstr(protocol_name, "IR_LASERTAG") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kLasertagBits;
|
||||
irsend.sendLasertag(data, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_CARRIER_AC
|
||||
if (strstr(protocol_name, "IR_CARRIER_AC") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kCarrierAcBits;
|
||||
irsend.sendCarrierAC(data, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_MITSUBISHI2
|
||||
if (strstr(protocol_name, "IR_MITSUBISHI2") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kMitsubishiBits;
|
||||
if (valueRPT == repeatIRwNumber) valueRPT = std::max(valueRPT, kMitsubishiMinRepeat);
|
||||
irsend.sendMitsubishi2(data, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_AIWA_RC_T501
|
||||
if (strstr(protocol_name, "IR_AIWA_RC_T501") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kAiwaRcT501Bits;
|
||||
if (valueRPT == repeatIRwNumber) valueRPT = std::max(valueRPT, kAiwaRcT501MinRepeats);
|
||||
irsend.sendAiwaRCT501(data, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_SANYO
|
||||
if (strstr(protocol_name, "IR_SANYO") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kSanyoLC7461Bits;
|
||||
irsend.sendSanyoLC7461(data, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_DAIKIN
|
||||
if (strstr(protocol_name, "IR_DAIKIN") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kDaikinStateLength;
|
||||
irsend.sendDaikin(dataarray, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_KELVINATOR
|
||||
if (strstr(protocol_name, "IR_KELVINATOR") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kKelvinatorStateLength;
|
||||
irsend.sendKelvinator(dataarray, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_MITSUBISHI_AC
|
||||
if (strstr(protocol_name, "IR_MITSUBISHI_AC") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kMitsubishiACStateLength;
|
||||
irsend.sendMitsubishiAC(dataarray, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_SANYO
|
||||
if (strstr(protocol_name, "IR_SANYO") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kSanyoLC7461Bits;
|
||||
irsend.sendSanyoLC7461(data, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_GREE
|
||||
if (strstr(protocol_name, "IR_GREE") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kGreeStateLength;
|
||||
irsend.sendGree(data, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_ARGO
|
||||
if (strstr(protocol_name, "IR_ARGO") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kArgoStateLength;
|
||||
irsend.sendArgo(dataarray, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_TROTEC
|
||||
if (strstr(protocol_name, "IR_TROTEC") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kTrotecStateLength;
|
||||
irsend.sendTrotec(dataarray, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_TOSHIBA_AC
|
||||
if (strstr(protocol_name, "IR_TOSHIBA_AC") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kToshibaACBits;
|
||||
irsend.sendToshibaAC(dataarray, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_FUJITSU_AC
|
||||
if (strstr(protocol_name, "IR_FUJITSU_AC") != NULL){
|
||||
irsend.sendFujitsuAC(dataarray, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_MAGIQUEST
|
||||
if (strstr(protocol_name, "IR_MAGIQUEST") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kMagiquestBits;
|
||||
irsend.sendMagiQuest(data, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_HAIER_AC
|
||||
if (strstr(protocol_name, "IR_HAIER_AC") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kHaierACStateLength;
|
||||
irsend.sendHaierAC(dataarray, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_HITACHI_AC
|
||||
if (strstr(protocol_name, "IR_HITACHI_AC") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kHitachiAc2StateLength;
|
||||
irsend.sendHitachiAC(dataarray, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_HITACHI_AC1
|
||||
if (strstr(protocol_name, "IR_HITACHI_AC1") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kHitachiAc2StateLength;
|
||||
irsend.sendHitachiAC1(dataarray, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_HITACHI_AC2
|
||||
if (strstr(protocol_name, "IR_HITACHI_AC2") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kHitachiAc2StateLength;
|
||||
irsend.sendHitachiAC2(dataarray, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_GICABLE
|
||||
if (strstr(protocol_name, "IR_GICABLE") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kGicableBits;
|
||||
irsend.sendGICable(data, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_HAIER_AC_YRW02
|
||||
if (strstr(protocol_name, "IR_HAIER_AC_YRW02") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kHaierACYRW02StateLength;
|
||||
irsend.sendHaierACYRW02(dataarray, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_WHIRLPOOL_AC
|
||||
if (strstr(protocol_name, "IR_WHIRLPOOL_AC") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kWhirlpoolAcStateLength;
|
||||
irsend.sendWhirlpoolAC(dataarray, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_SAMSUNG_AC
|
||||
if (strstr(protocol_name, "IR_SAMSUNG_AC") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kSamsungAcStateLength;
|
||||
irsend.sendSamsungAC(dataarray, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_LUTRON
|
||||
if (strstr(protocol_name, "IR_LUTRON") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kLutronBits;
|
||||
irsend.sendLutron(data, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_ELECTRA_AC
|
||||
if (strstr(protocol_name, "IR_ELECTRA_AC") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kElectraAcStateLength;
|
||||
irsend.sendElectraAC(dataarray, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_PANASONIC_AC
|
||||
if (strstr(protocol_name, "IR_PANASONIC_AC") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kPanasonicAcStateLength;
|
||||
irsend.sendPanasonicAC(dataarray, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_PIONEER
|
||||
if (strstr(protocol_name, "IR_PIONEER") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kPioneerBits;
|
||||
irsend.sendPioneer(data, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_LG2
|
||||
if (strstr(protocol_name, "IR_LG2") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kLgBits;
|
||||
irsend.sendLG2(data, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_MWM
|
||||
if (strstr(protocol_name, "IR_MWM") != NULL){
|
||||
irsend.sendMWM(dataarray, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_DAIKIN2
|
||||
if (strstr(protocol_name, "IR_DAIKIN2") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kDaikin2StateLength;
|
||||
irsend.sendDaikin2(dataarray, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_VESTEL_AC
|
||||
if (strstr(protocol_name, "IR_VESTEL_AC") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kVestelAcBits;
|
||||
irsend.sendVestelAc(data, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_SAMSUNG36
|
||||
if (strstr(protocol_name, "IR_SAMSUNG36") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kSamsung36Bits;
|
||||
irsend.sendSamsung36(data, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_TCL112AC
|
||||
if (strstr(protocol_name, "IR_TCL112AC") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kTcl112AcStateLength;
|
||||
irsend.sendTcl112Ac(dataarray, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_TECO
|
||||
if (strstr(protocol_name, "IR_TECO") != NULL){
|
||||
if (valueBITS == 0) valueBITS = kTecoBits;
|
||||
irsend.sendTeco(data, valueBITS, valueRPT);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR/BLE signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This gateway enables to:
|
||||
- receive MQTT data from a topic and send LORA signal corresponding to the received MQTT data
|
||||
- publish MQTT data to a different topic related to received LORA 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 ZgatewayLORA
|
||||
|
||||
#include <SPI.h>
|
||||
#include <LoRa.h>
|
||||
#include <Wire.h>
|
||||
|
||||
void setupLORA() {
|
||||
SPI.begin(LORA_SCK,LORA_MISO,LORA_MOSI,LORA_SS);
|
||||
LoRa.setPins(LORA_SS,LORA_RST,LORA_DI0);
|
||||
|
||||
if (!LoRa.begin(LORA_BAND)) {
|
||||
trc(F("ZgatewayLORA setup failed!"));
|
||||
while (1);
|
||||
}
|
||||
LoRa.receive();
|
||||
trc(F("LORA_SCK"));
|
||||
trc(LORA_SCK);
|
||||
trc(F("LORA_MISO"));
|
||||
trc(LORA_MISO);
|
||||
trc(F("LORA_MOSI"));
|
||||
trc(LORA_MOSI);
|
||||
trc(F("LORA_SS"));
|
||||
trc(LORA_SS);
|
||||
trc(F("LORA_RST"));
|
||||
trc(LORA_RST);
|
||||
trc(F("LORA_DI0"));
|
||||
trc(LORA_DI0);
|
||||
trc(F("ZgatewayLORA setup done"));
|
||||
|
||||
}
|
||||
|
||||
void LORAtoMQTT(){
|
||||
int packetSize = LoRa.parsePacket();
|
||||
if (packetSize) {
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject& LORAdata = jsonBuffer.createObject();
|
||||
trc(F("Rcv. LORA"));
|
||||
#ifdef ESP32
|
||||
String taskMessage = "LORA Task running on core ";
|
||||
taskMessage = taskMessage + xPortGetCoreID();
|
||||
trc(taskMessage);
|
||||
#endif
|
||||
String packet ;
|
||||
packet ="";
|
||||
for (int i = 0; i < packetSize; i++) { packet += (char) LoRa.read(); }
|
||||
LORAdata.set("rssi", (int)LoRa.packetRssi());
|
||||
LORAdata.set("snr",(float)LoRa.packetSnr());
|
||||
LORAdata.set("pferror",(float)LoRa.packetFrequencyError());
|
||||
LORAdata.set("packetSize", (int)packetSize);
|
||||
LORAdata.set("message", (char *)packet.c_str());
|
||||
pub(subjectLORAtoMQTT,LORAdata);
|
||||
if (repeatLORAwMQTT){
|
||||
trc(F("Pub LORA for rpt"));
|
||||
pub(subjectMQTTtoLORA,LORAdata);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef jsonReceiving
|
||||
void MQTTtoLORA(char * topicOri, JsonObject& LORAdata) { // json object decoding
|
||||
if (strcmp(topicOri,subjectMQTTtoLORA) == 0){
|
||||
trc(F("MQTTtoLORA json"));
|
||||
const char * message = LORAdata["message"];
|
||||
int txPower = LORAdata["txpower"]|LORA_TX_POWER;
|
||||
int spreadingFactor = LORAdata["spreadingfactor"]|LORA_SPREADING_FACTOR;
|
||||
long int frequency = LORAdata["frequency "]|LORA_BAND;
|
||||
long int signalBandwidth = LORAdata["signalbandwidth"]|LORA_SIGNAL_BANDWIDTH;
|
||||
int codingRateDenominator = LORAdata["codingrate"]|LORA_CODING_RATE;
|
||||
int preambleLength = LORAdata["preamblelength"]|LORA_PREAMBLE_LENGTH;
|
||||
byte syncWord = LORAdata["syncword"]|LORA_SYNC_WORD;
|
||||
bool Crc = LORAdata["enablecrc"];
|
||||
if (message) {
|
||||
LoRa.setTxPower(txPower);
|
||||
LoRa.setFrequency(frequency);
|
||||
LoRa.setSpreadingFactor(spreadingFactor);
|
||||
LoRa.setSignalBandwidth(signalBandwidth);
|
||||
LoRa.setCodingRate4(codingRateDenominator);
|
||||
LoRa.setPreambleLength(preambleLength);
|
||||
LoRa.setSyncWord(syncWord);
|
||||
if (Crc) LoRa.enableCrc();
|
||||
LoRa.beginPacket();
|
||||
LoRa.print(message);
|
||||
LoRa.endPacket();
|
||||
trc(F("MQTTtoLORA OK"));
|
||||
pub(subjectGTWLORAtoMQTT, LORAdata);// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
}else{
|
||||
trc(F("MQTTtoLORA Fail json"));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#ifdef simpleReceiving
|
||||
void MQTTtoLORA(char * topicOri, char * LORAdata) { // json object decoding
|
||||
if (strcmp(topicOri,subjectMQTTtoLORA) == 0){
|
||||
LoRa.beginPacket();
|
||||
LoRa.print(LORAdata);
|
||||
LoRa.endPacket();
|
||||
trc(F("MQTTtoLORA OK"));
|
||||
pub(subjectGTWLORAtoMQTT, LORAdata);// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This gateway enables to:
|
||||
- receive MQTT data from a topic and send RF 433Mhz signal corresponding to the received MQTT data based on ESPilight library
|
||||
- publish MQTT data to a different topic related to received 433Mhz signal based on ESPilight library
|
||||
|
||||
Copyright: (c)Florian ROBERT
|
||||
Pilight Gateway made by steadramon, improvments with the help of puuu
|
||||
|
||||
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 ZgatewayPilight
|
||||
#include <ESPiLight.h>
|
||||
ESPiLight rf(RF_EMITTER_PIN); // use -1 to disable transmitter
|
||||
|
||||
void pilightCallback(const String &protocol,const String &message, int status,
|
||||
size_t repeats, const String &deviceID) {
|
||||
if (status == VALID) {
|
||||
trc(F("Creating RF PiLight buffer"));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject& RFPiLightdata = jsonBuffer.createObject();
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer2;
|
||||
JsonObject& msg = jsonBuffer2.parseObject(message);
|
||||
RFPiLightdata.set("message", msg);
|
||||
RFPiLightdata.set("protocol",(char *)protocol.c_str());
|
||||
RFPiLightdata.set("length", (char *)deviceID.c_str());
|
||||
RFPiLightdata.set("repeats", (int)repeats);
|
||||
RFPiLightdata.set("status", (int)status);
|
||||
pub(subjectPilighttoMQTT,RFPiLightdata);
|
||||
if (repeatPilightwMQTT){
|
||||
trc(F("Pub Pilight for rpt"));
|
||||
pub(subjectMQTTtoPilight,RFPiLightdata);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setupPilight(){
|
||||
#ifndef ZgatewayRF && ZgatewayRF2 && ZgatewayRF315 //receiving with Pilight is not compatible with ZgatewayRF or RF2 or RF315 as far as I can tell
|
||||
rf.setCallback(pilightCallback);
|
||||
rf.initReceiver(RF_RECEIVER_PIN);
|
||||
trc(F("RF_EMITTER_PIN "));
|
||||
trc(String(RF_EMITTER_PIN));
|
||||
trc(F("RF_RECEIVER_PIN "));
|
||||
trc(String(RF_RECEIVER_PIN));
|
||||
trc(F("ZgatewayPilight setup done "));
|
||||
#else
|
||||
trc(F("ZgatewayPilight setup cannot be done, comment first ZgatewayRF && ZgatewayRF2 && ZgatewayRF315"));
|
||||
#endif
|
||||
}
|
||||
|
||||
void PilighttoMQTT(){
|
||||
rf.loop();
|
||||
}
|
||||
|
||||
void MQTTtoPilight(char * topicOri, JsonObject& Pilightdata) {
|
||||
|
||||
int result = 0;
|
||||
|
||||
if (strcmp(topicOri,subjectMQTTtoPilight) == 0){
|
||||
trc(F("MQTTtoPilight json data analysis"));
|
||||
const char * message = Pilightdata["message"];
|
||||
const char * protocol = Pilightdata["protocol"];
|
||||
const char * raw = Pilightdata["raw"];
|
||||
if(raw){
|
||||
int msgLength = 0;
|
||||
uint16_t codes[MAXPULSESTREAMLENGTH];
|
||||
msgLength = rf.stringToPulseTrain(
|
||||
raw,
|
||||
codes, MAXPULSESTREAMLENGTH);
|
||||
if (msgLength > 0) {
|
||||
trc(F("MQTTtoPilight raw ok"));
|
||||
rf.sendPulseTrain(codes, msgLength);
|
||||
result = msgLength;
|
||||
}else{
|
||||
trc(F("MQTTtoPilight raw KO"));
|
||||
switch (result) {
|
||||
case ESPiLight::ERROR_INVALID_PULSETRAIN_MSG_C:
|
||||
trc(F("'c' not found in string, or has no data"));
|
||||
break;
|
||||
case ESPiLight::ERROR_INVALID_PULSETRAIN_MSG_P:
|
||||
trc(F("'p' not found in string, or has no data"));
|
||||
break;
|
||||
case ESPiLight::ERROR_INVALID_PULSETRAIN_MSG_END:
|
||||
trc(F("';' or '@' not found in data string"));
|
||||
break;
|
||||
case ESPiLight::ERROR_INVALID_PULSETRAIN_MSG_TYPE:
|
||||
trc(F("pulse type not defined"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}else if (message && protocol) {
|
||||
trc(F("MQTTtoPilight msg & protocol ok"));
|
||||
result = rf.send(protocol, message);
|
||||
}else{
|
||||
trc(F("MQTTtoPilight fail reading from json"));
|
||||
}
|
||||
|
||||
if (result > 0) {
|
||||
trc(F("Adv data MQTTtoPilight push state via PilighttoMQTT"));
|
||||
pub(subjectGTWPilighttoMQTT, Pilightdata);
|
||||
} else {
|
||||
switch (result) {
|
||||
case ESPiLight::ERROR_UNAVAILABLE_PROTOCOL:
|
||||
trc(F("protocol is not available"));
|
||||
break;
|
||||
case ESPiLight::ERROR_INVALID_PILIGHT_MSG:
|
||||
trc(F("message is invalid"));
|
||||
break;
|
||||
case ESPiLight::ERROR_INVALID_JSON:
|
||||
trc(F("message is not a proper json object"));
|
||||
break;
|
||||
case ESPiLight::ERROR_NO_OUTPUT_PIN:
|
||||
trc(F("no transmitter pin"));
|
||||
break;
|
||||
default:
|
||||
trc(F("invalid json data, can't read raw or message/protocol"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,180 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This gateway enables to:
|
||||
- receive MQTT data from a topic and send RF 433Mhz signal corresponding to the received MQTT data
|
||||
- publish MQTT data to a different topic related to received 433Mhz 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 ZgatewayRF
|
||||
|
||||
#include <RCSwitch.h> // library for controling Radio frequency switch
|
||||
|
||||
RCSwitch mySwitch = RCSwitch();
|
||||
|
||||
#ifdef ZmqttDiscovery
|
||||
void RFtoMQTTdiscovery(unsigned long MQTTvalue){//on the fly switch creation from received RF values
|
||||
char val[11];
|
||||
sprintf(val, "%lu", MQTTvalue);
|
||||
trc(F("switchRFDiscovery"));
|
||||
char * switchRF[8] = {"switch", val, "", "","",val, "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySwitch"));
|
||||
trc(switchRF[1]);
|
||||
createDiscovery(switchRF[0],
|
||||
subjectRFtoMQTT, switchRF[1], (char *)getUniqueId(switchRF[1], switchRF[2]).c_str(),
|
||||
will_Topic, switchRF[3], switchRF[4],
|
||||
switchRF[5], switchRF[6], switchRF[7],
|
||||
0,"","",true,subjectMQTTtoRF);
|
||||
}
|
||||
#endif
|
||||
|
||||
void setupRF(){
|
||||
|
||||
//RF init parameters
|
||||
mySwitch.enableTransmit(RF_EMITTER_PIN);
|
||||
trc(F("RF_EMITTER_PIN "));
|
||||
trc(RF_EMITTER_PIN);
|
||||
mySwitch.setRepeatTransmit(RF_EMITTER_REPEAT);
|
||||
mySwitch.enableReceive(RF_RECEIVER_PIN);
|
||||
trc(F("RF_RECEIVER_PIN "));
|
||||
trc(RF_RECEIVER_PIN);
|
||||
trc(F("RF setup ok"));
|
||||
}
|
||||
|
||||
void RFtoMQTT(){
|
||||
|
||||
if (mySwitch.available()){
|
||||
const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(4);
|
||||
StaticJsonBuffer<JSON_MSG_CALC_BUFFER> jsonBuffer;
|
||||
JsonObject& RFdata = jsonBuffer.createObject();
|
||||
trc(F("Rcv. RF"));
|
||||
#ifdef ESP32
|
||||
String taskMessage = "RF Task running on core ";
|
||||
taskMessage = taskMessage + xPortGetCoreID();
|
||||
trc(taskMessage);
|
||||
#endif
|
||||
RFdata.set("value", (unsigned long)mySwitch.getReceivedValue());
|
||||
RFdata.set("protocol",(int)mySwitch.getReceivedProtocol());
|
||||
RFdata.set("length", (int)mySwitch.getReceivedBitlength());
|
||||
RFdata.set("delay", (int)mySwitch.getReceivedDelay());
|
||||
mySwitch.resetAvailable();
|
||||
|
||||
unsigned long MQTTvalue = RFdata.get<unsigned long>("value");
|
||||
if (!isAduplicate(MQTTvalue) && MQTTvalue!=0) {// conditions to avoid duplications of RF -->MQTT
|
||||
#ifdef ZmqttDiscovery //component creation for HA
|
||||
RFtoMQTTdiscovery(MQTTvalue);
|
||||
#endif
|
||||
pub(subjectRFtoMQTT,RFdata);
|
||||
trc(F("Store to avoid duplicate"));
|
||||
storeValue(MQTTvalue);
|
||||
if (repeatRFwMQTT){
|
||||
trc(F("Pub RF for rpt"));
|
||||
pub(subjectMQTTtoRF,RFdata);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef simpleReceiving
|
||||
void MQTTtoRF(char * topicOri, char * datacallback) {
|
||||
|
||||
unsigned long data = strtoul(datacallback, NULL, 10); // we will not be able to pass values > 4294967295
|
||||
|
||||
// RF DATA ANALYSIS
|
||||
//We look into the subject to see if a special RF protocol is defined
|
||||
String topic = topicOri;
|
||||
int valuePRT = 0;
|
||||
int valuePLSL = 0;
|
||||
int valueBITS = 0;
|
||||
int pos = topic.lastIndexOf(RFprotocolKey);
|
||||
if (pos != -1){
|
||||
pos = pos + +strlen(RFprotocolKey);
|
||||
valuePRT = (topic.substring(pos,pos + 1)).toInt();
|
||||
trc(F("RF Protocol:"));
|
||||
trc(valuePRT);
|
||||
}
|
||||
//We look into the subject to see if a special RF pulselength is defined
|
||||
int pos2 = topic.lastIndexOf(RFpulselengthKey);
|
||||
if (pos2 != -1) {
|
||||
pos2 = pos2 + strlen(RFpulselengthKey);
|
||||
valuePLSL = (topic.substring(pos2,pos2 + 3)).toInt();
|
||||
trc(F("RF Pulse Lgth:"));
|
||||
trc(valuePLSL);
|
||||
}
|
||||
int pos3 = topic.lastIndexOf(RFbitsKey);
|
||||
if (pos3 != -1){
|
||||
pos3 = pos3 + strlen(RFbitsKey);
|
||||
valueBITS = (topic.substring(pos3,pos3 + 2)).toInt();
|
||||
trc(F("Bits nb:"));
|
||||
trc(valueBITS);
|
||||
}
|
||||
|
||||
if ((topic == subjectMQTTtoRF) && (valuePRT == 0) && (valuePLSL == 0) && (valueBITS == 0)){
|
||||
trc(F("MQTTtoRF dflt"));
|
||||
mySwitch.setProtocol(1,350);
|
||||
mySwitch.send(data, 24);
|
||||
// Acknowledgement to the GTWRF topic
|
||||
pub(subjectGTWRFtoMQTT, datacallback);
|
||||
} else if ((valuePRT != 0) || (valuePLSL != 0)|| (valueBITS != 0)){
|
||||
trc(F("MQTTtoRF usr par."));
|
||||
if (valuePRT == 0) valuePRT = 1;
|
||||
if (valuePLSL == 0) valuePLSL = 350;
|
||||
if (valueBITS == 0) valueBITS = 24;
|
||||
trc(valuePRT);
|
||||
trc(valuePLSL);
|
||||
trc(valueBITS);
|
||||
mySwitch.setProtocol(valuePRT,valuePLSL);
|
||||
mySwitch.send(data, valueBITS);
|
||||
// Acknowledgement to the GTWRF topic
|
||||
pub(subjectGTWRFtoMQTT, datacallback);// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef jsonReceiving
|
||||
void MQTTtoRF(char * topicOri, JsonObject& RFdata) { // json object decoding
|
||||
|
||||
if (strcmp(topicOri,subjectMQTTtoRF) == 0){
|
||||
trc(F("MQTTtoRF json"));
|
||||
unsigned long data = RFdata["value"];
|
||||
if (data != 0) {
|
||||
int valuePRT = RFdata["protocol"]|1;
|
||||
int valuePLSL = RFdata["delay"]|350;
|
||||
int valueBITS = RFdata["length"]|24;
|
||||
int valueRPT = RFdata["repeat"]|RF_EMITTER_REPEAT;
|
||||
mySwitch.setRepeatTransmit(valueRPT);
|
||||
mySwitch.setProtocol(valuePRT,valuePLSL);
|
||||
mySwitch.send(data, valueBITS);
|
||||
trc(F("MQTTtoRF OK"));
|
||||
pub(subjectGTWRFtoMQTT, RFdata);// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
mySwitch.setRepeatTransmit(RF_EMITTER_REPEAT); // Restore the default value
|
||||
}else{
|
||||
trc(F("MQTTtoRF Fail json"));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,271 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This gateway enables to:
|
||||
- publish MQTT data to a different topic related to received 433Mhz signal DIO/new kaku protocol
|
||||
|
||||
Copyright: (c)Florian ROBERT
|
||||
Copyright: (c)Randy Simons http://randysimons.nl/
|
||||
|
||||
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/>.
|
||||
*/
|
||||
|
||||
/*Command example for switching off:
|
||||
sudo mosquitto_pub -t home/commands/MQTTtoRF2/CODE_8233372/UNIT_0/PERIOD_272 -m 0
|
||||
Command example for switching on:
|
||||
sudo mosquitto_pub -t home/commands/MQTTtoRF2/CODE_8233372/UNIT_0/PERIOD_272 -m 1
|
||||
Command example for dimming:
|
||||
sudo mosquitto_pub -t home/commands/MQTTtoRF2/CODE_8233372/UNIT_0/PERIOD_272 -m/DIM 8
|
||||
*/
|
||||
#include "User_config.h"
|
||||
|
||||
#ifdef ZgatewayRF2
|
||||
|
||||
#include <NewRemoteTransmitter.h>
|
||||
#include <NewRemoteReceiver.h>
|
||||
|
||||
struct RF2rxd
|
||||
{
|
||||
unsigned int period;
|
||||
unsigned long address;
|
||||
unsigned long groupBit;
|
||||
unsigned long unit;
|
||||
unsigned long switchType;
|
||||
bool hasNewData;
|
||||
};
|
||||
|
||||
RF2rxd rf2rd;
|
||||
|
||||
void setupRF2(){
|
||||
#ifndef ZgatewayRF //receiving with RF2 is not compatible with ZgatewayRF
|
||||
NewRemoteReceiver::init(RF_RECEIVER_PIN, 2, rf2Callback);
|
||||
trc(F("RF_EMITTER_PIN "));
|
||||
trc(RF_EMITTER_PIN);
|
||||
trc(F("RF_RECEIVER_PIN "));
|
||||
trc(RF_RECEIVER_PIN);
|
||||
trc(F("ZgatewayRF2 setup done "));
|
||||
#endif
|
||||
pinMode(RF_EMITTER_PIN, OUTPUT);
|
||||
digitalWrite(RF_EMITTER_PIN, LOW);
|
||||
}
|
||||
|
||||
void RF2toMQTT(){
|
||||
|
||||
if(rf2rd.hasNewData){
|
||||
trc(F("Creating RF2 buffer"));
|
||||
const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(5);
|
||||
StaticJsonBuffer<JSON_MSG_CALC_BUFFER> jsonBuffer;
|
||||
JsonObject& RF2data = jsonBuffer.createObject();
|
||||
|
||||
rf2rd.hasNewData = false;
|
||||
|
||||
trc(F("Rcv. RF2"));
|
||||
RF2data.set("unit", (int)rf2rd.unit);
|
||||
RF2data.set("groupBit", (int)rf2rd.groupBit);
|
||||
RF2data.set("period", (int)rf2rd.period);
|
||||
RF2data.set("address", (unsigned long)rf2rd.address);
|
||||
RF2data.set("switchType", (int)rf2rd.switchType);
|
||||
|
||||
trc(F("Adv data RF2toMQTT"));
|
||||
pub(subjectRF2toMQTT,RF2data);
|
||||
}
|
||||
}
|
||||
|
||||
void rf2Callback(unsigned int period, unsigned long address, unsigned long groupBit, unsigned long unit, unsigned long switchType) {
|
||||
|
||||
rf2rd.period=period;
|
||||
rf2rd.address=address;
|
||||
rf2rd.groupBit=groupBit;
|
||||
rf2rd.unit=unit;
|
||||
rf2rd.switchType=switchType;
|
||||
rf2rd.hasNewData=true;
|
||||
|
||||
}
|
||||
|
||||
#ifdef simpleReceiving
|
||||
void MQTTtoRF2(char * topicOri, char * datacallback) {
|
||||
|
||||
// RF DATA ANALYSIS
|
||||
//We look into the subject to see if a special RF protocol is defined
|
||||
String topic = topicOri;
|
||||
bool boolSWITCHTYPE;
|
||||
boolSWITCHTYPE = to_bool(datacallback);
|
||||
bool isDimCommand = false;
|
||||
|
||||
long valueCODE = 0;
|
||||
int valueUNIT = -1;
|
||||
int valuePERIOD = 0;
|
||||
int valueGROUP = 0;
|
||||
int valueDIM = -1;
|
||||
|
||||
int pos = topic.lastIndexOf(RF2codeKey);
|
||||
if (pos != -1){
|
||||
pos = pos + +strlen(RF2codeKey);
|
||||
valueCODE = (topic.substring(pos,pos + 8)).toInt();
|
||||
trc(F("RF2 code:"));
|
||||
trc(valueCODE);
|
||||
}
|
||||
int pos2 = topic.lastIndexOf(RF2periodKey);
|
||||
if (pos2 != -1) {
|
||||
pos2 = pos2 + strlen(RF2periodKey);
|
||||
valuePERIOD = (topic.substring(pos2,pos2 + 3)).toInt();
|
||||
trc(F("RF2 Period:"));
|
||||
trc(valuePERIOD);
|
||||
}
|
||||
int pos3 = topic.lastIndexOf(RF2unitKey);
|
||||
if (pos3 != -1){
|
||||
pos3 = pos3 + strlen(RF2unitKey);
|
||||
valueUNIT = (topic.substring(pos3, topic.indexOf("/", pos3))).toInt();
|
||||
trc(F("Unit:"));
|
||||
trc(valueUNIT);
|
||||
}
|
||||
int pos4 = topic.lastIndexOf(RF2groupKey);
|
||||
if (pos4 != -1) {
|
||||
pos4 = pos4 + strlen(RF2groupKey);
|
||||
valueGROUP = (topic.substring(pos4,pos4 + 1)).toInt();
|
||||
trc(F("RF2 Group:"));
|
||||
trc(valueGROUP);
|
||||
}
|
||||
int pos5 = topic.lastIndexOf(RF2dimKey);
|
||||
if (pos5 != -1) {
|
||||
isDimCommand = true;
|
||||
valueDIM = atoi(datacallback);
|
||||
trc(F("RF2 Dim:"));
|
||||
trc(valueDIM);
|
||||
}
|
||||
|
||||
if ((topic == subjectMQTTtoRF2) || (valueCODE != 0) || (valueUNIT != -1)|| (valuePERIOD != 0)){
|
||||
trc(F("MQTTtoRF2"));
|
||||
if (valueCODE == 0) valueCODE = 8233378;
|
||||
if (valueUNIT == -1) valueUNIT = 0;
|
||||
if (valuePERIOD == 0) valuePERIOD = 272;
|
||||
trc(valueCODE);
|
||||
trc(valueUNIT);
|
||||
trc(valuePERIOD);
|
||||
trc(valueGROUP);
|
||||
trc(boolSWITCHTYPE);
|
||||
trc(valueDIM);
|
||||
NewRemoteReceiver::disable();
|
||||
trc(F("Creating transmitter"));
|
||||
NewRemoteTransmitter transmitter(valueCODE, RF_EMITTER_PIN, valuePERIOD);
|
||||
trc(F("Sending data"));
|
||||
if (valueGROUP) {
|
||||
if (isDimCommand) {
|
||||
transmitter.sendGroupDim(valueDIM);
|
||||
}
|
||||
else {
|
||||
transmitter.sendGroup(boolSWITCHTYPE);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (isDimCommand) {
|
||||
transmitter.sendDim(valueUNIT, valueDIM);
|
||||
}
|
||||
else {
|
||||
transmitter.sendUnit(valueUNIT, boolSWITCHTYPE);
|
||||
}
|
||||
}
|
||||
trc(F("Data sent"));
|
||||
NewRemoteReceiver::enable();
|
||||
|
||||
// Publish state change back to MQTT
|
||||
String MQTTAddress;
|
||||
String MQTTperiod;
|
||||
String MQTTunit;
|
||||
String MQTTgroupBit;
|
||||
String MQTTswitchType;
|
||||
String MQTTdimLevel;
|
||||
|
||||
MQTTAddress = String(valueCODE);
|
||||
MQTTperiod = String(valuePERIOD);
|
||||
MQTTunit = String(valueUNIT);
|
||||
MQTTgroupBit = String(rf2rd.groupBit);
|
||||
MQTTswitchType = String(boolSWITCHTYPE);
|
||||
MQTTdimLevel = String(valueDIM);
|
||||
String MQTTRF2string;
|
||||
trc(F("Adv data MQTTtoRF2 push state via RF2toMQTT"));
|
||||
if (isDimCommand) {
|
||||
MQTTRF2string = subjectRF2toMQTT+String("/")+RF2codeKey+MQTTAddress+String("/")+RF2unitKey+MQTTunit+String("/")+RF2groupKey+MQTTgroupBit+String("/")+RF2dimKey+String("/")+RF2periodKey+MQTTperiod;
|
||||
pub(MQTTRF2string,MQTTdimLevel);
|
||||
}
|
||||
else {
|
||||
MQTTRF2string = subjectRF2toMQTT+String("/")+RF2codeKey+MQTTAddress+String("/")+RF2unitKey+MQTTunit+String("/")+RF2groupKey+MQTTgroupBit+String("/")+RF2periodKey+MQTTperiod;
|
||||
pub(MQTTRF2string,MQTTswitchType);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef jsonReceiving
|
||||
void MQTTtoRF2(char * topicOri, JsonObject& RF2data) { // json object decoding
|
||||
|
||||
if (strcmp(topicOri,subjectMQTTtoRF2) == 0){
|
||||
trc(F("MQTTtoRF2 json"));
|
||||
int boolSWITCHTYPE = RF2data["switchType"] | 99;
|
||||
if (boolSWITCHTYPE != 99) {
|
||||
trc(F("MQTTtoRF2 switch type ok"));
|
||||
bool isDimCommand = boolSWITCHTYPE == 2;
|
||||
unsigned long valueCODE = RF2data["address"];
|
||||
int valueUNIT = RF2data["unit"] | -1;
|
||||
int valuePERIOD = RF2data["period"];
|
||||
int valueGROUP = RF2data["group"];
|
||||
int valueDIM = RF2data["dim"] | -1;
|
||||
if ((valueCODE != 0) || (valueUNIT != -1)|| (valuePERIOD != 0)){
|
||||
trc(F("MQTTtoRF2"));
|
||||
if (valueCODE == 0) valueCODE = 8233378;
|
||||
if (valueUNIT == -1) valueUNIT = 0;
|
||||
if (valuePERIOD == 0) valuePERIOD = 272;
|
||||
trc(valueCODE);
|
||||
trc(valueUNIT);
|
||||
trc(valuePERIOD);
|
||||
trc(valueGROUP);
|
||||
trc(boolSWITCHTYPE);
|
||||
trc(valueDIM);
|
||||
NewRemoteReceiver::disable();
|
||||
NewRemoteTransmitter transmitter(valueCODE, RF_EMITTER_PIN, valuePERIOD);
|
||||
trc(F("Sending"));
|
||||
if (valueGROUP) {
|
||||
if (isDimCommand) {
|
||||
transmitter.sendGroupDim(valueDIM);
|
||||
}
|
||||
else {
|
||||
transmitter.sendGroup(boolSWITCHTYPE);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (isDimCommand) {
|
||||
transmitter.sendDim(valueUNIT, valueDIM);
|
||||
}
|
||||
else {
|
||||
transmitter.sendUnit(valueUNIT, boolSWITCHTYPE);
|
||||
}
|
||||
}
|
||||
trc(F("MQTTtoRF2 OK"));
|
||||
NewRemoteReceiver::enable();
|
||||
|
||||
// Publish state change back to MQTT
|
||||
pub(subjectGTWRF2toMQTT,RF2data);
|
||||
}
|
||||
}else{
|
||||
trc(F("MQTTtoRF2 Fail reading from json"));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,239 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your RF/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This gateway enables to:
|
||||
- receive MQTT data from a topic and send RF signal corresponding to the received MQTT data with an RFM69 module
|
||||
- publish MQTT data to a different topic related to received signal from an RFM69 module
|
||||
|
||||
Copyright: (c)Felix Rusu LowPowerLab.com
|
||||
Library and code by Felix Rusu - felix@lowpowerlab.com
|
||||
Modification of the code nanohab from bbx10 https://github.com/bbx10/nanohab
|
||||
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 ZgatewayRFM69
|
||||
|
||||
#include <RFM69.h> //https://www.github.com/lowpowerlab/rfm69
|
||||
#include <SPI.h>
|
||||
#include <EEPROM.h>
|
||||
|
||||
char RadioConfig[128];
|
||||
|
||||
// vvvvvvvvv Global Configuration vvvvvvvvvvv
|
||||
|
||||
struct _GLOBAL_CONFIG {
|
||||
uint32_t checksum;
|
||||
char rfmapname[32];
|
||||
char encryptkey[16+1];
|
||||
uint8_t networkid;
|
||||
uint8_t nodeid;
|
||||
uint8_t powerlevel; // bits 0..4 power leve, bit 7 RFM69HCW 1=true
|
||||
uint8_t rfmfrequency;
|
||||
};
|
||||
|
||||
#define GC_POWER_LEVEL (pGC->powerlevel & 0x1F)
|
||||
#define GC_IS_RFM69HCW ((pGC->powerlevel & 0x80) != 0)
|
||||
#define SELECTED_FREQ(f) ((pGC->rfmfrequency==f)?"selected":"")
|
||||
|
||||
struct _GLOBAL_CONFIG *pGC;
|
||||
|
||||
// vvvvvvvvv Global Configuration vvvvvvvvvvv
|
||||
uint32_t gc_checksum() {
|
||||
uint8_t *p = (uint8_t *)pGC;
|
||||
uint32_t checksum = 0;
|
||||
p += sizeof(pGC->checksum);
|
||||
for (size_t i = 0; i < (sizeof(*pGC) - 4); i++) {
|
||||
checksum += *p++;
|
||||
}
|
||||
return checksum;
|
||||
}
|
||||
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
void eeprom_setup() {
|
||||
EEPROM.begin(4096);
|
||||
pGC = (struct _GLOBAL_CONFIG *)EEPROM.getDataPtr();
|
||||
// if checksum bad init GC else use GC values
|
||||
if (gc_checksum() != pGC->checksum) {
|
||||
trc(F("Factory reset"));
|
||||
memset(pGC, 0, sizeof(*pGC));
|
||||
strcpy_P(pGC->encryptkey, ENCRYPTKEY);
|
||||
strcpy_P(pGC->rfmapname, RFM69AP_NAME);
|
||||
pGC->networkid = NETWORKID;
|
||||
pGC->nodeid = NODEID;
|
||||
pGC->powerlevel = ((IS_RFM69HCW)?0x80:0x00) | POWER_LEVEL;
|
||||
pGC->rfmfrequency = FREQUENCY;
|
||||
pGC->checksum = gc_checksum();
|
||||
EEPROM.commit();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
RFM69 radio;
|
||||
|
||||
void setupRFM69(void) {
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
eeprom_setup();
|
||||
#endif
|
||||
int freq;
|
||||
static const char PROGMEM JSONtemplate[] =
|
||||
R"({"msgType":"config","freq":%d,"rfm69hcw":%d,"netid":%d,"power":%d})";
|
||||
char payload[128];
|
||||
|
||||
radio = RFM69(RFM69_CS, RFM69_IRQ, GC_IS_RFM69HCW, RFM69_IRQN);
|
||||
|
||||
// Initialize radio
|
||||
if (!radio.initialize(pGC->rfmfrequency, pGC->nodeid, pGC->networkid))
|
||||
{
|
||||
trc(F("ZgatewayRFM69 initialization failed"));
|
||||
}
|
||||
|
||||
if (GC_IS_RFM69HCW) {
|
||||
radio.setHighPower(); // Only for RFM69HCW & HW!
|
||||
}
|
||||
radio.setPowerLevel(GC_POWER_LEVEL); // power output ranges from 0 (5dBm) to 31 (20dBm)
|
||||
|
||||
if (pGC->encryptkey[0] != '\0') radio.encrypt(pGC->encryptkey);
|
||||
|
||||
trc(F("ZgatewayRFM69 Listening and transmitting at"));
|
||||
switch (pGC->rfmfrequency) {
|
||||
case RF69_433MHZ:
|
||||
freq = 433;
|
||||
break;
|
||||
case RF69_868MHZ:
|
||||
freq = 868;
|
||||
break;
|
||||
case RF69_915MHZ:
|
||||
freq = 915;
|
||||
break;
|
||||
case RF69_315MHZ:
|
||||
freq = 315;
|
||||
break;
|
||||
default:
|
||||
freq = -1;
|
||||
break;
|
||||
}
|
||||
trc(freq);
|
||||
|
||||
size_t len = snprintf_P(RadioConfig, sizeof(RadioConfig), JSONtemplate,
|
||||
freq, GC_IS_RFM69HCW, pGC->networkid, GC_POWER_LEVEL);
|
||||
if (len >= sizeof(RadioConfig)) {
|
||||
trc(F("\n\n*** RFM69 config truncated ***\n"));
|
||||
}
|
||||
}
|
||||
|
||||
bool RFM69toMQTT(void) {
|
||||
//check if something was received (could be an interrupt from the radio)
|
||||
if (radio.receiveDone())
|
||||
{
|
||||
trc(F("Creating RFM69 buffer"));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject& RFM69data = jsonBuffer.createObject();
|
||||
uint8_t data[RF69_MAX_DATA_LEN+1]; // For the null character
|
||||
uint8_t SENDERID = radio.SENDERID;
|
||||
uint8_t DATALEN = radio.DATALEN;
|
||||
uint16_t RSSI = radio.RSSI;
|
||||
|
||||
//save packet because it may be overwritten
|
||||
memcpy(data, (void *)radio.DATA, DATALEN);
|
||||
data[DATALEN] = '\0'; // Terminate the string
|
||||
|
||||
// Ack as soon as possible
|
||||
//check if sender wanted an ACK
|
||||
if (radio.ACKRequested())
|
||||
{
|
||||
radio.sendACK();
|
||||
}
|
||||
//updateClients(senderId, rssi, (const char *)data);
|
||||
|
||||
trc(F("Data received"));
|
||||
trc((const char *)data);
|
||||
|
||||
char buff[sizeof(subjectRFM69toMQTT)+4];
|
||||
sprintf(buff, "%s/%d", subjectRFM69toMQTT, SENDERID);
|
||||
RFM69data.set("data", (char *)data);
|
||||
RFM69data.set("rssi", (int)radio.RSSI);
|
||||
RFM69data.set("senderid", (int)radio.SENDERID);
|
||||
pub(buff,RFM69data);
|
||||
|
||||
return true;
|
||||
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef simpleReceiving
|
||||
void MQTTtoRFM69(char * topicOri, char * datacallback) {
|
||||
|
||||
if(strstr(topicOri, subjectMQTTtoRFM69) != NULL){
|
||||
trc(F("MQTTtoRFM69 data analysis"));
|
||||
char data[RF69_MAX_DATA_LEN+1];
|
||||
memcpy(data, (void *)datacallback, RF69_MAX_DATA_LEN);
|
||||
data[RF69_MAX_DATA_LEN] = '\0';
|
||||
|
||||
//We look into the subject to see if a special RF protocol is defined
|
||||
String topic = topicOri;
|
||||
int valueRCV = defaultRFM69ReceiverId; //default receiver id value
|
||||
int pos = topic.lastIndexOf(RFM69receiverKey);
|
||||
if (pos != -1){
|
||||
pos = pos + +strlen(RFM69receiverKey);
|
||||
valueRCV = (topic.substring(pos,pos + 3)).toInt();
|
||||
trc(F("RFM69 receiver ID:"));
|
||||
trc(valueRCV);
|
||||
}
|
||||
if(radio.sendWithRetry(valueRCV, data, strlen(data),10)) {
|
||||
trc(F(" OK "));
|
||||
// Acknowledgement to the GTWRF topic
|
||||
char buff[sizeof(subjectGTWRFM69toMQTT)+4];
|
||||
sprintf(buff, "%s/%d", subjectGTWRFM69toMQTT, radio.SENDERID);
|
||||
pub(buff, data);
|
||||
} else {
|
||||
trc(F("RFM69 sending failed"));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#ifdef jsonReceiving
|
||||
void MQTTtoRFM69(char * topicOri, JsonObject& RFM69data) {
|
||||
|
||||
if (strcmp(topicOri,subjectMQTTtoRFM69) == 0){
|
||||
const char * data = RFM69data["data"];
|
||||
trc(F("MQTTtoRFM69 json data analysis"));
|
||||
if(data){
|
||||
trc(F("MQTTtoRFM69 data ok"));
|
||||
int valueRCV = RFM69data["receiverid"]| defaultRFM69ReceiverId; //default receiver id value
|
||||
trc(F("RFM69 receiver ID:"));
|
||||
trc(valueRCV);
|
||||
if(radio.sendWithRetry(valueRCV, data, strlen(data),10)) {
|
||||
trc(F(" OK "));
|
||||
// Acknowledgement to the GTWRF topic
|
||||
pub(subjectGTWRFM69toMQTT, RFM69data);// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
} else {
|
||||
trc(F("MQTTtoRFM69 sending failed"));
|
||||
}
|
||||
}else{
|
||||
trc(F("MQTTtoRFM69 Fail reading from json"));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,338 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This gateway enables to:
|
||||
- receive MQTT data from a topic and send RF 433Mhz signal corresponding to the received MQTT data using SONOFF RF BRIDGE
|
||||
- publish MQTT data to a different topic related to received 433Mhz signal using SONOFF RF BRIDGE
|
||||
|
||||
This implementation into OpenMQTTGateway is based on Xose Pérez work ESPURNA (https://bitbucket.org/xoseperez/espurna)
|
||||
|
||||
Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
|
||||
OpenMQTTGateway integration by 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 ZgatewaySRFB
|
||||
|
||||
unsigned char _uartbuf[RF_MESSAGE_SIZE+3] = {0};
|
||||
unsigned char _uartpos = 0;
|
||||
|
||||
void setupSRFB(){
|
||||
trc(F("ZgatewaySRFB setup done "));
|
||||
trc("Serial Baud" + String(SERIAL_BAUD));
|
||||
}
|
||||
|
||||
void _rfbSend(byte * message) {
|
||||
Serial.println();
|
||||
Serial.write(RF_CODE_START);
|
||||
Serial.write(RF_CODE_RFOUT);
|
||||
for (unsigned char j=0; j<RF_MESSAGE_SIZE; j++) {
|
||||
Serial.write(message[j]);
|
||||
}
|
||||
Serial.write(RF_CODE_STOP);
|
||||
Serial.flush();
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void _rfbSend(byte * message, int times) {
|
||||
|
||||
char buffer[RF_MESSAGE_SIZE];
|
||||
_rfbToChar(message, buffer);
|
||||
trc(F("[RFBRIDGE] Sending MESSAGE '%s' %d time(s)\n"));
|
||||
|
||||
for (int i=0; i<times; i++) {
|
||||
if (i>0) {
|
||||
unsigned long start = millis();
|
||||
while (millis() - start < RF_SEND_DELAY) delay(1);
|
||||
}
|
||||
_rfbSend(message);
|
||||
}
|
||||
}
|
||||
|
||||
bool SRFBtoMQTT() {
|
||||
|
||||
static bool receiving = false;
|
||||
|
||||
while (Serial.available()) {
|
||||
yield();
|
||||
byte c = Serial.read();
|
||||
|
||||
if (receiving) {
|
||||
if (c == RF_CODE_STOP) {
|
||||
_rfbDecode();
|
||||
receiving = false;
|
||||
} else {
|
||||
_uartbuf[_uartpos++] = c;
|
||||
}
|
||||
} else if (c == RF_CODE_START) {
|
||||
_uartpos = 0;
|
||||
receiving = true;
|
||||
}
|
||||
|
||||
}
|
||||
return receiving;
|
||||
}
|
||||
|
||||
void _rfbDecode() {
|
||||
|
||||
static unsigned long last = 0;
|
||||
if (millis() - last < RF_RECEIVE_DELAY) return;
|
||||
last = millis();
|
||||
|
||||
byte action = _uartbuf[0];
|
||||
char buffer[RF_MESSAGE_SIZE * 2 + 1] = {0};
|
||||
|
||||
if (action == RF_CODE_RFIN) {
|
||||
_rfbToChar(&_uartbuf[1], buffer);
|
||||
|
||||
trc(F("Creating SRFB buffer"));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject& SRFBdata = jsonBuffer.createObject();
|
||||
SRFBdata.set("raw", (char *)buffer);
|
||||
|
||||
char Tsyn[4]= {0};
|
||||
extract_char(buffer, Tsyn , 0 ,4, false, true);
|
||||
int val_Tsyn = (int)strtol(Tsyn, NULL, 10);
|
||||
SRFBdata.set("delay", (int)val_Tsyn);
|
||||
|
||||
char Tlow[4]= {0};
|
||||
extract_char(buffer,Tlow , 4 ,4, false, true);
|
||||
int val_Tlow = (int)strtol(Tlow, NULL, 10);
|
||||
SRFBdata.set("val_Tlow", (int)val_Tlow);
|
||||
|
||||
char Thigh[4]= {0};
|
||||
extract_char(buffer, Thigh , 8 ,4, false, true);
|
||||
int val_Thigh = (int)strtol(Thigh, NULL, 10);
|
||||
SRFBdata.set("val_Thigh", (int)val_Thigh);
|
||||
|
||||
char val[8]= {0};
|
||||
extract_char(buffer, val, 12 ,8, false,true);
|
||||
unsigned long MQTTvalue = (unsigned long)strtoul(val, NULL, 10);
|
||||
SRFBdata.set("value", (unsigned long)MQTTvalue);
|
||||
|
||||
if (!isAduplicate(MQTTvalue) && MQTTvalue!=0) {// conditions to avoid duplications of RF -->MQTT
|
||||
trc(F("Adv data SRFBtoMQTT"));
|
||||
pub(subjectSRFBtoMQTT,SRFBdata);
|
||||
trc(F("Store to avoid duplicate"));
|
||||
storeValue(MQTTvalue);
|
||||
if (repeatSRFBwMQTT){
|
||||
trc(F("Publish SRFB for repeat"));
|
||||
pub(subjectMQTTtoSRFB,SRFBdata);
|
||||
}
|
||||
}
|
||||
_rfbAck();
|
||||
}
|
||||
}
|
||||
|
||||
void _rfbAck() {
|
||||
trc(F("[RFBRIDGE] Sending ACK\n"));
|
||||
Serial.println();
|
||||
Serial.write(RF_CODE_START);
|
||||
Serial.write(RF_CODE_ACK);
|
||||
Serial.write(RF_CODE_STOP);
|
||||
Serial.flush();
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
/*
|
||||
From an hexa char array ("A220EE...") to a byte array (half the size)
|
||||
*/
|
||||
bool _rfbToArray(const char * in, byte * out) {
|
||||
if (strlen(in) != RF_MESSAGE_SIZE * 2) return false;
|
||||
char tmp[3] = {0};
|
||||
for (unsigned char p = 0; p<RF_MESSAGE_SIZE; p++) {
|
||||
memcpy(tmp, &in[p*2], 2);
|
||||
out[p] = strtol(tmp, NULL, 16);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
From a byte array to an hexa char array ("A220EE...", double the size)
|
||||
*/
|
||||
bool _rfbToChar(byte * in, char * out) {
|
||||
for (unsigned char p = 0; p<RF_MESSAGE_SIZE; p++) {
|
||||
sprintf_P(&out[p*2], PSTR("%02X"), in[p]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef simpleReceiving
|
||||
void MQTTtoSRFB(char * topicOri, char * datacallback) {
|
||||
|
||||
// RF DATA ANALYSIS
|
||||
String topic = topicOri;
|
||||
int valueRPT = 0;
|
||||
|
||||
if (topic == subjectMQTTtoSRFB){
|
||||
|
||||
int valueMiniPLSL = 0;
|
||||
int valueMaxiPLSL = 0;
|
||||
int valueSYNC = 0;
|
||||
|
||||
int pos = topic.lastIndexOf(SRFBRptKey);
|
||||
if (pos != -1){
|
||||
pos = pos + +strlen(SRFBRptKey);
|
||||
valueRPT = (topic.substring(pos,pos + 1)).toInt();
|
||||
trc(F("SRFB Repeat:"));
|
||||
trc(valueRPT);
|
||||
}
|
||||
|
||||
int pos2 = topic.lastIndexOf(SRFBminipulselengthKey);
|
||||
if (pos2 != -1) {
|
||||
pos2 = pos2 + strlen(SRFBminipulselengthKey);
|
||||
valueMiniPLSL = (topic.substring(pos2,pos2 + 3)).toInt();
|
||||
trc(F("RF Mini Pulse Lgth:"));
|
||||
trc(valueMiniPLSL);
|
||||
}
|
||||
|
||||
int pos3 = topic.lastIndexOf(SRFBmaxipulselengthKey);
|
||||
if (pos3 != -1){
|
||||
pos3 = pos3 + strlen(SRFBmaxipulselengthKey);
|
||||
valueMaxiPLSL = (topic.substring(pos3,pos3 + 2)).toInt();
|
||||
trc(F("RF Maxi Pulse Lgth:"));
|
||||
trc(valueMaxiPLSL);
|
||||
}
|
||||
|
||||
int pos4 = topic.lastIndexOf(SRFBsyncKey);
|
||||
if (pos4 != -1){
|
||||
pos4 = pos4 + strlen(SRFBsyncKey);
|
||||
valueSYNC = (topic.substring(pos4,pos4 + 2)).toInt();
|
||||
trc(F("RF sync:"));
|
||||
trc(valueSYNC);
|
||||
}
|
||||
|
||||
trc(F("MQTTtoSRFB prts"));
|
||||
if (valueRPT == 0) valueRPT = 1;
|
||||
if (valueMiniPLSL == 0) valueMiniPLSL = 320;
|
||||
if (valueMaxiPLSL == 0) valueMaxiPLSL = 900;
|
||||
if (valueSYNC == 0) valueSYNC = 9500;
|
||||
|
||||
byte hex_valueMiniPLSL[2];
|
||||
hex_valueMiniPLSL[0] = (int)((valueMiniPLSL >> 8) & 0xFF) ;
|
||||
hex_valueMiniPLSL[1] = (int)(valueMiniPLSL & 0xFF) ;
|
||||
|
||||
byte hex_valueMaxiPLSL[2];
|
||||
hex_valueMaxiPLSL[0] = (int)((valueMaxiPLSL >> 8) & 0xFF) ;
|
||||
hex_valueMaxiPLSL[1] = (int)(valueMaxiPLSL & 0xFF) ;
|
||||
|
||||
byte hex_valueSYNC[2];
|
||||
hex_valueSYNC[0] = (int)((valueSYNC >> 8) & 0xFF) ;
|
||||
hex_valueSYNC[1] = (int)(valueSYNC & 0xFF) ;
|
||||
|
||||
unsigned long data = strtoul(datacallback, NULL, 10); // we will not be able to pass values > 4294967295
|
||||
byte hex_data[3];
|
||||
hex_data[0] = (unsigned long)((data >> 16) & 0xFF) ;
|
||||
hex_data[1] = (unsigned long)((data >> 8) & 0xFF) ;
|
||||
hex_data[2] = (unsigned long)(data & 0xFF) ;
|
||||
|
||||
byte message_b[RF_MESSAGE_SIZE];
|
||||
|
||||
memcpy(message_b, hex_valueSYNC, 2);
|
||||
memcpy(message_b + 2, hex_valueMiniPLSL, 2);
|
||||
memcpy(message_b + 4, hex_valueMaxiPLSL, 2);
|
||||
memcpy(message_b + 6, hex_data, 3);
|
||||
|
||||
_rfbSend(message_b, valueRPT);
|
||||
// Acknowledgement to the GTWRF topic
|
||||
pub(subjectGTWSRFBtoMQTT, datacallback);// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
}
|
||||
if (topic == subjectMQTTtoSRFBRaw){
|
||||
|
||||
int pos = topic.lastIndexOf(SRFBRptKey);
|
||||
if (pos != -1){
|
||||
pos = pos + +strlen(SRFBRptKey);
|
||||
valueRPT = (topic.substring(pos,pos + 1)).toInt();
|
||||
trc(F("SRFB Repeat:"));
|
||||
trc(valueRPT);
|
||||
}
|
||||
if (valueRPT == 0) valueRPT = 1;
|
||||
|
||||
byte message_b[RF_MESSAGE_SIZE];
|
||||
_rfbToArray(datacallback,message_b);
|
||||
_rfbSend(message_b, valueRPT);
|
||||
// Acknowledgement to the GTWRF topic
|
||||
pub(subjectGTWSRFBtoMQTT, datacallback);// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#ifdef jsonReceiving
|
||||
void MQTTtoSRFB(char * topicOri, JsonObject& SRFBdata) {
|
||||
|
||||
// RF DATA ANALYSIS
|
||||
const char * raw = SRFBdata["raw"];
|
||||
int valueRPT = SRFBdata["repeat"]|1;
|
||||
if (strcmp(topicOri,subjectMQTTtoSRFB) == 0){
|
||||
trc(F("MQTTtoSRFB json"));
|
||||
if (raw){ // send raw in priority when defined in the json
|
||||
trc(F("MQTTtoSRFB raw ok"));
|
||||
byte message_b[RF_MESSAGE_SIZE];
|
||||
_rfbToArray(raw,message_b);
|
||||
_rfbSend(message_b, valueRPT);
|
||||
}else{
|
||||
unsigned long data = SRFBdata["value"];
|
||||
if (data != 0) {
|
||||
trc(F("MQTTtoSRFB data ok"));
|
||||
int valueMiniPLSL = SRFBdata["val_Tlow"];
|
||||
int valueMaxiPLSL =SRFBdata["val_Thigh"];
|
||||
int valueSYNC = SRFBdata["delay"];
|
||||
|
||||
if (valueRPT == 0) valueRPT = 1;
|
||||
if (valueMiniPLSL == 0) valueMiniPLSL = 320;
|
||||
if (valueMaxiPLSL == 0) valueMaxiPLSL = 900;
|
||||
if (valueSYNC == 0) valueSYNC = 9500;
|
||||
|
||||
byte hex_valueMiniPLSL[2];
|
||||
hex_valueMiniPLSL[0] = (int)((valueMiniPLSL >> 8) & 0xFF) ;
|
||||
hex_valueMiniPLSL[1] = (int)(valueMiniPLSL & 0xFF) ;
|
||||
|
||||
byte hex_valueMaxiPLSL[2];
|
||||
hex_valueMaxiPLSL[0] = (int)((valueMaxiPLSL >> 8) & 0xFF) ;
|
||||
hex_valueMaxiPLSL[1] = (int)(valueMaxiPLSL & 0xFF) ;
|
||||
|
||||
byte hex_valueSYNC[2];
|
||||
hex_valueSYNC[0] = (int)((valueSYNC >> 8) & 0xFF) ;
|
||||
hex_valueSYNC[1] = (int)(valueSYNC & 0xFF) ;
|
||||
|
||||
byte hex_data[3];
|
||||
hex_data[0] = (unsigned long)((data >> 16) & 0xFF) ;
|
||||
hex_data[1] = (unsigned long)((data >> 8) & 0xFF) ;
|
||||
hex_data[2] = (unsigned long)(data & 0xFF) ;
|
||||
|
||||
byte message_b[RF_MESSAGE_SIZE];
|
||||
|
||||
memcpy(message_b, hex_valueSYNC, 2);
|
||||
memcpy(message_b + 2, hex_valueMiniPLSL, 2);
|
||||
memcpy(message_b + 4, hex_valueMaxiPLSL, 2);
|
||||
memcpy(message_b + 6, hex_data, 3);
|
||||
|
||||
trc(F("MQTTtoSRFB OK"));
|
||||
_rfbSend(message_b, valueRPT);
|
||||
// Acknowledgement to the GTWRF topic
|
||||
pub(subjectGTWSRFBtoMQTT, SRFBdata);// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
}else{
|
||||
trc(F("MQTTtoSRFB error decoding value"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,424 @@
|
||||
/*
|
||||
OpenMQTTGateway Addon - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This is the Home Assistant mqtt Discovery addon.
|
||||
|
||||
Copyright: (c) Rafal Herok
|
||||
|
||||
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 ZmqttDiscovery
|
||||
|
||||
String getMacAddress()
|
||||
{
|
||||
uint8_t baseMac[6];
|
||||
char baseMacChr[13] = {0};
|
||||
#if defined(ESP8266)
|
||||
WiFi.macAddress(baseMac);
|
||||
sprintf(baseMacChr, "%02X%02X%02X%02X%02X%02X", baseMac[0], baseMac[1], baseMac[2], baseMac[3], baseMac[4], baseMac[5]);
|
||||
#elif defined(ESP32)
|
||||
esp_read_mac(baseMac, ESP_MAC_WIFI_STA);
|
||||
sprintf(baseMacChr, "%02X%02X%02X%02X%02X%02X", baseMac[0], baseMac[1], baseMac[2], baseMac[3], baseMac[4], baseMac[5]);
|
||||
#else
|
||||
sprintf(baseMacChr, "%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
#endif
|
||||
return String(baseMacChr);
|
||||
}
|
||||
|
||||
String getUniqueId(String name, String sufix)
|
||||
{
|
||||
String uniqueId = (String)getMacAddress() + name + sufix;
|
||||
return String(uniqueId);
|
||||
}
|
||||
|
||||
void createDiscovery(char * sensor_type,
|
||||
char * state_topic, char * s_name, char * unique_id,
|
||||
char * availability_topic, char * device_class, char * value_template,
|
||||
char * payload_on, char * payload_off, char * unit_of_meas,
|
||||
int off_delay,
|
||||
char * payload_available, char * payload_not_avalaible, bool child_device , char * command_topic)
|
||||
{
|
||||
const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(14) + JSON_OBJECT_SIZE(5)+ JSON_ARRAY_SIZE(1);
|
||||
StaticJsonBuffer<JSON_MSG_CALC_BUFFER> jsonBuffer;
|
||||
JsonObject &sensor = jsonBuffer.createObject();
|
||||
sensor.set("stat_t", state_topic); //state_topic
|
||||
sensor.set("name", s_name); //name
|
||||
sensor.set("uniq_id", unique_id); //unique_id
|
||||
if (device_class[0]) sensor.set("dev_cla", device_class); //device_class
|
||||
if (value_template[0]) sensor.set("val_tpl", value_template); //value_template
|
||||
if (payload_on[0]) sensor.set("pl_on", payload_on); // payload_on
|
||||
if (payload_off[0]) sensor.set("pl_off", payload_off); //payload_off
|
||||
if (unit_of_meas[0]) sensor.set("unit_of_meas", unit_of_meas); //unit_of_measurement*/
|
||||
if (off_delay != 0) sensor.set("off_delay", off_delay); //off_delay
|
||||
if (payload_available[0]) sensor.set("pl_avail", payload_available); // payload_on
|
||||
if (payload_not_avalaible[0]) sensor.set("pl_not_avail", payload_not_avalaible); //payload_off
|
||||
if (command_topic[0]) sensor.set("cmd_t", command_topic); //command_topic
|
||||
|
||||
/*if (strcmp(s_name, Gateway_Name) == 0){
|
||||
JsonArray &json_attributes = sensor.createNestedArray("json_attributes");
|
||||
json_attributes.add("uptime");
|
||||
json_attributes.add("freemem");
|
||||
json_attributes.add("rssi");
|
||||
json_attributes.add("SSID");
|
||||
}*/
|
||||
|
||||
|
||||
if (child_device){
|
||||
const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(5) + JSON_ARRAY_SIZE(2);
|
||||
StaticJsonBuffer<JSON_MSG_CALC_BUFFER> jsonDeviceBuffer;
|
||||
JsonObject &device = jsonDeviceBuffer.createObject();
|
||||
device.set("name", Gateway_Name);
|
||||
device.set("manufacturer", DEVICEMANUFACTURER);
|
||||
device.set("sw_version", OMG_VERSION);
|
||||
JsonArray &identifiers = device.createNestedArray("identifiers");
|
||||
identifiers.add(getMacAddress());
|
||||
sensor.set("device", device); //device sensor is connected to
|
||||
}
|
||||
String topic = String(discovery_Topic) + "/" + String(sensor_type) + "/" + String(unique_id) + "/config";
|
||||
pub((char *)topic.c_str(), sensor);
|
||||
}
|
||||
|
||||
void pubMqttDiscovery()
|
||||
{
|
||||
trc(F("omgStatusDiscovery"));
|
||||
createDiscovery("binary_sensor", //set sensorType
|
||||
will_Topic, Gateway_Name, (char *)getUniqueId("", "").c_str(), //set state_topic,name,uniqueId
|
||||
will_Topic, "connectivity", "", //set availability_topic,device_class,value_template,
|
||||
Gateway_AnnouncementMsg, will_Message, "", //set,payload_on,payload_off,unit_of_meas,
|
||||
0, //set optimistic,retain, off_delay
|
||||
Gateway_AnnouncementMsg, will_Message,true,"" //set,payload_avalaible,payload_not avalaible ,is a child device, command topic, state on, state off
|
||||
);
|
||||
|
||||
#ifdef ZsensorBME280
|
||||
#define BMEparametersCount 6
|
||||
trc(F("bme280Discovery"));
|
||||
char * BMEsensor[BMEparametersCount][8] = {
|
||||
{"sensor", "tempc", "bme","temperature","{{ value_json.tempc }}","", "", "°C"} ,
|
||||
{"sensor", "tempf", "bme","temperature","{{ value_json.tempf }}","", "", "°F"} ,
|
||||
{"sensor", "pa", "bme","","{{ float(value_json.pa) * 0.01 }}","", "", "hPa"} ,
|
||||
{"sensor", "hum", "bme","humidity","{{ value_json.hum }}","", "", "%"} ,
|
||||
{"sensor", "altim", "bme","","{{ value_json.altim }}","", "", "m"} ,
|
||||
{"sensor", "altift", "bme","","{{ value_json.altift }}","", "", "ft"}
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
};
|
||||
|
||||
for (int i=0;i<BMEparametersCount;i++){
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(BMEsensor[i][1]);
|
||||
createDiscovery(BMEsensor[i][0],
|
||||
BMETOPIC, BMEsensor[i][1], (char *)getUniqueId(BMEsensor[i][1], BMEsensor[i][2]).c_str(),
|
||||
will_Topic, BMEsensor[i][3], BMEsensor[i][4],
|
||||
BMEsensor[i][5], BMEsensor[i][6], BMEsensor[i][7],
|
||||
0,"","",true,"");
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef ZsensorDHT
|
||||
#define DHTparametersCount 2
|
||||
trc(F("DHTDiscovery"));
|
||||
char * DHTsensor[DHTparametersCount][8] = {
|
||||
{"sensor", "tempc", "dht", "temperature","{{ value_json.temp }}","", "", "°C"} ,
|
||||
{"sensor", "hum", "dht", "humidity","{{ value_json.hum }}","", "", "%"}
|
||||
};
|
||||
|
||||
for (int i=0;i<DHTparametersCount;i++){
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(DHTsensor[i][1]);
|
||||
createDiscovery(DHTsensor[i][0],
|
||||
DHTTOPIC, DHTsensor[i][1], (char *)getUniqueId(DHTsensor[i][1], DHTsensor[i][2]).c_str(),
|
||||
will_Topic, DHTsensor[i][3], DHTsensor[i][4],
|
||||
DHTsensor[i][5], DHTsensor[i][6], DHTsensor[i][7],
|
||||
0,"","",true,"");
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef ZsensorADC
|
||||
trc(F("ADCDiscovery"));
|
||||
char * ADCsensor[8] = {"sensor", "adc", "adc", "","{{ value_json.adc }}","", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(ADCsensor[1]);
|
||||
createDiscovery(ADCsensor[0],
|
||||
ADCTOPIC, ADCsensor[1], (char *)getUniqueId(ADCsensor[1], ADCsensor[2]).c_str(),
|
||||
will_Topic, ADCsensor[3], ADCsensor[4],
|
||||
ADCsensor[5], ADCsensor[6], ADCsensor[7],
|
||||
0,"","",true,"");
|
||||
#endif
|
||||
|
||||
#ifdef ZsensorBH1750
|
||||
#define BH1750parametersCount 3
|
||||
trc(F("BH1750Discovery"));
|
||||
char * BH1750sensor[BH1750parametersCount][8] = {
|
||||
{"sensor", "lux", "BH1750", "illuminance","{{ value_json.lux }}","", "", "lu"} ,
|
||||
{"sensor", "ftCd", "BH1750","","{{ value_json.ftCd }}","", "", ""} ,
|
||||
{"sensor", "wattsm2", "BH1750","","{{ value_json.wattsm2 }}","", "", "wm²"}
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
};
|
||||
|
||||
for (int i=0;i<BH1750parametersCount;i++){
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(BH1750sensor[i][1]);
|
||||
createDiscovery(BH1750sensor[i][0],
|
||||
subjectBH1750toMQTT, BH1750sensor[i][1], (char *)getUniqueId(BH1750sensor[i][1], BH1750sensor[i][2]).c_str(),
|
||||
will_Topic, BH1750sensor[i][3], BH1750sensor[i][4],
|
||||
BH1750sensor[i][5], BH1750sensor[i][6], BH1750sensor[i][7],
|
||||
0,"","",true,"");
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef ZsensorTSL2561
|
||||
#define TSL2561parametersCount 3
|
||||
trc(F("TSL2561Discovery"));
|
||||
char * TSL2561sensor[TSL2561parametersCount][8] = {
|
||||
{"sensor", "lux", "TSL2561", "illuminance","{{ value_json.lux }}","", "", "lu"} ,
|
||||
{"sensor", "ftcd", "TSL2561","","{{ value_json.ftcd }}","", "", ""} ,
|
||||
{"sensor", "wattsm2", "TSL2561","","{{ value_json.wattsm2 }}","", "", "wm²"}
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
};
|
||||
|
||||
for (int i=0;i<TSL2561parametersCount;i++){
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(TSL2561sensor[i][1]);
|
||||
createDiscovery(TSL2561sensor[i][0],
|
||||
subjectTSL12561toMQTT, TSL2561sensor[i][1], (char *)getUniqueId(TSL2561sensor[i][1], TSL2561sensor[i][2]).c_str(),
|
||||
will_Topic, TSL2561sensor[i][3], TSL2561sensor[i][4],
|
||||
TSL2561sensor[i][5], TSL2561sensor[i][6], TSL2561sensor[i][7],
|
||||
0,"","",true,"");
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef ZsensorHCSR501
|
||||
trc(F("HCSR501Discovery"));
|
||||
char * HCSR501sensor[8] = {"binary_sensor", "hcsr501", "hcsr501", "","{{value_json.hcsr501}}","true", "false", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(HCSR501sensor[1]);
|
||||
createDiscovery(HCSR501sensor[0],
|
||||
subjectHCSR501toMQTT, HCSR501sensor[1], (char *)getUniqueId(HCSR501sensor[1], HCSR501sensor[2]).c_str(),
|
||||
will_Topic, HCSR501sensor[3], HCSR501sensor[4],
|
||||
HCSR501sensor[5], HCSR501sensor[6], HCSR501sensor[7],
|
||||
0,"","",true,"");
|
||||
#endif
|
||||
|
||||
#ifdef ZsensorGPIOInput
|
||||
trc(F("GPIOInputDiscovery"));
|
||||
char * GPIOInputsensor[8] = {"binary_sensor", "GPIOInput", "GPIOInput", "","{{value_json.gpio}}","HIGH", "LOW", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(GPIOInputsensor[1]);
|
||||
createDiscovery(GPIOInputsensor[0],
|
||||
subjectGPIOInputtoMQTT, GPIOInputsensor[1], (char *)getUniqueId(GPIOInputsensor[1], GPIOInputsensor[2]).c_str(),
|
||||
will_Topic, GPIOInputsensor[3], GPIOInputsensor[4],
|
||||
GPIOInputsensor[5], GPIOInputsensor[6], GPIOInputsensor[7],
|
||||
0,"","",true,"");
|
||||
#endif
|
||||
|
||||
#ifdef ZsensorINA226
|
||||
#define INA226parametersCount 3
|
||||
trc(F("INA226Discovery"));
|
||||
char * INA226sensor[INA226parametersCount][8] = {
|
||||
{"sensor", "volt", "INA226", "","{{ value_json.volt }}","", "", "V"} ,
|
||||
{"sensor", "current", "INA226","","{{ value_json.current }}","", "", "A"} ,
|
||||
{"sensor", "power", "INA226","","{{ value_json.power }}","", "", "W"}
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
};
|
||||
|
||||
for (int i=0;i<INA226parametersCount;i++){
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(INA226sensor[i][1]);
|
||||
createDiscovery(INA226sensor[i][0],
|
||||
subjectINA226toMQTT, INA226sensor[i][1], (char *)getUniqueId(INA226sensor[i][1], INA226sensor[i][2]).c_str(),
|
||||
will_Topic, INA226sensor[i][3], INA226sensor[i][4],
|
||||
INA226sensor[i][5], INA226sensor[i][6], INA226sensor[i][7],
|
||||
0,"","",true,"");
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef ZactuatorONOFF
|
||||
trc(F("actuatorONOFFDiscovery"));
|
||||
char * actuatorONOFF[8] = {"switch", "actuatorONOFF", "", "","","ON", "OFF", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(actuatorONOFF[1]);
|
||||
createDiscovery(actuatorONOFF[0],
|
||||
subjectGTWONOFFtoMQTT, actuatorONOFF[1], (char *)getUniqueId(actuatorONOFF[1], actuatorONOFF[2]).c_str(),
|
||||
will_Topic, actuatorONOFF[3], actuatorONOFF[4],
|
||||
actuatorONOFF[5], actuatorONOFF[6], actuatorONOFF[7],
|
||||
0,"","",true,subjectMQTTtoONOFF);
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef ZgatewayRF
|
||||
// Sensor to display RF received value
|
||||
trc(F("gatewayRFDiscovery"));
|
||||
char * gatewayRF[8] = {"sensor", "gatewayRF", "", "","{{ value_json.value }}","", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(gatewayRF[1]);
|
||||
createDiscovery(gatewayRF[0],
|
||||
subjectRFtoMQTT, gatewayRF[1], (char *)getUniqueId(gatewayRF[1], gatewayRF[2]).c_str(),
|
||||
will_Topic, gatewayRF[3], gatewayRF[4],
|
||||
gatewayRF[5], gatewayRF[6], gatewayRF[7],
|
||||
0,"","",true,"");
|
||||
#endif
|
||||
|
||||
#ifdef ZgatewayRF2
|
||||
// Sensor to display RF received value
|
||||
trc(F("gatewayRF2Discovery"));
|
||||
char * gatewayRF2[8] = {"sensor", "gatewayRF2", "", "","{{ value_json.value }}","", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(gatewayRF2[1]);
|
||||
createDiscovery(gatewayRF2[0],
|
||||
subjectRF2toMQTT, gatewayRF2[1], (char *)getUniqueId(gatewayRF2[1], gatewayRF2[2]).c_str(),
|
||||
will_Topic, gatewayRF2[3], gatewayRF2[4],
|
||||
gatewayRF2[5], gatewayRF2[6], gatewayRF2[7],
|
||||
0,"","",true,"");
|
||||
#endif
|
||||
|
||||
#ifdef ZgatewayRF315
|
||||
// Sensor to display RF received value
|
||||
trc(F("gatewayRF315Discovery"));
|
||||
char * gatewayRF315[8] = {"sensor", "gatewayRF315", "", "","{{ value_json.value }}","", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(gatewayRF315[1]);
|
||||
createDiscovery(gatewayRF315[0],
|
||||
subjectRF315toMQTT, gatewayRF315[1], (char *)getUniqueId(gatewayRF315[1], gatewayRF315[2]).c_str(),
|
||||
will_Topic, gatewayRF315[3], gatewayRF315[4],
|
||||
gatewayRF315[5], gatewayRF315[6], gatewayRF315[7],
|
||||
0,"","",true,"");
|
||||
#endif
|
||||
|
||||
#ifdef ZgatewayRFM69
|
||||
// Sensor to display RF received value
|
||||
trc(F("gatewayRFM69Discovery"));
|
||||
char * gatewayRFM69[8] = {"sensor", "gatewayRFM69", "", "","{{ value_json.value }}","", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(gatewayRFM69[1]);
|
||||
createDiscovery(gatewayRFM69[0],
|
||||
subjectRFM69toMQTT, gatewayRFM69[1], (char *)getUniqueId(gatewayRFM69[1], gatewayRFM69[2]).c_str(),
|
||||
will_Topic, gatewayRFM69[3], gatewayRFM69[4],
|
||||
gatewayRFM69[5], gatewayRFM69[6], gatewayRFM69[7],
|
||||
0,"","",true,"");
|
||||
#endif
|
||||
|
||||
#ifdef ZgatewayLORA
|
||||
// Sensor to display RF received value
|
||||
trc(F("gatewayLORADiscovery"));
|
||||
char * gatewayLORA[8] = {"sensor", "gatewayLORA", "", "","{{ value_json.message }}","", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(gatewayLORA[1]);
|
||||
createDiscovery(gatewayLORA[0],
|
||||
subjectLORAtoMQTT, gatewayLORA[1], (char *)getUniqueId(gatewayLORA[1], gatewayLORA[2]).c_str(),
|
||||
will_Topic, gatewayLORA[3], gatewayLORA[4],
|
||||
gatewayLORA[5], gatewayLORA[6], gatewayLORA[7],
|
||||
0,"","",true,"");
|
||||
#endif
|
||||
|
||||
#ifdef ZgatewaySRFB
|
||||
// Sensor to display RF received value
|
||||
trc(F("gatewaySRFBDiscovery"));
|
||||
char * gatewaySRFB[8] = {"sensor", "gatewaySRFB", "", "","{{ value_json.value }}","", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(gatewaySRFB[1]);
|
||||
createDiscovery(gatewaySRFB[0],
|
||||
subjectSRFBtoMQTT, gatewaySRFB[1], (char *)getUniqueId(gatewaySRFB[1], gatewaySRFB[2]).c_str(),
|
||||
will_Topic, gatewaySRFB[3], gatewaySRFB[4],
|
||||
gatewaySRFB[5], gatewaySRFB[6], gatewaySRFB[7],
|
||||
0,"","",true,"");
|
||||
#endif
|
||||
|
||||
#ifdef ZgatewayPilight
|
||||
// Sensor to display RF received value
|
||||
trc(F("gatewayPilightDiscovery"));
|
||||
char * gatewayPilight[8] = {"sensor", "gatewayPilight", "", "","{{ value_json.message }}","", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(gatewayPilight[1]);
|
||||
createDiscovery(gatewayPilight[0],
|
||||
subjectPilighttoMQTT, gatewayPilight[1], (char *)getUniqueId(gatewayPilight[1], gatewayPilight[2]).c_str(),
|
||||
will_Topic, gatewayPilight[3], gatewayPilight[4],
|
||||
gatewayPilight[5], gatewayPilight[6], gatewayPilight[7],
|
||||
0,"","",true,"");
|
||||
#endif
|
||||
|
||||
#ifdef ZgatewayIR
|
||||
// Sensor to display IR received value
|
||||
trc(F("gatewayIRDiscovery"));
|
||||
char * gatewayIR[8] = {"sensor", "gatewayIR", "", "","{{ value_json.value }}","", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(gatewayIR[1]);
|
||||
createDiscovery(gatewayIR[0],
|
||||
subjectIRtoMQTT, gatewayIR[1], (char *)getUniqueId(gatewayIR[1], gatewayIR[2]).c_str(),
|
||||
will_Topic, gatewayIR[3], gatewayIR[4],
|
||||
gatewayIR[5], gatewayIR[6], gatewayIR[7],
|
||||
0,"","",true,"");
|
||||
#endif
|
||||
|
||||
#ifdef Zgateway2G
|
||||
// Sensor to display 2G received value
|
||||
trc(F("gateway2GDiscovery"));
|
||||
char * gateway2G[8] = {"sensor", "gateway2G", "", "","{{ value_json.message }}","", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(gateway2G[1]);
|
||||
createDiscovery(gateway2G[0],
|
||||
subject2GtoMQTT, gateway2G[1], (char *)getUniqueId(gateway2G[1], gateway2G[2]).c_str(),
|
||||
will_Topic, gateway2G[3], gateway2G[4],
|
||||
gateway2G[5], gateway2G[6], gateway2G[7],
|
||||
0,"","",true,"");
|
||||
#endif
|
||||
|
||||
#ifdef ZgatewayBT
|
||||
// Sensor to display BT received value
|
||||
trc(F("gatewayBTDiscovery"));
|
||||
char * gatewayBT[8] = {"sensor", "gatewayBT", "", "","{{ value_json.id }}","", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(gatewayBT[1]);
|
||||
createDiscovery(gatewayBT[0],
|
||||
subjectBTtoMQTT, gatewayBT[1], (char *)getUniqueId(gatewayBT[1], gatewayBT[2]).c_str(),
|
||||
will_Topic, gatewayBT[3], gatewayBT[4],
|
||||
gatewayBT[5], gatewayBT[6], gatewayBT[7],
|
||||
0,"","",true,"");
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
OpenMQTTGateway Addon - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
Analog pin reading Addon
|
||||
|
||||
Copyright: (c)Florian ROBERT
|
||||
|
||||
Contributors:
|
||||
- 1technophile
|
||||
|
||||
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 ZsensorADC
|
||||
|
||||
#if defined(ESP8266)
|
||||
ADC_MODE(ADC_TOUT);
|
||||
#endif
|
||||
|
||||
//Time used to wait for an interval before resending adc value
|
||||
unsigned long timeadc = 0;
|
||||
|
||||
void MeasureADC(){
|
||||
if (millis() > (timeadc + TimeBetweenReadingADC)) {//retriving value of temperature and humidity of the box from DHT every xUL
|
||||
#if defined(ESP8266)
|
||||
yield();
|
||||
#endif
|
||||
timeadc = millis();
|
||||
static int persistedadc;
|
||||
int val = analogRead(ADC_PIN);
|
||||
if (isnan(val)) {
|
||||
trc(F("Failed to read from ADC !"));
|
||||
}else{
|
||||
if(val >= persistedadc + ThresholdReadingADC || val <= persistedadc - ThresholdReadingADC){
|
||||
trc(F("Creating ADC buffer"));
|
||||
const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(1);
|
||||
StaticJsonBuffer<JSON_MSG_CALC_BUFFER> jsonBuffer;
|
||||
JsonObject& ADCdata = jsonBuffer.createObject();
|
||||
ADCdata.set("adc", (int)val);
|
||||
pub(ADCTOPIC,ADCdata);
|
||||
persistedadc = val;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
OpenMQTTGateway Addon - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This is the Light Meter Addon:
|
||||
- Measures ambient Light Intensity in Lux (lx), Foot Candela (ftcd) and Watt/m^2 (wattsm2)
|
||||
- Required Hardware Module: BH1750
|
||||
|
||||
Connection Schemata:
|
||||
--------------------
|
||||
|
||||
BH1750 ------> Arduino Uno ----------> ESP8266
|
||||
==============================================
|
||||
Vcc ---------> 5V -------------------> Vu (5V)
|
||||
GND ---------> GND ------------------> GND
|
||||
SCL ---------> Pin A5 ---------------> D1
|
||||
SDA ---------> Pin A4 ---------------> D2
|
||||
ADD ---------> N/C (Not Connected) --> N/C (Not Connected)
|
||||
|
||||
Copyright: (c) Hans-Juergen Dinges
|
||||
|
||||
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 ZsensorBH1750
|
||||
#include "math.h" // Library for trig and exponential functions
|
||||
#include "Wire.h" // Library for communication with I2C / TWI devices
|
||||
|
||||
void setupZsensorBH1750()
|
||||
{
|
||||
Wire.begin();
|
||||
Wire.beginTransmission(BH1750_i2c_addr);
|
||||
Wire.write(0x10); // Set resolution to 1 Lux
|
||||
Wire.endTransmission();
|
||||
delay(300);
|
||||
}
|
||||
|
||||
// void loop()
|
||||
void MeasureLightIntensity()
|
||||
{
|
||||
if (millis() > (timebh1750 + TimeBetweenReadingBH1750)) {//retriving value of Lux, FtCd and Wattsm2 from BH1750
|
||||
trc(F("Creating BH1750 buffer"));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject& BH1750data = jsonBuffer.createObject();
|
||||
|
||||
timebh1750 = millis();
|
||||
unsigned int i=0;
|
||||
static float persistedll;
|
||||
static float persistedlf;
|
||||
static float persistedlw;
|
||||
unsigned int Lux;
|
||||
float ftcd;
|
||||
float Wattsm2;
|
||||
|
||||
// Check if reads failed and exit early (to try again).
|
||||
Wire.requestFrom(BH1750_i2c_addr, 2);
|
||||
if(Wire.available() != 2) {
|
||||
trc(F("Failed to read from LightSensor BH1750!"));
|
||||
}else{
|
||||
i = Wire.read();
|
||||
i <<=8;
|
||||
i |= Wire.read();
|
||||
|
||||
// Calculate the Values
|
||||
Lux = i/1.2; // Convert to Lux
|
||||
ftcd = Lux/10.764;
|
||||
Wattsm2 = Lux/683.0;
|
||||
|
||||
/*
|
||||
Useful Information ;-)
|
||||
lux (lx) # 1 lx = 1 lm/m² = 1 cd·sr·m⁻².
|
||||
meter-candle (m-cd) # 1 m·cd = 1 lx = 1 lm/m² = 1 cd·sr·m⁻².
|
||||
centimeter-candle (cm-sd) # 1 m·cd = 1 lx = 1 lm/m² = 1 cd·sr·m⁻².
|
||||
foot-candle (ft-c) #
|
||||
phot (ph) # 1 ph = 1 lm/cm² = 10,000 lm/m² - 10,000 lx = 10 klx
|
||||
nox (nx) # 1 nox = 1 millilux
|
||||
candela steradin/meter2(cd·sr·m⁻²) # 1 lx = 1 lm/m² = 1 cd·sr·m⁻²
|
||||
lumen/meter2 (lm·m⁻²) # 1 lx = 1 lm/m² = 1 cd·sr·m⁻²
|
||||
lumen/centimeter2 (lm·cm⁻²) # 1 lm/cm² = 10,000 lx = 10,000 cd·sr·m⁻²
|
||||
lumen/foot2 (lm·ft⁻²) # (lm·ft⁻²)
|
||||
watt/centimeter2 at 555nm (W·cm⁻²) #
|
||||
*/
|
||||
|
||||
// Generate Lux
|
||||
if(Lux != persistedll || bh1750_always){
|
||||
BH1750data.set("lux", (unsigned int)Lux);
|
||||
}else{
|
||||
trc(F("Same lux don't send it"));
|
||||
}
|
||||
|
||||
// Generate FtCd
|
||||
if(ftcd != persistedlf || bh1750_always){
|
||||
BH1750data.set("ftcd", (unsigned int)ftcd);
|
||||
}else{
|
||||
trc(F("Same ftcd don't send it"));
|
||||
}
|
||||
|
||||
// Generate Watts/m2
|
||||
if(Wattsm2 != persistedlw || bh1750_always){
|
||||
BH1750data.set("wattsm2", (unsigned int)Wattsm2);
|
||||
}else{
|
||||
trc(F("Same wattsm2 don't send it"));
|
||||
}
|
||||
if(BH1750data.size()>0) pub(subjectBH1750toMQTT,BH1750data);
|
||||
}
|
||||
persistedll = Lux;
|
||||
persistedlf = ftcd;
|
||||
persistedlw = Wattsm2;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,189 @@
|
||||
/*
|
||||
OpenMQTTGateway Addon - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This is the Climate Addon:
|
||||
- Measures Temperature, Humidity and Pressure
|
||||
- Generates Values for: Temperature in degrees C and F, Humidity in %, Pressure in Pa, Altitude in Meter and Feet
|
||||
- Required Hardware Module: Bosch BME280
|
||||
- Required Library: SparkFun BME280 Library v1.1.0 by Marshall Taylor
|
||||
|
||||
Connection Schemata:
|
||||
--------------------
|
||||
|
||||
bme280 ------> Arduino Uno ----------> ESP8266
|
||||
==============================================
|
||||
Vcc ---------> 5V -------------------> Vu (5V)
|
||||
GND ---------> GND ------------------> GND
|
||||
SCL ---------> Pin A5 ---------------> D1
|
||||
SDA ---------> Pin A4 ---------------> D2
|
||||
|
||||
Copyright: (c) Hans-Juergen Dinges
|
||||
|
||||
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 ZsensorBME280
|
||||
#include "Wire.h" // Library for communication with I2C / TWI devices
|
||||
#include <stdint.h>
|
||||
#include "SparkFunBME280.h"
|
||||
|
||||
//Global sensor object
|
||||
BME280 mySensor;
|
||||
|
||||
void setupZsensorBME280()
|
||||
{
|
||||
mySensor.settings.commInterface = I2C_MODE;
|
||||
mySensor.settings.I2CAddress = 0x76; // Bosch BME280 I2C Address
|
||||
|
||||
//***Operation settings*****************************//
|
||||
|
||||
// runMode Setting - Values:
|
||||
// -------------------------
|
||||
// 0, Sleep mode
|
||||
// 1 or 2, Forced mode
|
||||
// 3, Normal mode
|
||||
mySensor.settings.runMode = 3; //Normal mode
|
||||
|
||||
// tStandby Setting - Values:
|
||||
// --------------------------
|
||||
// 0, 0.5ms
|
||||
// 1, 62.5ms
|
||||
// 2, 125ms
|
||||
// 3, 250ms
|
||||
// 4, 500ms
|
||||
// 5, 1000ms
|
||||
// 6, 10ms
|
||||
// 7, 20ms
|
||||
mySensor.settings.tStandby = 1;
|
||||
|
||||
// Filter can be off or number of FIR coefficients - Values:
|
||||
// ---------------------------------------------------------
|
||||
// 0, filter off
|
||||
// 1, coefficients = 2
|
||||
// 2, coefficients = 4
|
||||
// 3, coefficients = 8
|
||||
// 4, coefficients = 16
|
||||
mySensor.settings.filter = 4;
|
||||
|
||||
// tempOverSample - Values:
|
||||
// ------------------------
|
||||
// 0, skipped
|
||||
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
|
||||
mySensor.settings.tempOverSample = 1;
|
||||
|
||||
// pressOverSample - Values:
|
||||
// -------------------------
|
||||
// 0, skipped
|
||||
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
|
||||
mySensor.settings.pressOverSample = 1;
|
||||
|
||||
// humidOverSample - Values:
|
||||
// -------------------------
|
||||
// 0, skipped
|
||||
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
|
||||
mySensor.settings.humidOverSample = 1;
|
||||
|
||||
delay(10); // Gives the Sensor enough time to turn on (The BME280 requires 2ms to start up)
|
||||
Serial.print("Bosch BME280 Initialized - Result of .begin(): 0x");
|
||||
Serial.println(mySensor.begin(), HEX);
|
||||
}
|
||||
|
||||
void MeasureTempHumAndPressure()
|
||||
{
|
||||
|
||||
if (millis() > (timebme280 + TimeBetweenReadingbme280)) {
|
||||
|
||||
timebme280 = millis();
|
||||
static float persisted_bme_tempc;
|
||||
static float persisted_bme_tempf;
|
||||
static float persisted_bme_hum;
|
||||
static float persisted_bme_pa;
|
||||
static float persisted_bme_altim;
|
||||
static float persisted_bme_altift;
|
||||
|
||||
float BmeTempC = mySensor.readTempC();
|
||||
float BmeTempF = mySensor.readTempF();
|
||||
float BmeHum = mySensor.readFloatHumidity();
|
||||
float BmePa = mySensor.readFloatPressure();
|
||||
float BmeAltiM = mySensor.readFloatAltitudeMeters();
|
||||
float BmeAltiFt = mySensor.readFloatAltitudeFeet();
|
||||
|
||||
// Check if reads failed and exit early (to try again).
|
||||
if (isnan(BmeTempC) || isnan(BmeTempF) || isnan(BmeHum) || isnan(BmePa) || isnan(BmeAltiM) || isnan(BmeAltiFt)) {
|
||||
trc(F("Failed to read from Weather Sensor BME280!"));
|
||||
}else{
|
||||
trc(F("Creating BME280 buffer"));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject& BME280data = jsonBuffer.createObject();
|
||||
// Generate Temperature in degrees C
|
||||
if(BmeTempC != persisted_bme_tempc || bme280_always){
|
||||
BME280data.set("tempc", (float)BmeTempC);
|
||||
}else{
|
||||
trc(F("Same Degrees C don't send it"));
|
||||
}
|
||||
|
||||
// Generate Temperature in degrees F
|
||||
if(BmeTempF != persisted_bme_tempf || bme280_always){
|
||||
BME280data.set("tempf", (float)BmeTempF);
|
||||
}else{
|
||||
trc(F("Same Degrees F don't send it"));
|
||||
}
|
||||
|
||||
// Generate Humidity in percent
|
||||
if(BmeHum != persisted_bme_hum || bme280_always){
|
||||
BME280data.set("hum", (float)BmeHum);
|
||||
}else{
|
||||
trc(F("Same Humidity don't send it"));
|
||||
}
|
||||
|
||||
// Generate Pressure in Pa
|
||||
if(BmePa != persisted_bme_pa || bme280_always){
|
||||
BME280data.set("pa", (float)BmePa);
|
||||
}else{
|
||||
trc(F("Same Pressure don't send it"));
|
||||
}
|
||||
|
||||
// Generate Altitude in Meter
|
||||
if(BmeAltiM != persisted_bme_altim || bme280_always){
|
||||
trc(F("Sending Altitude Meter to MQTT"));
|
||||
BME280data.set("altim", (float)BmeAltiM);
|
||||
}else{
|
||||
trc(F("Same Altitude Meter don't send it"));
|
||||
}
|
||||
|
||||
// Generate Altitude in Feet
|
||||
if(BmeAltiFt != persisted_bme_altift || bme280_always){
|
||||
BME280data.set("altift", (float)BmeAltiFt);
|
||||
}else{
|
||||
trc(F("Same Altitude Feet don't send it"));
|
||||
}
|
||||
if(BME280data.size()>0) pub(BMETOPIC,BME280data);
|
||||
}
|
||||
persisted_bme_tempc = BmeTempC;
|
||||
persisted_bme_tempf = BmeTempF;
|
||||
persisted_bme_hum = BmeHum;
|
||||
persisted_bme_pa = BmePa;
|
||||
persisted_bme_altim = BmeAltiM;
|
||||
persisted_bme_altift = BmeAltiFt;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
OpenMQTTGateway Addon - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
DHT reading Addon
|
||||
|
||||
Copyright: (c)Florian ROBERT
|
||||
|
||||
Contributors:
|
||||
- prahjister
|
||||
- 1technophile
|
||||
|
||||
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 ZsensorDHT
|
||||
#include <DHT.h>
|
||||
#include <DHT_U.h>
|
||||
|
||||
DHT dht(DHT_RECEIVER_PIN,DHT_SENSOR_TYPE);
|
||||
|
||||
//Time used to wait for an interval before resending temp and hum
|
||||
unsigned long timedht = 0;
|
||||
|
||||
void MeasureTempAndHum(){
|
||||
if (millis() > (timedht + TimeBetweenReadingDHT)) {//retriving value of temperature and humidity of the box from DHT every xUL
|
||||
timedht = millis();
|
||||
static float persistedh;
|
||||
static float persistedt;
|
||||
float h = dht.readHumidity();
|
||||
// Read temperature as Celsius (the default)
|
||||
float t = dht.readTemperature();
|
||||
// Check if any reads failed and exit early (to try again).
|
||||
if (isnan(h) || isnan(t)) {
|
||||
trc(F("Failed to read from DHT sensor!"));
|
||||
}else{
|
||||
trc(F("Creating DHT buffer"));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject& DHTdata = jsonBuffer.createObject();
|
||||
if(h != persistedh || dht_always){
|
||||
DHTdata.set("hum", (float)h);
|
||||
}else{
|
||||
trc(F("Same hum don't send it"));
|
||||
}
|
||||
if(t != persistedt || dht_always){
|
||||
DHTdata.set("temp", (float)t);
|
||||
}else{
|
||||
trc(F("Same temp don't send it"));
|
||||
}
|
||||
if(DHTdata.size()>0) pub(DHTTOPIC,DHTdata);
|
||||
}
|
||||
persistedh = h;
|
||||
persistedt = t;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
OpenMQTTGateway Addon - ESP8266 or Arduino program for home automation
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
GPIO Input derived from HC SR-501 reading Addon and https://www.arduino.cc/en/Tutorial/Debounce
|
||||
|
||||
This reads a high (open) or low (closed) through a circuit (switch, float sensor, etc.) connected to ground.
|
||||
|
||||
Copyright: (c)Florian ROBERT
|
||||
|
||||
Contributors:
|
||||
- 1technophile
|
||||
- QuagmireMan
|
||||
|
||||
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 ZsensorGPIOInput
|
||||
|
||||
unsigned long lastDebounceTime = 0;
|
||||
int InputState = 3; // Set to 3 so that it reads on startup
|
||||
int lastInputState = 3;
|
||||
|
||||
|
||||
void setupGPIOInput() {
|
||||
pinMode(GPIOInput_PIN, INPUT_PULLUP); // declare GPIOInput pin as input_pullup to prevent floating. Pin will be high when not connected to ground
|
||||
}
|
||||
|
||||
void MeasureGPIOInput(){
|
||||
int reading = digitalRead(GPIOInput_PIN);
|
||||
|
||||
// check to see if you just pressed the button
|
||||
// (i.e. the input went from LOW to HIGH), and you've waited long enough
|
||||
// since the last press to ignore any noise:
|
||||
|
||||
// If the switch changed, due to noise or pressing:
|
||||
if (reading != lastInputState) {
|
||||
// reset the debouncing timer
|
||||
lastDebounceTime = millis();
|
||||
|
||||
}
|
||||
|
||||
if ((millis() - lastDebounceTime) > GPIOInputDebounceDelay) {
|
||||
// whatever the reading is at, it's been there for longer than the debounce
|
||||
// delay, so take it as the actual current state:
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
yield();
|
||||
#endif
|
||||
// if the Input state has changed:
|
||||
if (reading != InputState) {
|
||||
InputState = reading;
|
||||
trc(F("Creating GPIOInput buffer"));
|
||||
const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(1);
|
||||
StaticJsonBuffer<JSON_MSG_CALC_BUFFER> jsonBuffer;
|
||||
JsonObject& GPIOdata = jsonBuffer.createObject();
|
||||
if (InputState == HIGH) {
|
||||
trc(F("GPIO HIGH"));
|
||||
pub(subjectGPIOInputtoMQTT,"HIGH");
|
||||
GPIOdata.set("gpio", "HIGH");
|
||||
}
|
||||
if (InputState == LOW) {
|
||||
trc(F("GPIO LOW"));
|
||||
GPIOdata.set("gpio","LOW");
|
||||
pub(subjectGPIOInputtoMQTT,"LOW");
|
||||
}
|
||||
if(GPIOdata.size()>0) pub(subjectGPIOInputtoMQTT,GPIOdata);
|
||||
}
|
||||
}
|
||||
|
||||
// save the reading. Next time through the loop, it'll be the lastInputState:
|
||||
lastInputState = reading;
|
||||
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
OpenMQTTGateway Addon - ESP8266 or Arduino program for home automation
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
GPIO KeyCode derived from GPIO Input
|
||||
|
||||
This reads up to 4 gpio with latch signal (gpio line) and combines the bits as KeyKode - can be connected
|
||||
to EV1527 receiver.
|
||||
|
||||
Copyright: (c)Grzegorz Rajtar
|
||||
|
||||
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 ZsensorGPIOKeyCode
|
||||
|
||||
int InputStateGPIOKeyCode = 0x0f; // Set to 3 so that it reads on startup
|
||||
int lastInputStateGPIOKeyCode = 0x0f;
|
||||
int lastLatchStateGPIOKeyCode = 0;
|
||||
|
||||
void setupGPIOKeyCode() {
|
||||
pinMode(GPIOKeyCode_LATCH_PIN, INPUT_PULLUP); //
|
||||
pinMode(GPIOKeyCode_D0_PIN, INPUT_PULLUP); //
|
||||
pinMode(GPIOKeyCode_D1_PIN, INPUT_PULLUP); //
|
||||
pinMode(GPIOKeyCode_D2_PIN, INPUT_PULLUP); //
|
||||
//pinMode(GPIOKeyCode_D3_PIN, INPUT_PULLUP); //
|
||||
}
|
||||
|
||||
void MeasureGPIOKeyCode(){
|
||||
|
||||
int latch = digitalRead(GPIOKeyCode_LATCH_PIN);
|
||||
|
||||
// check to see if you just pressed the button
|
||||
// (i.e. the input went from LOW to HIGH), and you've waited long enough
|
||||
// since the last press to ignore any noise:
|
||||
|
||||
{
|
||||
// whatever the reading is at, it's been there for longer than the debounce
|
||||
// delay, so take it as the actual current state:
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
yield();
|
||||
#endif
|
||||
// if the Input state has changed:
|
||||
if (latch > 0 && lastLatchStateGPIOKeyCode != latch) {
|
||||
int reading = digitalRead(GPIOKeyCode_D0_PIN)
|
||||
| (digitalRead(GPIOKeyCode_D1_PIN) << 1)
|
||||
| (digitalRead(GPIOKeyCode_D2_PIN) << 2);
|
||||
//| digitalRead(GPIOKeyCode_D3_PIN) << 3;
|
||||
|
||||
char hex[3];
|
||||
|
||||
InputStateGPIOKeyCode = reading;
|
||||
sprintf(hex, "%02x", InputStateGPIOKeyCode);
|
||||
hex[2] = 0;
|
||||
Serial.printf("GPIOKeyCode %s\n", hex);
|
||||
client.publish(subjectGPIOKeyCodetoMQTT,hex);
|
||||
lastLatchStateGPIOKeyCode = latch;
|
||||
}
|
||||
|
||||
if (latch != lastLatchStateGPIOKeyCode) {
|
||||
lastLatchStateGPIOKeyCode = latch;
|
||||
Serial.printf("GPIOKeyCode latch %d\n", latch);
|
||||
if (latch == 0)
|
||||
client.publish(subjectGPIOKeyCodeStatetoMQTT, "done");
|
||||
}
|
||||
|
||||
// save the reading. Next time through the loop, it'll be the lastInputState:
|
||||
lastInputStateGPIOKeyCode = InputStateGPIOKeyCode;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
OpenMQTTGateway Addon - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
HC SR-501 reading Addon
|
||||
|
||||
Copyright: (c)Florian ROBERT
|
||||
|
||||
Contributors:
|
||||
- 1technophile
|
||||
|
||||
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 ZsensorHCSR501
|
||||
|
||||
void setupHCSR501() {
|
||||
pinMode(HCSR501_PIN, INPUT); // declare HC SR-501 pin as input
|
||||
}
|
||||
|
||||
void MeasureHCSR501(){
|
||||
if (millis() > TimeBeforeStartHCSR501) {//let time to init the PIR sensor
|
||||
trc(F("Creating HCSR501 buffer"));
|
||||
const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(1);
|
||||
StaticJsonBuffer<JSON_MSG_CALC_BUFFER> jsonBuffer;
|
||||
JsonObject& HCSR501data = jsonBuffer.createObject();
|
||||
static int pirState = LOW;
|
||||
int PresenceValue = digitalRead(HCSR501_PIN);
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
yield();
|
||||
#endif
|
||||
if (PresenceValue == HIGH) {
|
||||
if (pirState == LOW) {
|
||||
//turned on
|
||||
HCSR501data.set("hcsr501", "true");
|
||||
pirState = HIGH;
|
||||
}
|
||||
} else {
|
||||
if (pirState == HIGH){
|
||||
// turned off
|
||||
HCSR501data.set("hcsr501", "false");
|
||||
trc(F("HC SR501 Motion ended"));
|
||||
pirState = LOW;
|
||||
}
|
||||
}
|
||||
if(HCSR501data.size()>0) pub(subjectHCSR501toMQTT,HCSR501data);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
OpenMQTTGateway Addon - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
INA226 reading Addon
|
||||
|
||||
From the orgiginal work of Matthias Busse http://shelvin.de/ein-batteriemonitor-fuer-strom-und-spannung-mit-dem-ina226-und-dem-arduino-uno/
|
||||
MQTT add - 1technophile
|
||||
|
||||
Contributors:
|
||||
- 1technophile
|
||||
|
||||
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/>.
|
||||
*/
|
||||
// Pinout
|
||||
// INA226 - Uno - Mega - NODEMCU
|
||||
// SCL - A5 - 21 - D1
|
||||
// SDA - A4 - 20 - D2
|
||||
//
|
||||
#include "User_config.h"
|
||||
|
||||
#ifdef ZsensorINA226
|
||||
#include <Wire.h>
|
||||
|
||||
float rShunt=0.1; // Shunt Widerstand festlegen, hier 0.1 Ohm
|
||||
const int INA226_ADDR = 0x40; // A0 und A1 auf GND > Adresse 40 Hex auf Seite 18 im Datenblatt
|
||||
|
||||
//Time used to wait for an interval before resending temp and hum
|
||||
unsigned long timeINA226 = 0;
|
||||
|
||||
void setupINA226() {
|
||||
Wire.begin();
|
||||
// Configuration Register Standard Einstellung 0x4127, hier aber 16 Werte Mitteln > 0x4427
|
||||
writeRegister(0x00, 0x4427); // 1.1ms Volt und Strom A/D-Wandlung, Shunt und VBus continous
|
||||
}
|
||||
|
||||
void MeasureINA226(){
|
||||
if (millis() > (timeINA226 + TimeBetweenReadingINA226)) {//retriving value of temperature and humidity of the box from DHT every xUL
|
||||
timeINA226 = millis();
|
||||
trc(F("Creating INA226 buffer"));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject& INA226data = jsonBuffer.createObject();
|
||||
// Topic on which we will send data
|
||||
trc(F("Retrieving electrical data"));
|
||||
// Bus Spannung, read-only, 16Bit, 0...40.96V max., LSB 1.25mV
|
||||
trc(F(" Volt: "));
|
||||
float volt = readRegister(0x02) * 0.00125;
|
||||
trc(volt);
|
||||
trc(F(" V, Current: "));
|
||||
// Seite 24: Shunt Spannung +- 81,92mV mit 16 Bit, LSB 2,5uV
|
||||
int shuntvolt = readRegister(0x01);
|
||||
if (shuntvolt && 0x8000) {// eine negative Zahl? Dann 2er Komplement bilden
|
||||
shuntvolt = ~shuntvolt; // alle Bits invertieren
|
||||
shuntvolt += 1; // 1 dazuzählen
|
||||
shuntvolt *= -1 ; // negativ machen
|
||||
}
|
||||
float current = shuntvolt * 0.0000025 / rShunt; // * LSB / R
|
||||
trc(current);
|
||||
trc(F(" A, Power: "));
|
||||
float power=abs(volt*current);
|
||||
trc(power);
|
||||
trc(F(" W"));
|
||||
|
||||
char volt_c[7];
|
||||
char current_c[7];
|
||||
char power_c[7];
|
||||
dtostrf(volt,6,3,volt_c);
|
||||
dtostrf(current,6,3,current_c);
|
||||
dtostrf(power,6,3,power_c);
|
||||
INA226data.set("volt", (char *)volt_c);
|
||||
INA226data.set("current", (char *)current_c);
|
||||
INA226data.set("power", (char *)power_c);
|
||||
pub(subjectINA226toMQTT,INA226data);
|
||||
}
|
||||
}
|
||||
|
||||
static void writeRegister(byte reg, word value) {
|
||||
Wire.beginTransmission(INA226_ADDR);
|
||||
Wire.write(reg);
|
||||
Wire.write((value >> 8) & 0xFF);
|
||||
Wire.write(value & 0xFF);
|
||||
Wire.endTransmission();
|
||||
}
|
||||
|
||||
static word readRegister(byte reg) {
|
||||
word res = 0x0000;
|
||||
Wire.beginTransmission(INA226_ADDR);
|
||||
Wire.write(reg);
|
||||
if (Wire.endTransmission() == 0) {
|
||||
if (Wire.requestFrom(INA226_ADDR, 2) >= 2) {
|
||||
res = Wire.read() * 256;
|
||||
res += Wire.read();
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
OpenMQTTGateway Addon - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This is the Light Meter Addon based on modules with a TSL2561:
|
||||
- Measures ambient Light Intensity in Lux (lx), Foot Candela (ftcd) and Watt/m^2 (wattsm2)
|
||||
- Required Hardware Module: TSL2561 (for instance Banggood.com product ID 1129550)
|
||||
- Dependencies: Adafruit_TSL2561 and Adafruit_Sensor
|
||||
|
||||
Connection Scheme:
|
||||
--------------------
|
||||
|
||||
TSL2561------> Arduino Uno ----------> ESP8266
|
||||
==============================================
|
||||
Vcc ---------> 3.3V -----------------> 3.3V
|
||||
GND ---------> GND ------------------> GND
|
||||
SCL ---------> Pin A5 ---------------> D1
|
||||
SDA ---------> Pin A4 ---------------> D2
|
||||
ADD ---------> N/C (Not Connected) --> N/C (Not Connected)
|
||||
|
||||
Copyright: (c) Chris Broekema
|
||||
|
||||
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 ZsensorTSL2561
|
||||
#include "math.h"
|
||||
#include "Wire.h"
|
||||
#include <Adafruit_Sensor.h>
|
||||
#include <Adafruit_TSL2561_U.h>
|
||||
|
||||
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
|
||||
|
||||
void displaySensorDetails(void)
|
||||
{
|
||||
sensor_t sensor;
|
||||
tsl.getSensor(&sensor);
|
||||
trc(F("------------------------------------"));
|
||||
trc("Sensor: " + String(sensor.name));
|
||||
trc("Driver Ver: " + String(sensor.version));
|
||||
trc("Unique ID: " + String(sensor.sensor_id));
|
||||
trc("Max Value: " + String(sensor.max_value) + " lux");
|
||||
trc("Min Value: " + String(sensor.min_value) + " lux");
|
||||
trc("Resolution: " + String(sensor.resolution) + " lux");
|
||||
trc(F("------------------------------------"));
|
||||
trc(F(""));
|
||||
delay(500);
|
||||
}
|
||||
|
||||
void setupZsensorTSL2561()
|
||||
{
|
||||
Wire.begin();
|
||||
Wire.beginTransmission(TSL2561_ADDR_FLOAT);
|
||||
|
||||
if (!tsl.begin())
|
||||
{
|
||||
trc(F("No TSL2561 detected"));
|
||||
}
|
||||
|
||||
// enable auto ranging
|
||||
// tsl.setGain(TSL2561_GAIN_1X); /* No gain ... use in bright light to avoid sensor saturation */
|
||||
// tsl.setGain(TSL2561_GAIN_16X); /* 16x gain ... use in low light to boost sensitivity */
|
||||
tsl.enableAutoRange(true); /* Auto-gain ... switches automatically between 1x and 16x */
|
||||
// since we're slowly sampling, enable high resolution but slow mode TSL2561
|
||||
// tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); /* fast but low resolution */
|
||||
// tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS); /* medium resolution and speed */
|
||||
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);
|
||||
|
||||
trc(F("TSL2561 Initialized. Printing detials now."));
|
||||
displaySensorDetails();
|
||||
}
|
||||
|
||||
void MeasureLightIntensityTSL2561()
|
||||
{
|
||||
if (millis() > (timetsl2561 + TimeBetweenReadingtsl2561)) {
|
||||
static uint32_t persisted_lux;
|
||||
timetsl2561 = millis();
|
||||
|
||||
trc(F("Creating TSL2561 buffer"));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject& TSL2561data = jsonBuffer.createObject();
|
||||
|
||||
sensors_event_t event;
|
||||
tsl.getEvent(&event);
|
||||
if (event.light)
|
||||
// if event.light == 0 the sensor is clipping, do not send
|
||||
{
|
||||
if (persisted_lux != event.light || tsl2561_always ) {
|
||||
persisted_lux = event.light;
|
||||
|
||||
TSL2561data.set("lux", (float)event.light);
|
||||
TSL2561data.set("ftcd", (float)(event.light)/10.764);
|
||||
TSL2561data.set("wattsm2", (float)(event.light)/683.0);
|
||||
|
||||
pub(subjectTSL12561toMQTT,TSL2561data);
|
||||
} else {
|
||||
trc(F("Same lux value, do not send"));
|
||||
}
|
||||
} else {
|
||||
trc(F("Failed to read from TSL2561"));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This gateway enables to set your parameters for the 2G 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_2G_h
|
||||
#define config_2G_h
|
||||
|
||||
extern void setup2G();
|
||||
extern bool _2GtoMQTT();
|
||||
extern void MQTTto2G(char * topicOri, char * datacallback);
|
||||
extern void MQTTto2G(char * topicOri, JsonObject& SMSdata);
|
||||
/*-------------------2G topics & parameters----------------------*/
|
||||
//433Mhz MQTT Subjects and keys
|
||||
#define subjectMQTTto2G Base_Topic Gateway_Name "/commands/MQTTto2G"
|
||||
#define subject2GtoMQTT Base_Topic Gateway_Name "/2GtoMQTT"
|
||||
#define subjectGTW2GtoMQTT Base_Topic Gateway_Name "/2GtoMQTT"
|
||||
#define _2GPhoneKey "PHO_" // phone number define the phone number to send the SMS MQTT->2G
|
||||
|
||||
#define _2G_MODULE_BAUDRATE 9600
|
||||
#define _2G_MIN_SIGNAL 30
|
||||
#define _2G_MAX_SIGNAL 1000
|
||||
|
||||
/*-------------------PIN DEFINITIONS----------------------*/
|
||||
#ifdef ESP8266
|
||||
#define _2G_TX_PIN D6 //D6 to A6 RX,
|
||||
#define _2G_RX_PIN D7 //D7 to A6 TX
|
||||
#define _2G_PWR_PIN D5 // connect a MOSFET to power on and off your A6/7 module
|
||||
#elif defined(ESP32)
|
||||
#define _2G_TX_PIN 16 //D16 to A6 RX,
|
||||
#define _2G_RX_PIN 17 //D17 to A6 TX
|
||||
#define _2G_PWR_PIN 5 // connect a MOSFET to power on and off your A6/7 module
|
||||
#else
|
||||
#define _2G_TX_PIN 6 //D6 to A6 RX,
|
||||
#define _2G_RX_PIN 7 //D7 to A6 TX
|
||||
#define _2G_PWR_PIN 5 // connect a MOSFET to power on and off your A6/7 module
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This files enables to set your parameter for the ADC value
|
||||
|
||||
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_ADC_h
|
||||
#define config_ADC_h
|
||||
|
||||
extern void setupADC();
|
||||
extern void ADCtoMQTT();
|
||||
/*----------------------------USER PARAMETERS-----------------------------*/
|
||||
/*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/
|
||||
#define ADCTOPIC Base_Topic Gateway_Name "/ADCtoMQTT"
|
||||
#define TimeBetweenReadingADC 500 // time between 2 ADC readings, minimum 200 to let the time of the ESP to keep the connection
|
||||
#define ThresholdReadingADC 50 // following the comparison between the previous value and the current one +- the threshold the value will be published or not
|
||||
|
||||
/*-------------------PIN DEFINITIONS----------------------*/
|
||||
#define ADC_PIN A0 //on nodeMCU this is D3 GPIO0
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This files enables to set your parameter for the BH1750 sensor
|
||||
|
||||
Copyright: (c) Hans-Juergen Dinges
|
||||
|
||||
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/>.
|
||||
|
||||
Connection Schemata:
|
||||
--------------------
|
||||
|
||||
BH1750 ------> Arduino Uno ----------> ESP8266
|
||||
==============================================
|
||||
Vcc ---------> 5V -------------------> Vu (5V)
|
||||
GND ---------> GND ------------------> GND
|
||||
SCL ---------> Pin A5 ---------------> D1
|
||||
SDA ---------> Pin A4 ---------------> D2
|
||||
ADD ---------> N/C (Not Connected) --> N/C (Not Connected)
|
||||
|
||||
*/
|
||||
#ifndef config_BH1750_h
|
||||
#define config_BH1750_h
|
||||
|
||||
extern void setupBH1750();
|
||||
extern void BH1750toMQTT();
|
||||
|
||||
#define bh1750_always true // if false when the current value for light Level (Lux) is the same as previous one don't send it by MQTT
|
||||
#define TimeBetweenReadingBH1750 30000
|
||||
|
||||
/*----------------------------USER PARAMETERS-----------------------------*/
|
||||
/*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/
|
||||
#define subjectBH1750toMQTT Base_Topic Gateway_Name "/BH1750toMQTT"
|
||||
//Time used to wait for an interval before resending measured values
|
||||
unsigned long timebh1750 = 0;
|
||||
int BH1750_i2c_addr = 0x23; // Light Sensor I2C Address
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This files enables to set your parameter for the BME280 sensor
|
||||
|
||||
Copyright: (c) Hans-Juergen Dinges
|
||||
|
||||
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/>.
|
||||
|
||||
Connection Schemata:
|
||||
--------------------
|
||||
|
||||
BH1750 ------> Arduino Uno ----------> ESP8266
|
||||
==============================================
|
||||
Vcc ---------> 5V -------------------> Vu (5V)
|
||||
GND ---------> GND ------------------> GND
|
||||
SCL ---------> Pin A5 ---------------> D1
|
||||
SDA ---------> Pin A4 ---------------> D2
|
||||
ADD ---------> N/C (Not Connected) --> N/C (Not Connected)
|
||||
|
||||
*/
|
||||
#ifndef config_BME280_h
|
||||
#define config_BME280_h
|
||||
|
||||
extern void setupBME280();
|
||||
extern void BME280toMQTT();
|
||||
|
||||
#define bme280_always true // if false when the current value of the parameter is the same as previous one don't send it by MQTT
|
||||
#define TimeBetweenReadingbme280 30000
|
||||
|
||||
/*----------------------------USER PARAMETERS-----------------------------*/
|
||||
/*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/
|
||||
#define BMETOPIC Base_Topic Gateway_Name "/CLIMAtoMQTT/bme"
|
||||
|
||||
//Time used to wait for an interval before resending measured values
|
||||
unsigned long timebme280 = 0;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This files enables to set your parameter for the bluetooth low energy gateway (beacons detection)
|
||||
|
||||
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_BT_h
|
||||
#define config_BT_h
|
||||
|
||||
extern void setupBT();
|
||||
extern bool BTtoMQTT();
|
||||
extern void MQTTtoBT(char * topicOri, JsonObject& RFdata);
|
||||
/*----------------------BT topics & parameters-------------------------*/
|
||||
#define subjectBTtoMQTT Base_Topic Gateway_Name "/BTtoMQTT/"
|
||||
#define subjectMQTTtoBTset Base_Topic Gateway_Name "/commands/MQTTtoBT/set"
|
||||
#define TimeBtw_Read 55555 //define default time between 2 scans
|
||||
#define Scan_duration 10 //define the time for a scan
|
||||
#define HM-10
|
||||
//#define HM-11 // uncomment this line if you use HM-11 and comment the line above
|
||||
//#define HM_BLUE_LED_STOP true //uncomment to stop the blue led light of HM1X
|
||||
#define BLEdelimiter "4f4b2b444953413a"
|
||||
#define pubBLEServiceData true // comment if you don't want to publish service data (in case you are having too heavy service data) https://github.com/1technophile/OpenMQTTGateway/issues/318#issuecomment-446064707
|
||||
|
||||
/*-------------------HOME ASSISTANT ROOM PRESENCE ----------------------*/
|
||||
// if not commented Home presence integration with HOME ASSISTANT is activated
|
||||
#define subjectHomePresence Base_Topic "home_presence/" Gateway_Name // will send Home Assistant room presence message to this topic (first part is same for all rooms, second is room name)
|
||||
|
||||
unsigned int BLEinterval ; //time between 2 scans
|
||||
|
||||
struct BLEdevice{
|
||||
char macAdr[13];
|
||||
bool isDisc;
|
||||
bool isWhtL;
|
||||
bool isBlkL;
|
||||
};
|
||||
|
||||
struct decompose{
|
||||
char subject[4];
|
||||
int start;
|
||||
int len;
|
||||
bool reverse;
|
||||
char extract[60];
|
||||
};
|
||||
|
||||
/*-------------------PIN DEFINITIONS----------------------*/
|
||||
#ifdef ESP8266
|
||||
#define BT_RX 13 //D7 ESP8266 RX connect HM-10 or 11 TX
|
||||
#define BT_TX 12 //D6 ESP8266 TX connect HM-10 or 11 RX
|
||||
#elif defined(ESP32)
|
||||
#define BT_RX 18 // not tested
|
||||
#define BT_TX 19 // not tested
|
||||
#else
|
||||
#define BT_RX 5 //arduino RX connect HM-10 or 11 TX
|
||||
#define BT_TX 6 //arduino TX connect HM-10 or 11 RX
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This files enables to set your parameter for the DHT11/22 sensor
|
||||
|
||||
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_DHT_h
|
||||
#define config_DHT_h
|
||||
|
||||
extern void setupDHT();
|
||||
extern void DHTtoMQTT();
|
||||
/*----------------------------USER PARAMETERS-----------------------------*/
|
||||
/*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/
|
||||
#define DHTTOPIC Base_Topic Gateway_Name "/DHTtoMQTT/dht1"
|
||||
#define dht_always true // if false when the current value for temp or hum is the same as previous one don't send it by MQTT
|
||||
#define TimeBetweenReadingDHT 30000 // time between 2 DHT readings
|
||||
/*-------------DHT SENSOR TYPE-------------*/
|
||||
//#define DHT_SENSOR_TYPE DHT11 //uncomment for DHT11 Sensor
|
||||
//#define DHT_SENSOR_TYPE DHT21 //uncomment for DHT21 Sensor
|
||||
#define DHT_SENSOR_TYPE DHT22 //uncomment for DHT22 Sensor (default for backwards compatibility)
|
||||
/*-------------------PIN DEFINITIONS----------------------*/
|
||||
#if defined(ESP8266)
|
||||
#define DHT_RECEIVER_PIN 5 //5 = D1 you can put 14 = D5 if you don't use HCSR501 sensor and the RFM69
|
||||
#elif defined(ESP32)
|
||||
#define DHT_RECEIVER_PIN 16
|
||||
#else
|
||||
#define DHT_RECEIVER_PIN 8
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This files enables to set your parameter for the GPIOInput sensor
|
||||
|
||||
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_GPIOInput_h
|
||||
#define config_GPIOInput_h
|
||||
|
||||
extern void setupGPIOInput();
|
||||
extern void GPIOInputtoMQTT();
|
||||
/*----------------------------USER PARAMETERS-----------------------------*/
|
||||
/*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/
|
||||
#define subjectGPIOInputtoMQTT Base_Topic Gateway_Name "/GPIOInputtoMQTT"
|
||||
#define GPIOInputDebounceDelay 60 //debounce time, increase if there are issues
|
||||
|
||||
/*-------------------PIN DEFINITIONS----------------------*/
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
#define GPIOInput_PIN 14
|
||||
#else
|
||||
#define GPIOInput_PIN 7
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This files enables to set your parameter for the GPIOKeyCode multi input
|
||||
|
||||
Copyright: (c)Grzegorz Rajtar
|
||||
|
||||
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_GPIOKeyCode_h
|
||||
#define config_GPIOKeyCode_h
|
||||
|
||||
extern void setupGPIOKeyCode();
|
||||
extern void GPIOKeyCodetoMQTT();
|
||||
/*----------------------------USER PARAMETERS-----------------------------*/
|
||||
/*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/
|
||||
#define subjectGPIOKeyCodetoMQTT Base_Topic Gateway_Name "/keycode"
|
||||
#define subjectGPIOKeyCodeStatetoMQTT subjectGPIOKeyCodetoMQTT "/status"
|
||||
#define GPIOKeyCodeDebounceDelay 60 //debounce time, increase if there are issues
|
||||
|
||||
/*-------------------PIN DEFINITIONS----------------------*/
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
|
||||
#ifndef GPIOKeyCode_LATCH_PIN
|
||||
#define GPIOKeyCode_LATCH_PIN 12 //D6
|
||||
#endif
|
||||
#ifndef GPIOKeyCode_D0_PIN
|
||||
#define GPIOKeyCode_D0_PIN 14 //D5
|
||||
#endif
|
||||
#ifndef GPIOKeyCode_D1_PIN
|
||||
#define GPIOKeyCode_D1_PIN 5 //D1
|
||||
#endif
|
||||
#ifndef GPIOKeyCode_D2_PIN
|
||||
#define GPIOKeyCode_D2_PIN 13 //D7
|
||||
#endif
|
||||
#ifndef GPIOKeyCode_D3_PIN
|
||||
#define GPIOKeyCode_D3_PIN XX //??
|
||||
#endif
|
||||
#else
|
||||
// must define !!!
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This files enables to set your parameter for the HC SR-501 sensor
|
||||
|
||||
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_HCSR501_h
|
||||
#define config_HCSR501_h
|
||||
|
||||
extern void setupHCSR501();
|
||||
extern void HCSR501toMQTT();
|
||||
/*----------------------------USER PARAMETERS-----------------------------*/
|
||||
/*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/
|
||||
#define subjectHCSR501toMQTT Base_Topic Gateway_Name "/HCSR501toMQTT"
|
||||
|
||||
#define TimeBeforeStartHCSR501 60000 //define the time necessary for HC SR501 init
|
||||
|
||||
/*-------------------PIN DEFINITIONS----------------------*/
|
||||
#if defined(ESP8266)
|
||||
#define HCSR501_PIN D5
|
||||
#elif defined(ESP32)
|
||||
#define HCSR501_PIN 5
|
||||
#else
|
||||
#define HCSR501_PIN 7
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This files enables to set your parameter for the INA226 sensor
|
||||
|
||||
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_INA226_h
|
||||
#define config_INA226_h
|
||||
|
||||
extern void setupINA226();
|
||||
extern void INA226toMQTT();
|
||||
/*----------------------------USER PARAMETERS-----------------------------*/
|
||||
/*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/
|
||||
#define subjectINA226toMQTT Base_Topic Gateway_Name "/INA226toMQTT"
|
||||
#define TimeBetweenReadingINA226 30000 // time between 2 INA226 readings
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This files enables to set your parameter for the infrared 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_IR_h
|
||||
#define config_IR_h
|
||||
|
||||
extern void setupIR();
|
||||
extern void IRtoMQTT();
|
||||
extern void MQTTtoIR(char * topicOri, char * datacallback);
|
||||
extern void MQTTtoIR(char * topicOri, JsonObject& RFdata);
|
||||
/*-------------------IR topics & parameters----------------------*/
|
||||
//IR MQTT Subjects
|
||||
#define subjectGTWIRtoMQTT Base_Topic Gateway_Name "/IRtoMQTT"
|
||||
#define subjectIRtoMQTT Base_Topic Gateway_Name "/IRtoMQTT"
|
||||
#define subjectMQTTtoIR Base_Topic Gateway_Name "/commands/MQTTtoIR"
|
||||
|
||||
// subject monitored to listen traffic processed by other gateways to store data and avoid ntuple
|
||||
#define subjectMultiGTWIR "+/+/IRtoMQTT"
|
||||
#define IRbitsKey "IRBITS_" // bits will be defined if a subject contains IRbitsKey followed by a value of 2 digits
|
||||
#define IRRptKey "RPT_" // repeats will be defined if a subject contains IRRptKey followed by a value of 1 digit
|
||||
#define repeatIRwMQTT false // do we repeat a received signal by using mqtt
|
||||
#define repeatIRwNumber 3 // default repeat of the signal
|
||||
#define RawDirectForward false // direct repeat of IR signal with raw data
|
||||
#define RawFrequency 38 // raw frequency sending
|
||||
//#define DumpMode true // uncomment so as to see big dumps of IR codes
|
||||
|
||||
#define pubIRunknownPrtcl false // key to avoid mqtt publication of unknown IR protocol (set to true if you want to publish unknown protocol)
|
||||
#define PanasonicAddress 0x4004 // Panasonic address (Pre data)
|
||||
|
||||
#if defined(ESP8266) || defined(ESP32) //IR supported protocols on ESP8266, all supported per default
|
||||
#define IR_GC
|
||||
#define IR_Raw
|
||||
#define IR_COOLIX
|
||||
#define IR_Whynter
|
||||
#define IR_LG
|
||||
#define IR_Sony
|
||||
#define IR_DISH
|
||||
#define IR_RC5
|
||||
#define IR_RC6
|
||||
#define IR_Sharp
|
||||
#define IR_SAMSUNG
|
||||
#define IR_PANASONIC
|
||||
#define IR_RCMM
|
||||
#define IR_MITSUBISHI
|
||||
#define IR_GICABLE
|
||||
#define IR_MITSUBISHI2
|
||||
#define IR_LASERTAG
|
||||
#define IR_CARRIER_AC
|
||||
#define IR_MIDEA
|
||||
#define IR_NIKAI
|
||||
#define IR_SHERWOOD
|
||||
#define IR_DENON
|
||||
#define IR_AIWA_RC_T501
|
||||
#define IR_JVC
|
||||
#define IR_SANYO
|
||||
#define IR_DAIKIN
|
||||
#define IR_KELVINATOR
|
||||
#define IR_MITSUBISHI_AC
|
||||
#define IR_SANYO_LC7461
|
||||
#define IR_GREE
|
||||
#define IR_ARGO
|
||||
#define IR_TROTEC
|
||||
#define IR_TOSHIBA_AC
|
||||
#define IR_FUJITSU_AC
|
||||
#define IR_MAGIQUEST
|
||||
#define IR_HAIER_AC
|
||||
#define IR_HITACHI_AC
|
||||
#define IR_HITACHI_AC1
|
||||
#define IR_HITACHI_AC2
|
||||
#define IR_GICABLE
|
||||
#define IR_HAIER_AC_YRW02
|
||||
#define IR_WHIRLPOOL_AC
|
||||
#define IR_SAMSUNG_AC
|
||||
#define IR_LUTRON
|
||||
#define IR_ELECTRA_AC
|
||||
#define IR_PANASONIC_AC
|
||||
#define IR_PIONEER
|
||||
#define IR_LG2
|
||||
#define IR_MWM
|
||||
#define IR_DAIKIN2
|
||||
#define IR_VESTEL_AC
|
||||
#define IR_SAMSUNG36
|
||||
#define IR_TCL112AC
|
||||
#define IR_TECO
|
||||
#elif __AVR_ATmega2560__
|
||||
#define IR_COOLIX
|
||||
#define IR_Whynter
|
||||
#define IR_LG
|
||||
#define IR_Sony
|
||||
#define IR_DISH
|
||||
#define IR_RC5
|
||||
#define IR_Sharp
|
||||
#define IR_SAMSUNG
|
||||
#define IR_Raw
|
||||
#define IR_PANASONIC
|
||||
#else //IR supported protocols on arduino uncomment if you want to send with this protocol, NEC protocol is available per default
|
||||
//#define IR_COOLIX
|
||||
//#define IR_Whynter
|
||||
//#define IR_LG
|
||||
//#define IR_Sony
|
||||
//#define IR_DISH
|
||||
//#define IR_RC5
|
||||
//#define IR_Sharp
|
||||
//#define IR_SAMSUNG
|
||||
//#define IR_Raw
|
||||
//#define IR_PANASONIC
|
||||
#endif
|
||||
|
||||
/*-------------------PIN DEFINITIONS----------------------*/
|
||||
#ifdef ESP8266
|
||||
#define IR_RECEIVER_PIN 2 //D4 /replace by 4 with sonoff rf bridge
|
||||
#define IR_EMITTER_PIN 16 //D0/ replace by 0 (D3) if you use IR LOLIN controller shield /replace by 5 with sonoff rf bridge
|
||||
#elif ESP32
|
||||
#define IR_RECEIVER_PIN 27
|
||||
#define IR_EMITTER_PIN 14
|
||||
#elif __AVR_ATmega2560__
|
||||
#define IR_RECEIVER_PIN 2 // 2 = D2 on arduino mega
|
||||
#define IR_EMITTER_PIN 9
|
||||
#else
|
||||
#define IR_RECEIVER_PIN 0 // 0 = D2 on arduino UNO
|
||||
#define IR_EMITTER_PIN 9
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This files enables to set your parameter for the LORA 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_LORA_h
|
||||
#define config_LORA_h
|
||||
|
||||
extern void setupLORA();
|
||||
extern void LORAtoMQTT();
|
||||
extern void MQTTtoLORA(char * topicOri, char * datacallback);
|
||||
extern void MQTTtoLORA(char * topicOri, JsonObject& RFdata);
|
||||
/*----------------------LORA topics & parameters-------------------------*/
|
||||
#define subjectLORAtoMQTT Base_Topic Gateway_Name "/LORAtoMQTT"
|
||||
#define subjectMQTTtoLORA Base_Topic Gateway_Name "/commands/MQTTtoLORA"
|
||||
#define subjectGTWLORAtoMQTT Base_Topic Gateway_Name "/LORAtoMQTT"
|
||||
|
||||
//Default parameters used when the parameters are not set in the json data
|
||||
#define LORA_BAND 868E6
|
||||
#define LORA_SIGNAL_BANDWIDTH 125E3
|
||||
#define LORA_TX_POWER 17
|
||||
#define LORA_SPREADING_FACTOR 7
|
||||
#define LORA_CODING_RATE 5
|
||||
#define LORA_PREAMBLE_LENGTH 8
|
||||
#define LORA_SYNC_WORD 0x12
|
||||
|
||||
#define repeatLORAwMQTT false // do we repeat a received signal by using mqtt with LORA gateway
|
||||
|
||||
/*-------------------PIN DEFINITIONS----------------------*/
|
||||
//TTGO LORA BOARD ESP32 PIN DEFINITION
|
||||
#define LORA_SCK 5 // GPIO5 -- SX1278's SCK
|
||||
#define LORA_MISO 19 // GPIO19 -- SX1278's MISO
|
||||
#define LORA_MOSI 27 // GPIO27 -- SX1278's MOSI
|
||||
#define LORA_SS 18 // GPIO18 -- SX1278's CS
|
||||
#define LORA_RST 14 // GPIO14 -- SX1278's RESET
|
||||
#define LORA_DI0 26 // GPIO26 -- SX1278's IRQ(Interrupt Request)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This files enables to set your parameter for the ON OFF actuator
|
||||
|
||||
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_ONOFF_h
|
||||
#define config_ONOFF_h
|
||||
|
||||
extern void setupONOFF();
|
||||
extern void MQTTtoONOFF(char * topicOri, char * datacallback);
|
||||
extern void MQTTtoONOFF(char * topicOri, JsonObject& RFdata);
|
||||
/*----------------------------USER PARAMETERS-----------------------------*/
|
||||
/*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/
|
||||
#define subjectMQTTtoONOFF Base_Topic Gateway_Name "/commands/MQTTtoONOFF"
|
||||
#define subjectGTWONOFFtoMQTT Base_Topic Gateway_Name "/ONOFFtoMQTT"
|
||||
|
||||
/*-------------------PIN DEFINITIONS----------------------*/
|
||||
#ifdef ESP8266
|
||||
#define ACTUATOR_ONOFF_PIN 15 //12 for sonoff basic relay
|
||||
#elif ESP32
|
||||
#define ACTUATOR_ONOFF_PIN 15
|
||||
#else
|
||||
#define ACTUATOR_ONOFF_PIN 13
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This files enables to set your parameter for the radiofrequency gateways (ZgatewayRF and ZgatewayRF2) with RCswitch and newremoteswitch library
|
||||
|
||||
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_RF_h
|
||||
#define config_RF_h
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#ifdef ZgatewayRF
|
||||
extern void setupRF();
|
||||
extern void RFtoMQTT();
|
||||
extern void MQTTtoRF(char * topicOri, char * datacallback);
|
||||
extern void MQTTtoRF(char * topicOri, JsonObject& RFdata);
|
||||
#endif
|
||||
#ifdef ZgatewayRF2
|
||||
extern void setupRF2();
|
||||
extern void RF2toMQTT();
|
||||
extern void MQTTtoRF2(char * topicOri, char * datacallback);
|
||||
extern void MQTTtoRF2(char * topicOri, JsonObject& RFdata);
|
||||
#endif
|
||||
#ifdef ZgatewayPilight
|
||||
extern void setupPilight();
|
||||
extern void PilighttoMQTT();
|
||||
extern void MQTTtoPilight(char * topicOri, char * datacallback);
|
||||
extern void MQTTtoPilight(char * topicOri, JsonObject& RFdata);
|
||||
#endif
|
||||
/*-------------------RF topics & parameters----------------------*/
|
||||
//433Mhz MQTT Subjects and keys
|
||||
#define subjectMQTTtoRF Base_Topic Gateway_Name "/commands/MQTTto433"
|
||||
#define subjectRFtoMQTT 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 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
|
||||
|
||||
/*
|
||||
RF supported protocols
|
||||
433_1
|
||||
433_2
|
||||
433_3
|
||||
433_4
|
||||
433_5
|
||||
433_6
|
||||
*/
|
||||
#define RFpulselengthKey "PLSL_" // pulselength will be defined if a subject contains RFprotocolKey followed by a value of 3 digits
|
||||
// subject monitored to listen traffic processed by other gateways to store data and avoid ntuple
|
||||
#define subjectMultiGTWRF "+/+/433toMQTT"
|
||||
//RF number of signal repetition - Can be overridden by specifying "repeat" in a JSON message.
|
||||
#define RF_EMITTER_REPEAT 20
|
||||
|
||||
/*-------------------RF2 topics & parameters----------------------*/
|
||||
//433Mhz newremoteswitch MQTT Subjects and keys
|
||||
#define subjectMQTTtoRF2 Base_Topic Gateway_Name "/commands/MQTTtoRF2"
|
||||
#define subjectRF2toMQTT Base_Topic Gateway_Name "/RF2toMQTT"
|
||||
#define subjectGTWRF2toMQTT Base_Topic Gateway_Name "/433toMQTT"
|
||||
#define RF2codeKey "ADDRESS_" // code will be defined if a subject contains RF2codeKey followed by a value of 7 digits
|
||||
#define RF2periodKey "PERIOD_" // period will be defined if a subject contains RF2periodKey followed by a value of 3 digits
|
||||
#define RF2unitKey "UNIT_" // number of your unit value will be defined if a subject contains RF2unitKey followed by a value of 1-2 digits
|
||||
#define RF2groupKey "GROUP_" // number of your group value will be defined if a subject contains RF2groupKey followed by a value of 1 digit
|
||||
#define RF2dimKey "DIM" // number of your dim value will be defined if a subject contains RF2dimKey and the payload contains the dim value as digits
|
||||
|
||||
/*-------------------ESPPiLight topics & parameters----------------------*/
|
||||
//433Mhz Pilight MQTT Subjects and keys
|
||||
#define subjectMQTTtoPilight Base_Topic Gateway_Name "/commands/MQTTtoPilight"
|
||||
#define subjectPilighttoMQTT Base_Topic Gateway_Name "/PilighttoMQTT"
|
||||
#define subjectGTWPilighttoMQTT Base_Topic Gateway_Name "/PilighttoMQTT"
|
||||
#define PilightRAW "RAW"
|
||||
#define repeatPilightwMQTT false // do we repeat a received signal by using mqtt with Pilight gateway
|
||||
|
||||
/*-------------------PIN DEFINITIONS----------------------*/
|
||||
#ifdef ESP8266
|
||||
#define RF_RECEIVER_PIN 0 // D3 on nodemcu // put 4 with rf bridge direct mod
|
||||
#define RF_EMITTER_PIN 3 // RX on nodemcu if it doesn't work with 3, try with 4 (D2) // put 5 with rf bridge direct mod
|
||||
#elif ESP32
|
||||
#define RF_RECEIVER_PIN 27 // D27 on DOIT ESP32
|
||||
#define RF_EMITTER_PIN 12 // D12 on DOIT ESP32
|
||||
#elif __AVR_ATmega2560__
|
||||
#define RF_RECEIVER_PIN 1 //1 = D3 on mega
|
||||
#define RF_EMITTER_PIN 4
|
||||
#else
|
||||
//IMPORTANT NOTE: On arduino UNO connect IR emitter pin to D9 , comment #define IR_USE_TIMER2 and uncomment #define IR_USE_TIMER1 on library <library>IRremote/boarddefs.h so as to free pin D3 for RF RECEIVER PIN
|
||||
//RF PIN definition
|
||||
#define RF_RECEIVER_PIN 1 //1 = D3 on arduino
|
||||
#define RF_EMITTER_PIN 4 //4 = D4 on arduino
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This files enables to set your parameter for the radiofrequency gateways (ZgatewayRF and ZgatewayRF2) with RCswitch and newremoteswitch library
|
||||
|
||||
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_RFM69_h
|
||||
#define config_RFM69_h
|
||||
|
||||
extern void setupRFM69();
|
||||
extern bool RFM69toMQTT();
|
||||
extern void MQTTtoRFM69(char * topicOri, char * datacallback);
|
||||
extern void MQTTtoRFM69(char * topicOri, JsonObject& RFdata);
|
||||
/*----------------------RFM69 topics & parameters -------------------------*/
|
||||
// Topic where the message from RFM69 will be published by the gateway,
|
||||
// appended with the nodeID of the sender
|
||||
#define subjectRFM69toMQTT Base_Topic Gateway_Name "/RFM69toMQTT"
|
||||
|
||||
// Topic subscribed by the gateway. Messages received will be sent to RFM69
|
||||
#define subjectMQTTtoRFM69 Base_Topic Gateway_Name "/commands/MQTTtoRFM69"
|
||||
#define RFM69receiverKey "RCV_" // receiver id will be defined if a subject contains RFM69receiverKey followed by a value of 3 digits
|
||||
// On reception of an ack from RFM69, the message that has been sent is published here
|
||||
#define subjectGTWRFM69toMQTT Base_Topic Gateway_Name "/RFM69toMQTT/acked"
|
||||
#define defaultRFM69ReceiverId 99
|
||||
|
||||
// Default values
|
||||
const char PROGMEM ENCRYPTKEY[] = "sampleEncryptKey";
|
||||
const char PROGMEM MDNS_NAME[] = "rfm69gw1";
|
||||
const char PROGMEM MQTT_BROKER[] = "raspi2";
|
||||
const char PROGMEM RFM69AP_NAME[] = "RFM69-AP";
|
||||
#define NETWORKID 200 //the same on all nodes that talk to each other
|
||||
#define NODEID 10
|
||||
|
||||
//Match frequency to the hardware version of the radio
|
||||
#define FREQUENCY RF69_433MHZ
|
||||
//#define FREQUENCY RF69_868MHZ
|
||||
//#define FREQUENCY RF69_915MHZ
|
||||
#define IS_RFM69HCW true // set to 'true' if you are using an RFM69HCW module
|
||||
#define POWER_LEVEL 31
|
||||
|
||||
/*-------------------PIN DEFINITIONS----------------------*/
|
||||
#if defined(ESP8266)
|
||||
#define RFM69_CS D1
|
||||
#define RFM69_IRQ D8 // GPIO15/D8
|
||||
#define RFM69_IRQN digitalPinToInterrupt(RFM69_IRQ)
|
||||
#define RFM69_RST D4 // GPIO02/D4
|
||||
#elif defined(ESP32)
|
||||
#define RFM69_CS 1
|
||||
#define RFM69_IRQ 8 // GPIO15/D8
|
||||
#define RFM69_IRQN digitalPinToInterrupt(RFM69_IRQ)
|
||||
#define RFM69_RST 4 // GPIO02/D4
|
||||
#else
|
||||
//RFM69 not tested with arduino
|
||||
#define RFM69_CS 10
|
||||
#define RFM69_IRQ 0
|
||||
#define RFM69_IRQN digitalPinToInterrupt(RFM69_IRQ)
|
||||
#define RFM69_RST 9
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This files enables to set your parameter for the radiofrequency gateway on sonoff rf bridge (ZgatewaySRFB)
|
||||
This implementation is based on Xose Pérez work ESPURNA (https://bitbucket.org/xoseperez/espurna)
|
||||
|
||||
Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
|
||||
|
||||
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_SRFB_h
|
||||
#define config_SRFB_h
|
||||
|
||||
extern void setupSRFB();
|
||||
extern bool SRFBtoMQTT();
|
||||
extern void MQTTtoSRFB(char * topicOri, char * datacallback);
|
||||
extern void MQTTtoSRFB(char * topicOri, JsonObject& RFdata);
|
||||
/*-------------------RF topics & parameters----------------------*/
|
||||
//433Mhz MQTT Subjects and keys
|
||||
#define subjectMQTTtoSRFB Base_Topic Gateway_Name "/commands/MQTTtoSRFB"
|
||||
#define subjectMQTTtoSRFBRaw Base_Topic Gateway_Name "/commands/MQTTtoSRFB/Raw"
|
||||
#define subjectSRFBtoMQTT Base_Topic Gateway_Name "/SRFBtoMQTT"
|
||||
#define subjectGTWSRFBtoMQTT Base_Topic Gateway_Name "/SRFBtoMQTT"
|
||||
#define SRFBRptKey "RPT_"
|
||||
#define SRFBmaxipulselengthKey "Thigh_"
|
||||
#define SRFBminipulselengthKey "Tlow_"
|
||||
#define SRFBsyncKey "Tsyn_"
|
||||
|
||||
#define repeatSRFBwMQTT false // do we repeat a received signal by using mqtt with Sonoff RF Bridge
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// RFBRIDGE
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#define RF_SEND_TIMES 4 // How many times to send the message
|
||||
#define RF_SEND_DELAY 500 // Interval between sendings in ms
|
||||
#define RF_RECEIVE_DELAY 500 // Interval between recieving in ms (avoid debouncing)
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// DEFINITIONS
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#define RF_MESSAGE_SIZE 9
|
||||
#define RF_CODE_START 0xAA
|
||||
#define RF_CODE_ACK 0xA0
|
||||
#define RF_CODE_RFIN 0xA4
|
||||
#define RF_CODE_RFOUT 0xA5
|
||||
#define RF_CODE_STOP 0x55
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This files exposes light level measured by a TSL2651 module
|
||||
Heavily inspired by the driver for the BH1750 light sensor
|
||||
|
||||
Copyright: (c) Chris Broekema
|
||||
|
||||
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/>.
|
||||
|
||||
Connection Schemata:
|
||||
--------------------
|
||||
|
||||
TSL2561 ------> Arduino Uno ----------> ESP8266
|
||||
==============================================
|
||||
Vcc ---------> 3.3V -----------------> Vu (3.3V)
|
||||
GND ---------> GND ------------------> GND
|
||||
SCL ---------> Pin A5 ---------------> D1
|
||||
SDA ---------> Pin A4 ---------------> D2
|
||||
ADD ---------> N/C (Not Connected) --> N/C (Not Connected)
|
||||
|
||||
*/
|
||||
#ifndef config_TSL2561_h
|
||||
#define config_TSL2561_h
|
||||
|
||||
extern void setupTSL2561();
|
||||
extern void TSL2561toMQTT();
|
||||
|
||||
#define tsl2561_always true // if false only send current value if it has changed
|
||||
#define TimeBetweenReadingtsl2561 30000
|
||||
/*----------------------------USER PARAMETERS-----------------------------*/
|
||||
/*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/
|
||||
#define subjectTSL12561toMQTT Base_Topic Gateway_Name "/LIGHTtoMQTT"
|
||||
|
||||
//Time used to wait for an interval before resending measured values
|
||||
unsigned long timetsl2561 = 0;
|
||||
//int TSL2561_i2c_addr = 0x37; // Light Sensor I2C Address (Set in Adafruit library)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
OpenMQTTGateway - ESP8266 or Arduino program for home automation
|
||||
|
||||
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
|
||||
Send and receiving command by MQTT
|
||||
|
||||
This files enables to set your parameter for the Home assistant mqtt Discovery
|
||||
|
||||
Copyright: (c) Rafal Herok
|
||||
|
||||
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_mqttDiscovery_h
|
||||
#define config_mqttDiscovery_h
|
||||
|
||||
extern String getUniqueId(String name, String sufix);
|
||||
extern void pubMqttDiscovery();
|
||||
extern void createDiscovery(char * sensor_type,
|
||||
char * state_topic, char * s_name, char * unique_id,
|
||||
char * availability_topic, char * device_class, char * value_template,
|
||||
char * payload_on, char * payload_off, char * unit_of_meas,
|
||||
int off_delay,
|
||||
char * payload_available, char * payload_not_avalaible, bool child_device , char * command_topic);
|
||||
|
||||
#define discovery_Topic "homeassistant"
|
||||
|
||||
#define DEVICENAME "OpenMQTTGateway " Gateway_Name
|
||||
#define DEVICEMANUFACTURER "OMG_community"
|
||||
|
||||
#endif
|
||||
+1156
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user