@@ -16,7 +16,7 @@ extern "C" {
16
16
#endif
17
17
18
18
#ifndef THINX_COMMIT_ID
19
- #define THINX_COMMIT_ID " c680610df8457bb634cbaa80ce00b2b761208347 "
19
+ #define THINX_COMMIT_ID " 0 "
20
20
#endif
21
21
22
22
char * THiNX::thinx_api_key;
@@ -177,7 +177,7 @@ THiNX::THiNX(const char * __apikey, const char * __owner_id) {
177
177
void THiNX::initWithAPIKey (const char * __apikey) {
178
178
179
179
#ifdef __USE_SPIFFS__
180
- Serial.println (F (" *TH: Checking FS ..." ));
180
+ Serial.println (F (" *TH: Checking filesystem, please don't turn off or reset the device now ..." ));
181
181
if (!fsck ()) {
182
182
Serial.println (F (" *TH: Filesystem check failed, disabling THiNX." ));
183
183
return ;
@@ -456,13 +456,12 @@ void THiNX::senddata(String body) {
456
456
457
457
void THiNX::parse (String payload) {
458
458
459
- // TODO: Should parse response only for this device_id (which must be internal and not a mac)
459
+ // TODO: Should parse response only for this device
460
460
461
461
payload_type ptype = Unknown;
462
462
463
463
int start_index = 0 ;
464
464
int endIndex = payload.length ();
465
-
466
465
int reg_index = payload.indexOf (" {\" registration\" " );
467
466
int upd_index = payload.indexOf (" {\" FIRMWARE_UPDATE\" " );
468
467
int not_index = payload.indexOf (" {\" notification\" " );
@@ -525,33 +524,38 @@ void THiNX::parse(String payload) {
525
524
Serial.println (String (" mac: " ) + mac);
526
525
527
526
if (!mac.equals (this_mac)) {
528
- Serial.println (F (" *TH: Warning: firmware is dedicated to device with different MAC." ));
527
+ Serial.println (F (" *TH: Firmware is dedicated to device with different MAC. Skipping update." ));
528
+ Serial.print (" Local MAC: " ); Serial.println (this_mac);
529
+ Serial.print (" Remote MAC: " ); Serial.println (mac);
530
+ return ;
529
531
}
530
532
531
533
String udid = root[" udid" ];
532
534
if ( udid.length () > 4 ) {
533
535
thinx_udid = strdup (udid.c_str ());
534
- Serial.println (String (" thinx_udid: " ) + thinx_udid);
536
+ Serial.println (String (" Update for thinx_udid: " ) + thinx_udid);
535
537
}
536
538
537
539
// Check current firmware based on commit id and store Updated state...
538
540
String commit = update[" commit" ];
539
- Serial.println (String (" commit: " ) + commit);
541
+ Serial.println (String (" Update commit: " ) + commit);
540
542
541
543
// Check current firmware based on version and store Updated state...
542
544
String version = update[" version" ];
543
- Serial.println (String (" version: " ) + version);
544
-
545
- // if ((commit == thinx_commit_id) && (version == thinx_version_id)) { WHY?
546
- if (strlen (available_update_url) > 5 ) {
547
- Serial.println (F (" *TH: firmware has same thx_commit_id as current and update availability is stored. Firmware has been installed." ));
548
- available_update_url = strdup (" " );
549
- notify_on_successful_update ();
550
- return ;
551
- } else {
552
- Serial.println (F (" *TH: Info: firmware has same thx_commit_id as current and no update is available." ));
545
+ Serial.println (String (" Update version: " ) + version);
546
+
547
+ // Well, the backend should not actually provide firmware when these two things are same,
548
+ // following block may deprecate.
549
+ if ((commit.equals (thinx_commit_id)) && (version.equals (thinx_version_id))) {
550
+ if (strlen (available_update_url) > 5 ) {
551
+ Serial.println (F (" *TH: firmware has same thx_commit_id as current and update availability is stored. Firmware has been installed." ));
552
+ available_update_url = strdup (" " );
553
+ notify_on_successful_update ();
554
+ return ;
555
+ } else {
556
+ Serial.println (F (" *TH: Info: firmware has same thx_commit_id as current and no update is available." ));
557
+ }
553
558
}
554
- // }
555
559
556
560
save_device_info ();
557
561
@@ -852,11 +856,15 @@ String THiNX::date(const char* optional_format) {
852
856
*/
853
857
854
858
const char * THiNX::thinx_mac () {
859
+
855
860
#if defined(ESP8266)
856
- sprintf (mac_string, " 5CCF7F%6X" , ESP.getChipId ());
857
- #else
861
+ sprintf (mac_string, " 5CCF7F%6X" , ESP.getChipId ()); // ESP8266 only!
862
+ #endif
863
+
864
+ #if defined(ESP32)
858
865
sprintf (mac_string, " 5CCF7F%6X" , ESP.getEfuseMac ());
859
866
#endif
867
+
860
868
return mac_string;
861
869
}
862
870
@@ -873,7 +881,7 @@ void THiNX::notify_on_successful_update() {
873
881
);
874
882
mqtt_client->loop ();
875
883
} else {
876
- Serial.println (F (" *TH: Device updated but MQTT not active to notify. TODO: Store. " ));
884
+ Serial.println (F (" *TH: Device updated but MQTT not active to notify." )); // TODO: Store as boot status
877
885
}
878
886
}
879
887
@@ -1000,8 +1008,6 @@ bool THiNX::start_mqtt() {
1000
1008
}
1001
1009
1002
1010
} else {
1003
- Serial.println (F (" *TH: MQTT Type: String or JSON..." ));
1004
- Serial.println (pub.payload_string ());
1005
1011
parse (pub.payload_string ());
1006
1012
if (_mqtt_callback) {
1007
1013
_mqtt_callback (pub.payload_string ());
@@ -1025,8 +1031,6 @@ bool THiNX::start_mqtt() {
1025
1031
1026
1032
void THiNX::restore_device_info () {
1027
1033
1028
- Serial.println (F (" *TH: Restoring device info..." ));
1029
-
1030
1034
int json_end = 0 ;
1031
1035
1032
1036
#ifndef __USE_SPIFFS__
@@ -1067,21 +1071,18 @@ void THiNX::restore_device_info() {
1067
1071
1068
1072
// Validating bracket count
1069
1073
if (json_end != 0 ) {
1070
- Serial.println (F (" *TH: JSON invalid... bailing out." ));
1074
+ // Serial.println(F("*TH: JSON invalid... bailing out."));
1071
1075
return ;
1072
1076
}
1073
1077
1074
- Serial.println (F (" *TH: Converting data to String..." ));
1075
-
1076
1078
#else
1077
1079
if (!SPIFFS.exists (" /thx.cfg" )) {
1078
- Serial.println (F (" *TH: No saved configuration." ));
1080
+ // Serial.println(F("*TH: No saved configuration."));
1079
1081
return ;
1080
1082
}
1081
1083
File f = SPIFFS.open (" /thx.cfg" , " r" );
1082
- Serial.println (F (" *TH: Found persistent data..." ));
1083
1084
if (!f) {
1084
- Serial.println (F (" *TH: No remote configuration found so far..." ));
1085
+ // Serial.println(F("*TH: No remote configuration found so far..."));
1085
1086
return ;
1086
1087
}
1087
1088
if (f.size () == 0 ) {
@@ -1092,9 +1093,6 @@ void THiNX::restore_device_info() {
1092
1093
f.readBytesUntil (' \n ' , json_info, 511 );
1093
1094
#endif
1094
1095
1095
- Serial.println (json_info);
1096
- Serial.println (F (" *TH: Parsing..." ));
1097
-
1098
1096
DynamicJsonBuffer jsonBuffer (512 );
1099
1097
JsonObject& config = jsonBuffer.parseObject ((char *)json_info); // must not be String!
1100
1098
@@ -1104,12 +1102,8 @@ void THiNX::restore_device_info() {
1104
1102
1105
1103
} else {
1106
1104
1107
- Serial.println (F (" *TH: Reading JSON..." ));
1108
-
1109
1105
if (config[" alias" ]) {
1110
1106
thinx_alias = strdup (config[" alias" ]);
1111
- Serial.print (" alias: " );
1112
- Serial.println (thinx_alias);
1113
1107
}
1114
1108
1115
1109
if (config[" udid" ]) {
@@ -1123,25 +1117,16 @@ void THiNX::restore_device_info() {
1123
1117
thinx_udid = strdup (THINX_UDID);
1124
1118
}
1125
1119
1126
- Serial.print (" thinx_udid: " );
1127
- Serial.println (thinx_udid);
1128
-
1129
1120
if (config[" apikey" ]) {
1130
1121
thinx_api_key = strdup (config[" apikey" ]);
1131
- Serial.print (" apikey: " );
1132
- Serial.println (thinx_api_key);
1133
1122
}
1134
1123
1135
1124
if (config[" owner" ]) {
1136
1125
thinx_owner = strdup (config[" owner" ]);
1137
- Serial.print (" owner: " );
1138
- Serial.println (thinx_owner);
1139
1126
}
1140
1127
1141
1128
if (config[" ott" ]) {
1142
1129
available_update_url = strdup (config[" ott" ]);
1143
- Serial.print (" available_update_url: " );
1144
- Serial.println (available_update_url);
1145
1130
}
1146
1131
1147
1132
#ifdef __USE_SPIFFS__
@@ -1223,7 +1208,7 @@ void THiNX::deviceInfo() {
1223
1208
1224
1209
void THiNX::update_and_reboot (String url) {
1225
1210
1226
- Serial.print (" *TH: Update with URL: " );
1211
+ Serial.print (" *TH: Update from URL: " );
1227
1212
Serial.println (url);
1228
1213
1229
1214
// #define __USE_STREAM_UPDATER__ ; // Warning, this is MQTT-based streamed update!
@@ -1343,13 +1328,11 @@ void THiNX::evt_save_api_key() {
1343
1328
if (should_save_config) {
1344
1329
if (strlen (thx_api_key) > 4 ) {
1345
1330
thinx_api_key = thx_api_key;
1346
- Serial.print (F (" Saving thx_api_key from Captive Portal: " ));
1347
- Serial.println (thinx_api_key);
1331
+ Serial.print (F (" Saving thx_api_key from Captive Portal." ));
1348
1332
}
1349
1333
if (strlen (thx_owner_key) > 4 ) {
1350
1334
thinx_owner_key = thx_owner_key;
1351
- Serial.print (F (" Saving thx_owner_key from Captive Portal: " ));
1352
- Serial.println (thinx_owner_key);
1335
+ Serial.print (F (" Saving thx_owner_key from Captive Portal." ));
1353
1336
}
1354
1337
save_device_info ();
1355
1338
should_save_config = false ;
@@ -1400,11 +1383,8 @@ void THiNX::loop() {
1400
1383
wifi_connected = false ;
1401
1384
if (wifi_connection_in_progress != true ) {
1402
1385
Serial.println (F (" *TH: CONNECTING »" ));
1403
- Serial.println (F (" *TH: LOOP «÷»" )); Serial.flush ();
1404
1386
connect (); // blocking
1405
- Serial.println (F (" *TH: Enabling connection state (1283)" ));
1406
1387
wifi_connection_in_progress = true ;
1407
- Serial.println (F (" *TH: LOOP «" ));
1408
1388
wifi_connection_in_progress = true ;
1409
1389
return ;
1410
1390
} else {
@@ -1418,18 +1398,11 @@ void THiNX::loop() {
1418
1398
Serial.println (F (" *TH: Error setting up mDNS" ));
1419
1399
} else {
1420
1400
// Query MDNS proxy
1421
- Serial.println (F (" *TH: Searching for thinx-connect on local network..." ));
1401
+ Serial.println (F (" *TH: Searching for thinx-connect proxy on local network..." ));
1422
1402
int n = MDNS.queryService (" thinx" , " tcp" ); // TODO: WARNING! may be _tcp!
1423
1403
if (n > 0 ) {
1424
1404
thinx_cloud_url = strdup (String (MDNS.hostname (0 )).c_str ());
1425
1405
thinx_mqtt_url = strdup (String (MDNS.hostname (0 )).c_str ());
1426
-
1427
- Serial.println (F (" *TH: Routing traffic through thinx-connect..." ));
1428
- Serial.print (F (" host : " )); Serial.println (MDNS.hostname (0 ));
1429
- Serial.print (F (" IP : " )); Serial.println (MDNS.IP (0 ));
1430
- Serial.print (F (" port : " )); Serial.println (MDNS.port (0 ));
1431
- Serial.print (F (" thinx_cloud_url : " )); Serial.println (thinx_cloud_url);
1432
- Serial.print (F (" thinx_mqtt_url : " )); Serial.println (thinx_mqtt_url);
1433
1406
}
1434
1407
}
1435
1408
@@ -1442,10 +1415,7 @@ void THiNX::loop() {
1442
1415
thinx_mqtt_channel (); // initialize channel variable
1443
1416
if (strlen (mqtt_device_channel) > 5 ) {
1444
1417
if (mqtt_client->subscribe (mqtt_device_channel)) {
1445
- Serial.print (F (" *TH: MQTT device topic: " ));
1446
- Serial.print (mqtt_device_channel);
1447
- Serial.println (F (" successfully subscribed." ));
1448
- Serial.println (F (" *TH: Publishing device status over MQTT... " ));
1418
+ Serial.print (F (" *TH: MQTT device topic subscribed." ));
1449
1419
// Publish status on status channel
1450
1420
mqtt_client->publish (
1451
1421
mqtt_device_status_channel,
@@ -1483,7 +1453,6 @@ void THiNX::loop() {
1483
1453
// If connected, perform the MQTT loop and bail out ASAP
1484
1454
if (thinx_phase == CONNECT_API) {
1485
1455
if (WiFi.getMode () == WIFI_AP) {
1486
- Serial.println (F (" *TH: LOOP « (AP_MODE)" ));
1487
1456
return ;
1488
1457
}
1489
1458
if (strlen (thinx_api_key) > 4 ) {
0 commit comments