Skip to content
This repository has been archived by the owner on Feb 9, 2022. It is now read-only.

Commit

Permalink
Major Release v1.0.5
Browse files Browse the repository at this point in the history
### Major Releases v1.0.5

1. Multiple WiFi Credentials (SSID, Password) and system will autoconnect to the best and available WiFi SSID.
2. Multiple Blynk Credentials (Server, Token) and system will autoconnect to the available Blynk Servers.
3. New powerful-yet-simple-to-use feature to enable adding dynamic custom parameters from sketch and input using the same Config Portal. Config Portal will be auto-adjusted to match the number of dynamic parameters.
4. Dynamic custom parameters to be saved automatically in EEPROM, or SPIFFS.
5. WiFi Password max length increased to 63 from 31, according to WPA2 standard.
6. Permit to input special chars such as % and # into data fields.
7. Config Portal AP Channel is configurable (either static or random channel) to avoid channel conflict to other APs.
  • Loading branch information
khoih-prog authored Apr 19, 2020
1 parent 82fd0af commit 2a7a891
Show file tree
Hide file tree
Showing 17 changed files with 2,232 additions and 267 deletions.
479 changes: 417 additions & 62 deletions README.md

Large diffs are not rendered by default.

212 changes: 196 additions & 16 deletions examples/ESP32_BLE_WF/ESP32_BLE_WF.ino
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Forked from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases
Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_ESPManager
Licensed under MIT license
Version: 1.0.4
Version: 1.0.5
Based on orignal code by Crosswalkersam (https://community.blynk.cc/u/Crosswalkersam)
posted in https://community.blynk.cc/t/select-connection-type-via-switch/43176
Expand All @@ -20,25 +20,39 @@
1.0.2 K Hoang 04/02/2020 Add Blynk WiFiManager support similar to Blynk_WM library
1.0.3 K Hoang 24/02/2020 Add checksum, clearConfigData()
1.0.4 K Hoang 14/03/2020 Enhance GUI. Reduce code size.
1.0.5 K Hoang 18/04/2020 MultiWiFi/Blynk. Dynamic custom parameters. SSID password maxlen is 63 now.
Permit special chars # and % in input data.
*****************************************************************************************************************************/

/****************************************************************************************************************************
Important Notes:
1) Sketch is ~0.9MB of code because only 1 instance of Blynk if #define BLYNK_USE_BT_ONLY => true
2) Sketch is very large (~1.3MB code) because 2 instances of Blynk if #define BLYNK_USE_BT_ONLY => false
3) To conmpile, use Partition Scheem with large APP size, such as
a) 8MB Flash (3MB APP, 1.5MB FAT) if use EEPROM
b) No OTA (2MB APP, 2MB SPIFFS)
c) No OTA (2MB APP, 2MB FATFS) if use EEPROM
d) Huge APP (3MB No OTA, 1MB SPIFFS) <===== Preferable if use SPIFFS
e) Minimal SPIFFS (1.9MB APP with OTA, 190KB SPIFFS)
*****************************************************************************************************************************/
#ifndef ESP32
#error This code is intended to run on the ESP32 platform! Please check your Tools->Board setting.
#endif

#define BLYNK_PRINT Serial

#define ESP32_BLE_WF_DEBUG true

#define USE_BLYNK_WM true
//#define USE_BLYNK_WM false

#define USE_SPIFFS true
//#define USE_SPIFFS false
//#define USE_SPIFFS true
#define USE_SPIFFS false

#if (!USE_SPIFFS)
// EEPROM_SIZE must be <= 2048 and >= CONFIG_DATA_SIZE
#define EEPROM_SIZE (2 * 1024)
// EEPROM_START + CONFIG_DATA_SIZE must be <= EEPROM_SIZE
#define EEPROM_START 768
#define EEPROM_START 0
#endif

// Force some params in Blynk, only valid for library version 1.0.1 and later
Expand All @@ -58,6 +72,64 @@
#if USE_BLYNK_WM
#warning Please select 1.3MB+ for APP (Minimal SPIFFS (1.9MB APP, OTA), HugeAPP(3MB APP, NoOTA) or NoOA(2MB APP)
#include <BlynkSimpleEsp32_WFM.h>

#define USE_DYNAMIC_PARAMETERS true

/////////////// Start dynamic Credentials ///////////////

//Defined in <BlynkSimpleEsp32_WFM.h>
/**************************************
#define MAX_ID_LEN 5
#define MAX_DISPLAY_NAME_LEN 16
typedef struct
{
char id [MAX_ID_LEN + 1];
char displayName [MAX_DISPLAY_NAME_LEN + 1];
char *pdata;
uint8_t maxlen;
} MenuItem;
**************************************/

#if USE_DYNAMIC_PARAMETERS

#define MAX_MQTT_SERVER_LEN 34
char MQTT_Server [MAX_MQTT_SERVER_LEN + 1] = "";

#define MAX_MQTT_PORT_LEN 6
char MQTT_Port [MAX_MQTT_PORT_LEN + 1] = "";

#define MAX_MQTT_USERNAME_LEN 34
char MQTT_UserName [MAX_MQTT_USERNAME_LEN + 1] = "";

#define MAX_MQTT_PW_LEN 34
char MQTT_PW [MAX_MQTT_PW_LEN + 1] = "";

#define MAX_MQTT_SUBS_TOPIC_LEN 34
char MQTT_SubsTopic [MAX_MQTT_SUBS_TOPIC_LEN + 1] = "";

#define MAX_MQTT_PUB_TOPIC_LEN 34
char MQTT_PubTopic [MAX_MQTT_PUB_TOPIC_LEN + 1] = "";

MenuItem myMenuItems [] =
{
{ "mqtt", "MQTT Server", MQTT_Server, MAX_MQTT_SERVER_LEN },
{ "mqpt", "Port", MQTT_Port, MAX_MQTT_PORT_LEN },
{ "user", "MQTT UserName", MQTT_UserName, MAX_MQTT_USERNAME_LEN },
{ "mqpw", "MQTT PWD", MQTT_PW, MAX_MQTT_PW_LEN },
{ "subs", "Subs Topics", MQTT_SubsTopic, MAX_MQTT_SUBS_TOPIC_LEN },
{ "pubs", "Pubs Topics", MQTT_PubTopic, MAX_MQTT_PUB_TOPIC_LEN },
};

#else

MenuItem myMenuItems [] = {};

#endif

uint16_t NUM_MENU_ITEMS = sizeof(myMenuItems) / sizeof(MenuItem); //MenuItemSize;
/////// // End dynamic Credentials ///////////

#else
#include <BlynkSimpleEsp32_WF.h>

Expand All @@ -77,23 +149,68 @@ char auth[] = "****";
bool USE_BLE = true;
long timePreviousMeassure = 0;

#define MEASURE_INTERVAL_MS 20000L

#define WIFI_BLE_SELECTION_PIN 14 //Pin D14 mapped to pin GPIO14/HSPI_SCK/ADC16/TOUCH6/TMS of ESP32

void checkStatus()
BlynkTimer timer;

#include <Ticker.h>
Ticker led_ticker;

void set_led(byte status)
{
if (millis() - timePreviousMeassure > MEASURE_INTERVAL_MS)
digitalWrite(LED_BUILTIN, status);
}

void noticeAlive(void)
{
if (USE_BLE)
Blynk_BLE.virtualWrite(V0, F("OK"));
else
Blynk_WF.virtualWrite(V0, F("OK"));
}

void heartBeatPrint(void)
{
static int num = 1;

if (Blynk.connected())
{
set_led(HIGH);
led_ticker.once_ms(111, set_led, (byte) LOW);
Serial.print("B");
}
else
{
Serial.print("F");
}

if (num == 80)
{
Serial.println();
num = 1;
}
else if (num++ % 10 == 0)
{
Serial.print(" ");
}
}

void checkStatus()
{
static unsigned long checkstatus_timeout = 0;

#define STATUS_CHECK_INTERVAL 60000L

// Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to send updates frequently if there is no status change.
if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
{
if (!USE_BLE)
{
if (Blynk.connected())
Serial.println(F("B"));
else
Serial.println(F("F"));
// report Blynk connection
heartBeatPrint();
}

timePreviousMeassure = millis();
checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL;
}
}

Expand All @@ -104,19 +221,34 @@ void setup()
Serial.begin(115200);
Serial.println(F("\nStarting ESP32_BLE_WF"));

pinMode(WIFI_BLE_SELECTION_PIN, INPUT);
pinMode(WIFI_BLE_SELECTION_PIN, INPUT_PULLUP);

#if BLYNK_USE_BLE_ONLY
Blynk_BLE.setDeviceName(BLE_Device_Name);

#if ESP32_BLE_WF_DEBUG
Serial.println(F("Blynk_BLE begin"));
#endif

Blynk_BLE.begin(auth);
#else
if (digitalRead(WIFI_BLE_SELECTION_PIN) == HIGH)
{
USE_BLE = false;
Serial.println(F("GPIO14 HIGH, Use WiFi"));
#if USE_BLYNK_WM
#if ESP32_BLE_WF_DEBUG
Serial.println(F("USE_BLYNK_WM: Blynk_WF begin"));
#endif
// Set config portal channel, defalut = 1. Use 0 => random channel from 1-13 to avoid conflict
Blynk_WF.setConfigPortalChannel(0);

Blynk_WF.begin(BLE_Device_Name);
#else
//Blynk_WF.begin(auth, ssid, pass);
#if ESP32_BLE_WF_DEBUG
Serial.println(F("Not USE_BLYNK_WM: Blynk_WF begin"));
#endif
Blynk_WF.begin(auth, ssid, pass, cloudBlynkServer.c_str(), BLYNK_SERVER_HARDWARE_PORT);
#endif
}
Expand All @@ -126,10 +258,15 @@ void setup()
Serial.println(F("GPIO14 LOW, Use BLE"));
Blynk_BLE.setDeviceName(BLE_Device_Name);
#if USE_BLYNK_WM
if (Blynk_WF.getBlynkBLEToken() == String("nothing"))
if (Blynk_WF.getBlynkBLEToken() == NO_CONFIG) //String("blank"))
{
Serial.println(F("No valid stored BLE auth. Have to run WiFi then enter config portal"));
USE_BLE = false;

#if ESP32_BLE_WF_DEBUG
Serial.println(F("USE_BLYNK_WM: No BLE Token. Blynk_WF begin"));
#endif

Blynk_WF.begin(BLE_Device_Name);
}
String BLE_auth = Blynk_WF.getBlynkBLEToken();
Expand All @@ -141,11 +278,32 @@ void setup()
{
Serial.print(F("Connecting Blynk via BLE, using auth = "));
Serial.println(BLE_auth);

#if ESP32_BLE_WF_DEBUG
Serial.println(F("USE_BLE: Blynk_BLE begin"));
#endif

Blynk_BLE.begin(BLE_auth.c_str());
}
}
#endif

// Important, need to keep constant communication to Blynk Server at least once per ~25s
// Or Blynk will lost and have to (auto)reconnect
timer.setInterval(10000L, noticeAlive);
}

#if (USE_BLYNK_WM && USE_DYNAMIC_PARAMETERS)
void displayCredentials(void)
{
Serial.println("\nYour stored Credentials :");

for (int i = 0; i < NUM_MENU_ITEMS; i++)
{
Serial.println(String(myMenuItems[i].displayName) + " = " + myMenuItems[i].pdata);
}
}
#endif

void loop()
{
Expand All @@ -158,5 +316,27 @@ void loop()
Blynk_WF.run();
#endif

timer.run();
checkStatus();

#if (USE_BLYNK_WM && USE_DYNAMIC_PARAMETERS)
static bool displayedCredentials = false;

if (!displayedCredentials)
{
for (int i = 0; i < NUM_MENU_ITEMS; i++)
{
if (!strlen(myMenuItems[i].pdata))
{
break;
}

if ( i == (NUM_MENU_ITEMS - 1) )
{
displayedCredentials = true;
displayCredentials();
}
}
}
#endif
}
Loading

0 comments on commit 2a7a891

Please sign in to comment.