Browse Source

start experimenting with lora

Thomas Buck 2 months ago
parent
commit
e70faaa5d9
5 changed files with 174 additions and 5 deletions
  1. 1
    1
      compile_commands.json
  2. 24
    0
      include/lora.h
  3. 26
    2
      platformio.ini
  4. 104
    0
      src/lora.cpp
  5. 19
    2
      src/main.cpp

+ 1
- 1
compile_commands.json View File

@@ -1 +1 @@
1
-.pio/build/cyd/compile_commands.json
1
+.pio/build/lora/compile_commands.json

+ 24
- 0
include/lora.h View File

@@ -0,0 +1,24 @@
1
+/*
2
+ * lora.h
3
+ *
4
+ * ESP8266 / ESP32 Environmental Sensor
5
+ *
6
+ * ----------------------------------------------------------------------------
7
+ * "THE BEER-WARE LICENSE" (Revision 42):
8
+ * <xythobuz@xythobuz.de> wrote this file.  As long as you retain this notice
9
+ * you can do whatever you want with this stuff. If we meet some day, and you
10
+ * think this stuff is worth it, you can buy me a beer in return.   Thomas Buck
11
+ * ----------------------------------------------------------------------------
12
+ */
13
+
14
+#ifndef __ESP_ENV_LORA__
15
+#define __ESP_ENV_LORA__
16
+
17
+#ifdef FEATURE_LORA
18
+
19
+void lora_init(void);
20
+void lora_run(void);
21
+
22
+#endif // FEATURE_LORA
23
+
24
+#endif // __ESP_ENV_LORA__

+ 26
- 2
platformio.ini View File

@@ -13,8 +13,8 @@ platform = platformio/espressif32@6.4.0
13 13
 board = esp32dev
14 14
 framework = arduino
15 15
 upload_protocol = esptool
16
-upload_port = /dev/ttyUSB1
17
-monitor_port = /dev/ttyUSB1
16
+upload_port = /dev/ttyUSB2
17
+monitor_port = /dev/ttyUSB2
18 18
 monitor_speed = 115200
19 19
 extra_scripts = pre:extra_script.py
20 20
 build_flags =
@@ -36,6 +36,29 @@ lib_deps =
36 36
     https://github.com/Bodmer/TFT_eSPI.git#01483a1043a4915c4e34c928fb48a87d3b38a248
37 37
     https://github.com/PaulStoffregen/XPT2046_Touchscreen.git#5d5120e93ab5d28b3f1db6d754819c354c2da019
38 38
 
39
+[env:lora]
40
+platform = espressif32
41
+board = heltec_wifi_lora_32_V3
42
+framework = arduino
43
+upload_protocol = esptool
44
+upload_port = /dev/ttyUSB2
45
+monitor_port = /dev/ttyUSB2
46
+monitor_speed = 115200
47
+extra_scripts = pre:extra_script.py
48
+build_flags =
49
+  -DSENSOR_HOSTNAME_PREFIX=\"lora-\"
50
+  "-DNAME_OF_FEATURE=\"Lora Gatway\""
51
+  -DENABLE_WEBSOCKETS
52
+  -DENABLE_DEBUGLOG
53
+  -DENABLE_MQTT
54
+  -DNEW_ESP32_LIB
55
+  -DFEATURE_LORA
56
+lib_deps =
57
+    https://github.com/knolleary/pubsubclient.git#2d228f2f862a95846c65a8518c79f48dfc8f188c
58
+    https://github.com/rlogiacco/CircularBuffer.git#f29cf01b6e8603422f3668d51036ac124f803404
59
+    https://github.com/Links2004/arduinoWebSockets.git#30d5e136665a52880f641ddd7245b3ba05ecd32b
60
+    https://github.com/beegee-tokyo/SX126x-Arduino.git#98ef65b5a2e9c35eabfd80a466327a9fdbb8e38e
61
+
39 62
 [env:esp8266env]
40 63
 platform = espressif8266
41 64
 board = esp01_1m
@@ -205,3 +228,4 @@ lib_deps =
205 228
     Adafruit BME280 Library
206 229
     https://github.com/jandrassy/UnoWiFiDevEdSerial1
207 230
     https://github.com/jandrassy/arduino-library-wifilink
231
+

+ 104
- 0
src/lora.cpp View File

@@ -0,0 +1,104 @@
1
+/*
2
+ * lora.cpp
3
+ *
4
+ * ESP8266 / ESP32 Environmental Sensor
5
+ *
6
+ * https://github.com/beegee-tokyo/SX126x-Arduino/blob/master/examples/PingPongPio/src/main.cpp
7
+ *
8
+ * ----------------------------------------------------------------------------
9
+ * "THE BEER-WARE LICENSE" (Revision 42):
10
+ * <xythobuz@xythobuz.de> wrote this file.  As long as you retain this notice
11
+ * you can do whatever you want with this stuff. If we meet some day, and you
12
+ * think this stuff is worth it, you can buy me a beer in return.   Thomas Buck
13
+ * ----------------------------------------------------------------------------
14
+ */
15
+
16
+#ifdef FEATURE_LORA
17
+
18
+#include <Arduino.h>
19
+#include <SX126x-Arduino.h>
20
+
21
+#include "config.h"
22
+#include "DebugLog.h"
23
+#include "lora.h"
24
+
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
37
+
38
+#define BUFFER_SIZE 64 // Define the payload size here
39
+
40
+static hw_config hwConfig;
41
+static RadioEvents_t RadioEvents;
42
+
43
+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);
95
+}
96
+
97
+void lora_run(void) {
98
+#ifdef ARDUINO_ARCH_ESP8266
99
+    // Handle Radio events
100
+    Radio.IrqProcess();
101
+#endif // ARDUINO_ARCH_ESP8266
102
+}
103
+
104
+#endif // FEATURE_LORA

+ 19
- 2
src/main.cpp View File

@@ -33,6 +33,7 @@
33 33
 #include "html.h"
34 34
 #include "servers.h"
35 35
 #include "ui.h"
36
+#include "lora.h"
36 37
 
37 38
 unsigned long last_led_blink_time = 0;
38 39
 
@@ -91,6 +92,13 @@ void setup() {
91 92
     debug.println(F("Sensors"));
92 93
     initSensors();
93 94
 
95
+#ifdef FEATURE_LORA
96
+    debug.println(F("Lora"));
97
+    lora_init();
98
+#endif // FEATURE_LORA
99
+
100
+#ifndef FEATURE_DISABLE_WIFI
101
+
94 102
     // Build hostname string
95 103
     String hostname = SENSOR_HOSTNAME_PREFIX;
96 104
     hostname += SENSOR_ID;
@@ -196,7 +204,7 @@ void setup() {
196 204
     ui_progress(UI_WIFI_CONNECTED);
197 205
 #endif // FEATURE_UI
198 206
 
199
-#endif
207
+#endif // ARCH
200 208
 
201 209
     debug.println(F("Seeding"));
202 210
     randomSeed(micros());
@@ -210,6 +218,8 @@ void setup() {
210 218
     debug.println(F("Servers"));
211 219
     initServers(hostname);
212 220
 
221
+#endif // FEATURE_DISABLE_WIFI
222
+
213 223
     debug.println(F("Ready! Starting..."));
214 224
 
215 225
 #ifdef FEATURE_UI
@@ -219,15 +229,22 @@ void setup() {
219 229
 }
220 230
 
221 231
 void loop() {
222
-    runServers();
223 232
     runSensors();
233
+
234
+#ifndef FEATURE_DISABLE_WIFI
235
+    runServers();
224 236
     runMQTT();
225 237
     runInflux();
238
+#endif // FEATURE_DISABLE_WIFI
226 239
 
227 240
 #ifdef FEATURE_UI
228 241
     ui_run();
229 242
 #endif
230 243
 
244
+#ifdef FEATURE_LORA
245
+    lora_run();
246
+#endif // FEATURE_LORA
247
+
231 248
     // blink heartbeat LED
232 249
     unsigned long time = millis();
233 250
     if ((time - last_led_blink_time) >= LED_BLINK_INTERVAL) {

Loading…
Cancel
Save