Skip to content

Commit 4d8f700

Browse files
committed
Add example LoRandom
1 parent 6f5ab06 commit 4d8f700

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

examples/LoRandom/LoRandom.ino

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
LoRandomSeed
3+
4+
Example demonstrating how to generate random numbers using LoRa.
5+
6+
created 7 Sep 2020
7+
by Konduino
8+
adapted by Andres Sabas
9+
10+
SX1276 Register (address) Register bit field (bit #) Values Note
11+
RegOpMode (0x01) LongRangeMode[7] ‘1’ LoRa mode enable
12+
Mode[2:0] ‘101’ Receive Continuous mode
13+
------------------------------------------------------------------------------------------------------------------
14+
RegModemConfig1 (0x1D) Bw[7:4] ‘0111’ ‘0111’ for 125kHz modulation Bandwidth
15+
CodingRate[3:1] ‘001’ 4/5 error coding rate
16+
ImplicitHeaderModeOn[0] ‘0’ Packets have up-front header
17+
------------------------------------------------------------------------------------------------------------------
18+
RegModemConfig2 (0x1E) SpreadingFactor[7:4] ‘0111’ ‘0111’ (SF7) = 6kbit/s
19+
20+
To generate an N bit random number, perform N read operation of the register RegRssiWideband (address 0x2c)
21+
and use the LSB of the fetched value. The value from RegRssiWideband is derived from a wideband (4MHz) signal strength
22+
at the receiver input and the LSB of this value constantly and randomly changes.
23+
*/
24+
25+
#include <SPI.h>
26+
#include <LoRa.h>
27+
28+
void hexDump(unsigned char *buf, uint16_t len) {
29+
String s = "|", t = "| |";
30+
Serial.println(F(" |.0 .1 .2 .3 .4 .5 .6 .7 .8 .9 .a .b .c .d .e .f |"));
31+
Serial.println(F(" +------------------------------------------------+ +----------------+"));
32+
for (uint16_t i = 0; i < len; i += 16) {
33+
for (uint8_t j = 0; j < 16; j++) {
34+
if (i + j >= len) {
35+
s = s + " "; t = t + " ";
36+
} else {
37+
char c = buf[i + j];
38+
if (c < 16) s = s + "0";
39+
s = s + String(c, HEX) + " ";
40+
if (c < 32 || c > 127) t = t + ".";
41+
else t = t + (char)c;
42+
}
43+
}
44+
uint8_t index = i / 16;
45+
Serial.print(index, HEX); Serial.write('.');
46+
Serial.println(s + t + "|");
47+
s = "|"; t = "| |";
48+
}
49+
Serial.println(F(" +------------------------------------------------+ +----------------+"));
50+
}
51+
52+
void setup() {
53+
Serial.begin(115200);
54+
while(!Serial);
55+
Serial.print(F("\n\n\n[SX1276] Initializing ... "));
56+
LoRa.setPins(SS, RFM_RST, RFM_DIO0);
57+
Serial.println("SS: " + String(SS));
58+
Serial.println("RFM_RST: " + String(RFM_RST));
59+
Serial.println("RFM_DIO0: " + String(RFM_DIO0));
60+
Serial.println("RFM_SWITCH: " + String(RFM_SWITCH));
61+
if (!LoRa.begin(868E6)) {
62+
Serial.println("Starting LoRa failed!");
63+
while (1);
64+
}
65+
Serial.print("Setting up LoRa ");
66+
pinMode(RFM_SWITCH, OUTPUT);
67+
digitalWrite(RFM_SWITCH, 1);
68+
LoRa.setTxPower(20, PA_OUTPUT_PA_BOOST_PIN);
69+
LoRa.setPreambleLength(8);
70+
LoRa.LoRandomSeed();
71+
Serial.println("[o]");
72+
delay(1000);
73+
Serial.println("End of setup\n\n");
74+
}
75+
76+
void loop() {
77+
unsigned char randomStock[256];
78+
// We'll build a stock of random bytes for use in code
79+
uint8_t randomIndex = 0;
80+
uint16_t i;
81+
for (i = 0; i < 256; i++) {
82+
uint8_t x = LoRa.random();
83+
randomStock[i] = x;
84+
}
85+
randomIndex = 0;
86+
hexDump(randomStock, 256);
87+
delay(2000);
88+
}

0 commit comments

Comments
 (0)