Browse Source

lora rx tx test with unofficial lib

Thomas Buck 4 months ago
parent
commit
4585b3c25b
6 changed files with 241 additions and 80 deletions
  1. 8
    5
      include/DebugLog.h
  2. 3
    0
      include/lora.h
  3. 2
    1
      platformio.ini
  4. 31
    0
      src/DebugLog.cpp
  5. 192
    73
      src/lora.cpp
  6. 5
    1
      src/main.cpp

+ 8
- 5
include/DebugLog.h View File

@@ -26,21 +26,24 @@ public:
26 26
 #ifdef ENABLE_DEBUGLOG
27 27
     String getBuffer(void);
28 28
 #endif // ENABLE_DEBUGLOG
29
-    
29
+
30 30
     void write(char c);
31 31
     void print(String s);
32 32
     void print(int n);
33
-    
33
+
34 34
     void println(void);
35 35
     void println(String s);
36 36
     void println(int n);
37
-    
37
+
38
+    size_t printf(const char * format, ...)  __attribute__ ((format (printf, 2, 3)));
39
+    size_t printf(const char * format, va_list args);
40
+
38 41
 private:
39 42
     void sendToTargets(String s);
40
-    
43
+
41 44
 #ifdef ENABLE_DEBUGLOG
42 45
     void addToBuffer(String s);
43
-    
46
+
44 47
     CircularBuffer<char, DEBUG_LOG_HISTORY_SIZE> buffer;
45 48
 #endif // ENABLE_DEBUGLOG
46 49
 };

+ 3
- 0
include/lora.h View File

@@ -16,6 +16,9 @@
16 16
 
17 17
 #ifdef FEATURE_LORA
18 18
 
19
+void lora_oled_init(void);
20
+void lora_oled_print(String s);
21
+
19 22
 void lora_init(void);
20 23
 void lora_run(void);
21 24
 

+ 2
- 1
platformio.ini View File

@@ -53,11 +53,12 @@ build_flags =
53 53
   -DENABLE_MQTT
54 54
   -DNEW_ESP32_LIB
55 55
   -DFEATURE_LORA
56
+  -DFEATURE_DISABLE_WIFI
56 57
 lib_deps =
57 58
     https://github.com/knolleary/pubsubclient.git#2d228f2f862a95846c65a8518c79f48dfc8f188c
58 59
     https://github.com/rlogiacco/CircularBuffer.git#f29cf01b6e8603422f3668d51036ac124f803404
59 60
     https://github.com/Links2004/arduinoWebSockets.git#30d5e136665a52880f641ddd7245b3ba05ecd32b
60
-    https://github.com/beegee-tokyo/SX126x-Arduino.git#98ef65b5a2e9c35eabfd80a466327a9fdbb8e38e
61
+    https://github.com/ropg/heltec_esp32_lora_v3.git#9f281354507849755a597ebcce3721582f3b59b8
61 62
 
62 63
 [env:esp8266env]
63 64
 platform = espressif8266

+ 31
- 0
src/DebugLog.cpp View File

@@ -15,6 +15,7 @@
15 15
 
16 16
 #include "config.h"
17 17
 #include "servers.h"
18
+#include "lora.h"
18 19
 #include "DebugLog.h"
19 20
 
20 21
 DebugLog debug;
@@ -40,6 +41,10 @@ void DebugLog::addToBuffer(String s) {
40 41
 void DebugLog::sendToTargets(String s) {
41 42
     Serial.print(s);
42 43
 
44
+#ifdef FEATURE_LORA
45
+    lora_oled_print(s);
46
+#endif // FEATURE_LORA
47
+
43 48
     s = "log:" + s;
44 49
     wifi_send_websocket(s);
45 50
 }
@@ -72,3 +77,29 @@ void DebugLog::println(String s) {
72 77
 void DebugLog::println(int n) {
73 78
     println(String(n));
74 79
 }
80
+
81
+size_t DebugLog::printf(const char *format, va_list args) {
82
+    char line_buff[128];
83
+    int l = vsnprintf((char *)line_buff, sizeof(line_buff), format, args);
84
+
85
+    if (l < 0) {
86
+        // encoding error
87
+        l = snprintf((char *)line_buff, sizeof(line_buff), "%s: encoding error\r\n", __func__);
88
+    } else if (l >= (ssize_t)sizeof(line_buff)) {
89
+        // not enough space for string
90
+        l = snprintf((char *)line_buff, sizeof(line_buff), "%s: message too long (%d)\r\n", __func__, l);
91
+    }
92
+    if ((l > 0) && (l <= (int)sizeof(line_buff))) {
93
+        print(String(line_buff));
94
+    }
95
+
96
+    return (l < 0) ? 0 : l;
97
+}
98
+
99
+size_t DebugLog::printf(const char * format, ...) {
100
+    va_list args;
101
+    va_start(args, format);
102
+    size_t r = printf(format, args);
103
+    va_end(args);
104
+    return r;
105
+}

+ 192
- 73
src/lora.cpp View File

@@ -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

+ 5
- 1
src/main.cpp View File

@@ -58,6 +58,10 @@ void setup() {
58 58
 
59 59
     Serial.begin(115200);
60 60
 
61
+#ifdef FEATURE_LORA
62
+    lora_oled_init();
63
+#endif // FEATURE_LORA
64
+
61 65
     debug.println(F("Initializing..."));
62 66
 
63 67
     // Blink LED for init
@@ -93,7 +97,7 @@ void setup() {
93 97
     initSensors();
94 98
 
95 99
 #ifdef FEATURE_LORA
96
-    debug.println(F("Lora"));
100
+    debug.println(F("LoRa"));
97 101
     lora_init();
98 102
 #endif // FEATURE_LORA
99 103
 

Loading…
Cancel
Save