Skip to content

Commit 25ea909

Browse files
Support for changing CC1101 Receive and Transmit Frequency (#847)
* Support for changing CC1101 Receive and Transmit Frequency * Docs * Resolve feedback * Update Somfy module to use Global receiveMhz ELECHOUSE_cc1101.SpiStrobe(CC1101_SIDLE); // Idle receiver prior to setting a new frequency
1 parent d84f64b commit 25ea909

File tree

7 files changed

+79
-16
lines changed

7 files changed

+79
-16
lines changed

docs/use/rf.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ You can add a "repeat" key/value to the MQTTto433 JSON message to override the d
6161
Example:
6262
`home/OpenMQTTGateway/commands/MQTTto433 {"value":1315156,"protocol":1,"length":24,"delay":317, "repeat":10}`
6363

64+
### Set Transmit and Receive Frequency of CC1101 Transceiver Module
65+
66+
Default transmit frequency of the CC1101 module is 433.92 Mhz, and this can be can changed by including the frequency in the transmit message. Parameter is `mhz` and valid values are 300-348 Mhz, 387-464Mhz and 779-928Mhz. Actual frequency support will depend on your CC1101 board.
67+
68+
`home/OpenMQTTGateway/commands/MQTTto433 {"value":1150,"protocol":6,"length":12,"delay":450,"repeat":8,"mhz":303.732}`
69+
70+
Default receive frequency of the CC1101 module is 433.92 Mhz, and this can be can changed by sending a message with the frequency. Parameter is `mhz` and valid values are 300-348 Mhz, 387-464Mhz and 779-928Mhz. Actual frequency support will depend on your CC1101 board
71+
72+
`home/OpenMQTTGateway/commands/MQTTto433 {"mhz":315.026}`
73+
74+
Messages received will include the frequency, and when transmitting on a different frequency the module return to the receive frequency afterwards. ie transmit messages on 303.732 Mhz then receive messages on 433.92 Mhz
75+
76+
`{"value":4534142,"protocol":6,"length":26,"delay":356,"mhz":315.026}`
77+
6478
## Pilight gateway
6579

6680
### Receiving data from RF signal

main/User_config.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,8 @@ uint8_t wifiProtocol = 0; // default mode, automatic selection
384384
#define subjectMQTTtoSYSset "/commands/MQTTtoSYS/config"
385385

386386
/*-------------------DEFINE LOG LEVEL----------------------*/
387-
#define LOG_LEVEL LOG_LEVEL_NOTICE
388-
387+
#ifndef LOG_LEVEL
388+
# define LOG_LEVEL LOG_LEVEL_NOTICE
389389
#endif
390+
391+
#endif

main/ZactuatorSomfy.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ void MQTTtoSomfy(char* topicOri, JsonObject& jsonData) {
7171
SomfyRemote somfyRemote(RF_EMITTER_GPIO, somfyRemotes[remoteIndex], &rollingCodeStorage);
7272
somfyRemote.sendCommand(command);
7373
# ifdef ZradioCC1101
74-
ELECHOUSE_cc1101.SetRx(CC1101_FREQUENCY); // set Receive on
74+
ELECHOUSE_cc1101.SpiStrobe(CC1101_SIDLE); // Idle receiver prior to setting a new frequency
75+
ELECHOUSE_cc1101.SetRx(receiveMhz); // set Receive on
7576
# endif
7677
}
7778
}

main/ZgatewayRF.ino

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ void setupRF() {
6161
Log.notice(F("RF_RECEIVER_GPIO: %d " CR), RF_RECEIVER_GPIO);
6262
# ifdef ZradioCC1101 //receiving with CC1101
6363
ELECHOUSE_cc1101.Init();
64-
ELECHOUSE_cc1101.setMHZ(CC1101_FREQUENCY);
65-
ELECHOUSE_cc1101.SetRx(CC1101_FREQUENCY);
64+
ELECHOUSE_cc1101.SetRx(receiveMhz);
6665
# endif
6766
# ifdef RF_DISABLE_TRANSMIT
6867
mySwitch.disableTransmit();
@@ -76,7 +75,11 @@ void setupRF() {
7675

7776
void RFtoMQTT() {
7877
if (mySwitch.available()) {
78+
# ifdef ZradioCC1101 //receiving with CC1101
79+
const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(5);
80+
# else
7981
const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(4);
82+
# endif
8083
StaticJsonBuffer<JSON_MSG_CALC_BUFFER> jsonBuffer;
8184
JsonObject& RFdata = jsonBuffer.createObject();
8285
Log.trace(F("Rcv. RF" CR));
@@ -88,6 +91,9 @@ void RFtoMQTT() {
8891
RFdata.set("protocol", (int)mySwitch.getReceivedProtocol());
8992
RFdata.set("length", (int)mySwitch.getReceivedBitlength());
9093
RFdata.set("delay", (int)mySwitch.getReceivedDelay());
94+
# ifdef ZradioCC1101 // set Receive off and Transmitt on
95+
RFdata.set("mhz", receiveMhz);
96+
# endif
9197
mySwitch.resetAvailable();
9298

9399
if (!isAduplicateSignal(MQTTvalue) && MQTTvalue != 0) { // conditions to avoid duplications of RF -->MQTT
@@ -109,7 +115,7 @@ void RFtoMQTT() {
109115
# ifdef simpleReceiving
110116
void MQTTtoRF(char* topicOri, char* datacallback) {
111117
# ifdef ZradioCC1101 // set Receive off and Transmitt on
112-
ELECHOUSE_cc1101.SetTx(CC1101_FREQUENCY);
118+
ELECHOUSE_cc1101.SetTx(receiveMhz);
113119
mySwitch.disableReceive();
114120
mySwitch.enableTransmit(RF_EMITTER_GPIO);
115121
# endif
@@ -161,7 +167,8 @@ void MQTTtoRF(char* topicOri, char* datacallback) {
161167
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
162168
}
163169
# ifdef ZradioCC1101 // set Receive on and Transmitt off
164-
ELECHOUSE_cc1101.SetRx(CC1101_FREQUENCY);
170+
ELECHOUSE_cc1101.SpiStrobe(CC1101_SIDLE); // Idle receiver prior to setting a new frequency
171+
ELECHOUSE_cc1101.SetRx(receiveMhz);
165172
mySwitch.disableTransmit();
166173
mySwitch.enableReceive(RF_RECEIVER_GPIO);
167174
# endif
@@ -170,11 +177,6 @@ void MQTTtoRF(char* topicOri, char* datacallback) {
170177

171178
# ifdef jsonReceiving
172179
void MQTTtoRF(char* topicOri, JsonObject& RFdata) { // json object decoding
173-
# ifdef ZradioCC1101 // set Receive off and Transmitt on
174-
ELECHOUSE_cc1101.SetTx(CC1101_FREQUENCY);
175-
mySwitch.disableReceive();
176-
mySwitch.enableTransmit(RF_EMITTER_GPIO);
177-
# endif
178180
if (cmpToMainTopic(topicOri, subjectMQTTtoRF)) {
179181
Log.trace(F("MQTTtoRF json" CR));
180182
SIGNAL_SIZE_UL_ULL data = RFdata["value"];
@@ -186,21 +188,59 @@ void MQTTtoRF(char* topicOri, JsonObject& RFdata) { // json object decoding
186188
Log.notice(F("RF Protocol:%d" CR), valuePRT);
187189
Log.notice(F("RF Pulse Lgth: %d" CR), valuePLSL);
188190
Log.notice(F("Bits nb: %d" CR), valueBITS);
191+
# ifdef ZradioCC1101 // set Receive off and Transmitt on
192+
float trMhz = RFdata["mhz"] | CC1101_FREQUENCY;
193+
if (validFrequency((int)trMhz)) {
194+
ELECHOUSE_cc1101.SetTx(trMhz);
195+
Log.notice(F("Transmit mhz: %F" CR), trMhz);
196+
mySwitch.disableReceive();
197+
mySwitch.enableTransmit(RF_EMITTER_GPIO);
198+
}
199+
# endif
189200
mySwitch.setRepeatTransmit(valueRPT);
190201
mySwitch.setProtocol(valuePRT, valuePLSL);
191202
mySwitch.send(data, valueBITS);
192203
Log.notice(F("MQTTtoRF OK" CR));
193204
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
194205
mySwitch.setRepeatTransmit(RF_EMITTER_REPEAT); // Restore the default value
195206
} else {
207+
# ifdef ZradioCC1101 // set Receive on and Transmitt off
208+
float tempMhz = RFdata["mhz"];
209+
if (tempMhz != 0 && validFrequency((int)tempMhz)) {
210+
receiveMhz = tempMhz;
211+
Log.notice(F("Receive mhz: %F" CR), receiveMhz);
212+
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
213+
} else {
214+
pub(subjectGTWRFtoMQTT, "{\"Status\": \"Error\"}"); // Fail feedback
215+
Log.error(F("MQTTtoRF Fail json" CR));
216+
}
217+
# else
218+
# ifndef ARDUINO_AVR_UNO // Space issues with the UNO
219+
pub(subjectGTWRFtoMQTT, "{\"Status\": \"error\"}"); // Fail feedback
220+
# endif
196221
Log.error(F("MQTTtoRF Fail json" CR));
222+
# endif
197223
}
198224
}
199225
# ifdef ZradioCC1101 // set Receive on and Transmitt off
200-
ELECHOUSE_cc1101.SetRx(CC1101_FREQUENCY);
226+
ELECHOUSE_cc1101.SpiStrobe(CC1101_SIDLE); // Idle receiver prior to setting a new frequency
227+
ELECHOUSE_cc1101.SetRx(receiveMhz);
201228
mySwitch.disableTransmit();
202229
mySwitch.enableReceive(RF_RECEIVER_GPIO);
203230
# endif
204231
}
205232
# endif
206233
#endif
234+
235+
#ifdef ZradioCC1101
236+
bool validFrequency(int mhz) {
237+
// CC1101 valid frequencies 300-348 MHZ, 387-464MHZ and 779-928MHZ.
238+
if (mhz >= 300 && mhz <= 348)
239+
return true;
240+
if (mhz >= 387 && mhz <= 464)
241+
return true;
242+
if (mhz >= 779 && mhz <= 928)
243+
return true;
244+
return false;
245+
}
246+
#endif

main/config_RF.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,13 @@ extern void MQTTtoPilight(char* topicOri, JsonObject& RFdata);
8282

8383
/*-------------------CC1101 frequency----------------------*/
8484
//Match frequency to the hardware version of the radio if ZradioCC1101 is used.
85-
#define CC1101_FREQUENCY 433.92
85+
#ifndef CC1101_FREQUENCY
86+
# define CC1101_FREQUENCY 433.92
87+
#endif
88+
// Allow ZGatewayRF Module to change receive frequency of CC1101 Transceiver module
89+
#ifdef ZradioCC1101
90+
float receiveMhz = CC1101_FREQUENCY;
91+
#endif
8692

8793
/*-------------------PIN DEFINITIONS----------------------*/
8894
#ifndef RF_RECEIVER_GPIO

main/main.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ void connectMQTT() {
493493
#ifdef ZgatewayIR
494494
client.subscribe(subjectMultiGTWIR); // subject on which other OMG will publish, this OMG will store these msg and by the way don't republish them if they have been already published
495495
#endif
496-
Log.trace(F("Subscription OK to the subjects" CR));
496+
Log.trace(F("Subscription OK to the subjects %s" CR), topic2);
497497
}
498498
} else {
499499
failure_number_mqtt++; // we count the failure

platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ dallastemperature = DallasTemperature
111111
m5stickc = M5StickC@0.2.0
112112
m5stickcp = https://github.com/m5stack/M5StickC-Plus.git
113113
m5stack = M5Stack@0.3.0
114-
smartrc-cc1101-driver-lib = SmartRC-CC1101-Driver-Lib@2.3.5
114+
smartrc-cc1101-driver-lib = SmartRC-CC1101-Driver-Lib@2.5.2
115115
stl = https://github.com/mike-matera/ArduinoSTL.git#7411816
116116
shtc3 = https://github.com/sparkfun/SparkFun_SHTC3_Arduino_Library
117117
somfy_remote=Somfy_Remote_Lib@0.2.0

0 commit comments

Comments
 (0)