|
@@ -3,8 +3,6 @@
|
3
|
3
|
*
|
4
|
4
|
* ESP8266 / ESP32 Environmental Sensor
|
5
|
5
|
*
|
6
|
|
- * https://github.com/beegee-tokyo/SX126x-Arduino/blob/master/examples/PingPongPio/src/main.cpp
|
7
|
|
- *
|
8
|
6
|
* ----------------------------------------------------------------------------
|
9
|
7
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
10
|
8
|
* <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
|
|
@@ -16,89 +14,210 @@
|
16
|
14
|
#ifdef FEATURE_LORA
|
17
|
15
|
|
18
|
16
|
#include <Arduino.h>
|
19
|
|
-#include <SX126x-Arduino.h>
|
|
17
|
+
|
|
18
|
+// Turns the 'PRG' button into the power button, long press is off
|
|
19
|
+#define HELTEC_POWER_BUTTON
|
|
20
|
+
|
|
21
|
+#include <heltec_unofficial.h>
|
20
|
22
|
|
21
|
23
|
#include "config.h"
|
22
|
24
|
#include "DebugLog.h"
|
23
|
25
|
#include "lora.h"
|
24
|
26
|
|
25
|
|
-// Define LoRa parameters
|
26
|
|
-#define RF_FREQUENCY 868000000 // Hz
|
27
|
|
-#define TX_OUTPUT_POWER 22 // dBm
|
28
|
|
-#define LORA_BANDWIDTH 0 // [0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: Reserved]
|
29
|
|
-#define LORA_SPREADING_FACTOR 7 // [SF7..SF12]
|
30
|
|
-#define LORA_CODINGRATE 1 // [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
|
31
|
|
-#define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
|
32
|
|
-#define LORA_SYMBOL_TIMEOUT 0 // Symbols
|
33
|
|
-#define LORA_FIX_LENGTH_PAYLOAD_ON false
|
34
|
|
-#define LORA_IQ_INVERSION_ON false
|
35
|
|
-#define RX_TIMEOUT_VALUE 3000
|
36
|
|
-#define TX_TIMEOUT_VALUE 3000
|
|
27
|
+#define OLED_BAT_INTERVAL (10UL * 1000UL) // in ms
|
|
28
|
+#define LORA_LED_BRIGHTNESS 25 // in percent, 50% brightness is plenty for this LED
|
|
29
|
+
|
|
30
|
+// Pause between transmited packets in seconds.
|
|
31
|
+// Set to zero to only transmit a packet when pressing the user button
|
|
32
|
+// Will not exceed 1% duty cycle, even if you set a lower value.
|
|
33
|
+#define PAUSE 10
|
|
34
|
+
|
|
35
|
+// Frequency in MHz. Keep the decimal point to designate float.
|
|
36
|
+// Check your own rules and regulations to see what is legal where you are.
|
|
37
|
+#define FREQUENCY 866.3 // for Europe
|
|
38
|
+// #define FREQUENCY 905.2 // for US
|
|
39
|
+
|
|
40
|
+// LoRa bandwidth. Keep the decimal point to designate float.
|
|
41
|
+// Allowed values are 7.8, 10.4, 15.6, 20.8, 31.25, 41.7, 62.5, 125.0, 250.0 and 500.0 kHz.
|
|
42
|
+#define BANDWIDTH 250.0
|
|
43
|
+
|
|
44
|
+// Number from 5 to 12. Higher means slower but higher "processor gain",
|
|
45
|
+// meaning (in nutshell) longer range and more robust against interference.
|
|
46
|
+#define SPREADING_FACTOR 9
|
|
47
|
+
|
|
48
|
+// Transmit power in dBm. 0 dBm = 1 mW, enough for tabletop-testing. This value can be
|
|
49
|
+// set anywhere between -9 dBm (0.125 mW) to 22 dBm (158 mW). Note that the maximum ERP
|
|
50
|
+// (which is what your antenna maximally radiates) on the EU ISM band is 25 mW, and that
|
|
51
|
+// transmissting without an antenna can damage your hardware.
|
|
52
|
+// 25mW = 14dBm
|
|
53
|
+#define MAX_TX_POWER 14
|
|
54
|
+#define ANTENNA_GAIN 5
|
|
55
|
+#define TRANSMIT_POWER (MAX_TX_POWER - ANTENNA_GAIN)
|
|
56
|
+
|
|
57
|
+#define RADIOLIB_xy(action) \
|
|
58
|
+ debug.print(#action); \
|
|
59
|
+ debug.print(" = "); \
|
|
60
|
+ debug.print(state); \
|
|
61
|
+ debug.print(" ("); \
|
|
62
|
+ debug.print(radiolib_result_string(state)); \
|
|
63
|
+ debug.println(")");
|
|
64
|
+
|
|
65
|
+#define RADIOLIB_CHECK(action) do { \
|
|
66
|
+ int state = action; \
|
|
67
|
+ if (state != RADIOLIB_ERR_NONE) { \
|
|
68
|
+ RADIOLIB_xy(action); \
|
|
69
|
+ success = false; \
|
|
70
|
+ } \
|
|
71
|
+} while (false);
|
|
72
|
+
|
|
73
|
+static unsigned long last_bat_time = 0;
|
|
74
|
+static bool use_lora = true;
|
|
75
|
+static unsigned long last_tx = 0, counter = 0, tx_time = 0, minimum_pause = 0;
|
|
76
|
+static volatile bool rx_flag = false;
|
|
77
|
+
|
|
78
|
+void lora_oled_init(void) {
|
|
79
|
+ heltec_setup();
|
|
80
|
+}
|
|
81
|
+
|
|
82
|
+void lora_oled_print(String s) {
|
|
83
|
+ display.print(s);
|
|
84
|
+}
|
37
|
85
|
|
38
|
|
-#define BUFFER_SIZE 64 // Define the payload size here
|
|
86
|
+static void print_bat(void) {
|
|
87
|
+ float vbat = heltec_vbat();
|
|
88
|
+ debug.printf("Vbat: %.2fV (%d%%)\n", vbat, heltec_battery_percent(vbat));
|
|
89
|
+}
|
39
|
90
|
|
40
|
|
-static hw_config hwConfig;
|
41
|
|
-static RadioEvents_t RadioEvents;
|
|
91
|
+static void lora_rx(void) {
|
|
92
|
+ rx_flag = true;
|
|
93
|
+}
|
42
|
94
|
|
43
|
95
|
void lora_init(void) {
|
44
|
|
- // Define the HW configuration between MCU and SX126x
|
45
|
|
- hwConfig.CHIP_TYPE = SX1262_CHIP;
|
46
|
|
-
|
47
|
|
- // TODO pin config!
|
48
|
|
- hwConfig.PIN_LORA_RESET = 4;
|
49
|
|
- hwConfig.PIN_LORA_NSS = 5;
|
50
|
|
- hwConfig.PIN_LORA_SCLK = 18;
|
51
|
|
- hwConfig.PIN_LORA_MISO = 19;
|
52
|
|
- hwConfig.PIN_LORA_DIO_1 = 21;
|
53
|
|
- hwConfig.PIN_LORA_BUSY = 22;
|
54
|
|
- hwConfig.PIN_LORA_MOSI = 23;
|
55
|
|
- hwConfig.RADIO_TXEN = 26;
|
56
|
|
- hwConfig.RADIO_RXEN = 27;
|
57
|
|
-
|
58
|
|
- hwConfig.USE_DIO2_ANT_SWITCH = true;
|
59
|
|
- hwConfig.USE_DIO3_TCXO = true;
|
60
|
|
- hwConfig.USE_DIO3_ANT_SWITCH = false;
|
61
|
|
-
|
62
|
|
- // Initialize the LoRa chip
|
63
|
|
- debug.println("Starting lora_hardware_init");
|
64
|
|
- lora_hardware_init(hwConfig);
|
65
|
|
-
|
66
|
|
- // Initialize the Radio callbacks
|
67
|
|
- RadioEvents.TxDone = OnTxDone;
|
68
|
|
- RadioEvents.RxDone = OnRxDone;
|
69
|
|
- RadioEvents.TxTimeout = OnTxTimeout;
|
70
|
|
- RadioEvents.RxTimeout = OnRxTimeout;
|
71
|
|
- RadioEvents.RxError = OnRxError;
|
72
|
|
- RadioEvents.CadDone = OnCadDone;
|
73
|
|
-
|
74
|
|
- // Initialize the Radio
|
75
|
|
- Radio.Init(&RadioEvents);
|
76
|
|
-
|
77
|
|
- // Set Radio channel
|
78
|
|
- Radio.SetChannel(RF_FREQUENCY);
|
79
|
|
-
|
80
|
|
- // Set Radio TX configuration
|
81
|
|
- Radio.SetTxConfig(MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
|
82
|
|
- LORA_SPREADING_FACTOR, LORA_CODINGRATE,
|
83
|
|
- LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
|
84
|
|
- true, 0, 0, LORA_IQ_INVERSION_ON, TX_TIMEOUT_VALUE);
|
85
|
|
-
|
86
|
|
- // Set Radio RX configuration
|
87
|
|
- Radio.SetRxConfig(MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
|
88
|
|
- LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
|
89
|
|
- LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
|
90
|
|
- 0, true, 0, 0, LORA_IQ_INVERSION_ON, true);
|
91
|
|
-
|
92
|
|
- // Start LoRa
|
93
|
|
- debug.println("Starting Radio.Rx");
|
94
|
|
- Radio.Rx(RX_TIMEOUT_VALUE);
|
|
96
|
+ print_bat();
|
|
97
|
+
|
|
98
|
+ bool success = true;
|
|
99
|
+
|
|
100
|
+ RADIOLIB_CHECK(radio.begin());
|
|
101
|
+ if (!success) {
|
|
102
|
+ use_lora = false;
|
|
103
|
+ return;
|
|
104
|
+ }
|
|
105
|
+
|
|
106
|
+ radio.setDio1Action(lora_rx);
|
|
107
|
+
|
|
108
|
+ debug.printf("Frequency: %.2f MHz\n", FREQUENCY);
|
|
109
|
+ RADIOLIB_CHECK(radio.setFrequency(FREQUENCY));
|
|
110
|
+ if (!success) {
|
|
111
|
+ use_lora = false;
|
|
112
|
+ return;
|
|
113
|
+ }
|
|
114
|
+
|
|
115
|
+ debug.printf("Bandwidth: %.1f kHz\n", BANDWIDTH);
|
|
116
|
+ RADIOLIB_CHECK(radio.setBandwidth(BANDWIDTH));
|
|
117
|
+ if (!success) {
|
|
118
|
+ use_lora = false;
|
|
119
|
+ return;
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ debug.printf("Spreading Factor: %i\n", SPREADING_FACTOR);
|
|
123
|
+ RADIOLIB_CHECK(radio.setSpreadingFactor(SPREADING_FACTOR));
|
|
124
|
+ if (!success) {
|
|
125
|
+ use_lora = false;
|
|
126
|
+ return;
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+ debug.printf("TX power: %i dBm\n", TRANSMIT_POWER);
|
|
130
|
+ RADIOLIB_CHECK(radio.setOutputPower(TRANSMIT_POWER));
|
|
131
|
+ if (!success) {
|
|
132
|
+ use_lora = false;
|
|
133
|
+ return;
|
|
134
|
+ }
|
|
135
|
+
|
|
136
|
+ // Start receiving
|
|
137
|
+ RADIOLIB_CHECK(radio.startReceive(RADIOLIB_SX126X_RX_TIMEOUT_INF));
|
|
138
|
+ if (!success) {
|
|
139
|
+ use_lora = false;
|
|
140
|
+ return;
|
|
141
|
+ }
|
95
|
142
|
}
|
96
|
143
|
|
97
|
144
|
void lora_run(void) {
|
98
|
|
-#ifdef ARDUINO_ARCH_ESP8266
|
99
|
|
- // Handle Radio events
|
100
|
|
- Radio.IrqProcess();
|
101
|
|
-#endif // ARDUINO_ARCH_ESP8266
|
|
145
|
+ heltec_loop();
|
|
146
|
+
|
|
147
|
+#ifdef OLED_BAT_INTERVAL
|
|
148
|
+ unsigned long time = millis();
|
|
149
|
+ if ((time - last_bat_time) >= OLED_BAT_INTERVAL) {
|
|
150
|
+ last_bat_time = time;
|
|
151
|
+ print_bat();
|
|
152
|
+ }
|
|
153
|
+#endif
|
|
154
|
+
|
|
155
|
+ if (!use_lora) {
|
|
156
|
+ return;
|
|
157
|
+ }
|
|
158
|
+
|
|
159
|
+ // If a packet was received, display it and the RSSI and SNR
|
|
160
|
+ if (rx_flag) {
|
|
161
|
+ rx_flag = false;
|
|
162
|
+
|
|
163
|
+ bool success = true;
|
|
164
|
+ String data;
|
|
165
|
+ RADIOLIB_CHECK(radio.readData(data));
|
|
166
|
+ if (success) {
|
|
167
|
+ debug.printf("RX [%s]\n", data.c_str());
|
|
168
|
+ debug.printf(" RSSI: %.2f dBm\n", radio.getRSSI());
|
|
169
|
+ debug.printf(" SNR: %.2f dB\n", radio.getSNR());
|
|
170
|
+ }
|
|
171
|
+
|
|
172
|
+ success = true;
|
|
173
|
+ RADIOLIB_CHECK(radio.startReceive(RADIOLIB_SX126X_RX_TIMEOUT_INF));
|
|
174
|
+ if (!success) {
|
|
175
|
+ use_lora = false;
|
|
176
|
+ return;
|
|
177
|
+ }
|
|
178
|
+ }
|
|
179
|
+
|
|
180
|
+ bool tx_legal = millis() > last_tx + minimum_pause;
|
|
181
|
+
|
|
182
|
+ // Transmit a packet every PAUSE seconds or when the button is pressed
|
|
183
|
+ if ((PAUSE && tx_legal && millis() - last_tx > (PAUSE * 1000)) || button.isSingleClick()) {
|
|
184
|
+ // In case of button click, tell user to wait
|
|
185
|
+ if (!tx_legal) {
|
|
186
|
+ debug.printf("Legal limit, wait %i sec.\n", (int)((minimum_pause - (millis() - last_tx)) / 1000) + 1);
|
|
187
|
+ return;
|
|
188
|
+ }
|
|
189
|
+
|
|
190
|
+ debug.printf("TX [%s] ", String(counter).c_str());
|
|
191
|
+ radio.clearDio1Action();
|
|
192
|
+
|
|
193
|
+ heltec_led(LORA_LED_BRIGHTNESS);
|
|
194
|
+
|
|
195
|
+ bool success = true;
|
|
196
|
+ tx_time = millis();
|
|
197
|
+ RADIOLIB_CHECK(radio.transmit(String(counter++).c_str()));
|
|
198
|
+ tx_time = millis() - tx_time;
|
|
199
|
+
|
|
200
|
+ heltec_led(0);
|
|
201
|
+
|
|
202
|
+ if (success) {
|
|
203
|
+ debug.printf("OK (%i ms)\n", (int)tx_time);
|
|
204
|
+ } else {
|
|
205
|
+ debug.printf("fail (%i)\n", _radiolib_status);
|
|
206
|
+ }
|
|
207
|
+
|
|
208
|
+ // Maximum 1% duty cycle
|
|
209
|
+ minimum_pause = tx_time * 100;
|
|
210
|
+ last_tx = millis();
|
|
211
|
+
|
|
212
|
+ radio.setDio1Action(lora_rx);
|
|
213
|
+
|
|
214
|
+ success = true;
|
|
215
|
+ RADIOLIB_CHECK(radio.startReceive(RADIOLIB_SX126X_RX_TIMEOUT_INF));
|
|
216
|
+ if (!success) {
|
|
217
|
+ use_lora = false;
|
|
218
|
+ return;
|
|
219
|
+ }
|
|
220
|
+ }
|
102
|
221
|
}
|
103
|
222
|
|
104
|
223
|
#endif // FEATURE_LORA
|