forked from jrodanapolis/WE_AV-ABR_GPC_NanoESP32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lunarGateway.cpp
241 lines (210 loc) · 6.67 KB
/
lunarGateway.cpp
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
lunarGateway.cpp - Library for connecting the ESP32 with the Lunar 2021 scale
Created by Frowin Ellermann, 2022.
Released under the MIT license.
https://github.com/frowin/LunarGateway
*/
#include "lunarGateway.h"
#include <stdio.h>
lunarGateway::lunarGateway(){
_lastHeartBeat = 0;
}
bool lunarGateway::read(){
if (_readChar.valueUpdated()) {
_readChar.readValue(_readBuffer, sizeof(_readBuffer));
decodeMessage(_readBuffer);
return true;
}
return false;
}
bool lunarGateway::connect(BLEDevice *dev){
if (!dev)
return false;
if (dev->connect()) {
Serial.println("Connected");
} else {
Serial.println("Failed to connect!");
return false;
}
// discover peripheral attributes
if (!dev->discoverAttributes()) {
Serial.println("Attribute discovery failed!");
dev->disconnect();
return false;
}
BLEService service = dev->service("49535343-FE7D-4AE5-8FA9-9FAFD205E455");
if (!service) {
Serial.println("Peripheral does not have needed service!");
dev->disconnect();
return false;
}
_commandChar = service.characteristic("49535343-8841-43f4-a8d4-ecbe34729bb3");
if (!_commandChar) {
Serial.println("Peripheral does not have command characteristic!");
dev->disconnect();
return false;
} else if (!_commandChar.canWrite()) {
Serial.println("Peripheral does not have a writable command characteristic!");
dev->disconnect();
return false;
}
Serial.println("Found our command characteristic!");
if (!_commandChar.canSubscribe()) {
Serial.println("Peripheral command characteristic is not subscribable!");
} else {
if (_commandChar.subscribe()) {
Serial.println("command subscription failed!");
}
}
_readChar = service.characteristic("49535343-1e4d-4bd9-ba61-23c647249616");
if (!_readChar) {
Serial.println("Peripheral does not have read characteristic!");
dev->disconnect();
return false;
}
Serial.println("Found our read characteristic!");
if (_readChar.canWrite())
Serial.println("Peripheral does not have a writable read characteristic!");
if (!_readChar.canSubscribe()) {
Serial.println("Peripheral read characteristic is not subscribable!");
} else {
if (_readChar.subscribe()) {
Serial.println("read subscription failed!");
}
}
_peripheral = *dev;
return true;
}
bool lunarGateway::connected(){
if (_peripheral && _peripheral.connected())
return true;
return false;
}
void lunarGateway::getSettings(){
sendId();
uint8_t payload_msg[16] = {0};
uint8_t* prepared_msg = _prepareRequest(6,payload_msg, (size_t)sizeof(payload_msg));
_commandChar.writeValue(prepared_msg,sizeof(payload_msg)+5);
}
void lunarGateway::doTare(){
sendId();
uint8_t payload_msg[1] = {0};
uint8_t* prepared_msg = _prepareRequest(4,payload_msg, (size_t)sizeof(payload_msg));
_commandChar.writeValue(prepared_msg,sizeof(payload_msg)+5);
}
void lunarGateway::startTimer(){
sendId();
uint8_t payload_msg[2] = {0, 0};
uint8_t* prepared_msg = _prepareRequest(13,payload_msg, (size_t)sizeof(payload_msg));
_commandChar.writeValue(prepared_msg,sizeof(payload_msg)+5);
}
void lunarGateway::stopTimer(){
sendId();
uint8_t payload_msg[2] = {0,2};
uint8_t* prepared_msg = _prepareRequest(13,payload_msg, (size_t)sizeof(payload_msg));
_commandChar.writeValue(prepared_msg,sizeof(payload_msg)+5);
}
void lunarGateway::resetTimer(){
sendId();
uint8_t payload_msg[2] = {0,1};
uint8_t* prepared_msg = _prepareRequest(13,payload_msg, (size_t)sizeof(payload_msg));
_commandChar.writeValue(prepared_msg,sizeof(payload_msg)+5);
}
void lunarGateway::sendId(){
uint8_t id_array[21] = {0xef, 0xdd, 0x0b, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x9a, 0x6d, 0x00};
_commandChar.writeValue(id_array,21);
}
void lunarGateway::notificationRequest(){
sendId();
uint8_t id_array[14] = {0xef, 0xdd, 0x0c, 0x09, 0x00, 0x01, 0x01, 0x02, 0x02, 0x05, 0x03, 0x04, 0x15, 0x06 };
_commandChar.writeValue(id_array,14);
}
void lunarGateway::sendHeartBeat(){
if ((millis() - _lastHeartBeat) > 2750) {
notificationRequest();
sendId();
uint8_t payload_msg[2] = {2,0};
uint8_t* heartbeat_msg = _prepareRequest(0,payload_msg, (size_t)sizeof(payload_msg));
_commandChar.writeValue(heartbeat_msg, sizeof(payload_msg)+5);
_lastHeartBeat = millis();
}
}
uint8_t* lunarGateway::_prepareRequest(int msgtype,uint8_t * message, size_t size){
static uint8_t request[32];
request[0] = 0xef;
request[1] = 0xdd;
request[2] = msgtype;
uint8_t checksum1 = 0;
uint8_t checksum2 = 0;
uint8_t tempval = 0;
for (int i = 0; i < size; i++) {
tempval = message[i] & 0xff;
request[3+i] = tempval;
if (i % 2 == 0){
checksum1 += tempval;
}else{
checksum2 += tempval;
}
}
request[size + 3] = (checksum1 & 0xff);
request[size + 4] = (checksum2 & 0xff);
return request;
}
void lunarGateway::decodeMessage(uint8_t* message){
if(false){ // set to true for debug information
for (int i = 0; i < 32; i++){
char buf[9];
sprintf(buf, "%02x", message[i]);
Serial.print(buf);
Serial.print(" ");
}
Serial.print("\n");
}
int messageStart = -1;
int messageEnd = -1;
for (int i = 0; message[i]!=0 ; i++){
if(message[i] == 0xEF && message[i+1] == 0xDD){
messageStart=i;
break;
}
}
messageEnd = messageStart+message[messageStart+3]+5;
uint8_t msgTopic = message[messageStart+2];
uint8_t msgType = message[messageStart+4];
uint8_t* msgPayloadMessage = _substr(message, messageStart+5, messageEnd);
uint8_t* msgPayloadSettings = _substr(message, messageStart+4, messageEnd);
if(msgTopic == 0x08){
battery = msgPayloadSettings[0] & 0x7f;
}
if(msgTopic == 0x0C && msgType == 0x05){
int weight_int = ((msgPayloadMessage[1] & 0xff) << 8) + (msgPayloadMessage[0] & 0xff);
char weight_unit = msgPayloadMessage[4] & 0xff;
float weight_float = (float)weight_int;
if(weight_unit == 1){
weight_float /= 10.0;
} else if(weight_unit == 2){
weight_float /= 100.0;
} else if(weight_unit == 3){
weight_float /= 1000.0;
} else if(weight_unit == 4){
weight_float /= 10000.0;
}
if ((msgPayloadMessage[5] & 0x02) == 0x02){
weight_float *= -1;
}
weight = weight_float;
}
if (msgPayloadMessage)
delete(msgPayloadMessage);
if (msgPayloadSettings)
delete(msgPayloadSettings);
}
uint8_t* lunarGateway::_substr(const uint8_t* src, int start_index, int end_index) {
int length = end_index - start_index;
if (length < 0)
return NULL;
uint8_t* dest = new uint8_t[length + 1];
std::copy(src + start_index, src + end_index, dest);
dest[length] = '\0';
return dest;
}