Skip to content

Commit 6225702

Browse files
committed
update codes, upgrade BLINKER AP/SMARTCONFIG codes.
1 parent 639a30e commit 6225702

File tree

3 files changed

+132
-27
lines changed

3 files changed

+132
-27
lines changed

src/Adapters/BlinkerMQTT.h

Lines changed: 116 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
WiFiMulti wifiMulti;
2626
#endif
2727

28-
// #include <EEPROM.h>
28+
#include <EEPROM.h>
2929

3030
#include "../modules/WebSockets/WebSocketsServer.h"
3131
#include "../modules/mqtt/Adafruit_MQTT.h"
@@ -47,6 +47,8 @@ enum b_config_t {
4747
};
4848

4949
enum b_configStatus_t {
50+
AUTO_INIT,
51+
AUTO_DONE,
5052
SMART_BEGIN,
5153
SMART_DONE,
5254
SMART_TIMEOUT,
@@ -174,14 +176,16 @@ class BlinkerMQTT : public BlinkerStream
174176
int checkPrintLimit();
175177
void parseData(const char* data);
176178

179+
bool checkConfig();
180+
177181
protected :
178182
BlinkerSharer * _sharers[BLINKER_MQTT_MAX_SHARERS_NUM];
179183
uint8_t _sharerCount = 0;
180184
uint8_t _sharerFrom = BLINKER_MQTT_FROM_AUTHER;
181185
bool _isWiFiInit = false;
182186
bool _isBegin = false;
183187
b_config_t _configType = COMM;
184-
b_configStatus_t _configStatus = SMART_BEGIN;
188+
b_configStatus_t _configStatus = AUTO_INIT;
185189
uint32_t _connectTime = 0;
186190
uint8_t _connectTimes = 0;
187191
// const char* _authKey;
@@ -2674,6 +2678,8 @@ int BlinkerMQTT::isJson(const String & data)
26742678

26752679
bool BlinkerMQTT::checkInit()
26762680
{
2681+
char ok[2 + 1] = "OK";
2682+
26772683
if (!_isWiFiInit)
26782684
{
26792685
switch (_configType)
@@ -2707,6 +2713,25 @@ bool BlinkerMQTT::checkInit()
27072713
case BLINKER_SMART_CONFIG :
27082714
switch (_configStatus)
27092715
{
2716+
case AUTO_INIT :
2717+
if (WiFi.status() != WL_CONNECTED) {
2718+
::delay(500);
2719+
return false;
2720+
}
2721+
else {
2722+
BLINKER_LOG(BLINKER_F("WiFi Connected."));
2723+
BLINKER_LOG(BLINKER_F("IP Address: "));
2724+
BLINKER_LOG(WiFi.localIP());
2725+
2726+
_isWiFiInit = true;
2727+
_connectTime = 0;
2728+
// _isWiFiInit = true;
2729+
2730+
// begin();
2731+
_configStatus = AUTO_DONE;
2732+
2733+
return false;
2734+
}
27102735
case SMART_BEGIN :
27112736
if (WiFi.smartConfigDone())
27122737
{
@@ -2739,7 +2764,14 @@ bool BlinkerMQTT::checkInit()
27392764
BLINKER_LOG(BLINKER_F("IP Address: "));
27402765
BLINKER_LOG(WiFi.localIP());
27412766
_isWiFiInit = true;
2742-
_connectTime = 0;
2767+
_connectTime = 0;
2768+
2769+
EEPROM.begin(BLINKER_EEP_SIZE);
2770+
EEPROM.put(BLINKER_EEP_ADDR_WLAN_CHECK, ok);
2771+
EEPROM.commit();
2772+
EEPROM.end();
2773+
2774+
BLINKER_LOG(BLINKER_F("Save wlan config"));
27432775

27442776
// begin();
27452777

@@ -2752,12 +2784,32 @@ bool BlinkerMQTT::checkInit()
27522784
BLINKER_LOG(BLINKER_F("Waiting for SmartConfig."));
27532785
return false;
27542786
default :
2787+
yield();
27552788
return false;
27562789
}
27572790
case BLINKER_AP_CONFIG :
27582791
#if defined(BLINKER_APCONFIG)
27592792
switch (_configStatus)
27602793
{
2794+
case AUTO_INIT :
2795+
if (WiFi.status() != WL_CONNECTED) {
2796+
::delay(500);
2797+
return false;
2798+
}
2799+
else {
2800+
BLINKER_LOG(BLINKER_F("WiFi Connected."));
2801+
BLINKER_LOG(BLINKER_F("IP Address: "));
2802+
BLINKER_LOG(WiFi.localIP());
2803+
2804+
_isWiFiInit = true;
2805+
_connectTime = 0;
2806+
// _isWiFiInit = true;
2807+
2808+
// begin();
2809+
_configStatus = AUTO_DONE;
2810+
2811+
return false;
2812+
}
27612813
case APCFG_BEGIN :
27622814
checkAPCFG();
27632815
return false;
@@ -2780,6 +2832,11 @@ bool BlinkerMQTT::checkInit()
27802832
_connectTime = 0;
27812833

27822834
// begin();
2835+
2836+
EEPROM.begin(BLINKER_EEP_SIZE);
2837+
EEPROM.put(BLINKER_EEP_ADDR_WLAN_CHECK, ok);
2838+
EEPROM.commit();
2839+
EEPROM.end();
27832840

27842841
return false;
27852842
}
@@ -2788,6 +2845,7 @@ bool BlinkerMQTT::checkInit()
27882845
softAPinit();
27892846
return false;
27902847
default :
2848+
yield();
27912849
return false;
27922850
}
27932851
#endif
@@ -2848,7 +2906,7 @@ void BlinkerMQTT::smartconfigBegin()
28482906
_configType = BLINKER_SMART_CONFIG;
28492907

28502908
if (!autoInit()) smartconfig();
2851-
else _configStatus = SMART_DONE;
2909+
// else _configStatus = SMART_DONE;
28522910

28532911
#if defined(ESP8266)
28542912
BLINKER_LOG(BLINKER_F("ESP8266_MQTT initialized..."));
@@ -2886,9 +2944,9 @@ void BlinkerMQTT::apconfigBegin()
28862944
{
28872945
#if defined(BLINKER_APCONFIG)
28882946
_configType = BLINKER_AP_CONFIG;
2889-
2947+
28902948
if (!autoInit()) softAPinit();
2891-
else _configStatus = APCFG_DONE;
2949+
// else _configStatus = APCFG_DONE;
28922950

28932951
#if defined(ESP8266)
28942952
BLINKER_LOG(BLINKER_F("ESP8266_MQTT initialized..."));
@@ -2898,6 +2956,31 @@ void BlinkerMQTT::apconfigBegin()
28982956
#endif
28992957
}
29002958

2959+
bool BlinkerMQTT::checkConfig() {
2960+
BLINKER_LOG_ALL(BLINKER_F("check wlan config"));
2961+
2962+
char ok[2 + 1];
2963+
EEPROM.begin(BLINKER_EEP_SIZE);
2964+
EEPROM.get(BLINKER_EEP_ADDR_WLAN_CHECK, ok);
2965+
EEPROM.commit();
2966+
EEPROM.end();
2967+
2968+
if (String(ok) != String("OK")) {
2969+
2970+
BLINKER_LOG(BLINKER_F("wlan config check,fail"));
2971+
2972+
// _status = BWL_CONFIG_FAIL;
2973+
return false;
2974+
}
2975+
else {
2976+
2977+
BLINKER_LOG(BLINKER_F("wlan config check,success"));
2978+
2979+
// _status = BWL_CONFIG_SUCCESS;
2980+
return true;
2981+
}
2982+
}
2983+
29012984
bool BlinkerMQTT::autoInit()
29022985
{
29032986
WiFi.mode(WIFI_STA);
@@ -2910,34 +2993,41 @@ bool BlinkerMQTT::autoInit()
29102993
WiFi.setHostname(_hostname.c_str());
29112994
#endif
29122995

2913-
WiFi.begin();
2914-
::delay(500);
2996+
if (checkConfig())
2997+
{
29152998

2916-
// BLINKER_LOG(BLINKER_F("Waiting for WiFi "),
2917-
// BLINKER_WIFI_AUTO_INIT_TIMEOUT / 1000,
2918-
// BLINKER_F("s, will enter SMARTCONFIG or "),
2919-
// BLINKER_F("APCONFIG while WiFi not connect!"));
2999+
WiFi.begin();
3000+
::delay(500);
29203001

2921-
BLINKER_LOG(BLINKER_F("Connecting to WiFi"));
3002+
// BLINKER_LOG(BLINKER_F("Waiting for WiFi "),
3003+
// BLINKER_WIFI_AUTO_INIT_TIMEOUT / 1000,
3004+
// BLINKER_F("s, will enter SMARTCONFIG or "),
3005+
// BLINKER_F("APCONFIG while WiFi not connect!"));
29223006

2923-
uint8_t _times = 0;
2924-
while (WiFi.status() != WL_CONNECTED) {
2925-
::delay(500);
2926-
// if (_times > BLINKER_WIFI_AUTO_INIT_TIMEOUT / 500) break;
2927-
// _times++;
2928-
}
3007+
BLINKER_LOG(BLINKER_F("Connecting to WiFi"));
29293008

2930-
if (WiFi.status() != WL_CONNECTED) return false;
2931-
else {
2932-
// BLINKER_LOG(BLINKER_F("WiFi Connected."));
2933-
// BLINKER_LOG(BLINKER_F("IP Address: "));
2934-
// BLINKER_LOG(WiFi.localIP());
2935-
// _isWiFiInit = true;
3009+
// uint8_t _times = 0;
3010+
// while (WiFi.status() != WL_CONNECTED) {
3011+
// ::delay(500);
3012+
// // if (_times > BLINKER_WIFI_AUTO_INIT_TIMEOUT / 500) break;
3013+
// // _times++;
3014+
// }
3015+
3016+
// if (WiFi.status() != WL_CONNECTED) return false;
3017+
// else {
3018+
// // BLINKER_LOG(BLINKER_F("WiFi Connected."));
3019+
// // BLINKER_LOG(BLINKER_F("IP Address: "));
3020+
// // BLINKER_LOG(WiFi.localIP());
3021+
// // _isWiFiInit = true;
29363022

2937-
// begin();
3023+
// // begin();
29383024

3025+
// return true;
3026+
// }
29393027
return true;
29403028
}
3029+
3030+
return false;
29413031
}
29423032

29433033
void BlinkerMQTT::smartconfig()

src/Blinker/BlinkerApi.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,20 @@ class BlinkerApi : public BlinkerProtocol
613613
// void initCheck(uint32_t timeout = BLINKER_STREAM_TIMEOUT*10);
614614
#endif
615615

616+
#if defined(BLINKER_WIFI) || defined(BLINKER_MQTT)
617+
void reset()
618+
{
619+
BLINKER_LOG(BLINKER_F("Blinker reset..."));
620+
char config_check[3] = {0};
621+
EEPROM.begin(BLINKER_EEP_SIZE);
622+
EEPROM.put(BLINKER_EEP_ADDR_WLAN_CHECK, config_check);
623+
EEPROM.commit();
624+
EEPROM.end();
625+
626+
ESP.restart();
627+
}
628+
#endif
629+
616630
#if defined(BLINKER_MQTT_AT)
617631
void initCheck(const String & _data, uint32_t timeout = BLINKER_STREAM_TIMEOUT*10);
618632
int analogRead();

src/Blinker/BlinkerConfig.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1638,7 +1638,8 @@ Success--[AT+MIPLDISCOVERRSP=0,22903,1,24,"5850;5851;5852;5853;5750"]
16381638
defined(BLINKER_PRO_ESP) || defined(BLINKER_LOWPOWER_AIR202) || \
16391639
defined(BLINKER_WIFI_GATEWAY) || defined(BLINKER_WIFI_SUBDEVICE) || \
16401640
defined(BLINKER_QRCODE_NBIOT_SIM7020) || defined(BLINKER_NBIOT_SIM7000) || \
1641-
defined(BLINKER_QRCODE_NBIOT_SIM7000)
1641+
defined(BLINKER_QRCODE_NBIOT_SIM7000) || defined(BLINKER_WIFI) || \
1642+
defined(BLINKER_MQTT)
16421643

16431644
#ifndef BLINKER_BUTTON_PIN
16441645
#define BLINKER_BUTTON_PIN 2

0 commit comments

Comments
 (0)