forked from dmslabsbr/esphome-somfy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RFsomfy.h
422 lines (368 loc) · 10.7 KB
/
RFsomfy.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
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
#include "esphome.h"
using namespace esphome;
#include <SomfyRts.h>
#include <Ticker.h>
#include "FS.h"
// cmd 11 - program mode
// cmd 21 - delete rolling code file
// cmd 41 - List files
// cmd 51 - Test filesystem.
// cmd 61 - Format filesystem and test.
// cmd 71 - Show actual rolling code
// cmd 81 - Get all rolling code
// cmd 85 - Write new rolling codes
#define STATUS_LED_PIN D1
#define REMOTE_TX_PIN D0
#define REMOTE_FIRST_ADDR 0x121311 // <- Change remote name and remote code here!
#define REMOTE_COUNT 3 // <- Number of somfy blinds.
int xcode[REMOTE_COUNT];
uint16_t iCode[REMOTE_COUNT];
char const * string2char(String command) {
if(command.length()!=0){
char *p = const_cast<char*>(command.c_str());
return p;
}
return "";
}
String file_path(int remoteId) {
String path = "/data/remote/";
path += REMOTE_FIRST_ADDR + remoteId;
path += ".txt";
return path;
}
uint16_t getCodeFromFile(int remoteId) {
uint16_t code = 0;
SPIFFS.begin();
String arq = file_path(remoteId);
if (SPIFFS.exists(arq)) {
Serial.println("Reading config");
//ESP_LOGI("file", "Reading config");
ESP_LOGI("Arq", string2char(arq));
File f = SPIFFS.open(arq, "r");
if (f) {
String line = f.readStringUntil('\n');
code = line.toInt();
f.close();
}
else {
Serial.println("File open failed");
ESP_LOGI("file", "File open failed");
code = -1;
}
}
SPIFFS.end();
return code;
}
// get code from all files
void getCodeFromAllFiles() {
uint16_t code = 0;
ESP_LOGW("somfy", "* Get all rolling codes from files");
for (int i=0; i<REMOTE_COUNT; i++) {
code = getCodeFromFile(i);
ESP_LOGI("somfy", "Remoteid %d", REMOTE_FIRST_ADDR + i);
ESP_LOGI("file", "Code: %d", code);
xcode[i] = code;
}
}
void writeCode2file(int remoteId, uint16_t code) {
SPIFFS.begin();
Serial.println("Writing config");
String arq = file_path(remoteId);
File f = SPIFFS.open(arq, "w");
if (f) {
f.println(code);
f.close();
ESP_LOGI("somfy", "Writed code: %d", code);
}
else {
ESP_LOGW("somfy","File creation failed");
}
SPIFFS.end();
}
class RFsomfy : public Component, public Cover {
private:
int index;
Ticker ticker;
//protected:
// uint8_t width{8};
public:
int remoteId = -1;
unsigned char frame[7];
void set_code(const uint16_t code) {
// this->width = width;
iCode[remoteId] = code;
writeCode2file(remoteId, code);
xcode[remoteId] = code;
}
/*
void tickLed(void) {
//toggle state
int state = digitalRead(STATUS_LED_PIN);
digitalWrite(STATUS_LED_PIN, !state);
}
usar ticker.attach_ms(50, RFsomfy::tickLed);
*/
void readFile()
{
ESP_LOGW("info","Readfile");
Serial.println("reading");
File f = SPIFFS.open("/myFile.txt", "r");
if (!f) {
Serial.println("file not available");
ESP_LOGW("info","Readfile-File not avaliable");
} else if (f.available()<=0) {
ESP_LOGW("info","Readfile-File exist but not avaliable");
Serial.println("file exists but available <0");
}
else
{
ESP_LOGI("info","Readfile OK");
String ssidString = f.readStringUntil('#');
Serial.print("read from file: ");
Serial.println(ssidString);
}
f.close();
}
void writeFile()
{
ESP_LOGW("info","writefile");
Serial.println("writing");
File f = SPIFFS.open("/myFile.txt", "w");
if (!f) {
Serial.println("File creation failed");
ESP_LOGW("info","writefile-failed");
}
else
{
ESP_LOGW("info","Write_OK");
f.print("networkConfig");
f.print("#");
f.flush();
f.close();
}
}
void testFs() {
ESP_LOGW("tilt","Testing filesystem!");
Serial.begin(115200);
if (!SPIFFS.begin()) {
ESP_LOGW("tilt","error while mounting filesystem!");
Serial.println("error while mounting filesystem!");
} else {
readFile();
writeFile();
readFile();
Serial.println("done");
}
SPIFFS.end();
}
SomfyRts rtsDevices[REMOTE_COUNT] = {
SomfyRts(REMOTE_FIRST_ADDR),
SomfyRts(REMOTE_FIRST_ADDR + 1),
SomfyRts(REMOTE_FIRST_ADDR + 2)
};
RFsomfy(int rmx) : Cover() { //register
index = rmx;
remoteId = index;
ESP_LOGD("somfy", "Cover %d", index);
}
void setup() override {
// This will be called by App.setup()
ESP_LOGD("RFsomfy", "Starting Device");
Serial.begin(115200);
Serial.println("Initialize remote devices");
pinMode(STATUS_LED_PIN, OUTPUT);
digitalWrite(STATUS_LED_PIN, HIGH);
ESP_LOGD("RFsomfy","Somfy ESPHome Cover v0.12");
ESP_LOGD("RFsomfy","Initialize remote devices");
for (int i=0; i<REMOTE_COUNT; i++) {
rtsDevices[i].init();
Serial.println("i: " + String(i));
ESP_LOGD("somfy", "Initialize remote %d", REMOTE_FIRST_ADDR + i);
ESP_LOGD("somfy","file path:");
String fp = file_path(i);
Serial.print("Init remote: ");
Serial.println(fp);
xcode[i] = i;
}
digitalWrite(STATUS_LED_PIN, LOW);
// testFs();
getCodeFromAllFiles();
}
// delete rolling code . 0....n
void delete_code(int remoteId) {
SPIFFS.begin();
String path = file_path(remoteId);
Serial.println(path);
// SPIFFS.remove(path);
Serial.println("Deleted");
ESP_LOGD("RFsomfy","Deleted remote %i", remoteId);
SPIFFS.end();
}
CoverTraits get_traits() override {
auto traits = CoverTraits();
traits.set_is_assumed_state(false);
traits.set_supports_position(true);
traits.set_supports_tilt(true); // to send other commands
return traits;
}
void control(const CoverCall &call) override {
// This will be called every time the user requests a state change.
digitalWrite(STATUS_LED_PIN, HIGH);
delay(50);
ESP_LOGW("RFsomfy", "Using remote %d", REMOTE_FIRST_ADDR + index);
ESP_LOGW("RFsomfy", "Remoteid %d", remoteId);
ESP_LOGW("RFsomfy", "index %d", index);
Serial.print("remoteId: ");
Serial.println(remoteId);
if (call.get_position().has_value()) {
float pos = *call.get_position();
// Write pos (range 0-1) to cover
// ...
int ppos = pos * 100;
ESP_LOGD("RFsomfy", "get_position is: %d", ppos);
if (ppos == 0) {
ESP_LOGD("RFsomfy","POS 0");
Serial.println("* Command Down");
rtsDevices[remoteId].sendCommandDown();
pos = 0.01;
}
if (ppos == 100) {
ESP_LOGD("RFsomfy","POS 100");
Serial.println("* Command UP");
rtsDevices[remoteId].sendCommandUp();
pos = 0.99;
}
// Publish new state
this->position = pos;
this->publish_state();
}
if (call.get_stop()) {
// User requested cover stop
ESP_LOGD("RFsomfy","get_stop");
Serial.println("* Command STOP - ");
Serial.println(remoteId);
rtsDevices[remoteId].sendCommandStop();
}
if (call.get_tilt().has_value()) {
auto tpos = *call.get_tilt();
int xpos = tpos * 100;
ESP_LOGI("tilt", "Command tilt xpos: %d", xpos);
if (xpos == 11) {
ESP_LOGD("tilt","program mode");
digitalWrite(STATUS_LED_PIN, HIGH);
rtsDevices[remoteId].sendCommandProg();
delay(1000);
}
if (xpos == 21) {
ESP_LOGD("tilt","delete file");
digitalWrite(STATUS_LED_PIN, HIGH);
delete_code(remoteId);
delay(1000);
}
if (xpos == 41) {
ESP_LOGD("tilt","List Files");
String str = "";
SPIFFS.begin();
Dir dir = SPIFFS.openDir("/");
while (dir.next()) {
str += dir.fileName();
str += " / ";
str += dir.fileSize();
str += "\r\n";
}
Serial.print(str);
ESP_LOGD("files", string2char(str));
SPIFFS.end();
}
if (xpos == 51) {
ESP_LOGD("tilt","51 mode");
testFs();
}
if (xpos == 61) {
ESP_LOGD("tilt","61 mode");
SPIFFS.begin();
if (!SPIFFS.exists("/formatComplete.txt")) {
Serial.println("Please wait 30 secs for SPIFFS to be formatted");
ESP_LOGW("file", "Please wait 30 s");
SPIFFS.format();
delay(30000);
Serial.println("Spiffs formatted");
ESP_LOGW("file", "Spiffs formatted");
File f = SPIFFS.open("/formatComplete.txt", "w");
if (!f) {
Serial.println("file open failed");
ESP_LOGW("file", "file open failed");
} else {
f.println("Format Complete");
ESP_LOGW("file", "Format Complete");
}
} else {
Serial.println("SPIFFS is formatted. Moving along...");
ESP_LOGW("file", "SPIFFS is formatted. Moving along...");
}
SPIFFS.end();
}
if (xpos == 71) {
// get roling code from file
uint16_t code = 0;
code = getCodeFromFile(remoteId);
ESP_LOGI("file", "Code: %d", code);
xcode[remoteId] = code;
}
if (xpos == 81) {
// get all roling code from file
getCodeFromAllFiles();
}
if (xpos == 85) {
// Write new roling codes
for (int i=0; i<REMOTE_COUNT; i++) {
writeCode2file(REMOTE_FIRST_ADDR + i, iCode[i]);
}
}
/* Don't publish
this->tilt = tpos;
this->publish_state();
*/
}
digitalWrite(STATUS_LED_PIN, LOW);
delay(50);
}
};
class RFsomfyInfo : public PollingComponent, public TextSensor {
public:
// constructor
RFsomfyInfo() : PollingComponent(15000) {}
void setup() override {
// This will be called by App.setup()
}
void update() override {
// This will be called every "update_interval" milliseconds.
// Publish state
char tmp[REMOTE_COUNT * 100];
strcpy (tmp,"");
char str[5];
char str2[3];
String rem;
boolean bl_code {false};
for (int i=0; i<REMOTE_COUNT; i++) {
String rem = String(REMOTE_FIRST_ADDR + i, HEX);
/*
itoa(xcode[i],str,10);
itoa(i, str2, 10);
strcat(tmp, "(");
strcat(tmp, str2);
*/
char linha[100];
sprintf(linha, "\n ( %u - #%s) - code: %d / ", i, string2char(rem), xcode[i]);
strcat(tmp, linha);
if (iCode[i] != 0) {
bl_code = true;
ESP_LOGI("icode", "%u : icode: %d", i, iCode[i]);
}
}
publish_state(tmp);
if (bl_code) {
ESP_LOGW("set_code", "Atention! After set code, and it works, remove it from your YAML");
}
}
};