-
Notifications
You must be signed in to change notification settings - Fork 1
/
LoRaReceiver_T_git.ino
213 lines (201 loc) · 6.48 KB
/
LoRaReceiver_T_git.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
Copyright 2020 Stuart A Spray C.Eng. MIEE
*/
#include "heltec.h"
#include <HTTPClient.h>
#include <iostream>
#include <string>
using namespace std;
const char* ssid = "BTHub5-9R6R";
const char* password = "5a7fe98fab";
const char* ssidAlt = "BTHub5-9R6R";
const char* passwordAlt = "5a7fe98fab";
const char* serverName = "http://aerial-photoco.uk/polytunnel/post-data.php";
char buFfer[10];
int weight[3] = {0};
String rSSi = "";
String LoRaPacket;
String packet[10];
uint8_t packetPhase = 0;
uint8_t pktIDX;
uint8_t packetSize;
uint8_t postcount = 0;
uint8_t waitCount = 0;
int lastTime = 0;
int interval = 0;
int packetsReceived = 0;
int packetsLost = 0;
float packetQuality = 0;
String packetLast;
String tmp;
String apiKeyValue = "tPmAT5Ab3j7F9";
String rxreset;
String wIfIconnectStatus = "CT0";
#define BAND 868E6 //you can set band here directly,e.g. 868E6,915E6
#define qBufferLen 100
uint8_t qBuffer[qBufferLen];
int qBufferPtr = 0;
void setup()
{
Heltec.begin(true /*DisplayEnable Enable*/, true /*Heltec.LoRa Disable*/, true /*Serial Enable*/, true /*PABOOST Enable*/, BAND /*long BAND*/);
netConnect();
Serial.println("LoRaReceiver_T");
}
void netConnect()
{
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED)
{
delay(100);
if (++waitCount > 10) // allow 5 seconds to connect
{
waitCount = 0;
break;
}
wIfIconnectStatus = "CT0";
}
if (WiFi.status() == WL_CONNECTED) wIfIconnectStatus = "CT1";
}
bool tryPost()
{
bool stat;
HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = "api_key=" + apiKeyValue + "&pressure=" + packet[1]
+ "&temperature=" + packet[2] + "&humidity=" + packet[3] + "&light=" + packet[4]
+ "&battery=" + packet[5] + "&sequence=" + packet[6] + "&weight=" + packet[7]
+ "&rxreset=" + rxreset + "&packet=" + String(packetSize) + "&postcount=" + String(postcount)
+ "&rssi=" + rSSi + "&lostpackets=" + packetsLost + "&quality=" + packetQuality + "&CO2=" + packet[8] + "&VOC=" + packet[9] + "";
Serial.println(httpRequestData);
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode == 200)
{
stat = true;
postcount = 1;
}
else
{
stat = false;
++postcount;
}
http.end();
return stat;
}
String validateWeight() // return string form of lowest of 3 weights in weight array
{
int lowest = 0;
lowest = weight[0];
if (weight[1] < lowest) lowest = weight[1];
if (weight[2] < lowest) lowest = weight[2];
return String(lowest);
}
void processPacket()
{
++packetsReceived;
interval = 0;
LoRaPacket = "";
pktIDX = 0;
packet[0] = "";
while (LoRa.available())
{
LoRaPacket += (char)LoRa.read();
}
rSSi = LoRa.packetRssi();
for (int i = 0; i < packetSize; i++)
{
tmp = LoRaPacket[i];
if (tmp == ":") // parameter separator
{
packet[++pktIDX] = packet[0];
packet[0] = "";
}
else packet[0] += tmp;
// extract weight, convert to int and store in 'weight' array using packetIndex as ptr
weight[packetPhase] = packet[7].toInt();
}
// Serial.println(); // print whole packet
for(int i = 0; i < 3; i++) Serial.println(weight[i]);
Serial.print(rSSi);
if(++packetPhase > 2)
{
packetPhase = 0;
packet[7] = validateWeight();
if (tryPost()) rxreset = "Y"; // success or failure of previous post
else
{
rxreset = "N";
netConnect();
delay(500);
}
Serial.println(rxreset);
}
Serial.println(rxreset);
// calculate missing packets percentage for the most recent n received packets
// store differences between last and current sequence numbers in circular buffer
// for each received packet compute ratio of lost packets in buffer to all packets
// all packets = sum of lost packets in buffer + packets received (length of buffer n).
if(++qBufferPtr > qBufferLen-1) qBufferPtr = 0; // rotate circular buffer ptr
if(packet[6].toInt() > 0 && packetLast.toInt() > 0) // only process if both current and last sequence number are non-zero
{
packetsLost = packet[6].toInt() - packetLast.toInt() - 1;
Serial.println("Lost:" + String(packetsLost));
qBuffer[qBufferPtr] = packetsLost;
int lostAccum = 0; // calculate content of buffer
for (int i = 0; i < qBufferLen; i++)
{
lostAccum += qBuffer[i];
}
packetQuality = float(qBufferLen *100/(qBufferLen + lostAccum));
}
LoRaData();
Serial.println(qBufferPtr);
Serial.println(buFfer);
packetLast = packet[6];
digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level)
delay(50); // wait for a second
digitalWrite(LED, LOW);
}
void LoRaData()
{
Heltec.display->clear();
Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
Heltec.display->setFont(ArialMT_Plain_10);
Heltec.display->drawString(0, 0, "rssi: " + rSSi);
Heltec.display->drawString(45, 0, packet[6]);
Heltec.display->drawString(85 , 0 ,String(interval));
sprintf(buFfer,"Q:%2.0f%%",packetQuality);
Heltec.display->drawString(0 , 10 ,"Press: " + packet[1]);
Heltec.display->drawString(75 , 10 ,String(buFfer));
Heltec.display->drawString(0 , 20 ,"Temp: " + packet[2]);
Heltec.display->drawString(65 , 20 ,rxreset + " " + wIfIconnectStatus);
Heltec.display->drawString(0 , 30 ,"Humid: " + packet[3]);
Heltec.display->drawString(65 , 30 ,"CO2:" + packet[8]);
Heltec.display->drawString(0 , 40 ,"Light: " + packet[4]);
Heltec.display->drawString(65 , 40 ,"VOC:" + packet[9]);
Heltec.display->drawString(0 , 50 ,"BattV: " + packet[5]);
Heltec.display->drawString(65 , 50 , "Wt: "+ packet[7]);
Heltec.display->display();
}
void loop()
{
delay(10);
packetSize = LoRa.parsePacket();
if (packetSize) processPacket();
if (millis() - lastTime > 1000)
{
if(++interval > 300) // no lora data received for 5 minutes
{
for (int i = 0;i < 8; i++) packet[i] = "0000"; // set lora data to 0
if (tryPost()) rxreset = "Y X"; // X says posted without lora data
else
{
rxreset = "N";
}
interval = 0;
}
LoRaData();
lastTime = millis();
Serial.print(interval);
}
}