-
Notifications
You must be signed in to change notification settings - Fork 0
/
lorarecv.ino
150 lines (119 loc) · 3.51 KB
/
lorarecv.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*********
Modified from the examples of the Arduino LoRa library
More resources: https://randomnerdtutorials.com
*********/
#include <SPI.h>
#include <LoRa.h>
#include "secrets.h"
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include "WiFi.h"
#define AWS_IOT_PUBLISH_TOPIC "esp32/pub"
#define AWS_IOT_SUBSCRIBE_TOPIC "esp32/sub"
#define ss 5
#define rst 14 //define the pins used by the transceiver module
#define dio0 2
WiFiClientSecure net = WiFiClientSecure();
PubSubClient client(net);
String LoRaData;
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
void setup() {
//initialize Serial Monitor
Serial.begin(9600);
while (!Serial);
Serial.println("LoRa Receiver");
connectAWS();
//setup LoRa transceiver module
LoRa.setPins(ss, rst, dio0);
//replace the LoRa.begin(---E-) argument with your location's frequency
//433E6 for Asia
//866E6 for Europe
//915E6 for North America
while (!LoRa.begin(433E6)) {
Serial.println(".");
delay(500);
}
// Change sync word (0xF3) to match the receiver
// The sync word assures you don't get LoRa messages from other LoRa transceivers
// ranges from 0-0xFF
LoRa.setSyncWord(0xF3);
Serial.println("LoRa Initializing OK!");
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
void(* resetFunc) (void) = 0;
void loop() {
const char* message;
// try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
// received a packet
Serial.print("Received packet '");
// read packet
while (LoRa.available()) {
LoRaData = LoRa.readString();
message = LoRaData.c_str();
int questionCheck = 0;
for (int i = 0; message[i] != '\0'; ++i)
{
if (message[i] == '⸮')
{
resetFunc();
break;
}
}
if(client.publish(AWS_IOT_PUBLISH_TOPIC, message)==0)
resetFunc();
client.loop();
delay(5000);
}
// print RSSI of packet
Serial.println(LoRa.packetRssi());
}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
//Connect to AWS
void connectAWS()
{
WiFi.mode(WIFI_STA);
WiFi.begin("Fixbox-78F637", "MTU2NGRi");
Serial.println("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
// Configure WiFiClientSecure to use the AWS IoT device credentials
net.setCACert(AWS_CERT_CA);
net.setCertificate(AWS_CERT_CRT);
net.setPrivateKey(AWS_CERT_PRIVATE);
// Connect to the MQTT broker on the AWS endpoint we defined earlier
client.setServer(AWS_IOT_ENDPOINT, 8883);
// Create a message handler
client.setCallback(messageHandler);
Serial.println("Connecting to AWS IOT");
while (!client.connect(THINGNAME))
{
Serial.print(".");
delay(100);
}
if (!client.connected())
{
Serial.println("AWS IoT Timeout!");
return;
}
// Subscribe to a topic
client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC);
Serial.println("AWS IoT Connected!");
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
void messageHandler(char* topic, byte* payload, unsigned int length)
{
Serial.print("incoming: ");
}