-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUARTKeyValueReader.h
159 lines (129 loc) · 3.82 KB
/
UARTKeyValueReader.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
#include <SoftwareSerial.h>
#include "keyValuePairs.h"
#include "MQTTSender.h"
class UARTKeyValueReader {
private:
SoftwareSerial mySerial;
int firstByte = 0;
uint key = 0;
uint value = 0;
bool firstByteReceived = false;
bool secondByteReceived = false;
bool keyReceived = false;
bool valueReceived = false;
bool published = false;
MqttClient* mqttClient;
keyValuePairs<int, String> keyValueStore;
keyValuePairs<int, String>* nameMapping;
unsigned long currentMillis = 0;
unsigned long previousPublishMillis = 0;
unsigned long lastReadMillis = 0;
unsigned long mqttConnectCounter = 0;
String portName;
public:
UARTKeyValueReader(int rxPin, int txPin, MqttClient* _mqttClient, keyValuePairs<int, String>* _nameMapping, String _portName)
: mySerial(rxPin, txPin, true), mqttClient(_mqttClient), nameMapping(_nameMapping), portName(_portName) {
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
void begin(long baudRate) {
mySerial.begin(baudRate);
}
void listen() {
mySerial.listen();
published = false;
currentMillis = millis();
previousPublishMillis = currentMillis;
lastReadMillis = currentMillis; // reset the silence detection
}
const bool isPublished() {
return published;
}
void loop() {
unsigned long lastReadMillisTemp = readKeyValuePair();
if (lastReadMillisTemp != 0) {
lastReadMillis = lastReadMillisTemp;
}
currentMillis = millis();
// 1. Wait on timeout from setting.h
// 2. Wait on silence from controller
if (!published && (currentMillis - previousPublishMillis >= mqttSendInterval)) { // || currentMillis - lastReadMillis > 2500
previousPublishMillis = currentMillis;
lastReadMillis = currentMillis; // reset the silence detection
Serial.print("Publishing on ");
Serial.println(portName);
sendMqttMessage(mqttClient, &keyValueStore, nameMapping, portName);
published = true;
}
}
private:
unsigned long readKeyValuePair() {
uint16_t result = read2Bytes();
unsigned long lastReadMillis = 0;
if (secondByteReceived == true) {
if (keyReceived == false) {
key = result;
keyReceived = true;
} else {
value = result;
keyReceived = false;
valueReceived = true;
}
secondByteReceived = false;
}
if (valueReceived == true) {
#ifdef debug
if (key == 550) {
Serial.println("---------- Begin data frame ---------");
}
Serial.print("Key: ");
Serial.print(key);
Serial.print(" Value: ");
Serial.println(value);
if (key == 536) {
Serial.println("---------- End data frame ---------");
}
#endif
lastReadMillis = millis();
// Check existence in the key value store
if (keyValueStore.find(key) != NULL) {
// Delete the key
keyValueStore.erase(key);
}
// insert measurement to key value store
keyValueStore.insert(key, (String)value);
// Reset all bool variables
resetFlags();
}
return lastReadMillis;
}
uint16_t read2Bytes() {
uint16_t returnValue = 0;
if (mySerial.available() > 0) {
// Flash LED to indicate read
digitalWrite(LED_BUILTIN, LOW);
if (firstByteReceived == true) {
returnValue = (firstByte << 8) | mySerial.read();
firstByte = 0;
firstByteReceived = false;
secondByteReceived = true;
} else {
firstByte = mySerial.read();
firstByteReceived = true;
}
// Reset LED state
digitalWrite(LED_BUILTIN, HIGH);
}
return returnValue;
}
void resetFlags() {
firstByteReceived = false;
secondByteReceived = false;
keyReceived = false;
valueReceived = false;
key = 0;
value = 0;
firstByte = 0;
}
};