Skip to content

Commit

Permalink
Multiple updates, additions & improvements (#171)
Browse files Browse the repository at this point in the history
* Update config.h

Added defines for the default port nrs:
- OTA_HOST_HTTP_PORT 80
- OTA_HOST_HTTPS_PORT 443

* Update config.h

Changed default values for OTA_UPD_FILE & OTA_LOG_FILE
For backward compatibility the old addresses are still supported!

* Update CallServer.cpp

Changed hardcode port nrs to defines which are defined in the config.h file.

* Update CallServer.cpp

Changed hardcode port nrs to defines which are defined in the config.h file.

* Update WiFiConnector.h

Introduced a new private var _nrOfCfgFound
And made sure that Arduino compiler only compiles when ESP8266 & ESP32 are defined to prevent errors for boards like the Ard. Wifi MKR 1010

* Update WiFiConnector.cpp

Make sure that it does not try to connect when 0 credentials are found using the private var _nrOfCfgFound

And made sure that Arduino compiler only compiles this file when ESP8266 & ESP32 are defined to prevent errors for boards like the Ard. Wifi MKR 1010

* Update IOTAppStory.h

Added 
- public void preSetConnWifiOnBoot(bool automaticWifiConnectOnBoot);
- private bool _automaticWifiConnectOnBoot
This lets programmers decide whether or not to setup and initiate wifi on boot. (in IAS.begin())

Changed
- private bool _connected to public bool WiFiConnected
This lets programmers check if the device is connected to an WiFi AP. 
Or if they choose to use their own WiFi setup solution, tell this library if the device is connected.

Added a precompiler check for ARDUINO_SAMD_VARIANT_COMPLIANCE whether or not to compile the  servSaveAppInfo() function.

Changed the configmode station server js address. For backward compatibility the old address is still accepted by our servers!

* Update config.h

Added defines for the configTime function

* Update IOTAppStory.h

Added default value for
ntpSync(int retries = SNTP_CONN_MAX_RETRIES);

Removed redundant comments

* Update IOTAppStory.cpp

Added 
- public void preSetConnWifiOnBoot(bool automaticWifiConnectOnBoot);
- private bool _automaticWifiConnectOnBoot
This lets programmers decide whether or not to setup and initiate wifi on boot. (in IAS.begin())

Changed
- private bool _connected to public bool WiFiConnected
This lets programmers check if the device is connected to an WiFi AP. 
Or if they choose to use their own WiFi setup solution, tell this library if the device is connected.

Fixed bad code
- preSetWifi

NTP
- added ntpSync(int retries)
- changed setClock();

Updated the code to the new var & function names named above.

* Update serialFeedback_EN.h

* Update serialFeedback_EN.h

Added timeout message for configmode

* Update config.h

Added CFG_TIMEOUT define to tell Config mode to Exit after X time of inactivity.

* Update ConfigServer.h

Added unsigned int _lastAction 
last action taken by human to determine when to timeout

* Update ConfigServer.cpp

Added
- Config mode timeout. Exit after X time of inactivity. Set CFG_TIMEOUT in the config.h file
- Missing writeConfig() for storing firmware name & version after local(browser) upload.

Updated
- private bool _connected to public bool WiFiConnected

* Update IOTAppStory.cpp

Allow configmode announcement when stored in SPIFFS

* Update CallServer.h

Replaced PROGMEM stored certificate with the latest.

* Update CallServer.h

Replaced PROGMEM stored certificate with the latest.

* Update ConfigServer.cpp

Added missing CFG_TIMEOUT define instead of hardcoded nr

* Update and rename examples/IASLoader/IASLoader.ino to examples/Loaders/IASLoader/IASLoader.ino

On first boot copy hardcoded root certificate to a file in SPIFFS /cert/iasRootCa.cer
This saves the user time as users often forget to do this in config mode the first time.

* Updated certificate fingerprint to the latest version

* Update and rename examples/Other Examples/addDeviceToProject/addDeviceToProject.ino to examples/Loaders/BatchLoader/BatchLoader.ino

- Renamed to Batchloader
- Updated comments
- Updated root certifcate & fingerprint
- Put client connect into a loop untill connected
- Fixed typo

* Fixed typo

* Added ntpSync keyword

* increase version nr

* increase version nr

* Increase version nr

* Added missing define

* Sync changes with English file

* Update ConfigServer.cpp

Added temp workaround between setting softAP() and softAPConfig() to prevent crashes on the ESP32 when connecting.
  • Loading branch information
Onno-Dirkzwager committed Dec 16, 2020
1 parent d8bf2c0 commit 357486a
Show file tree
Hide file tree
Showing 18 changed files with 907 additions and 766 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/*
Warning: This is not for first time users. Use the IASLoader instead!
This sketch is meant for adding large quantities of devices to an existing project(product)
This sketch is meant for adding large quantities of devices to an existing Devicebatch
without having to do/repeat the initial setup.(Wifi, registration etc.)
This sketch:
- Erases EEPROM
- Sets up initial hardcode WiFi connection
- Contacts IOTAppStory.com over https
- Registers your device with IOTAppStory.com
- Adds the device to a project(product)
- Adds the device to a Devicebatch setup in your Control Panel
- Saves the device activation code
- Saves generated device name (eg. XXXX-22, XXXX-23 ...)
- Saves the Fingerprint or Certificate (depends on config.h settings)
Expand All @@ -18,9 +18,9 @@
You will need:
- IOTAppStory.com account
- Existing project(product) with at least 1 added device of the same
- Existing Devicebatch with at least 1 added device of the same
type as the devices you want to use this sketch for.
- Project hash
- Devicebatch hash
- Local WiFi credentials
Copyright (c) [2019] [Onno Dirkzwager]
Expand Down Expand Up @@ -54,54 +54,45 @@ IOTAppStory IAS(COMPDATE, MODEBUTTON); // Initialize IOTAppStory


// ================================================ VARS =================================================
const char* ssid = "YourSSID"; // Wifi SSID
const char* password = "password"; // WiFi password
const char* ssid = "***"; // Wifi SSID
const char* password = "***"; // WiFi password
const bool saveWifiCred = true; // true : Save the Wifi credentials for future use
// false: only use Wifi credentials for adding this device and doing the initial update

const char* host = "iotappstory.com";
const char* url = "/ota/addtoproject.php";
const char* hash = "EXAMPLE1234567890abcdefghij1234EXAMPLEghij1234567890abcdefghij12";
const char* url = "/ota/addtobatch";
const char* hash = "***";

#if defined ESP8266 && HTTPS_8266_TYPE == FNGPRINT
// Use web browser to view and copy SHA1 fingerprint of the certificate
const char fingerprint[] PROGMEM = "34 6d 0a 26 f0 40 3a 0a 1b f1 ca 8e c8 0c f5 14 21 83 7c b1";
const char fingerprint[] PROGMEM = "2b 14 1a f1 5e 54 87 fc 0d f4 6f 0e 01 1c 0d 77 25 28 5b 9e";
#else
// Use web browser to save a copy of the root certificate
const char ROOT_CA[] = \
"-----BEGIN CERTIFICATE-----\n" \
"MIIF2DCCA8CgAwIBAgIQTKr5yttjb+Af907YWwOGnTANBgkqhkiG9w0BAQwFADCB\n" \
"hTELMAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4G\n" \
"A1UEBxMHU2FsZm9yZDEaMBgGA1UEChMRQ09NT0RPIENB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G9w0BAQUF\n" \
"AAOCAQEACFb8AvCb6P+k+tZ7xkSAzk/ExfYAWMymtrwUSWgEdujm7l3sAg9g1o1Q\n" \
"GE8mTgHj5rCl7r+8dFRBv/38ErjHT1r0iWAFf2C3BUrz9vHCv8S5dIa2LX1rzNLz\n" \
"Rt0vxuBqw8M0Ayx9lt1awg6nCpnBBYurDC/zXDrPbDdVCYfeU0BsWO/8tqtlbgT2\n" \
"G9w84FoVxp7Z8VlIMCFlA2zs6SFz7JsDoeA3raAVGI/6ugLOpyypEBMs1OUIJqsi\n" \
"l2D4kF501KKaU73yqWjgom7C12yxow+ev+to51byrvLjKzg6CYG1a4XXvi3tPxq3\n" \
"smPi9WIsgtRqAEFQ8TmDn5XpNpaYbg==\n" \
"-----END CERTIFICATE-----\n";
#endif

Expand All @@ -121,13 +112,15 @@ void setup() {
delay(500);
Serial.print(".");
}
IAS.WiFiConnected = true;
Serial.print(F("\n WiFi connected\n IP address: "));
Serial.println(WiFi.localIP());

{
// Synchronize the internal clock useing SNTP | used for verifying certificates on the ESP8266
#if defined ESP8266 && HTTPS_8266_TYPE == CERTIFICATE
IAS.setClock();
IAS.ntpSync();
#endif

// Use WiFiClientSecure class to create TLS connection
Expand All @@ -144,14 +137,15 @@ void setup() {
#elif defined ESP32
client.setCACert(ROOT_CA);
#endif

if (!client.connect(host, 443)) {
Serial.println(F(" Connection failed"));
return;

Serial.print(" ");
while (!client.connect(host, 443)) {
delay(500);
Serial.print(".");
}

// request url from host
Serial.printf(" Requesting URL: %s%s\n", host, url);
Serial.printf("\n Requesting URL: %s%s\n", host, url);
client.print(String("GET ") + url + "?hash=" + hash + F(" HTTP/1.1") +
F("\r\nHost: ") + host +
F("\r\nUser-Agent: ESP-http-Update") +
Expand Down Expand Up @@ -203,7 +197,7 @@ void setup() {
Serial.println(F("\n Success! Added device to IAS.\n Updating device..."));

// create new config struc
configStruct newConfig;
ConfigStruct newConfig;

// read config | this gets the default settings
IAS.readConfig(newConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,33 @@
#include <IOTAppStory.h> // IOTAppStory.com library
IOTAppStory IAS(COMPDATE, MODEBUTTON); // Initialize IOTAppStory


// This certificate gets copied to a file in SPIFFS during first boot: /cert/iasRootCa.cer
const char ROOT_CA[] = \
"-----BEGIN CERTIFICATE-----\n" \
"MIIEMjCCAxqgAwIBAgIBATANBgkqhkiG9w0BAQUFADB7MQswCQYDVQQGEwJHQjEb\n" \
"MBkGA1UECAwSR3JlYXRlciBNYW5jaGVzdGVyMRAwDgYDVQQHDAdTYWxmb3JkMRow\n" \
"GAYDVQQKDBFDb21vZG8gQ0EgTGltaXRlZDEhMB8GA1UEAwwYQUFBIENlcnRpZmlj\n" \
"YXRlIFNlcnZpY2VzMB4XDTA0MDEwMTAwMDAwMFoXDTI4MTIzMTIzNTk1OVowezEL\n" \
"MAkGA1UEBhMCR0IxGzAZBgNVBAgMEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UE\n" \
"BwwHU2FsZm9yZDEaMBgGA1UECgwRQ29tb2RvIENBIExpbWl0ZWQxITAfBgNVBAMM\n" \
"GEFBQSBDZXJ0aWZpY2F0ZSBTZXJ2aWNlczCCASIwDQYJKoZIhvcNAQEBBQADggEP\n" \
"ADCCAQoCggEBAL5AnfRu4ep2hxxNRUSOvkbIgwadwSr+GB+O5AL686tdUIoWMQua\n" \
"BtDFcCLNSS1UY8y2bmhGC1Pqy0wkwLxyTurxFa70VJoSCsN6sjNg4tqJVfMiWPPe\n" \
"3M/vg4aijJRPn2jymJBGhCfHdr/jzDUsi14HZGWCwEiwqJH5YZ92IFCokcdmtet4\n" \
"YgNW8IoaE+oxox6gmf049vYnMlhvB/VruPsUK6+3qszWY19zjNoFmag4qMsXeDZR\n" \
"rOme9Hg6jc8P2ULimAyrL58OAd7vn5lJ8S3frHRNG5i1R8XlKdH5kBjHYpy+g8cm\n" \
"ez6KJcfA3Z3mNWgQIJ2P2N7Sw4ScDV7oL8kCAwEAAaOBwDCBvTAdBgNVHQ4EFgQU\n" \
"oBEKIz6W8Qfs4q8p74Klf9AwpLQwDgYDVR0PAQH/BAQDAgEGMA8GA1UdEwEB/wQF\n" \
"MAMBAf8wewYDVR0fBHQwcjA4oDagNIYyaHR0cDovL2NybC5jb21vZG9jYS5jb20v\n" \
"QUFBQ2VydGlmaWNhdGVTZXJ2aWNlcy5jcmwwNqA0oDKGMGh0dHA6Ly9jcmwuY29t\n" \
"b2RvLm5ldC9BQUFDZXJ0aWZpY2F0ZVNlcnZpY2VzLmNybDANBgkqhkiG9w0BAQUF\n" \
"AAOCAQEACFb8AvCb6P+k+tZ7xkSAzk/ExfYAWMymtrwUSWgEdujm7l3sAg9g1o1Q\n" \
"GE8mTgHj5rCl7r+8dFRBv/38ErjHT1r0iWAFf2C3BUrz9vHCv8S5dIa2LX1rzNLz\n" \
"Rt0vxuBqw8M0Ayx9lt1awg6nCpnBBYurDC/zXDrPbDdVCYfeU0BsWO/8tqtlbgT2\n" \
"G9w84FoVxp7Z8VlIMCFlA2zs6SFz7JsDoeA3raAVGI/6ugLOpyypEBMs1OUIJqsi\n" \
"l2D4kF501KKaU73yqWjgom7C12yxow+ev+to51byrvLjKzg6CYG1a4XXvi3tPxq3\n" \
"smPi9WIsgtRqAEFQ8TmDn5XpNpaYbg==\n" \
"-----END CERTIFICATE-----\n";

// ================================================ VARS =================================================
String deviceName = "initloader";
Expand Down Expand Up @@ -70,6 +96,29 @@ void setup() {

IAS.onFirstBoot([]() {
IAS.eraseEEPROM('F'); // Optional! What to do with EEPROM on First boot of the app? 'F' Fully erase | 'P' Partial erase

// if set write certificate to SPIFFS for future use
#if defined ESP32 || HTTPS_8266_TYPE == CERTIFICATE

// Mount SPIFFS
if(!ESP_SPIFFSBEGIN){
Serial.println(F(" Failed to mount SPIFFS"));
}

// open new SPIFFS file for writing data
File fsUploadFile;
fsUploadFile = SPIFFS.open("/cert/iasRootCa.cer", "w"); /// close file

// write certificate(hardcoded char array) to SPIFFS file
if(fsUploadFile.write((uint8_t *)ROOT_CA, strlen(ROOT_CA)) != strlen(ROOT_CA)){
Serial.println(F(" Failed to write certificate to SPIFFS"));
}

// close SPIFFS FILE
fsUploadFile.close();

Serial.println(F(" Added Certificate to SPIFFS"));
#endif
});


Expand Down
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ WiFiSetupAndConnect KEYWORD2
WiFiConnect KEYWORD2
WiFiDisconnect KEYWORD2
setClock KEYWORD2
ntpSync KEYWORD2

callHome KEYWORD2
iotUpdater KEYWORD2
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
"platforms": ["espressif8266", "espressif32"],
"authors": "Hristo Gochkov"
},
"version": "2.1.0-RC3"
"version": "2.1.0-RC4"
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=IOTAppStory-ESP
version=2.1.0-RC3
version=2.1.0-RC4
author=SensorsIot, Onno Dirkzwager
maintainer=iotappstory
sentence=Update your ESP8266, ESP32 & Nextion displays over the air(OTA)
Expand Down
Loading

0 comments on commit 357486a

Please sign in to comment.