Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated examples #183

Merged
merged 9 commits into from
Mar 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,15 @@ void setup() {
IAS.preSetDeviceName(deviceName); // preset deviceName this is also your MDNS responder: http://deviceName-123.local
IAS.preSetAppName(F("Wemos")); // preset appName | The appName & appVersion get updated when you receive OTA updates. As this is your first app we will set it manually.
IAS.preSetAppVersion(F("1.3.1")); // preset appVersion
IAS.preSetAutoUpdate(false); // automaticUpdate (true, false)

// default mode button not connected on LOLIN oled v2.1.0 shield
// default mode button not connected on LOLIN oled v2.1.0 shield
IAS.onModeButtonShortPress([]() {
Serial.println(F(" If mode button is released, I will enter in firmware update mode."));
Serial.println(F("*-------------------------------------------------------------------------*"));
dispTemplate_threeLineV2(F("Release"), F("for"), F("Updates"));
});

// default mode button not connected on LOLIN oled v2.1.0 shield
// default mode button not connected on LOLIN oled v2.1.0 shield
IAS.onModeButtonLongPress([]() {
Serial.println(F(" If mode button is released, I will enter in configuration mode."));
Serial.println(F("*-------------------------------------------------------------------------*"));
Expand Down
8 changes: 4 additions & 4 deletions examples/Loaders/BatchLoader/BatchLoader.ino
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

You will need:
- IOTAppStory.com account
- Existing Devicebatch with at least 1 added device of the same
type as the devices you want to use this sketch for.
- Existing Devicebatch
- Devicebatch hash
- Local WiFi credentials

Expand Down Expand Up @@ -59,9 +58,10 @@ 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* hash = "***";
const char* host = "iotappstory.com";
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
Expand Down Expand Up @@ -120,7 +120,7 @@ void setup() {
// Synchronize the internal clock useing SNTP | used for verifying certificates on the ESP8266
#if defined ESP8266 && HTTPS_8266_TYPE == CERTIFICATE
IAS.setClock();
IAS.ntpSync();
IAS.ntpWaitForSync();
#endif

// Use WiFiClientSecure class to create TLS connection
Expand Down
280 changes: 280 additions & 0 deletions examples/Loaders/DirectLoader/DirectLoader.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
/*
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 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
- Saves the device activation code
- Saves generated device name (eg. XXXX-22, XXXX-23 ...)
- Saves the Fingerprint or Certificate (depends on config.h settings)
- Saves the WiFi credentials (optional)
- Calls home to get the latest firmware

You will need:
- IOTAppStory.com account
- Device hash
- Local WiFi credentials

Copyright (c) [2021] [Onno Dirkzwager]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#define COMPDATE __DATE__ __TIME__
#define MODEBUTTON 0 // Button pin on the esp for selecting modes. D3 for the Wemos!


#include <IOTAppStory.h> // IOTAppStory.com library
IOTAppStory IAS(COMPDATE, MODEBUTTON); // Initialize IOTAppStory



// ================================================ VARS =================================================
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* hash = "***";
const char* host = "iotappstory.com";
const char* url = "/ota/addtoaccount";


#if defined ESP8266 && HTTPS_8266_TYPE == FNGPRINT
// Use web browser to view and copy SHA1 fingerprint of the certificate
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" \
"MIIEMjCCAxqgAwIBAgIBATANBgkqhkiG9w0BAQUFADB7MQswCQYDVQQGEwJHQjEb\n" \
"MBkGA1UECAwSR3JlYXRlciBNYW5jaGVzdGVyMRAwDgYDVQQHDAdTYWxmb3JkMRow\n" \
"GAYDVQQKDBFDb21vZG8gQ0EgTGltaXRlZDEhMB8GA1UEAwwYQUFBIENlcnRpZmlj\n" \
"YXRlIFNlcnZpY2VzMB4XDTA0MDEwMTAwMDAwMFoXDTI4MTIzMTIzNTk1OVowezEL\n" \
"MAkGA1UEBhMCR0IxGzAZBgNVBAgMEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UE\n" \
"BwwHU2FsZm9yZDEaMBgGA1UECgwRQ29tb2RvIENBIExpbWl0ZWQxITAfBgNVBAMM\n" \
"GEFBQSBDZXJ0aWZpY2F0ZSBTZXJ2aWNlczCCASIwDQYJKoZI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3tPxq3\n" \
"smPi9WIsgtRqAEFQ8TmDn5XpNpaYbg==\n" \
"-----END CERTIFICATE-----\n";
#endif



// ================================================ SETUP ================================================
void setup() {
Serial.println(FPSTR(SER_DEV)); // print divider
IAS.eraseEEPROM('F'); // Optional! What to do with EEPROM on First boot of the app? 'F' Fully erase | 'P' Partial erase
// This should not be necessary on new devices, but may be for existing (test) devices that have used EEPROM

// setup Wifi connection
Serial.printf("\n Connecting to %s\n ", ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
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.ntpWaitForSync();
#endif

// Use WiFiClientSecure class to create TLS connection
WiFiClientSecure client;
Serial.printf("\n Connecting to %s\n", host);

#if defined ESP8266
#if HTTPS_8266_TYPE == FNGPRINT
Serial.printf(" Using fingerprint '%s'\n", fingerprint);
client.setFingerprint(fingerprint);
#else
client.setCACert((uint8_t*)ROOT_CA, strlen(ROOT_CA));
#endif
#elif defined ESP32
client.setCACert(ROOT_CA);
#endif

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

// request url from host
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") +
F("\r\nx-ESP-STA-MAC: ") + WiFi.macAddress() +
F("\r\nx-ESP-LOCIP: ") + WiFi.localIP().toString() +
F("\r\nx-ESP-SKETCH-MD5: ") + ESP.getSketchMD5() +
F("\r\nx-ESP-FLASHCHIP-ID: ") + ESP_GETFLASHCHIPID +
F("\r\nx-ESP-CHIP-ID: ") + ESP_GETCHIPID +
F("\r\nx-ESP-FLASHCHIP-SIZE: ") + ESP.getFlashChipSize() +
F("\r\nConnection: close\r\n\r\n"));

// process server respons header & return error if not 200 - OK
int code;
String actcode, deviceName;
while (client.connected()) {
String line = client.readStringUntil('\n');

if(line.startsWith(F("HTTP/1.1 "))) {
line.remove(0, 9);
code = line.substring(0, 3).toInt();

if(code != 200){
line.remove(0, 4);
Serial.print(F(" Request error!\n Code\t\t: "));
Serial.print(code);
Serial.print(F("\n Message\t: "));
Serial.println(line);
break;
}
}else if(line.startsWith(F("actcode: "))) {
line.remove(0, 9);
actcode = line;
}else if(line.startsWith(F("devname: "))) {
line.remove(0, 9);
deviceName = line;
}else if (line == "\r") {
Serial.println(F("\n Headers received"));
break;
}
}

// Close connection
client.stop();
Serial.println(F(" Closing connection"));


// If server respons code is OK and we received the activation code
if(code == 200 && actcode != ""){
Serial.println(F("\n Success! Added device to IAS.\n Updating device..."));

// create new config struc
ConfigStruct newConfig;

// read config | this gets the default settings
IAS.readConfig(newConfig);
{
// copy activation code to newConfig struct
actcode.toCharArray(newConfig.actCode, 7);
Serial.println(F(" - Added activation code."));

// copy deviceName to newConfig struct
deviceName.toCharArray(newConfig.deviceName, STRUCT_BNAME_SIZE);
Serial.print(" - Added device name: ");
Serial.println(deviceName);

// copy fingerprint to newConfig struct OR certificate to SPIFFS
#if defined ESP8266 && HTTPS_8266_TYPE == FNGPRINT
strcpy(newConfig.sha1, fingerprint);
Serial.println(F(" - Added fingerprint"));
#endif

// write config to EEPROM
IAS.writeConfig(newConfig);
delay(100);

// 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
}

// write WiFi credentials to EEPROM
if(saveWifiCred){
WiFiConnector WiFiConn;
WiFiConn.addAPtoEEPROM(ssid, password, 1);
Serial.println(F(" - Added WiFi credentials"));
}

// print divider
Serial.println(FPSTR(SER_DEV));
}else{
return;
}
}

// callback for showing update progress
IAS.onFirmwareUpdateProgress([](int written, int total){
Serial.print(".");
});

// callhome and get the latest firmware
IAS.callHome();

// if callhome failed retry every 60 sec
IAS.setCallHomeInterval(60);
}



// ================================================ LOOP =================================================
void loop() {
IAS.loop(); // this routine handles the calling home functionality,
// reaction of the MODEBUTTON pin. If short press (<4 sec): update of sketch, long press (>7 sec): Configuration
// reconnecting WiFi when the connection is lost,
// and setting the internal clock (ESP8266 for BearSSL)
}
1 change: 0 additions & 1 deletion examples/Loaders/IASLoader/IASLoader.ino
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ void setup() {
IAS.preSetDeviceName(deviceName); // preset deviceName this is also your MDNS responder: http://deviceName-123.local
IAS.preSetAppName(F("INITLoader")); // preset appName | The appName & appVersion get updated when you receive OTA updates. As this is your first app we will set it manually.
IAS.preSetAppVersion(F("1.3.1")); // preset appVersion
IAS.preSetAutoUpdate(false); // automaticUpdate (true, false)


// You can configure callback functions that can give feedback to the app user about the current state of the application.
Expand Down
4 changes: 2 additions & 2 deletions examples/VirginSoil-Full/VirginSoil-Full.ino
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ char* chosen = "0";

char* updInt = "60";
char* ledPin = "2";
char* timeZone = "0.0";
char* timeZone = "Europe/Amsterdam";



Expand Down Expand Up @@ -83,7 +83,7 @@ void setup() {

IAS.addField(updInt, "Interval", 8, 'I');
IAS.addField(ledPin, "ledPin", 2, 'P');
IAS.addField(timeZone, "Timezone", 4, 'Z');
IAS.addField(timeZone, "Timezone", 48, 'Z');



Expand Down
Loading