-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.ino
213 lines (185 loc) · 6.07 KB
/
func.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
212
213
// core functions
bool getDeviceConfiguration(bool first) {
WiFiClient wifi;
HTTPClient http;
http.begin(wifi, String(HTTP_API_SERVER) + "init");
http.setUserAgent(deviceName);
http.setTimeout(6000);
http.addHeader("Content-Type", "application/json");
JsonDocument jsonRequestData;
jsonRequestData["mac"] = String(WiFi.macAddress());
jsonRequestData["deviceId"] = getSensorID();
jsonRequestData["token"] = TOKEN;
jsonRequestData["revision"] = String(DEVICE_REVISION);
jsonRequestData["model"] = String(DEVICE_MODEL);
jsonRequestData["firmware"] = String(DEVICE_FIRMWARE);
jsonRequestData["ip"] = WiFi.localIP().toString();
jsonRequestData["ssid"] = String(WiFi.SSID());
jsonRequestData["rssi"] = String(ESP.getVcc());
jsonRequestData["noAutoUpdate"] = NO_AUTO_UPDATE;
jsonRequestData["chipTest"] = CHIP_TEST;
String jsonRequest;
serializeJson(jsonRequestData, jsonRequest);
int httpCode = http.POST(jsonRequest);
if (!first && httpCode < 0) {
NO_SERVER = true;
return false;
} else {
if (httpCode != HTTP_CODE_OK && httpCode != HTTP_CODE_CREATED && !CHIP_TEST) {
ticker1.attach_ms(200, tickInternal);
ticker2.attach_ms(500, tickExternal, MAIN_MODE_OFFLINE);
Serial.println("Не могу инициализировать устройство в БольшоеАпи.ру");
Serial.println(httpCode);
delay(15000);
ESP.restart();
return false;
}
}
Serial.println("Get device configuration from server, HTTP Code: " + String(httpCode));
if (httpCode == HTTP_CODE_OK) {
NO_SERVER = false;
} else {
return false;
}
String jsonResponse = http.getString();
JsonDocument jsonResponseData;
deserializeJson(jsonResponseData, jsonResponse);
http.end();
int serverTime = jsonResponseData["time"].as<int>();
Serial.println(serverTime);
struct timeval tv;
tv.tv_sec = serverTime;
settimeofday(&tv, NULL); // Применяем время сервера, как локальное, так как у нас нет RTC
if (TOKEN != jsonResponseData["token"].as<String>()) {
TOKEN = jsonResponseData["token"].as<String>();
writeCfgFile("token", TOKEN);
Serial.println("Device token was updated in store");
}
if (jsonResponseData["state1"]) {
int LED_BRIGHT_NEW = jsonResponseData["state1"].as<int>();
if (LED_BRIGHT != LED_BRIGHT_NEW) {
LED_BRIGHT = LED_BRIGHT_NEW;
writeCfgFile("led_bright", jsonResponseData["state1"].as<String>());
Serial.println("Led brightness was updated in store: " + LED_BRIGHT);
}
}
if (jsonResponseData["state2"]) {
String MONITOR_SLUG_NEW = jsonResponseData["state2"].as<String>();
if (MONITOR_SLUG != MONITOR_SLUG_NEW) {
MONITOR_SLUG = MONITOR_SLUG_NEW;
writeCfgFile("monitor_slug", jsonResponseData["state2"].as<String>());
Serial.println("Monitor id was updated in store: " + MONITOR_SLUG);
}
}
if (jsonResponseData["state3"]) {
int MONITOR_INTERVAL_NEW = jsonResponseData["state3"].as<int>();
if (MONITOR_INTERVAL != MONITOR_INTERVAL_NEW) {
MONITOR_INTERVAL = MONITOR_INTERVAL_NEW;
writeCfgFile("monitor_interval", jsonResponseData["state3"].as<String>());
Serial.println("Monitor interval was updated in store: " + MONITOR_INTERVAL);
}
}
if (jsonResponseData["state4"]) {
String MONITOR_NAME_NEW = jsonResponseData["state4"].as<String>();
if (MONITOR_NAME != MONITOR_NAME_NEW) {
MONITOR_NAME = MONITOR_NAME_NEW;
writeCfgFile("monitor_name", jsonResponseData["state4"].as<String>());
Serial.println("Monitor name was updated in store: " + MONITOR_NAME);
}
}
// if (jsonResponseData["state5"].as<int>()) {
//Serial.println(jsonResponseData["state5"].as<String>());
//if (jsonResponseData["state5"].as<String>() != "null") {
// int SENS_INTERVAL_NEW = jsonResponseData["state5"].as<int>();
// if (SENS_INTERVAL != SENS_INTERVAL_NEW) {
// SENS_INTERVAL = SENS_INTERVAL_NEW;
// writeCfgFile("sensor_interval", jsonResponseData["state5"].as<String>());
// Serial.println("Sensor interval was updated in store: " + SENS_INTERVAL);
// }
//}
// }
return true;
}
// https://stackoverflow.com/questions/9072320/split-string-into-string-array
String getValue(String data, char separator, int index) {
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i + 1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
// // //
float decimalRound(float input, int decimals)
{
float scale=pow(10, decimals);
return round(input * scale) / scale;
}
//////
void setLedLen(int distance, int targetDistance) {
if (distance > targetDistance) {
distance = targetDistance;
} else if (distance < 25) {
Serial.println("Too low distance");
return;
}
int ledLen = distance / 25;
int flc = 0;
int brightMode = 1; // 15 - distance / 400;
mtrx.setBright(brightMode);
mtrx.clear();
mtrx.update();
Serial.println(brightMode);
Serial.println(distance);
Serial.println(ledLen);
int lastRow = 0;
int lastCol = 0;
bool reverse = false;
for (int r = 0; r < 32; r++) {
if (reverse) {
for (int i = 7; i >= 0; i--) {
mtrx.dot(r, i);
lastRow = r;
lastCol = i;
flc++;
if (flc == ledLen) {
break;
}
}
reverse = false;
} else {
for (int i = 0; i < 8; i++) {
mtrx.dot(r, i);
lastRow = r;
lastCol = i;
flc++;
if (flc == ledLen) {
break;
}
}
reverse = true;
}
if (flc == ledLen) {
break;
}
}
mtrx.update();
flashDot(lastRow, lastCol, reverse);
}
void flashDot(int r, int c, bool reverse) {
int nextRow = r;
int nextCol = c;
for (int i = 0; i < 3; i++) {
mtrx.dot(nextRow, nextCol, 1);
mtrx.update();
delay(500);
mtrx.dot(nextRow, nextCol, 0);
mtrx.update();
delay(500);
}
}