-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathLoRaGateway.ino
86 lines (64 loc) · 1.68 KB
/
LoRaGateway.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* LoRaGateway.ino
*
* Implements a minimal, single-channel LoRa to MQTT gateway
*
* (c) 2019 Lee Dowthwaite. All Rights Reserved.
*/
#include "Arduino.h"
#include <PubSubClient.h>
#include "HAL.h"
#include "LoRaInterface.h"
// WiFi credentials
#define WIFI_SSID "*****"
#define WIFI_PASSWORD "*****"
// MQTT Broker info
// Define either an IP address...
//#define MQTT_SERVER IPAddress(192, 168, 0, 2)
// ...or a hostname
#define MQTT_SERVER "Mac-mini"
// checkAndForwardPackets()
// This is the core function that checks for received LoRa packets and forwards the contents on to MQTT
//
static void checkAndForwardPackets() {
// check for received data
String *rxPacketString = checkRxBuffer();
if (rxPacketString) {
// forward packet content to MQTT
const char *msg = rxPacketString->c_str();
publishMQTT(msg);
Serial.print("rx packet: msg: ");
Serial.println(msg);
clearDisplay();
displayString(0, 0, "received msg: ");
displayString(8, 0, msg);
displayRssi(rssi());
}
}
// Arduino main hooks
void setup() {
// initialise the board
configureBoard();
Serial.begin(115200);
Serial.println("setup()");
pinMode(LED_BUILTIN, OUTPUT);
clearDisplay();
displayString(0, 0, "Initialising Gateway...");
// Initialise wifi connection
initWiFi(WIFI_SSID, WIFI_PASSWORD);
// Configure LoRa interface
configureLoRa();
if (isWiFiConnected()) {
connectToMQTTServer(MQTT_SERVER, 1883);
}
Serial.println("setup() done");
}
void loop() {
// ensure WiFi stays connected
checkWiFiStatus();
// Perform packet forwarding
checkAndForwardPackets();
// MQTT housekeeping
updateMQTT();
delay(100);
}