Browse Source

Update to latest RFM69 version

rfm69
Xose Pérez 6 years ago
parent
commit
0506f97f7b
9 changed files with 3477 additions and 3603 deletions
  1. +1
    -1
      code/espurna/config/prototypes.h
  2. BIN
      code/espurna/data/index.rfm69.html.gz
  3. +0
    -249
      code/espurna/libs/RFM69Manager.h
  4. +65
    -0
      code/espurna/libs/RFM69Wrap.h
  5. +70
    -12
      code/espurna/rfm69.ino
  6. +3338
    -3338
      code/espurna/static/index.rfm69.html.gz.h
  7. +1
    -1
      code/html/custom.js
  8. +1
    -1
      code/html/index.html
  9. +1
    -1
      code/platformio.ini

+ 1
- 1
code/espurna/config/prototypes.h View File

@ -127,7 +127,7 @@ typedef struct {
unsigned char packetID; unsigned char packetID;
unsigned char senderID; unsigned char senderID;
unsigned char targetID; unsigned char targetID;
char * name;
char * key;
char * value; char * value;
int16_t rssi; int16_t rssi;
} packet_t; } packet_t;


BIN
code/espurna/data/index.rfm69.html.gz View File


+ 0
- 249
code/espurna/libs/RFM69Manager.h View File

@ -1,249 +0,0 @@
/*
Radio
RFM69 Radio Manager for ESP8266
Based on sample code by Felix Rusu - http://LowPowerLab.com/contact
Copyright (C) 2016 by Xose Pérez <xose dot perez at gmail dot com>
This program 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.
This program 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/>.
*/
#pragma once
#include <RFM69.h>
#include <RFM69_ATC.h>
#include <SPI.h>
// -----------------------------------------------------------------------------
// Configuration
// -----------------------------------------------------------------------------
#define PING_EVERY 3
#define RETRIES 2
#define REQUESTACK 1
#define RADIO_DEBUG 0
#define SEND_PACKET_ID 1
#define PACKET_SEPARATOR ':'
/*
typedef struct {
unsigned long messageID;
unsigned char packetID;
unsigned char senderID;
unsigned char targetID;
char * name;
char * value;
int16_t rssi;
} packet_t;
*/
typedef void (*TMessageCallback)(packet_t *);
class RFM69Manager: public RFM69_ATC {
public:
RFM69Manager(uint8_t slaveSelectPin=RF69_SPI_CS, uint8_t interruptPin=RF69_IRQ_PIN, bool isRFM69HW=false, uint8_t interruptNum=RF69_IRQ_NUM):
RFM69_ATC(slaveSelectPin, interruptPin, isRFM69HW, interruptNum) {};
bool initialize(uint8_t frequency, uint8_t nodeID, uint8_t networkID, const char* key, uint8_t gatewayID = 0, int16_t targetRSSI = -70) {
bool ret = RFM69_ATC::initialize(frequency, nodeID, networkID);
encrypt(key);
_gatewayID = gatewayID;
if (_gatewayID > 0) enableAutoPower(targetRSSI);
if (_isRFM69HW) setHighPower();
#if RADIO_DEBUG
char buff[50];
sprintf(buff, "[RFM69] Working at %d Mhz", frequency == RF69_433MHZ ? 433 : frequency == RF69_868MHZ ? 868 : 915);
Serial.println(buff);
Serial.print(F("[RFM69] Node: "));
Serial.println(nodeID);
Serial.print(F("[RFM69] Network: "));
Serial.println(networkID);
if (gatewayID == 0) {
Serial.println("[RFM69] This node is a gateway");
} else {
Serial.print(F("[RFM69] Gateway: "));
Serial.println(gatewayID);
}
Serial.println(F("[RFM69] Auto Transmission Control (ATC) enabled"));
#endif
return ret;
}
void promiscuous(bool promiscuous) {
RFM69_ATC::promiscuous(promiscuous);
#if RADIO_DEBUG
if (_promiscuousMode) {
Serial.println(F("[RFM69] Promiscuous mode ON"));
} else {
Serial.println(F("[RFM69] Promiscuous mode OFF"));
}
#endif
}
void onMessage(TMessageCallback fn) {
_callback = fn;
}
void separator(char sep) {
_separator = sep;
}
bool loop() {
boolean ret = false;
if (receiveDone()) {
uint8_t senderID = SENDERID;
uint8_t targetID = _promiscuousMode ? TARGETID : _address;
int16_t rssi = RSSI;
uint8_t length = DATALEN;
char buffer[length + 1];
strncpy(buffer, (const char *) DATA, length);
buffer[length] = 0;
// Do not send ACKs in promiscuous mode,
// we want to listen without being heard
if (!_promiscuousMode) {
if (ACKRequested()) sendACK();
}
uint8_t parts = 1;
for (uint8_t i=0; i<length; i++) {
if (buffer[i] == _separator) ++parts;
}
if (parts > 1) {
char sep[2] = {_separator, 0};
uint8_t packetID = 0;
char * name = strtok(buffer, sep);
char * value = strtok(NULL, sep);
if (parts > 2) {
char * packet = strtok(NULL, sep);
packetID = atoi(packet);
}
_message.messageID = ++_receiveCount;
_message.packetID = packetID;
_message.senderID = senderID;
_message.targetID = targetID;
_message.name = name;
_message.value = value;
_message.rssi = rssi;
ret = true;
if (_callback != NULL) {
_callback(&_message);
}
}
}
return ret;
}
bool send(uint8_t destinationID, char * name, char * value, uint8_t retries = RETRIES, bool requestACK = REQUESTACK) {
char message[RF69_MAX_DATA_LEN];
#if SEND_PACKET_ID
if (++_sendCount == 0) _sendCount = 1;
snprintf(message, RF69_MAX_DATA_LEN-1, "%s%c%s%c%d", name, _separator, value, _separator, _sendCount);
#else
snprintf(message, RF69_MAX_DATA_LEN-1, "%s%c%s", name, _separator, value);
#endif
#if RADIO_DEBUG
Serial.print(F("[RFM69] Sending: "));
Serial.print(message);
#endif
bool ret = true;
if (retries > 0) {
ret = sendWithRetry(destinationID, message, strlen(message), retries);
} else {
RFM69_ATC::send(destinationID, message, strlen(message), requestACK);
}
#if RADIO_DEBUG
if (ret) {
Serial.println(" OK");
} else {
Serial.println(" KO");
}
#endif
return ret;
}
bool send(char * name, char * value, uint8_t retries = RETRIES) {
return send(_gatewayID, name, value, retries, false);
}
bool send(char * name, char * value, bool requestACK = REQUESTACK) {
return send(_gatewayID, name, value, 0, requestACK);
}
packet_t * getMessage() {
return &_message;
}
protected:
packet_t _message;
TMessageCallback _callback = NULL;
uint8_t _gatewayID = 0;
unsigned long _receiveCount = 0;
#if SEND_PACKET_ID
unsigned char _sendCount = 0;
#endif
unsigned int _ackCount = 0;
char _separator = PACKET_SEPARATOR;
// ---------------------------------------------------------------------
// overriding select the RFM69 transceiver (save SPI settings, set CS low)
void select() {
noInterrupts();
#if defined (SPCR) && defined (SPSR)
// save current SPI settings
_SPCR = SPCR;
_SPSR = SPSR;
#endif
// set RFM69 SPI settings
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
#if defined(ARDUINO_ARCH_ESP8266)
SPI.setClockDivider(SPI_CLOCK_DIV2); // speeding it up for the ESP8266
#else
SPI.setClockDivider(SPI_CLOCK_DIV4);
#endif
digitalWrite(_slaveSelectPin, LOW);
}
};

+ 65
- 0
code/espurna/libs/RFM69Wrap.h View File

@ -0,0 +1,65 @@
/*
RFM69Wrap
RFM69 by Felix Ruso (http://LowPowerLab.com/contact) wrapper for ESP8266
Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com>
This program 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.
This program 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/>.
*/
#pragma once
#include <RFM69.h>
#include <RFM69_ATC.h>
#include <SPI.h>
class RFM69Wrap: public RFM69_ATC {
public:
RFM69Wrap(uint8_t slaveSelectPin=RF69_SPI_CS, uint8_t interruptPin=RF69_IRQ_PIN, bool isRFM69HW=false, uint8_t interruptNum=0):
RFM69_ATC(slaveSelectPin, interruptPin, isRFM69HW, interruptNum) {};
protected:
// overriding SPI_CLOCK for ESP8266
void select() {
noInterrupts();
#if defined (SPCR) && defined (SPSR)
// save current SPI settings
_SPCR = SPCR;
_SPSR = SPSR;
#endif
// set RFM69 SPI settings
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
#if defined(__arm__)
SPI.setClockDivider(SPI_CLOCK_DIV16);
#elif defined(ARDUINO_ARCH_ESP8266)
SPI.setClockDivider(SPI_CLOCK_DIV2); // speeding it up for the ESP8266
#else
SPI.setClockDivider(SPI_CLOCK_DIV4);
#endif
digitalWrite(_slaveSelectPin, LOW);
}
};

+ 70
- 12
code/espurna/rfm69.ino View File

@ -8,13 +8,15 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
#if RFM69_SUPPORT #if RFM69_SUPPORT
#include "libs/RFM69Manager.h"
#include "libs/RFM69Wrap.h"
#define RFM69_PACKET_SEPARATOR ':'
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Locals // Locals
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
RFM69Manager * _rfm69_radio;
RFM69Wrap * _rfm69_radio;
struct _node_t { struct _node_t {
unsigned long count = 0; unsigned long count = 0;
@ -98,14 +100,14 @@ void _rfm69Configure() {
void _rfm69Debug(const char * level, packet_t * data) { void _rfm69Debug(const char * level, packet_t * data) {
DEBUG_MSG_P( DEBUG_MSG_P(
PSTR("[RFM69] %s: messageID:%05d senderID:%03d targetID:%03d packetID:%03d rssi:%-04d name:%s value:%s\n"),
PSTR("[RFM69] %s: messageID:%05d senderID:%03d targetID:%03d packetID:%03d rssi:%-04d key:%s value:%s\n"),
level, level,
data->messageID, data->messageID,
data->senderID, data->senderID,
data->targetID, data->targetID,
data->packetID, data->packetID,
data->rssi, data->rssi,
data->name,
data->key,
data->value data->value
); );
@ -149,9 +151,9 @@ void _rfm69Process(packet_t * data) {
snprintf_P( snprintf_P(
buffer, buffer,
sizeof(buffer) - 1, sizeof(buffer) - 1,
PSTR("{\"nodeCount\": %d, \"packetCount\": %lu, \"packet\": {\"senderID\": %u, \"targetID\": %u, \"packetID\": %u, \"name\": \"%s\", \"value\": \"%s\", \"rssi\": %d, \"duplicates\": %d, \"missing\": %d}}"),
PSTR("{\"nodeCount\": %d, \"packetCount\": %lu, \"packet\": {\"senderID\": %u, \"targetID\": %u, \"packetID\": %u, \"key\": \"%s\", \"value\": \"%s\", \"rssi\": %d, \"duplicates\": %d, \"missing\": %d}}"),
_rfm69_node_count, _rfm69_packet_count, _rfm69_node_count, _rfm69_packet_count,
data->senderID, data->targetID, data->packetID, data->name, data->value, data->rssi,
data->senderID, data->targetID, data->packetID, data->key, data->value, data->rssi,
_rfm69_node_info[data->senderID].duplicates , _rfm69_node_info[data->senderID].missing); _rfm69_node_info[data->senderID].duplicates , _rfm69_node_info[data->senderID].missing);
wsSend(buffer); wsSend(buffer);
} }
@ -163,7 +165,7 @@ void _rfm69Process(packet_t * data) {
for (unsigned int i=0; i<RFM69_MAX_TOPICS; i++) { for (unsigned int i=0; i<RFM69_MAX_TOPICS; i++) {
unsigned char node = getSetting("node", i, 0).toInt(); unsigned char node = getSetting("node", i, 0).toInt();
if (0 == node) break; if (0 == node) break;
if ((node == data->senderID) && (getSetting("key", i, "").equals(data->name))) {
if ((node == data->senderID) && (getSetting("key", i, "").equals(data->key))) {
mqttSendRaw((char *) getSetting("topic", i, "").c_str(), (char *) String(data->value).c_str()); mqttSendRaw((char *) getSetting("topic", i, "").c_str(), (char *) String(data->value).c_str());
return; return;
} }
@ -173,14 +175,63 @@ void _rfm69Process(packet_t * data) {
String topic = getSetting("rfm69Topic", RFM69_DEFAULT_TOPIC); String topic = getSetting("rfm69Topic", RFM69_DEFAULT_TOPIC);
if (topic.length() > 0) { if (topic.length() > 0) {
topic.replace("{node}", String(data->senderID)); topic.replace("{node}", String(data->senderID));
topic.replace("{key}", String(data->name));
topic.replace("{key}", String(data->key));
mqttSendRaw((char *) topic.c_str(), (char *) String(data->value).c_str()); mqttSendRaw((char *) topic.c_str(), (char *) String(data->value).c_str());
} }
} }
void _rfm69Loop() { void _rfm69Loop() {
_rfm69_radio->loop();
if (_rfm69_radio->receiveDone()) {
uint8_t senderID = _rfm69_radio->SENDERID;
uint8_t targetID = _rfm69_radio->TARGETID;
int16_t rssi = _rfm69_radio->RSSI;
uint8_t length = _rfm69_radio->DATALEN;
char buffer[length + 1];
strncpy(buffer, (const char *) _rfm69_radio->DATA, length);
buffer[length] = 0;
// Do not send ACKs in promiscuous mode,
// we want to listen without being heard
if (!RFM69_PROMISCUOUS) {
if (_rfm69_radio->ACKRequested()) _rfm69_radio->sendACK();
}
uint8_t parts = 1;
for (uint8_t i=0; i<length; i++) {
if (buffer[i] == RFM69_PACKET_SEPARATOR) ++parts;
}
if (parts > 1) {
char sep[2] = {RFM69_PACKET_SEPARATOR, 0};
uint8_t packetID = 0;
char * key = strtok(buffer, sep);
char * value = strtok(NULL, sep);
if (parts > 2) {
char * packet = strtok(NULL, sep);
packetID = atoi(packet);
}
packet_t message;
message.messageID = ++_rfm69_packet_count;
message.packetID = packetID;
message.senderID = senderID;
message.targetID = targetID;
message.key = key;
message.value = value;
message.rssi = rssi;
_rfm69Process(&message);
}
}
} }
void _rfm69Clear() { void _rfm69Clear() {
@ -202,10 +253,17 @@ void rfm69Setup() {
_rfm69Configure(); _rfm69Configure();
_rfm69_radio = new RFM69Manager(RFM69_CS_PIN, RFM69_IRQ_PIN, RFM69_IS_RFM69HW, digitalPinToInterrupt(RFM69_IRQ_PIN));
_rfm69_radio->initialize(RFM69_FREQUENCY, RFM69_NODE_ID, RFM69_NETWORK_ID, RFM69_ENCRYPTKEY);
_rfm69_radio = new RFM69Wrap(RFM69_CS_PIN, RFM69_IRQ_PIN, RFM69_IS_RFM69HW, digitalPinToInterrupt(RFM69_IRQ_PIN));
_rfm69_radio->initialize(RFM69_FREQUENCY, RFM69_NODE_ID, RFM69_NETWORK_ID);
_rfm69_radio->encrypt(RFM69_ENCRYPTKEY);
_rfm69_radio->promiscuous(RFM69_PROMISCUOUS); _rfm69_radio->promiscuous(RFM69_PROMISCUOUS);
_rfm69_radio->onMessage(_rfm69Process);
if (RFM69_IS_RFM69HW) _rfm69_radio->setHighPower();
DEBUG_MSG_P(PSTR("[RFM69] Worning at %u MHz\n"), RFM69_FREQUENCY == RF69_433MHZ ? 433 : RFM69_FREQUENCY == RF69_868MHZ ? 868 : 915);
DEBUG_MSG_P(PSTR("[RFM69] Node %u\n"), RFM69_NODE_ID);
DEBUG_MSG_P(PSTR("[RFM69] Network %u\n"), RFM69_NETWORK_ID);
DEBUG_MSG_P(PSTR("[RFM69] Promiscuous mode %s\n"), RFM69_PROMISCUOUS ? "ON" : "OFF");
DEBUG_MSG_P(PSTR("[RFM69] Auto Transmission Control (ATC) enabled\n"));
#if WEB_SUPPORT #if WEB_SUPPORT
wsOnSendRegister(_rfm69WebSocketOnSend); wsOnSendRegister(_rfm69WebSocketOnSend);


+ 3338
- 3338
code/espurna/static/index.rfm69.html.gz.h
File diff suppressed because it is too large
View File


+ 1
- 1
code/html/custom.js View File

@ -1165,7 +1165,7 @@ function processData(data) {
packet.senderID, packet.senderID,
packet.packetID, packet.packetID,
packet.targetID, packet.targetID,
packet.name,
packet.key,
packet.value, packet.value,
packet.rssi, packet.rssi,
packet.duplicates, packet.duplicates,


+ 1
- 1
code/html/index.html View File

@ -732,7 +732,7 @@
<th>SenderID</th> <th>SenderID</th>
<th>PacketID</th> <th>PacketID</th>
<th>TargetID</th> <th>TargetID</th>
<th>Name</th>
<th>Key</th>
<th>Value</th> <th>Value</th>
<th>RSSI</th> <th>RSSI</th>
<th>Duplicates</th> <th>Duplicates</th>


+ 1
- 1
code/platformio.ini View File

@ -88,7 +88,7 @@ lib_deps =
PZEM004T PZEM004T
PubSubClient PubSubClient
rc-switch rc-switch
https://github.com/LowPowerLab/RFM69#7f140f0
https://github.com/LowPowerLab/RFM69#1.1.3
https://github.com/xoseperez/Time https://github.com/xoseperez/Time
lib_ignore = lib_ignore =


Loading…
Cancel
Save