Add support for the new RegionCodes of meshtastic

This commit is contained in:
tuxphone 2020-09-27 20:47:17 +02:00 committed by GitHub
parent 71f52ba2ca
commit 52c96ae949
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 206 additions and 206 deletions

View File

@ -1,6 +1,6 @@
#include "mesh.pb.h" #include "mesh.pb.h"
// CONFIGURATION HERE: // CONFIGURATION HERE:
uint8_t REGION = RegionCode_EU865 ; // define your region here. For US, RegionCode_US, CN RegionCode_Cn etc. #define REGION RegionCode_EU865 // define your region here. For US, RegionCode_US, CN RegionCode_Cn etc.
char MESHTASTIC_NAME[12] = {"Default"}; // Channel Name, but without "-Xy" suffix , e.g. use "Test" instead of "Test-A" char MESHTASTIC_NAME[12] = {"Default"}; // Channel Name, but without "-Xy" suffix , e.g. use "Test" instead of "Test-A"
#define MESHTASTIC_SPEED 3 // 0 = short range, 1 = medium range, 2 = long range, 3 = very long range #define MESHTASTIC_SPEED 3 // 0 = short range, 1 = medium range, 2 = long range, 3 = very long range
@ -59,6 +59,7 @@ struct RegionInfo {
}; };
const RegionInfo regions[] = { const RegionInfo regions[] = {
RDEF(Unset, 903.08f, 2.16f, 13, 0), // Assume US freqs if unset
RDEF(US, 903.08f, 2.16f, 13, 0), RDEF(US, 903.08f, 2.16f, 13, 0),
RDEF(EU433, 433.175f, 0.2f, 8, 0), RDEF(EU433, 433.175f, 0.2f, 8, 0),
RDEF(EU865, 865.2f, 0.3f, 10, 0), RDEF(EU865, 865.2f, 0.3f, 10, 0),
@ -67,8 +68,7 @@ const RegionInfo regions[] = {
RDEF(ANZ, 916.0f, 0.5f, 20, 0), // AU/NZ channel settings 915-928MHz RDEF(ANZ, 916.0f, 0.5f, 20, 0), // AU/NZ channel settings 915-928MHz
RDEF(KR, 921.9f, 0.2f, 8, 0), // KR channel settings (KR920-923) Start from TTN download channel RDEF(KR, 921.9f, 0.2f, 8, 0), // KR channel settings (KR920-923) Start from TTN download channel
// freq. (921.9f is for download, others are for uplink) // freq. (921.9f is for download, others are for uplink)
RDEF(TW, 923.0f, 0.2f, 10, 0), // TW channel settings (AS2 bandplan 923-925MHz) RDEF(TW, 923.0f, 0.2f, 10, 0) // TW channel settings (AS2 bandplan 923-925MHz)
RDEF(Unset, 903.08f, 2.16f, 13, 0) // Assume US freqs if unset, Must be last
}; };
static const RegionInfo *myRegion; //static const RegionInfo *myRegion;

View File

@ -1,202 +1,202 @@
#include <Arduino.h> #include <Arduino.h>
#include "CubeCell_NeoPixel.h" #include "CubeCell_NeoPixel.h"
#include "config.h" #include "config.h"
// CONFIGURATION: change values in config.h ! // CONFIGURATION: change values in config.h !
#define VERBOSE // define to SILENT to turn off serial messages #define VERBOSE // define to SILENT to turn off serial messages
#define NOBLINK // define to NOBLINK to turn off LED signaling #define BLINK // define to NOBLINK to turn off LED signaling
MeshPacket thePacket; MeshPacket thePacket;
ChannelSettings ChanSet; ChannelSettings ChanSet;
static RadioEvents_t RadioEvents; static RadioEvents_t RadioEvents;
uint32_t lastreceivedID = 0x00000000; uint32_t lastreceivedID = 0x00000000;
#ifndef NOBLINK #ifndef NOBLINK
CubeCell_NeoPixel LED(1, RGB, NEO_GRB + NEO_KHZ800); CubeCell_NeoPixel LED(1, RGB, NEO_GRB + NEO_KHZ800);
#endif #endif
void setup() { void setup() {
#ifndef NOBLINK #ifndef NOBLINK
pinMode(Vext,OUTPUT); pinMode(Vext,OUTPUT);
digitalWrite(Vext,LOW); //SET POWER digitalWrite(Vext,LOW); //SET POWER
LED.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) LED.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
LED.clear( ); LED.clear( );
LED.show(); LED.show();
#endif #endif
#ifndef SILENT #ifndef SILENT
Serial.begin(115200); Serial.begin(115200);
MSG("\nSetting up Radio:\n"); MSG("\nSetting up Radio:\n");
#endif #endif
RadioEvents.TxDone = TxDone; RadioEvents.TxDone = TxDone;
RadioEvents.TxTimeout = TxTimeout; RadioEvents.TxTimeout = TxTimeout;
RadioEvents.RxDone = RxDone; RadioEvents.RxDone = RxDone;
Radio.Init( &RadioEvents ); Radio.Init( &RadioEvents );
Radio.Sleep(); Radio.Sleep();
memcpy(ChanSet.name, MESHTASTIC_NAME, 12); memcpy(ChanSet.name, MESHTASTIC_NAME, 12);
REGION -= 1; //myRegion = &regions[REGION];
ChanSet.channel_num = hash( MESHTASTIC_NAME ) % regions[REGION].numChannels; // see config.h ChanSet.channel_num = hash( MESHTASTIC_NAME ) % regions[REGION].numChannels; // see config.h
ChanSet.tx_power = (regions[REGION].powerLimit == 0) ? TX_MAX_POWER : regions[REGION].powerLimit ; ChanSet.tx_power = (regions[REGION].powerLimit == 0) ? TX_MAX_POWER : MAX(regions[REGION].powerLimit, TX_MAX_POWER) ;
if (ChanSet.tx_power > TX_MAX_POWER) ChanSet.tx_power = TX_MAX_POWER; ChanSet.psk = MESHTASTIC_PSK;
ChanSet.psk = MESHTASTIC_PSK; /* FYI:
/* FYI: "bandwidth":
"bandwidth": [0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: 62.5kHz, 4: 41.67kHz, 5: 31.25kHz, 6: 20.83kHz, 7: 15.63kHz, 8: 10.42kHz, 9: 7.81kHz]
[0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: 62.5kHz, 4: 41.67kHz, 5: 31.25kHz, 6: 20.83kHz, 7: 15.63kHz, 8: 10.42kHz, 9: 7.81kHz] "speed":
"speed": 0: ChannelSettings_ModemConfig_Bw125Cr45Sf128 aka short range
0: ChannelSettings_ModemConfig_Bw125Cr45Sf128 aka short range 1: ChannelSettings_ModemConfig_Bw500Cr45Sf128 aka medium range
1: ChannelSettings_ModemConfig_Bw500Cr45Sf128 aka medium range 2: ChannelSettings_ModemConfig_Bw31_25Cr48Sf512 aka long range
2: ChannelSettings_ModemConfig_Bw31_25Cr48Sf512 aka long range 3: ChannelSettings_ModemConfig_Bw125Cr48Sf4096 aka very long range
3: ChannelSettings_ModemConfig_Bw125Cr48Sf4096 aka very long range "coding rate":
"coding rate": [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
[1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8] */
*/ switch ( MESHTASTIC_SPEED ){
switch ( MESHTASTIC_SPEED ){ case 0: { // short range
case 0: { // short range ChanSet.bandwidth = 0; // 125 kHz
ChanSet.bandwidth = 0; // 125 kHz ChanSet.coding_rate = 1; // = 4/5
ChanSet.coding_rate = 1; // = 4/5 ChanSet.spread_factor = 7;
ChanSet.spread_factor = 7; break;
break; }
} case 1: { // medium range
case 1: { // medium range ChanSet.bandwidth = 2; // 500 kHz
ChanSet.bandwidth = 2; // 500 kHz ChanSet.coding_rate = 1; // = 4/5
ChanSet.coding_rate = 1; // = 4/5 ChanSet.spread_factor = 7;
ChanSet.spread_factor = 7; break;
break; }
} case 2: { // long range
case 2: { // long range ChanSet.bandwidth = 5; // 31.25 kHz
ChanSet.bandwidth = 5; // 31.25 kHz ChanSet.coding_rate = 4; // = 4/8
ChanSet.coding_rate = 4; // = 4/8 ChanSet.spread_factor = 9;
ChanSet.spread_factor = 9; break;
break; }
} case 3: { // very long range
case 3: { // very long range ChanSet.bandwidth = 0; // 125 kHz
ChanSet.bandwidth = 0; // 125 kHz ChanSet.coding_rate = 4; // = 4/8
ChanSet.coding_rate = 4; // = 4/8 ChanSet.spread_factor = 12;
ChanSet.spread_factor = 12; break;
break; }
} default:{ // default setting is medium range
default:{ // default setting is medium range ChanSet.bandwidth = 2; // 500 kHz
ChanSet.bandwidth = 2; // 500 kHz ChanSet.coding_rate = 1; // = 4/5
ChanSet.coding_rate = 1; // = 4/5 ChanSet.spread_factor = 7;
ChanSet.spread_factor = 7; }
} }
} ConfigureRadio( ChanSet );
ConfigureRadio( ChanSet ); #ifndef SILENT
#ifndef SILENT MSG("..done! Switch to Receive Mode.\n");
MSG("..done! Switch to Receive Mode.\n"); #endif
#endif Radio.Rx(0); // initial mode = receive
Radio.Rx(0); // initial mode = receive }
}
void loop( )
void loop( ) {
{ lowPowerHandler( );
lowPowerHandler( ); Radio.IrqProcess( );
Radio.IrqProcess( ); }
}
void TxDone( void )
void TxDone( void ) {
{ Radio.Sleep( );
Radio.Sleep( ); #ifndef NOBLINK
#ifndef NOBLINK LED.clear( );
LED.clear( ); LED.show( );
LED.show( ); #endif
#endif #ifndef SILENT
#ifndef SILENT MSG(".done! Switch to Receive Mode.\n");
MSG(".done! Switch to Receive Mode.\n"); #endif
#endif Radio.Rx( 0 ); // switch to receive mode
Radio.Rx( 0 ); // switch to receive mode }
}
void TxTimeout( void )
void TxTimeout( void ) {
{ Radio.Sleep( );
Radio.Sleep( ); #ifndef NOBLINK
#ifndef NOBLINK LED.clear( );
LED.clear( ); LED.show( );
LED.show( ); #endif
#endif #ifndef SILENT
#ifndef SILENT MSG(".failed (TX Timeout)! Switch to Receive Mode.\n");
MSG(".failed (TX Timeout)! Switch to Receive Mode.\n"); #endif
#endif Radio.Rx( 0 ); // switch to receive mode
Radio.Rx( 0 ); // switch to receive mode }
}
void RxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
void RxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ) {
{ Radio.Sleep( );
Radio.Sleep( ); if ( size > MAX_PAYLOAD_LENGTH ) size = MAX_PAYLOAD_LENGTH;
if ( size > MAX_PAYLOAD_LENGTH ) size = MAX_PAYLOAD_LENGTH; if ( !(size > sizeof(PacketHeader)) ) {
if ( !(size > sizeof(PacketHeader)) ) { #ifndef SILENT
#ifndef SILENT MSG("\nReceived packet! (Size %i bytes, RSSI %i, SNR %i)\n", size, rssi, snr);
MSG("\nReceived packet! (Size %i bytes, RSSI %i, SNR %i)\n", size, rssi, snr); MSG("Packet to small (No MeshPacket), discard. Switch to Receive Mode.\n");
MSG("Packet to small (No MeshPacket), discard. Switch to Receive Mode.\n"); #endif
#endif Radio.Rx( 0 );
Radio.Rx( 0 ); return;
return; }
} PacketHeader * h = (PacketHeader *)payload;
PacketHeader * h = (PacketHeader *)payload; MeshPacket * p = &thePacket;
MeshPacket * p = &thePacket; p->to = h->to;
p->to = h->to; p->from = h->from;
p->from = h->from; p->id = h->id;
p->id = h->id; p->hop_limit = h->flags && 0b00000111;
p->hop_limit = h->flags && 0b00000111; p->want_ack = h->flags && 0b00001000;
p->want_ack = h->flags && 0b00001000; p->which_payload = MeshPacket_encrypted_tag;
p->which_payload = MeshPacket_encrypted_tag; p->encrypted.size= size - sizeof(PacketHeader);
p->encrypted.size= size - sizeof(PacketHeader); p->decoded.data.payload.size = p->encrypted.size;
p->decoded.data.payload.size = p->encrypted.size; memcpy(p->encrypted.bytes, payload + sizeof(PacketHeader), p->encrypted.size);
memcpy(p->encrypted.bytes, payload + sizeof(PacketHeader), p->encrypted.size); #ifndef SILENT
#ifndef SILENT MSG("\nReceived packet! (Size %i bytes, RSSI %i, SNR %i)\n", size, rssi, snr);
MSG("\nReceived packet! (Size %i bytes, RSSI %i, SNR %i)\n", size, rssi, snr); MSG("TO: "); HEXMSG(thePacket.to);
MSG("TO: "); HEXMSG(thePacket.to); MSG(" FROM: "); HEXMSG(thePacket.from);
MSG(" FROM: "); HEXMSG(thePacket.from); MSG(" Packet ID: "); HEXMSG(thePacket.id);
MSG(" Packet ID: "); HEXMSG(thePacket.id); MSG(" Flags: WANT_ACK="); MSG((thePacket.want_ack) ? "YES " : "NO ");
MSG(" Flags: WANT_ACK="); MSG((thePacket.want_ack) ? "YES " : "NO "); MSG(" HOP_LIMIT=%i\n", thePacket.hop_limit);
MSG(" HOP_LIMIT=%i\n", thePacket.hop_limit); #endif
#endif if ( !(lastreceivedID == thePacket.id) ){
if ( !(lastreceivedID == thePacket.id) ){ // will repeat package
// will repeat package #ifndef SILENT
#ifndef SILENT MSG("Sending packet.. (Size: %i)..", size);
MSG("Sending packet.. (Size: %i)..", size); #endif
#endif lastreceivedID = thePacket.id;
lastreceivedID = thePacket.id; #ifndef NOBLINK
#ifndef NOBLINK LED.setPixelColor( 0, RGB_RED ); // send mode
LED.setPixelColor( 0, RGB_RED ); // send mode LED.show();
LED.show(); #endif
#endif Radio.Send( payload, size );
Radio.Send( payload, size ); }
} else{
else{ #ifndef SILENT
#ifndef SILENT MSG("PacketID = last PacketID, will not repeat again.\n");
MSG("PacketID = last PacketID, will not repeat again.\n"); #endif
#endif Radio.Rx( 0 ); // switch to Receive Mode
Radio.Rx( 0 ); // switch to Receive Mode }
} }
}
// hash a string into an integer - djb2 by Dan Bernstein. -
// hash a string into an integer - djb2 by Dan Bernstein. - // http://www.cse.yorku.ca/~oz/hash.html
// http://www.cse.yorku.ca/~oz/hash.html unsigned long hash(char *str)
unsigned long hash(char *str) {
{ unsigned long hash = 5381;
unsigned long hash = 5381; int c;
int c; while ((c = *str++) != 0)
while ((c = *str++) != 0) hash = ((hash << 5) + hash) + (unsigned char) c; // hash * 33 + c //
hash = ((hash << 5) + hash) + (unsigned char) c; // hash * 33 + c // return hash;
return hash; }
}
void ConfigureRadio( ChannelSettings ChanSet )
void ConfigureRadio( ChannelSettings ChanSet ) {
{ //uint32_t freq = (myRegion->freq + myRegion->spacing * ChanSet.channel_num)*1E6;
uint32_t freq = (regions[REGION].freq + regions[REGION].spacing * ChanSet.channel_num)*1E6; uint32_t freq = (regions[REGION].freq + regions[REGION].spacing * ChanSet.channel_num)*1E6;
#ifndef SILENT
MSG("\nRegion is: %s\n",regions[REGION].name); #ifndef SILENT
MSG("Setting frequency to %i Hz (meshtastic channel %i) .. \n",freq,ChanSet.channel_num ); MSG("\nRegion is: %s", regions[REGION].name);
MSG("Channel name is: %s .. \n", ChanSet.name ); MSG(" TX power: %i\n", ChanSet.tx_power);
MSG("Setting bandwidth to index %i ..\n",ChanSet.bandwidth); MSG("Setting frequency to %i Hz (meshtastic channel %i) .. \n",freq,ChanSet.channel_num );
MSG("Setting CodeRate to index %i .. \n", ChanSet.coding_rate); MSG("Channel name is: %s .. \n", ChanSet.name );
MSG("Setting SpreadingFactor to %i ..\n",ChanSet.spread_factor); MSG("Setting bandwidth to index %i ..\n",ChanSet.bandwidth);
#endif MSG("Setting CodeRate to index %i .. \n", ChanSet.coding_rate);
Radio.SetChannel( freq ); MSG("Setting SpreadingFactor to %i ..\n",ChanSet.spread_factor);
Radio.SetTxConfig( MODEM_LORA, ChanSet.tx_power ,0 , ChanSet.bandwidth, ChanSet.spread_factor, ChanSet.coding_rate, #endif
LORA_PREAMBLE_LENGTH, false, true, false, 0, false, 20000 ); Radio.SetChannel( freq );
Radio.SetTxConfig( MODEM_LORA, ChanSet.tx_power ,0 , ChanSet.bandwidth, ChanSet.spread_factor, ChanSet.coding_rate,
Radio.SetRxConfig( MODEM_LORA, ChanSet.bandwidth, ChanSet.spread_factor, ChanSet.coding_rate, 0, LORA_PREAMBLE_LENGTH, LORA_PREAMBLE_LENGTH, false, true, false, 0, false, 20000 );
LORA_SYMBOL_TIMEOUT, false , 0, true, false, 0, false, true ); Radio.SetRxConfig( MODEM_LORA, ChanSet.bandwidth, ChanSet.spread_factor, ChanSet.coding_rate, 0, LORA_PREAMBLE_LENGTH,
} LORA_SYMBOL_TIMEOUT, false , 0, true, false, 0, false, true );
}