-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWisol.cpp
executable file
·142 lines (126 loc) · 2.76 KB
/
Wisol.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
/**
* Wisol.h - Library for interfaciong with Wisol WSSFM1xR2D Module.
* Created by lgarciaos, February 9, 2020.
*/
#include "Arduino.h"
#include "Wisol.h"
Wisol::Wisol(HardwareSerial * serial,int sleep_pin){
_serial= serial;
_sleep_pin= sleep_pin;
_id="";
_pac="";
}
void Wisol::begin(){
_serial->begin(9600); //9600 is a fixed baudrate for wisol modules
_delay_ms(200);
_serial->println(CMD_CB);
_delay_ms(200);
read();
_serial->println(CMD_TR3); //setting repetition to 3
_delay_ms(200);
read();
}
String Wisol::read(){
char inData[25];
char rx_buffer;
byte index = 0;
_delay_ms(200);
if (_serial->available() > 0) {
while (_serial->available() > 0) {
rx_buffer = _serial->read();
inData[index] = rx_buffer;
index++;
}
inData[index] = '\0';
}
return inData;
}
String Wisol::get_id(){
if(_id.equals("")){
read(); //clean buffer
_serial->println(CMD_AT);
_delay_ms(100);
read(); //should return 'OK'
_serial->println(CMD_ID);
_delay_ms(100);
_id= read();
}
return _id;
}
String Wisol::get_pac(){
if(_pac.equals("")){
read(); //clean buffer
_serial->println(CMD_AT);
_delay_ms(100);
read(); //should return 'OK'
_serial->println(CMD_PAC);
_delay_ms(100);
_pac= read();
}
return _pac;
}
String Wisol::get_temp(){
read(); //clean buffer
_serial->println(CMD_AT);
_delay_ms(100);
read(); //should return 'OK'
_serial->println(CMD_TEMP);
_delay_ms(100);
return read();
}
void Wisol::send(String msg){ // takes ~4800ms BEWARE
String wisolResponse = "";
read(); //clean buffer
_serial->printf(CMD_AT);
_delay_ms(200);
wisolResponse = read();
_serial->println(CMD_GI);
_delay_ms(200);
wisolResponse = read();
if (int(wisolResponse[0]) == 48 || int(wisolResponse[2]) < 51) {
_serial->println(CMD_RC);
_delay_ms(200);
wisolResponse = read();
}
_serial->println(CMD_WRITE+msg);
//_serial.println(",1"); // uncomment if expecting downlink response
_delay_ms(4000);
wisolResponse = read();
Serial.println(wisolResponse);
_serial->println(CMD_AT);
_delay_ms(100);
wisolResponse = read();
}
void Wisol::sleep(){
read(); //clean buffer
_serial->println(CMD_AT);
_delay_ms(100);
read();
_serial->println(CMD_SLEEP);
read();
}
void Wisol::deep_sleep(){
read(); //clean buffer
_serial->println(CMD_AT);
_delay_ms(100);
read();
_serial->println(CMD_DEEP_SLEEP);
}
void Wisol::wakeup_sleep(){
digitalWrite(_sleep_pin,LOW);
_delay_ms(250);
digitalWrite(_sleep_pin,HIGH);
_serial->println(CMD_AT);
_delay_ms(100);
read();
}
void Wisol::wakeup_deep_sleep(){
_serial->println(CMD_AT);
_delay_ms(100);
_serial->println(CMD_AT);
_delay_ms(100);
read();
}
void Wisol::close(){
_serial->end();
}