-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrz_lora.h
executable file
·195 lines (156 loc) · 4.67 KB
/
rz_lora.h
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
TITLE:
rz_http.h
BRIEF:
header only library
DESC:
Arduino lib for ESP32 Lora
* don't use, deprecated
SOURCE:
https://github.com/Zheng-Bote/ESP32_libs
SYNTAX:
--
RETURN:
--
Version | Date | Developer | Comments
------- | ---------- | ---------- | ---------------------------------------------------------------
0.0.3 | 2019-10-06 | RZheng | created
*/
#include "Arduino.h"
#include <xxtea-iot-crypt.h>
#ifndef rz_lora
#define rz_lora
// https://www.arduinolibraries.info/libraries/xxtea-iot-crypt
// https://github.com/boseji/xxtea-lib
#include <xxtea-iot-crypt.h>
// #####
#define SS 18
#define RST 14
#define DI0 26
#define BAND 868.00
#define spreadingFactor 6
#define SignalBandwidth 125000
#define preambleLength 8
#define codingRateDenominator 5
// char sender[4] = "i01";
char* sender = rz_strToChar(rz_baseMac());
// #####
#include <LoRa.h>
int rz_lora_start()
{
LoRa.setPins(SS,RST,DI0);
Serial.println("LoRa Sender");
if (!LoRa.begin(BAND)) {
Serial.println("Starting LoRa failed!");
while (1);
}
LoRa.setSyncWord(0xF3); // ranges from 0-0xFF, default 0x34
Serial.print("LoRa Spreading Factor: ");
Serial.println(spreadingFactor);
LoRa.setSpreadingFactor(spreadingFactor);
Serial.print("LoRa Signal Bandwidth: ");
Serial.println(SignalBandwidth);
LoRa.setSignalBandwidth(SignalBandwidth);
LoRa.setCodingRate4(codingRateDenominator);
LoRa.setPreambleLength(preambleLength);
Serial.println("LoRa Initial OK!");
}
void rz_sendLora(char receiver[4], char command[3], char msg[32])
{
char msgtxt[50] = {0};
strcat(msgtxt, receiver);
strcat(msgtxt, "|");
strcat(msgtxt, sender);
strcat(msgtxt, "|");
strcat(msgtxt, command);
strcat(msgtxt, "|");
strcat(msgtxt, msg);
Serial.print("Lora sending Msg: ");
Serial.println(msgtxt);
//
String plaintext = F("Hi There we can work with this");
// Set the Password
xxtea.setKey("2Hot4U maybe");
// Perform Encryption on the Data
Serial.print(F(" Encrypted Data: "));
String result = xxtea.encrypt(msgtxt);
result.toLowerCase(); // (Optional)
Serial.println(result);
LoRa.beginPacket();
LoRa.print(result);
LoRa.endPacket();
//
// send packet
// LoRa.beginPacket();
// LoRa.print(msgtxt);
// LoRa.endPacket();
}
void rz_receiveLora(int packetSize)
{
if (packetSize == 0) return; // if there's no packet, return
// read packet header bytes:
String incoming = "";
while (LoRa.available())
{
incoming += (char)LoRa.read();
}
Serial.println("\n##########\nMessage: " + incoming);
Serial.println("RSSI: " + String(LoRa.packetRssi()));
Serial.println("Snr: " + String(LoRa.packetSnr()));
Serial.println("\n##########\n");
//
xxtea.setKey("2Hot4U maybe");
Serial.print("Decrypt: ");
// OK: Serial.println(xxtea.decrypt(incoming));
char *token, *string;
string = rz_strToChar(xxtea.decrypt(incoming));
Serial.println(string);
int count = 0;
boolean myMsg = false;
String receiver, sendfrom, command, msgjson;
while ((token = strsep(&string, "|")) != NULL)
{
Serial.println("##token##");
Serial.println(token);
if(count == 0 && strcmp(token, sender) == 0)
{
Serial.println("\n\t##payload for me ##");
myMsg = true;
}
if(myMsg == true)
{
/*
if(count == 0) {Serial.print("0 Receiver: "); strcpy(receiver, token); Serial.println(token);}
if(count == 1) {Serial.print("1 Sender "); strcpy(sendfrom, token);Serial.println(token);}
if(count == 3) {Serial.print("3 msg "); Serial.println(token); strcat(msgjson, token); break;}
*/
switch(count)
{
case 0: {Serial.print("0 Receiver: "); receiver = String(token); Serial.println(token); break; }
case 1: {Serial.print("1 Sender "); sendfrom = String(token); Serial.println(token); rz_sendLora(rz_strToChar(sendfrom), "T4", "{\"n\":\"LoRa\",\"d\":\"rec\"}"); rz_mqtt_sendMsg("C1FE/Test", "LoRa rec"); break;}
case 2: {Serial.print("2 cmd "); command = String(token); Serial.println(token); break;}
case 3: {Serial.print("3 msg "); msgjson = String(token); Serial.println(token); break;}
default: {Serial.println("irgendwas"); Serial.println(count);break;}
}
}
else
{
Serial.println("Not my Msg");
return;
}
count++;
}
/*
Serial.println("\t### LORA ###");
Serial.print("Receiver: ");
Serial.println(receiver);
Serial.print("Sender: ");
Serial.println(sendfrom);
Serial.print("command: ");
Serial.println(command);
Serial.print("MSG JSON: ");
Serial.println(msgjson);
*/
rz_action_msg_rcv(receiver, command, sendfrom, msgjson);
}
#endif