forked from tretyakovsa/Sonoff_WiFi_switch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAction.ino
212 lines (182 loc) · 7.25 KB
/
Action.ino
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
// ------------------- Инициализация Динамика
void initBuzer() {
configLive = jsonWrite(configLive, "pinBuzer", readArgsInt());
pinMode(jsonReadtoInt(configLive, "pinBuzer"), OUTPUT);
analogWrite(jsonReadtoInt(configLive, "pinBuzer"), readArgsInt());
analogWriteFreq(0);
}
void buzerBeep() {
analogWrite(jsonReadtoInt(configLive, "pinBuzer"), readArgsInt());
analogWriteFreq(readArgsInt());
}
// ------------------- Инициализация Реле
void initRelay() {
configLive = jsonWrite(configLive, "relay1Pin", readArgsInt());
pinMode(jsonReadtoInt(configLive, "relay1Pin"), OUTPUT);
sCmd.addCommand("relayon", relayOn);
sCmd.addCommand("relayoff", relayOff);
sCmd.addCommand("relaynot", relayNot);
HTTP.on("/relay", relay); // реакция на запрос
HTTP.on("/relayon", relayon); // реакция на запрос
HTTP.on("/relayoff", relayoff); // реакция на запрос
HTTP.on("/sonoff", relay); // реакция на запрос
modulesReg("relay");
}
void relay() {
sCmd.readStr("relaynot");
HTTP.send(200, "text/json", relayStatus(configJson, "state"));
}
void relayon() {
sCmd.readStr("relayon");
HTTP.send(200, "text/json", relayStatus(configJson, "state"));
}
void relayoff() {
sCmd.readStr("relayoff");
HTTP.send(200, "text/json", relayStatus(configJson, "state"));
}
// читает данные из раздела state строки json и возвращает строку для смены класса кнопки
String relayStatus(String json, String state) {
String out = "{}";
if (jsonReadtoInt(json, state)) {
out = jsonWrite(out, "title", "{{LangOff}}");
out = jsonWrite(out, "class", "btn btn-block btn-lg btn-info");
}
else {
out = jsonWrite(out, "title", "{{LangOn}}");
out = jsonWrite(out, "class", "btn btn-block btn-lg btn-primary");
}
return out;
}
void relayOn() {
configJson = jsonWrite(configJson, "state", 1);
int state0 = jsonReadtoInt(configJson, "state");
toggleRelay(state0);
digitalWrite(jsonReadtoInt(configLive, "relay1Pin"), state0);
topicPub("/RELE_1_not/status", String(state0), 1 );
}
void relayOff() {
configJson = jsonWrite(configJson, "state", 0);
int state0 = jsonReadtoInt(configJson, "state");
toggleRelay(state0);
digitalWrite(jsonReadtoInt(configLive, "relay1Pin"), state0);
topicPub("/RELE_1_not/status", String(state0), 1 );
}
void relayNot() {
String str = readArgsString();
if (str != "") Serial.println(timeToSec(str));
//configJson = jsonWrite(configJson, "mem", ESP.getFreeHeap());
configJson = jsonWrite(configJson, "state", !jsonReadtoInt(configJson, "state"));
int state0 = jsonReadtoInt(configJson, "state");
toggleRelay(state0);
digitalWrite(jsonReadtoInt(configLive, "relay1Pin"), state0);
topicPub("/RELE_1_not/status", String(state0), 1 );
}
void topicPub(String topic, String data, boolean retain ) {
client.publish(MQTT::Publish(prefix + "/" + chipID + topic, "{\"status\":" + data + "}").set_retain(1));
}
// -------------- Для управления реле по UART
// https://www.banggood.com/ru/ESP8266-5V-WiFi-Relay-Module-Internet-Of-Things-Smart-Home-Phone-APP-Remote-Control-Switch-p-1126605.html?rmmds=category
void toggleRelay(bool relayState) {
if (relayState) {
const byte miBufferON[] = {0xA0, 0x01, 0x01, 0xA2};
Serial.write(miBufferON, sizeof(miBufferON));
}
else {
//To disable the Relay send it by serial port:
const byte miBufferOFF[] = {0xA0, 0x01, 0x00, 0xA1};
Serial.write(miBufferOFF, sizeof(miBufferOFF));
}
}
// ---------------- Управление двигателем жалюзи
void initJalousie() {
// Сенсор будет работать по прерыванию
int pinTurn = readArgsInt();
attachInterrupt(pinTurn, turn_0, FALLING );
configLive = jsonWrite(configLive, "pinTurn", pinTurn);
int pinMotor1 = readArgsInt();
int pinMotor2 = readArgsInt();
configLive = jsonWrite(configLive, "pinMotor1", pinMotor1);
configLive = jsonWrite(configLive, "pinMotor2", pinMotor2);
configJson = jsonWrite(configJson, "stateJalousie", 1);
pinMode(pinMotor1, OUTPUT);
pinMode(pinMotor2, OUTPUT);
digitalWrite(pinMotor1, LOW);
digitalWrite(pinMotor2, LOW);
sCmd.addCommand("jalousieopen", jalousieOpen);
sCmd.addCommand("jalousieclose", jalousieClose);
sCmd.addCommand("jalousienot", jalousieNot);
sCmd.addCommand("jalousiestop", jalousieStop);
sCmd.addCommand("jalousieturn", jalousieTurn);
modulesReg("jalousie");
}
// Выполняется при вращение сенсора
void turn_0() {
static unsigned long millis_prev;
// Устроняем дребезг контакта
if (millis() - 500 > millis_prev) {
int turnSensor = jsonReadtoInt(configLive, "turnSensor");
turnSensor++; // счетчик количества оборотов
configLive = jsonWrite(configLive, "turnSensor", turnSensor);
int turn = jsonReadtoInt(configJson, "turn");
if (turnSensor == turn) { //Останавливаем
configLive = jsonWrite(configLive, "turnSensor", 0);
command = "jalousiestop";
}
}
millis_prev = millis();
}
void jalousieOpen() {
int state0 = jsonReadtoInt(configJson, "stateJalousie");
if (!state0) {
configJson = jsonWrite(configJson, "stateJalousie", 1);
state0 = !state0;
digitalWrite(jsonReadtoInt(configLive, "pinMotor2"), HIGH);
digitalWrite(jsonReadtoInt(configLive, "pinMotor1"), LOW);
topicPub("/jalousie/status", String(state0), 1 );
}
}
void jalousieClose() {
int state0 = jsonReadtoInt(configJson, "stateJalousie");
if (state0) {
configJson = jsonWrite(configJson, "stateJalousie", 0);
state0 = !state0;
digitalWrite(jsonReadtoInt(configLive, "pinMotor1"), HIGH);
digitalWrite(jsonReadtoInt(configLive, "pinMotor2"), LOW);
topicPub("/jalousie/status", String(state0), 1 );
}
}
void jalousieStop() {
digitalWrite(jsonReadtoInt(configLive, "pinMotor2"), LOW);
digitalWrite(jsonReadtoInt(configLive, "pinMotor1"), LOW);
}
void jalousieTurn() {
configJson = jsonWrite(configJson, "turn", readArgsInt());
writeFile("config.save.json", configJson );
}
void jalousieNot() {
//configJson = jsonWrite(configJson, "mem", ESP.getFreeHeap());
configJson = jsonWrite(configJson, "stateJalousie", !jsonReadtoInt(configJson, "stateJalousie"));
int state0 = jsonReadtoInt(configJson, "stateJalousie");
if (state0) {
digitalWrite(jsonReadtoInt(configLive, "pinMotor2"), HIGH);
digitalWrite(jsonReadtoInt(configLive, "pinMotor1"), LOW);
}
else {
digitalWrite(jsonReadtoInt(configLive, "pinMotor1"), HIGH);
digitalWrite(jsonReadtoInt(configLive, "pinMotor2"), LOW);
}
topicPub("/jalousie/status", String(state0), 1 );
}
// читает данные из раздела state строки json и возвращает строку для смены класса кнопки
String jalousieStatus(String json, String state) {
String out = "{}";
if (jsonReadtoInt(json, state)) {
out = jsonWrite(out, "title", "{{LangClose}}");
out = jsonWrite(out, "class", "btn btn-block btn-lg btn-info");
}
else {
out = jsonWrite(out, "title", "{{LangOpen}}");
out = jsonWrite(out, "class", "btn btn-block btn-lg btn-primary");
}
return out;
}