Merge pull request #39 from 1technophile/adding-bluetooth
Adding bluetooth BLE gateway
This commit is contained in:
commit
5a9f5e115f
6 files changed
+173
-33
No files matched your search
+20
-5
@@ -9,7 +9,8 @@
|
||||
- publish MQTT data to a different topic related to received 433Mhz signal
|
||||
- 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
|
||||
|
||||
- publish MQTT data to a different topic related to BLE devices rssi signal
|
||||
|
||||
Copyright: (c)1technophile
|
||||
|
||||
Contributors:
|
||||
@@ -38,7 +39,13 @@ CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFT
|
||||
#include <PubSubClient.h>
|
||||
|
||||
// array to store previous received RFs, IRs codes and their timestamps
|
||||
unsigned long ReceivedSignal[8][2] ={{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}};
|
||||
#ifdef ESP8266
|
||||
#define array_size 12
|
||||
unsigned long ReceivedSignal[array_size][2] ={{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}};
|
||||
#else
|
||||
#define array_size 4
|
||||
unsigned long ReceivedSignal[array_size][2] ={{0,0},{0,0},{0,0},{0,0}};
|
||||
#endif
|
||||
/*------------------------------------------------------------------------*/
|
||||
|
||||
//adding this to bypass the problem of the arduino builder issue 50
|
||||
@@ -126,6 +133,9 @@ void setup()
|
||||
#ifdef ZgatewayRF
|
||||
setupRF();
|
||||
#endif
|
||||
#ifdef ZgatewayBT
|
||||
setupBT();
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
@@ -191,6 +201,11 @@ void loop()
|
||||
if(resultIR)
|
||||
trc(F("IR successfully sent by MQTT"));
|
||||
#endif
|
||||
#ifdef ZgatewayBT
|
||||
boolean resultBT = BTtoMQTT();
|
||||
if(resultBT)
|
||||
trc(F("BT successfully sent by MQTT"));
|
||||
#endif
|
||||
|
||||
}
|
||||
delay(100);
|
||||
@@ -208,7 +223,7 @@ void storeValue(long MQTTvalue){
|
||||
trc(F("store this code :"));
|
||||
trc(String(ReceivedSignal[o][0])+"/"+String(ReceivedSignal[o][1]));
|
||||
trc(F("Col: value/timestamp"));
|
||||
for (int i = 0; i < 8; i++)
|
||||
for (int i = 0; i < array_size; i++)
|
||||
{
|
||||
trc(String(i) + ":" + String(ReceivedSignal[i][0])+"/"+String(ReceivedSignal[i][1]));
|
||||
}
|
||||
@@ -217,7 +232,7 @@ void storeValue(long MQTTvalue){
|
||||
int getMin(){
|
||||
int minimum = ReceivedSignal[0][1];
|
||||
int minindex=0;
|
||||
for (int i = 0; i < 8; i++)
|
||||
for (int i = 0; i < array_size; i++)
|
||||
{
|
||||
if (ReceivedSignal[i][1] < minimum) {
|
||||
minimum = ReceivedSignal[i][1];
|
||||
@@ -230,7 +245,7 @@ int getMin(){
|
||||
boolean isAduplicate(long value){
|
||||
trc(F("isAduplicate"));
|
||||
// check if the value has been already sent during the last time_avoid_duplicate
|
||||
for (int i=0; i<8;i++){
|
||||
for (int i = 0; i < array_size;i++){
|
||||
if (ReceivedSignal[i][0] == value){
|
||||
unsigned long now = millis();
|
||||
if (now - ReceivedSignal[i][1] < time_avoid_duplicate){ // change
|
||||
|
||||
+52
-26
@@ -9,7 +9,8 @@
|
||||
- publish MQTT data to a different topic related to received 433Mhz signal
|
||||
- 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
|
||||
|
||||
- publish MQTT data to a different topic related to BLE devices rssi signal
|
||||
|
||||
Copyright: (c)1technophile
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
||||
@@ -25,7 +26,7 @@ CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFT
|
||||
*/
|
||||
|
||||
/*----------------------------USER PARAMETERS-----------------------------*/
|
||||
/*-------------DEFINE YOUR MQTT & NETWORK PARAMETERS BELOW----------------*/
|
||||
/*-------------DEFINE YOUR NETWORK PARAMETERS BELOW----------------*/
|
||||
//MQTT Parameters definition
|
||||
#define mqtt_server "192.168.1.17"
|
||||
//#define mqtt_user "your_username" // not compulsory only if your broker needs authentication
|
||||
@@ -38,6 +39,7 @@ CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFT
|
||||
#define will_Message "Offline"
|
||||
#define Gateway_AnnouncementMsg "Online"
|
||||
|
||||
/*-------------DEFINE YOUR NETWORK PARAMETERS BELOW----------------*/
|
||||
// Update these with values suitable for your network.
|
||||
#ifdef ESP8266 // for nodemcu, weemos and esp8266
|
||||
#define wifi_ssid "wifi ssid"
|
||||
@@ -52,23 +54,23 @@ const byte gateway[] = { 192, 168, 1, 1 }; //ip adress
|
||||
const byte Dns[] = { 192, 168, 1, 1 }; //ip adress
|
||||
const byte subnet[] = { 255, 255, 255, 0 }; //ip adress
|
||||
|
||||
/*-------------DEFINE THE MODULES YOU WANT BELOW----------------*/
|
||||
//Addons and module management, comment the line if you don't use
|
||||
//#define ZsensorDHT
|
||||
#define ZgatewayRF
|
||||
#define ZgatewayIR
|
||||
#define ZgatewayBT
|
||||
/*----------------------------OTHER PARAMETERS-----------------------------*/
|
||||
/*-------------------CHANGING THEM IS NOT COMPULSORY-----------------------*/
|
||||
//variables to avoid duplicates for RF
|
||||
#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
|
||||
|
||||
//MQTT definitions
|
||||
/*--------------MQTT general topics-----------------*/
|
||||
// global MQTT subject listened by the gateway to execute commands (send RF, IR or others)
|
||||
#define subjectMQTTtoX "home/commands/#"
|
||||
// subject monitored to listen traffic processed by other gateways to store data and avoid ntuple
|
||||
#define subjectMultiGTWRF "+/433toMQTT"
|
||||
#define subjectMultiGTWIR "+/IRtoMQTT"
|
||||
#define subjectMultiGTWKey "toMQTT"
|
||||
|
||||
/*-------------------RF topics----------------------*/
|
||||
//433Mhz MQTT Subjects and keys
|
||||
#define subjectMQTTtoRF "home/commands/MQTTto433"
|
||||
#define subjectRFtoMQTT "home/433toMQTT"
|
||||
@@ -77,15 +79,6 @@ const byte subnet[] = { 255, 255, 255, 0 }; //ip adress
|
||||
#define subjectRFtoMQTTbits "home/433toMQTT/bits"
|
||||
#define subjectRFtoMQTTlength "home/433toMQTT/length"
|
||||
#define RFprotocolKey "433_" // protocol will be defined if a subject contains RFprotocolKey followed by a value of 1 digit
|
||||
#define RFpulselengthKey "PLSL_" // pulselength will be defined if a subject contains RFprotocolKey followed by a value of 3 digits
|
||||
//IR MQTT Subjects
|
||||
#define subjectGTWIRtoMQTT "home/IRtoMQTT"
|
||||
#define subjectIRtoMQTT "home/IRtoMQTT"
|
||||
#define subjectMQTTtoIR "home/commands/MQTTtoIR"
|
||||
#define subjectIRtoMQTTprotocol "home/IRtoMQTT/protocol"
|
||||
#define subjectIRtoMQTTbits "home/IRtoMQTT/bits"
|
||||
|
||||
#define pubIRunknownPrtcl false // key to avoid mqtt publication of unknown IR protocol (set to true if you want to publish unknown protocol)
|
||||
/*
|
||||
RF supported protocols
|
||||
433_1
|
||||
@@ -94,18 +87,47 @@ RF supported protocols
|
||||
433_4
|
||||
433_5
|
||||
433_6
|
||||
IR supported protocols
|
||||
IR_NEC
|
||||
IR_LG
|
||||
IR_Sony
|
||||
IR_DISH
|
||||
IR_Sharp
|
||||
IR_SAMSUNG
|
||||
IR_COOLIX
|
||||
IR_Whynter
|
||||
*/
|
||||
#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"
|
||||
|
||||
//IR PIN definition
|
||||
/*-------------------IR topics----------------------*/
|
||||
//IR MQTT Subjects
|
||||
#define subjectGTWIRtoMQTT "home/IRtoMQTT"
|
||||
#define subjectIRtoMQTT "home/IRtoMQTT"
|
||||
#define subjectMQTTtoIR "home/commands/MQTTtoIR"
|
||||
#define subjectIRtoMQTTprotocol "home/IRtoMQTT/protocol"
|
||||
#define subjectIRtoMQTTbits "home/IRtoMQTT/bits"
|
||||
// subject monitored to listen traffic processed by other gateways to store data and avoid ntuple
|
||||
#define subjectMultiGTWIR "+/IRtoMQTT"
|
||||
|
||||
#define pubIRunknownPrtcl false // key to avoid mqtt publication of unknown IR protocol (set to true if you want to publish unknown protocol)
|
||||
|
||||
#ifdef ESP8266 //IR supported protocols on ESP8266, all supported 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
|
||||
#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
|
||||
#endif
|
||||
|
||||
/*----------------------BT topics-------------------------*/
|
||||
#define subjectBTtoMQTT "home/BTtoMQTT/"
|
||||
|
||||
/*-------------------PIN DEFINITIONS----------------------*/
|
||||
#define IR_RECEIVER_PIN 2 // put 2 = D4 on nodemcu, 2 = D2 on arduino
|
||||
#define RF_EMITTER_PIN 4 //put 4 = D2 on nodemcu, 4 = D4 on arduino
|
||||
|
||||
@@ -113,14 +135,18 @@ IR_Whynter
|
||||
#define IR_EMITTER_PIN 14 // 14 = D5 on nodemcu #define only usefull for ESP8266
|
||||
//RF PIN definition
|
||||
#define RF_RECEIVER_PIN 5 // 5 = D1 on nodemcu
|
||||
#define BT_RX D7 //ESP8266 RX connect HM-10 TX
|
||||
#define BT_TX D6 //ESP8266 TX connect HM-10 RX
|
||||
#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/IRremoteInt.h so as to free pin D3 for RF RECEIVER PIN
|
||||
//RF PIN definition
|
||||
#define RF_RECEIVER_PIN 1 // 1 = D3 on arduino
|
||||
#define BT_RX 10
|
||||
#define BT_TX 11
|
||||
#endif
|
||||
//RF number of signal repetition
|
||||
#define RF_EMITTER_REPEAT 20
|
||||
|
||||
//Do we want to see trace for debugging purposes
|
||||
/*-------------------ACTIVATE TRACES----------------------*/
|
||||
#define TRACE 1 // 0= trace off 1 = trace on
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
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
|
||||
|
||||
Copyright: (c)1technophile
|
||||
|
||||
IMPORTANT NOTE: On arduino connect IR emitter pin to D9 , comment #define IR_USE_TIMER2 and uncomment #define IR_USE_TIMER1 on library <library>IRremote/IRremoteInt.h so as to free pin D3 for RF RECEIVER PIN
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
||||
and associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||||
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
||||
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#ifdef ZgatewayBT
|
||||
#include <SoftwareSerial.h>
|
||||
|
||||
#define STRING_MSG "OK+DISC:"
|
||||
#define QUESTION_MSG "AT+DISI?"
|
||||
#define RESPONSE_MSG "OK+DISIS"
|
||||
#define TimeBtw_Read 10000
|
||||
|
||||
SoftwareSerial softserial(BT_RX, BT_TX);
|
||||
|
||||
unsigned long time1 = 0;
|
||||
|
||||
void setupBT() {
|
||||
softserial.begin(9600);
|
||||
softserial.print(F("AT+ROLE1"));
|
||||
delay(100);
|
||||
softserial.print(F("AT+IMME1"));
|
||||
delay(100);
|
||||
softserial.print(F("AT+RESET"));
|
||||
delay(100);
|
||||
}
|
||||
|
||||
boolean BTtoMQTT() {
|
||||
while (softserial.available() > 0) {
|
||||
#ifdef ESP8266
|
||||
yield();
|
||||
#endif
|
||||
|
||||
String discResult = softserial.readString();
|
||||
if (discResult.indexOf(STRING_MSG)>=0){
|
||||
trc(F("Sending BT data to MQTT"));
|
||||
discResult.replace(RESPONSE_MSG,"");
|
||||
int device_number = discResult.length()/78;
|
||||
for (int i=0;i<device_number;i++){
|
||||
String onedevice = discResult.substring(0,78);
|
||||
onedevice.replace(STRING_MSG,"");
|
||||
String company = onedevice.substring(0,8);
|
||||
String uuid = onedevice.substring(9,41);
|
||||
String others = onedevice.substring(42,52);
|
||||
String mac = onedevice.substring(53,65);
|
||||
String rssi = onedevice.substring(66,70);
|
||||
String mactopic = subjectBTtoMQTT + mac;
|
||||
trc(mactopic + " " + rssi);
|
||||
client.publish((char *)mactopic.c_str(),(char *)rssi.c_str());
|
||||
discResult = discResult.substring(78);
|
||||
#ifdef ESP8266
|
||||
yield();
|
||||
#endif
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (millis() > (time1 + TimeBtw_Read)) {//retriving value of adresses and rssi
|
||||
time1 = millis();
|
||||
softserial.print(F(QUESTION_MSG));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
+17
-1
@@ -4,7 +4,7 @@
|
||||
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:
|
||||
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
|
||||
|
||||
@@ -81,43 +81,59 @@ void MQTTtoIR(char * topicOri, char * datacallback) {
|
||||
//send received MQTT value by IR signal (example of signal sent data = 1086296175)
|
||||
boolean signalSent = false;
|
||||
#ifdef ESP8266 // send coolix not available for arduino IRRemote library
|
||||
#ifdef IR_COOLIX
|
||||
if (strstr(topicOri, "IR_COOLIX") != NULL){
|
||||
irsend.sendCOOLIX(data, 24);
|
||||
signalSent = true;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
if (strstr(topicOri, "IR_NEC") != NULL || strstr(topicOri, subjectMQTTtoIR) != NULL ){
|
||||
irsend.sendNEC(data, 32);
|
||||
signalSent = true;
|
||||
}
|
||||
#ifdef IR_Whynter
|
||||
if (strstr(topicOri, "IR_Whynter") != NULL){
|
||||
irsend.sendWhynter(data, 32);
|
||||
signalSent = true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_LG
|
||||
if (strstr(topicOri, "IR_LG") != NULL){
|
||||
irsend.sendLG(data, 28);
|
||||
signalSent = true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_Sony
|
||||
if (strstr(topicOri, "IR_Sony") != NULL){
|
||||
irsend.sendSony(data, 12);
|
||||
signalSent = true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_DISH
|
||||
if (strstr(topicOri, "IR_DISH") != NULL){
|
||||
irsend.sendDISH(data, 16);
|
||||
signalSent = true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_RC5
|
||||
if (strstr(topicOri, "IR_RC5") != NULL){
|
||||
irsend.sendRC5(data, 12);
|
||||
signalSent = true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_Sharp
|
||||
if (strstr(topicOri, "IR_Sharp") != NULL){
|
||||
irsend.sendSharpRaw(data, 15);
|
||||
signalSent = true;
|
||||
}
|
||||
#endif
|
||||
#ifdef IR_SAMSUNG
|
||||
if (strstr(topicOri, "IR_SAMSUNG") != NULL){
|
||||
irsend.sendSAMSUNG(data, 32);
|
||||
signalSent = true;
|
||||
}
|
||||
#endif
|
||||
if (signalSent){
|
||||
boolean result = client.publish(subjectGTWIRtoMQTT, datacallback);
|
||||
if (result){
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@
|
||||
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:
|
||||
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
|
||||
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 212 KiB |
Reference in new issue
Block a user