-
Notifications
You must be signed in to change notification settings - Fork 0
/
stoveserial.h
313 lines (297 loc) · 8.11 KB
/
stoveserial.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
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
#include "esphome.h"
using namespace esphome;
#define stoveStateAddr 0x21
#define ambTempAddr 0x01
#define fumesTempAddr 0x5A
#define flamePowerAddr 0x34
#define speedFanFumesAddr 0x37
#define cocleaSpeedAddr 0x0D
//#define intakeAirTempAddr 0x73
class StoveSensor : public Component, public UARTDevice, public CustomMQTTDevice {
public:
String topic;
StoveSensor(UARTComponent *parent,String l_topic) : UARTDevice(parent) {topic=l_topic;} //constructor
String mqtt_topic;
String pong_topic;
char char_pong_topic[50];
String dump_topic;
char char_dump_topic[50];
String in_topic;
char char_in_topic[50];
const char stoveOn[4] = {0x80, 0x21, 0x01, 0xA2};
const char stoveOff[4] = {0x80, 0x21, 0x06, 0xA7};
const char forceOff[4] = {0x80, 0x21, 0x00, 0xA1};
long previousMillis;
String stoveOnOff;
int stoveState, fumesTemp, flamePower,speedFanFumes,cocleaSpeed;
float ambTemp;
char stoveRxData[2]; //When the heater is sending data, it sends two bytes: a checksum and the value
bool g_isSerialWorking = false; //check is serial to the stove is in use
bool g_read_error=false; //used to check if there is an error in reading
int g_retry =0; //retry for getstate
int g_maxretry=3; //num retry max for getstate
unsigned long m_dly=100; //delay for publish mqtt message
void IRAM_ATTR fullReset() //Reset all the settings but without erasing the program
{
Serial.println("Resetting…");
ESP.restart();
}
void f_dump(const std::string payload)
{
dump_topic = mqtt_topic;
dump_topic += "/dump";
byte readByte = 0x00; //read RAM
int readlimit = 151;
if ("DUMP_EEPROM" == payload){ // check the payload , otherwise read RAM
readByte = 0x20;readlimit=128; //read EEPROM
dump_topic += "_eeprom";
}
dump_topic.toCharArray(char_dump_topic, 50);
uint8_t retry=0;
uint8_t maxretry = 5;
while (true == g_isSerialWorking && retry < maxretry)
{
delay (500);
retry++;
}
if (retry < maxretry)
{
g_isSerialWorking=true;
ESP_LOGV("VERB", "f_dump cicle...");
for (int shift = 0; shift < readlimit; shift++)
{
write(readByte);
delay(10);
write((byte)shift);
delay(50);
byte val = f_checkStoveReply((byte)shift,readByte);
if (false == g_read_error)
{
String value = String(shift);
value += "-";
value += String(val);
publish(char_dump_topic, value.c_str(), true);
}
}
g_isSerialWorking=false;
}
}
void f_getStates() //Calls all the get…() functions
{
int retry=0;
int maxretry = 5;
while (true == g_isSerialWorking && retry < maxretry)
{
delay (500);
retry++;
}
if (retry < maxretry)
{
g_isSerialWorking=true;
ESP_LOGV("INFO", "Getting States...");
f_getState(stoveStateAddr);
f_getState(ambTempAddr);
f_getState(fumesTempAddr);
f_getState(flamePowerAddr);
f_getState(speedFanFumesAddr);
f_getState(cocleaSpeedAddr);
//f_getState(intakeAirTempAddr);
g_isSerialWorking=false;
// publish JSON using lambda syntax
publish_json("micronova/json", [=](JsonObject root2) {
root2["stoveOnOff"] = stoveOnOff;
root2["stoveState"] = stoveState;
root2["ambTemp"] = ambTemp;
root2["fumesTemp"] = fumesTemp;
root2["speedFanFumes"] = speedFanFumes;
root2["flamePower"] = flamePower;
root2["cocleaSpeed"] = cocleaSpeed;
});
}
}
void f_getState(byte msgAddr) //Get detailed stove state RAM
{
const byte readByte = 0x00;
write(readByte);
delay(10);
write(msgAddr);
delay(50);
byte val = f_checkStoveReply(msgAddr,readByte);
if (false == g_read_error)
{
switch (msgAddr)
{
case stoveStateAddr:
stoveState = val;
if ((stoveState>0) & (stoveState<6)){stoveOnOff = "ON";}
else {stoveOnOff = "OFF";}
break;
case ambTempAddr:
ambTemp = (float)val / 2;
break;
case fumesTempAddr:
fumesTemp = val;
break;
case flamePowerAddr:
if ((stoveState < 6) && (stoveState > 0)){flamePower = val;}
else{flamePower = 0;}
break;
case speedFanFumesAddr:
if (val > 0){ speedFanFumes = (val *10) +250;}
else {speedFanFumes =0;}
break;
case cocleaSpeedAddr:
cocleaSpeed = val;
break;
}
}
else
{
if (g_retry < g_maxretry)
{
g_retry++;
f_getState(msgAddr);
}
}
g_retry=0;
}
byte f_checkStoveReply(byte msgAddr,byte location) // msgAddr fro RAM or EEPROM
{
g_read_error=false;
uint8_t rxCount = 0;
stoveRxData[0] = 0x00;
stoveRxData[1] = 0x00;
while (available()) //It has to be exactly 2 bytes, otherwise it's an error
{
stoveRxData[rxCount] = read();
rxCount++;
}
if (2 == rxCount)
{
byte val = stoveRxData[1];
byte checksum = stoveRxData[0];
byte param = checksum - val;
if (param != msgAddr+location)
{
g_read_error=true;
return 255;
}
else
{
g_read_error=false;
return val;
}
}
else {g_read_error=true;}
return 255;
}
void f_on_message(const std::string &payload) {
if ("ON" == payload)
{
for (int i = 0; i < 4; i++)
{
if (0 == stoveState || stoveState > 5)
{
write(stoveOn[i]);
delay(5);
}
}
delay(1000);
f_getStates();
}
else if ("OFF" == payload)
{
for (int i = 0; i < 4; i++)
{
if (stoveState < 6 && stoveState > 0)
{
write(stoveOff[i]);
delay(5);
}
}
delay(1000);
f_getStates();
}
else if ("DUMP_RAM" == payload || "DUMP_EEPROM" == payload)
{
f_dump(payload);
publish(char_dump_topic, "DUMP_END", true);
}
else if ( std::string::npos != payload.find("WRITE_",0))
{
int n = payload.length();
// declaring character array
char char_array[n];
// copying the contents of the string to char array
strcpy(char_array, payload.c_str());
char * pch;
pch = strtok(char_array, "_");
pch = strtok (NULL,"_");
char location[sizeof pch];
strcpy(location, pch); //get location byte, 0=RAM 1=EEPROM
pch = strtok (NULL,"-");
char address[sizeof pch];
strcpy(address, pch); //get address byte
pch = strtok (NULL,"-");
char value[sizeof pch];
strcpy(value, pch);//get value byte
uint8_t addr = atoi(address);
uint8_t val = atoi(value);
byte writeByte=0x80; //default write in RAM
dump_topic = mqtt_topic;
dump_topic += "/dump";
if ('1' == location[0])
{ dump_topic += "_eeprom";writeByte = 0xA0; }//write in EEPROM
dump_topic.toCharArray(char_dump_topic, 50);
byte sum = writeByte + (byte)addr + (byte)val;
char writeData[4];
writeData[0]=writeByte;
writeData[1]=addr;
writeData[2]=val;
writeData[3]=sum;
for (int i = 0; i < 4; i++)
{
write(writeData[i]);
delay(5);
}
delay(50);
byte ret = f_checkStoveReply((byte)addr,writeByte);
if (false == g_read_error)
{
String msg = String(addr);
msg += "-";
msg += String(ret);
publish(char_dump_topic, msg.c_str(), true);
}
ESP_LOGV("INTOPIC", "END_WRITE");
}
else if ("RESET" == payload)
{
fullReset();
}
}
void setup() override {
mqtt_topic = topic;
mqtt_topic.trim();
pong_topic += mqtt_topic;
in_topic += mqtt_topic;
pong_topic += "/pong";
in_topic += "/intopic";
pong_topic.toCharArray(char_pong_topic, 50);
in_topic.toCharArray(char_in_topic, 50);
subscribe(char_in_topic, &StoveSensor::f_on_message);
}
void loop() override {
unsigned long currentMillis = millis();
if (previousMillis > currentMillis)
{
previousMillis = 0;
}
if (currentMillis - previousMillis >= 5000)
{
previousMillis = currentMillis;
f_getStates();
publish(char_pong_topic, "Connected");
}
}
};