-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFRobot_OBLOQ.cpp
559 lines (479 loc) · 13.7 KB
/
DFRobot_OBLOQ.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*
MIT License
Copyright (C) <2019> <@DFRobot zhixin.liu>
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so.
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "DFRobot_OBLOQ.h"
#include <Arduino.h>
#include <Wire.h>
DFRobot_OBLOQ::DFRobot_OBLOQ()
{
this->_addr = 0x16; //OBLOQ Device number fixed to 0x16
this->_addr = 0x16; //OBLOQ Device number fixed to 0x16
}
DFRobot_OBLOQ::~DFRobot_OBLOQ()
{
}
/* Join the I2C bus (master address fixed) */
bool DFRobot_OBLOQ::begin()
{
Wire.begin(); // connecting the i2c bus
Wire.beginTransmission(_addr);
if(Wire.endTransmission() == 0)
return true;
return false;
}
/* WiFi initialization ,Set the wifi password and connect to the wifi */
uint8_t DFRobot_OBLOQ::wifiBegin(uint8_t *Name, uint8_t *pwd)
{
if(_wifiName != Name) {
_wifiName = Name;
manageFunction(Name, OB_SET_COMMAND ,SET_WIFI_NAME);
/* Commands are written to registers with data */
manageFunction(pwd, OB_SET_COMMAND, SET_WIFI_PAD);
connectWifi();
}
return readWifiState();
}
/* Set up the server, port, id,pwd of mqtt and connect to mqtt */
uint8_t DFRobot_OBLOQ::mqttBegin(uint8_t *Server, uint8_t *port ,uint8_t *id, uint8_t *pwd)
{
if(_mqttID != id) {
_mqttID = id;
manageFunction(Server, OB_SET_COMMAND, SET_MQTT_SERVER);
manageFunction(port, OB_SET_COMMAND, SET_MQTT_PORT);
manageFunction(id, OB_SET_COMMAND, SET_MQTT_ID);
manageFunction(pwd, OB_SET_COMMAND, SET_MQTT_PWD);
connectMqtt();
}
return readMqttState();
}
/* set the port and address for http */
bool DFRobot_OBLOQ::httpBegin(uint8_t *ip, uint8_t *port)
{
if((!ip) || (!port))
return false;
manageFunction(ip, OB_SET_COMMAND, SET_HTTP_ID);
manageFunction(port, OB_SET_COMMAND, SET_HTTP_PORT);
return true;
}
/* Send ping command */
void DFRobot_OBLOQ::sendPing()
{
manageFunctionShort(OB_RUN_COMMAND ,SEND_PING);
}
/* connect wifi */
void DFRobot_OBLOQ::connectWifi()
{
manageFunctionShort(OB_RUN_COMMAND ,CONNECT_WIFI);
}
/* reconnect wifi */
void DFRobot_OBLOQ::reConnectWifi()
{
manageFunctionShort(OB_RUN_COMMAND ,RECONNECT_WIFI);
while(1) {
if(readWifiState() == 1)
break;
}
_wifiState = MQTT_SUCCESS_CON;
}
/* Disconnect wifi connection */
bool DFRobot_OBLOQ::disConnectWifi()
{
manageFunctionShort(OB_RUN_COMMAND ,BREAK_WIFI_CON);
while(1){
if(readWifiState() == 1)
break;
}
_wifiState = WIFI_NO_CON;
return true;
}
/* connect Mqtt */
void DFRobot_OBLOQ::connectMqtt()
{
manageFunctionShort(OB_RUN_COMMAND ,CONNECT_MQTT);
}
/* Subscribe to topic ,Up to 5 topic */
uint8_t DFRobot_OBLOQ::subscribe(uint8_t *Topic)
{
static uint8_t num = 0;
if(num > 5)
return 6;
if(_Topic != Topic) {
_Topic = Topic;
for(int i = 0; i < 5; i++) {
if(_topicName[i] == Topic)
return 7;
}
_topicName[num] = Topic;
switch(num) {
case 0:
manageFunction(Topic, OB_RUN_COMMAND, SUBSCRIBE_TOPIC0);
num++;
break;
case 1:
manageFunction(Topic, OB_RUN_COMMAND, SUBSCRIBE_TOPIC1);
num++;
break;
case 2:
manageFunction(Topic, OB_RUN_COMMAND, SUBSCRIBE_TOPIC2);
num++;
break;
case 3:
manageFunction(Topic, OB_RUN_COMMAND, SUBSCRIBE_TOPIC3);
num++;
break;
case 4:
manageFunction(Topic, OB_RUN_COMMAND, SUBSCRIBE_TOPIC4);
num++;
break;
default:
return RETURN_NONE;
}
}
return readTakeState();
}
/* Send message to topic ,Up to 5 topic can be sent at the same time */
bool DFRobot_OBLOQ::publish(String Data, String Topic)
{
/* Determine whether the user has not passed a value */
if((!Data) || (!Topic))
return false;
uint8_t num = 254;
for(int i = 0; i < 5; i++) {
if(_topicName[i] == Topic){
num = i;
break;
}
}
switch(num){
case 0:
manageFunction((uint8_t* )Data.c_str(), OB_RUN_COMMAND, SEND_TOPIC0);
break;
case 1:
manageFunction((uint8_t* )Data.c_str(), OB_RUN_COMMAND, SEND_TOPIC1);
break;
case 2:
manageFunction((uint8_t* )Data.c_str(), OB_RUN_COMMAND, SEND_TOPIC2);
break;
case 3:
manageFunction((uint8_t* )Data.c_str(), OB_RUN_COMMAND, SEND_TOPIC3);
break;
case 4:
manageFunction((uint8_t* )Data.c_str(), OB_RUN_COMMAND, SEND_TOPIC4);
break;
default:
return false;
}
return true;
}
/* http get url */
String DFRobot_OBLOQ::httpGet(uint8_t *GetUrl)
{
char recvHttpData[25] = {0x00};
manageFunction(GetUrl, OB_RUN_COMMAND, HTTP_GET_URL);
httpReturn(&recvHttpData[0]);
String data = recvHttpData;
return data;
}
/* http post url */
String DFRobot_OBLOQ::httpPost(String PostUrl, String Data)
{
char recvHttpData[25] = {0x00};
String pData;
pData = PostUrl;
pData += ',';
pData += Data;
manageFunction((uint8_t*)pData.c_str(), OB_RUN_COMMAND, HTTP_POST_URL_CON);
httpReturn(&recvHttpData[0]);
String data = recvHttpData;
return data;
}
/* http put url */
String DFRobot_OBLOQ::httpPut(uint8_t *PutUrl)
{
char recvHttpData[25] = {0x00};
manageFunction(PutUrl, OB_RUN_COMMAND, HTTP_PUT_URL_CON);
httpReturn(&recvHttpData[0]);
String data = recvHttpData;
return data;
}
/* Read ping state */
uint8_t DFRobot_OBLOQ::pingState()
{
uint8_t len = 2;
uint8_t buffer[len] = {0x00};
readRegister(OB_COMMAND_REGISTER, &buffer[0], len);
if(buffer[0] == PING_STATUE){
if(buffer[1] == PING_SUCCESS){
return 0;
}else{
return 1;
}
}else{
return 5;
}
}
/* Read wifi connect state */
uint8_t DFRobot_OBLOQ::readWifiState()
{
uint8_t len = 2;
uint8_t buffer[len] = {0x00};
readRegister(OB_COMMAND_REGISTER, &buffer[0], len);
if(buffer[0] == WIFI_STATUE_CON){
if(buffer[1] == WIFI_SUCCESS_CON){ //wifi connect
readWifiIp();
_wifiState = WIFI_SUCCESS_CON;
return 0;
}else if(buffer[1] == WIFI_NO_CON){ //wifi connect false
_wifiState = WIFI_NO_CON;
return 1;
}else if(buffer[1] == WIFI_RUN_CON){ //Connecting to the WiFi
_wifiState = WIFI_RUN_CON;
return 2;
}
}else
return 5;
}
/* Read the IP address of the wifi. */
void DFRobot_OBLOQ::readWifiIp()
{
char ipdata[16] = {0x00};
if(readRegisterData(&ipdata[0],IP_ADDRESS)) //Normal return wifi ip
_wifiIP = ipdata;
}
/* Get the IP address of the wifi. */
String DFRobot_OBLOQ::getWifiIp()
{
return _wifiIP;
}
/* Get hardware version */
String DFRobot_OBLOQ::getVersion()
{
char getVersionData[5] = {0x00};
manageFunctionShort(OB_RUN_COMMAND ,QUERY_VERSION);
delay(10);
readVersion(&getVersionData[0]);
delay(10);
String Version = getVersionData;
return Version;
}
/* Get Wifi State */
uint8_t DFRobot_OBLOQ::getWifiState()
{
return _wifiState;
}
/* Read wifi connect state */
uint8_t DFRobot_OBLOQ::readMqttState()
{
uint8_t len = 2;
uint8_t buffer[len] = {0x00};
readRegister(OB_COMMAND_REGISTER, &buffer[0], len);
if(buffer[0] == MQTT_STATUE_CON){
if(buffer[1] == MQTT_SUCCESS_CON){
return 0;
}else if(buffer[1] == MQTT_ERROR_CON){
return 1;
}else if(buffer[1] == MQTT_START){
return 2;
}
}else if((buffer[0] == NONE)){ //Normal state
return RETURN_NONE;
}else{
return 5; //other state
}
}
/* read Topic Subscription Status */
uint8_t DFRobot_OBLOQ::readTakeState()
{
uint8_t len = 2;
uint8_t buffer[len] = {0x00};
readRegister(OB_COMMAND_REGISTER, &buffer[0], len);
if(buffer[0] == SUBSCRIBE_STATUE){
/* Subscription Status */
if(buffer[1] == TAKE_SUCCESS){
return 0;
}else if(buffer[1] == TAKE_ERROR){
return 1;
}else if(buffer[1] == TAKE_UPPER_LIMIT){
return 2;
}else if(buffer[1] == TAKE_START){
return 3;
}
}else if((buffer[0] == NONE)){
return RETURN_NONE;
}else{
return 5;
}
}
/* Http return data */
void DFRobot_OBLOQ::httpReturn(char * Data)
{
readRegisterData(&Data[0], HTTP_NORMAL_RETURN, HTTP_ERROR_RETURN);
}
/* Read the hardware version */
void DFRobot_OBLOQ::readVersion(char * getVersionData)
{
readRegisterData(&getVersionData[0], GET_VERSION);
}
/* Command write register */
void DFRobot_OBLOQ::manageFunctionShort(uint8_t config, uint8_t command)
{
uint8_t buf[1];
buf[0] = config;
buf[1] = command;
I2CWriteData(OB_COMMAND_REGISTER, &buf[0], 2);
}
/* Commands are written to registers with data */
void DFRobot_OBLOQ::manageFunction(uint8_t *data, uint8_t command, uint8_t config)
{
uint8_t i = 0, j = 0;
while(data[i]) //Get data length
i++;
uint8_t datalen = i + 3; //overall length
uint8_t buf[datalen] = {0x00};
buf[0] = command;
buf[1] = config;
buf[2] = datalen;
for(i = 3; i < datalen;)
buf[i++] = data[j++];
I2CWriteData(OB_COMMAND_REGISTER, &buf[0], datalen);
}
/* Write data to an I2C register */
void DFRobot_OBLOQ::I2CWriteData(uint8_t Reg, uint8_t *pdata, uint8_t datalen)
{
Wire.beginTransmission(_addr); // transmit to device #8
Wire.write(Reg); // sends one byte
for (uint8_t i = 0; i < datalen; i++)
Wire.write(pdata[i]);
Wire.endTransmission(); // stop transmitting
}
/* Normal reading of I2c register data */
void DFRobot_OBLOQ::readRegister(uint8_t registaddress, uint8_t* buf, uint8_t len)
{
uint8_t i = 0;
Wire.beginTransmission(_addr); // transmit to device Address
Wire.write(registaddress); // sends one byte
Wire.write(OB_READ_COMMAND);
Wire.write(READ_COMMAND);
Wire.endTransmission(); // stop transmitting
Wire.requestFrom(_addr, len);
while (Wire.available()) // slave may send less than requested
buf[i++] = Wire.read();
}
/* Read twice, the first read length len, reads the len size data the second time */
bool DFRobot_OBLOQ::readRegisterData(uint8_t * buf, uint8_t config, uint8_t config1)
{
uint8_t i = 0, j = 0;
uint8_t buffer[2] = {0x00};
/* Get data length */
Wire.beginTransmission(_addr);
Wire.write(OB_COMMAND_REGISTER);
Wire.write(OB_READ_COMMAND);
Wire.write(READ_COMMAND);
Wire.endTransmission();
Wire.requestFrom(_addr, (uint8_t)2);
while(Wire.available()){
buffer[i++] = Wire.read();
}
/* Get the data length and re-get the data */
if(buffer[0] == config){
Wire.beginTransmission(_addr);
Wire.write(READ_DATA_REGISTER);
Wire.endTransmission();
Wire.requestFrom(_addr, buffer[1]);
while(Wire.available()){ //data
buf[j++] = Wire.read();
}
buf[j] = '\0';
return true;
}else if (buffer[0] == config1){ //http error number
buf[0] = buffer[1];
return false;
}else{
return false;
}
}
/* Read the data returned by topic */
void DFRobot_OBLOQ::readData(uint8_t * data ,uint8_t len)
{
uint8_t j = 0;
Wire.beginTransmission(_addr);
Wire.write(READ_DATA_REGISTER);
Wire.endTransmission();
Wire.requestFrom(_addr, len);
while(Wire.available()) //data
data[j++] = Wire.read();
data[j] = '\0';
}
/* Set callback function */
void DFRobot_OBLOQ::setCallback(void (*call)(char *, uint8_t*, uint8_t))
{
this->callback = call;
}
/* Loop Read State */
void DFRobot_OBLOQ::loop()
{
uint8_t i = 0;
uint8_t buffer[2] = {0,0};
/* Get data length */
Wire.beginTransmission(_addr);
Wire.write(OB_COMMAND_REGISTER);
Wire.write(OB_READ_COMMAND);
Wire.write(READ_COMMAND);
Wire.endTransmission();
Wire.requestFrom(_addr, (uint8_t)2);
while(Wire.available()){
buffer[i++] = Wire.read();
}
/* state */
if(buffer[0] == NONE){
}else if(buffer[0] == PING_STATUE){
}else if(buffer[0] == WIFI_STATUE_CON){
_wifiState = buffer[1];
}else if(buffer[0] == IP_ADDRESS){
}else if(buffer[0] == MQTT_STATUE_CON){
}else if(buffer[0] == SUBSCRIBE_STATUE){
}else if(buffer[0] == RECV_TOPIC0){
uint8_t data[buffer[1]];
readData(&data[0], buffer[1]);
if(callback)
callback((char *)(_topicName[0].c_str()),&data[0],buffer[1]);
}else if(buffer[0] == RECV_TOPIC1){
uint8_t data[buffer[1]];
readData(&data[0], buffer[1]);
if(callback)
callback((char *)(_topicName[1].c_str()),&data[0],buffer[1]);
}else if(buffer[0] == RECV_TOPIC2){
uint8_t data[buffer[1]];
readData(&data[0], buffer[1]);
if(callback)
callback((char *)(_topicName[2].c_str()),&data[0],buffer[1]);
}else if(buffer[0] == RECV_TOPIC3){
uint8_t data[buffer[1]];
readData(&data[0], buffer[1]);
if(callback)
callback((char *)(_topicName[3].c_str()),&data[0],buffer[1]);
}else if(buffer[0] == RECV_TOPIC4){
uint8_t data[buffer[1]];
readData(&data[0], buffer[1]);
if(callback)
callback((char *)(_topicName[4].c_str()),&data[0],buffer[1]);
}else if(buffer[0] == HTTP_NORMAL_RETURN){
}else if(buffer[0] == HTTP_ERROR_RETURN){
}else if(buffer[0] == GET_VERSION){
}else{
return;
}
}