Browse Source

refactor code

plus_tomsk_weather
Vladimir Smagin 2 months ago
parent
commit
9a6800db5d
1 changed files with 135 additions and 94 deletions
  1. 135
    94
      tm1640-ntp.ino

+ 135
- 94
tm1640-ntp.ino View File

@@ -54,7 +54,8 @@ int outside_termo = 0;
54 54
 //////// CALLBACK FUNCTIONS ////////
55 55
 
56 56
 //callback config mode
57
-void configModeCallback (WiFiManager *myWiFiManager) {
57
+void configModeCallback(WiFiManager *myWiFiManager)
58
+{
58 59
   Serial.println("Entered config mode");
59 60
   Serial.println(WiFi.softAPIP());
60 61
   //if you used auto generated SSID, print it
@@ -62,23 +63,29 @@ void configModeCallback (WiFiManager *myWiFiManager) {
62 63
 }
63 64
 
64 65
 //callback notifying us of the need to save config
65
-void saveConfigCallback () {
66
+void saveConfigCallback()
67
+{
66 68
   Serial.println("Should save config");
67 69
   shouldSaveConfig = true;
68 70
 }
69 71
 
70
-void processSyncEvent(NTPSyncEvent_t ntpEvent) {
71
-  if (ntpEvent) {
72
+void processSyncEvent(NTPSyncEvent_t ntpEvent)
73
+{
74
+  if (ntpEvent)
75
+  {
72 76
     ntp_online = false;
73 77
     Serial.print("Time Sync error: ");
74
-    if (ntpEvent == noResponse) {
78
+    if (ntpEvent == noResponse)
79
+    {
75 80
       Serial.println("NTP server not reachable");
76 81
     }
77
-    else if (ntpEvent == invalidAddress) {
82
+    else if (ntpEvent == invalidAddress)
83
+    {
78 84
       Serial.println("Invalid NTP server address");
79 85
     }
80 86
   }
81
-  else {
87
+  else
88
+  {
82 89
     ntp_online = true;
83 90
     Serial.print("Got NTP time: ");
84 91
     get_outside_termo();
@@ -86,23 +93,42 @@ void processSyncEvent(NTPSyncEvent_t ntpEvent) {
86 93
   }
87 94
 }
88 95
 
89
-//////// MAIN BODY ////////
96
+//////// PRIMITIVES /////////
90 97
 
91
-void setup() {
92
-  Wire.begin();
93
-  pinMode(ONBOARDLED, OUTPUT); // Onboard LED
94
-  digitalWrite(ONBOARDLED, HIGH); // Switch off LED 
95
-  first_start();
96
-  Serial.println("mounting FS...");
98
+void reset_device()
99
+{
100
+  // connection failed, reset
101
+  Serial.println("Fatal error! Resetting device");
102
+  display.clear();
103
+  display.showString("resetting...");
104
+  delay(3000);
105
+  ESP.reset();
106
+  delay(5000);
107
+}
97 108
 
98
-  // read wifi manager config
99
-  if (SPIFFS.begin()) {
109
+void hello()
110
+{
111
+  display.clear();
112
+
113
+  for (uint8_t l = 0; l <= 7; l++)
114
+  {
115
+    display.setBrightness(l); //set the diplay to maximum brightness
116
+    display.showString("hello int 21h");
117
+    delay(200);
118
+  }
119
+}
120
+
121
+void config_load() {
122
+  if (SPIFFS.begin())
123
+  {
100 124
     Serial.println("mounted file system");
101
-    if (SPIFFS.exists("/config.json")) {
125
+    if (SPIFFS.exists("/config.json"))
126
+    {
102 127
       //file exists, reading and loading
103 128
       Serial.println("reading config file");
104 129
       File configFile = SPIFFS.open("/config.json", "r");
105
-      if (configFile) {
130
+      if (configFile)
131
+      {
106 132
         Serial.println("opened config file");
107 133
         size_t size = configFile.size();
108 134
         // Allocate a buffer to store contents of the file.
@@ -110,26 +136,61 @@ void setup() {
110 136
 
111 137
         configFile.readBytes(buf.get(), size);
112 138
         DynamicJsonBuffer jsonBuffer;
113
-        JsonObject& json = jsonBuffer.parseObject(buf.get());
139
+        JsonObject &json = jsonBuffer.parseObject(buf.get());
114 140
         json.printTo(Serial);
115
-        if (json.success()) {
141
+        if (json.success())
142
+        {
116 143
           Serial.println("\nparsed json");
117 144
 
118 145
           strcpy(ntp_server, json["ntp_server"]);
119 146
           strcpy(ntp_timezone, json["ntp_timezone"]);
120 147
           strcpy(wifi_hostname, json["wifi_hostname"]);
121
-
122
-        } else {
148
+        }
149
+        else
150
+        {
123 151
           Serial.println("failed to load json config");
124 152
         }
125 153
         configFile.close();
126 154
       }
127 155
     }
128
-  } else {
156
+  }
157
+  else
158
+  {
129 159
     Serial.println("failed to mount FS");
130 160
   }
131
-  //end read
132
-  
161
+}
162
+
163
+void config_save() {
164
+  Serial.println("saving config");
165
+  DynamicJsonBuffer jsonBuffer;
166
+  JsonObject &json = jsonBuffer.createObject();
167
+  json["ntp_server"] = ntp_server;
168
+  json["ntp_timezone"] = ntp_timezone;
169
+  json["wifi_hostname"] = wifi_hostname;
170
+
171
+  File configFile = SPIFFS.open("/config.json", "w");
172
+  if (!configFile)
173
+  {
174
+    Serial.println("failed to open config file for writing");
175
+  }
176
+
177
+  json.printTo(Serial);
178
+  json.printTo(configFile);
179
+  configFile.close();
180
+}
181
+
182
+//////// MAIN BODY ////////
183
+
184
+void setup()
185
+{
186
+  Wire.begin();
187
+  pinMode(ONBOARDLED, OUTPUT);    // Onboard LED
188
+  digitalWrite(ONBOARDLED, HIGH); // Switch off LED
189
+  hello();
190
+  Serial.println("mounting FS...");
191
+
192
+  config_load();
193
+
133 194
   Serial.println("Wifi manager configuring...");
134 195
 
135 196
   // custom wifi parameters
@@ -140,7 +201,8 @@ void setup() {
140 201
   wifiManager.addParameter(&custom_hostname);
141 202
 
142 203
   Serial.println("Wifi connecting...");
143
-  if (!wifiManager.autoConnect("clock-x16-1-config")) {
204
+  if (!wifiManager.autoConnect("clock-x16-1-config"))
205
+  {
144 206
     reset_device();
145 207
   }
146 208
 
@@ -154,26 +216,13 @@ void setup() {
154 216
   strcpy(wifi_hostname, custom_hostname.getValue());
155 217
 
156 218
   // check hostname, set if needed
157
-  if (wifi_hostname != "clock-x16-1") WiFi.hostname("clock-x16-1");
219
+  if (wifi_hostname != "clock-x16-1")
220
+    WiFi.hostname("clock-x16-1");
158 221
 
159 222
   //save the custom parameters to FS
160
-  if (shouldSaveConfig) {
161
-    Serial.println("saving config");
162
-    DynamicJsonBuffer jsonBuffer;
163
-    JsonObject& json = jsonBuffer.createObject();
164
-    json["ntp_server"] = ntp_server;
165
-    json["ntp_timezone"] = ntp_timezone;
166
-    json["wifi_hostname"] = wifi_hostname;
167
-
168
-    File configFile = SPIFFS.open("/config.json", "w");
169
-    if (!configFile) {
170
-      Serial.println("failed to open config file for writing");
171
-    }
172
-
173
-    json.printTo(Serial);
174
-    json.printTo(configFile);
175
-    configFile.close();
176
-    //end save
223
+  if (shouldSaveConfig)
224
+  {
225
+    config_save();
177 226
   }
178 227
 
179 228
   Serial.print("local ip ");
@@ -183,43 +232,24 @@ void setup() {
183 232
     ntpEvent = event;
184 233
     syncEventTriggered = true;
185 234
   });
186
-
187 235
 }
188 236
 
189
-void get_outside_termo() {
237
+void get_outside_termo()
238
+{
190 239
   http.begin("http://termo.tomsk.ru/data.txt");
191 240
   int httpCode = http.GET();
192
-  if (httpCode > 0) {
193
-    if (httpCode == HTTP_CODE_OK) {
241
+  if (httpCode > 0)
242
+  {
243
+    if (httpCode == HTTP_CODE_OK)
244
+    {
194 245
       String result = http.getString();
195 246
       outside_termo = round(result.toInt());
196 247
     }
197 248
   }
198 249
 }
199 250
 
200
-void reset_device() {
201
-  // connection failed, reset
202
-  Serial.println("Fatal error! Resetting device");
203
-  display.clear();
204
-  display.showString("resetting...");
205
-  delay(3000);
206
-  ESP.reset();
207
-  delay(5000);
208
-}
209
-
210
-void first_start() {
211
-  display.clear();
212
-
213
-  for (uint8_t l = 0; l <= 7; l++) {
214
-    display.setBrightness(l); //set the diplay to maximum brightness
215
-    display.showString("hello int 21h");
216
-    delay(200);
217
-  }
218
-}
219
-
220
-void starting() {
221
-  NTP.begin(ntp_server, atoi(ntp_timezone), true);
222
-  NTP.setInterval(10, 600);
251
+void starting()
252
+{
223 253
   Serial.print("TZ: ");
224 254
   Serial.println(atoi(ntp_timezone));
225 255
 
@@ -227,12 +257,16 @@ void starting() {
227 257
 
228 258
   //Serial.println("Starting NTP client");
229 259
 
230
-  while (ntp_online == false) {
231
-    processSyncEvent (ntpEvent);
232
-    syncEventTriggered = true;
260
+  while (ntp_online == false)
261
+  {
233 262
     display.setBrightness(1);
234 263
     display.showString("connecting...");
235
-    delay(5000);
264
+    NTP.stop();
265
+    NTP.begin(ntp_server, atoi(ntp_timezone), true);
266
+    NTP.setInterval(3600);
267
+    processSyncEvent(ntpEvent);
268
+    syncEventTriggered = true;
269
+    delay(30000);
236 270
   }
237 271
   get_outside_termo();
238 272
   display.setBrightness(7);
@@ -242,21 +276,28 @@ void starting() {
242 276
 
243 277
 char timebuff[17];
244 278
 
245
-void loop() {
279
+void loop()
280
+{
246 281
 
247
-  if (WiFi.status() != WL_CONNECTED) {
282
+  if (WiFi.status() != WL_CONNECTED)
283
+  {
248 284
     wifi_connected = false;
249 285
   }
250 286
 
251 287
   // if wifi suddenly disconnected wait, if failed run configurator
252
-  if (wifi_connected == true) {
288
+  if (wifi_connected == true)
289
+  {
253 290
     // if ntp server is down try to reconnect
254
-    if (ntp_online == false) {
291
+    if (ntp_online == false)
292
+    {
255 293
       starting();
256
-    } else {
294
+    }
295
+    else
296
+    {
257 297
       // all fine, working here
258
-      if (syncEventTriggered) {
259
-        processSyncEvent (ntpEvent);
298
+      if (syncEventTriggered)
299
+      {
300
+        processSyncEvent(ntpEvent);
260 301
         syncEventTriggered = false;
261 302
       }
262 303
 
@@ -265,13 +306,16 @@ void loop() {
265 306
       // draw temperature
266 307
       String nozero = "";
267 308
       String sign;
268
-      if (outside_termo < 0) {
309
+      if (outside_termo < 0)
310
+      {
269 311
         sign = "";
270
-      } else
312
+      }
313
+      else
271 314
       {
272 315
         sign = " ";
273 316
       }
274
-      if (outside_termo > -10 && outside_termo < 10) {
317
+      if (outside_termo > -10 && outside_termo < 10)
318
+      {
275 319
         nozero = " ";
276 320
       }
277 321
 
@@ -279,22 +323,19 @@ void loop() {
279 323
 
280 324
       result.toCharArray(timebuff, 17);
281 325
 
282
-      display.showString( timebuff );
326
+      display.showString(timebuff);
283 327
 
284 328
       delay(1000);
285 329
     }
286
-  } else {
287
-    NTP.stop();
330
+  }
331
+  else
332
+  {
288 333
     delay(10000);
289 334
     Serial.println("Wifi connecting...");
290
-    if (wifiManager.autoConnect("clock-x16-1-config")) {
335
+    if (wifiManager.autoConnect("clock-x16-1-config"))
336
+    {
291 337
       wifi_connected = true;
292 338
       starting();
293 339
     };
294 340
   }
295 341
 }
296
-
297
-
298
-
299
-
300
-

Loading…
Cancel
Save