25
25
WiFiMulti wifiMulti;
26
26
#endif
27
27
28
- // #include <EEPROM.h>
28
+ #include < EEPROM.h>
29
29
30
30
#include " ../modules/WebSockets/WebSocketsServer.h"
31
31
#include " ../modules/mqtt/Adafruit_MQTT.h"
@@ -47,6 +47,8 @@ enum b_config_t {
47
47
};
48
48
49
49
enum b_configStatus_t {
50
+ AUTO_INIT,
51
+ AUTO_DONE,
50
52
SMART_BEGIN,
51
53
SMART_DONE,
52
54
SMART_TIMEOUT,
@@ -174,14 +176,16 @@ class BlinkerMQTT : public BlinkerStream
174
176
int checkPrintLimit ();
175
177
void parseData (const char * data);
176
178
179
+ bool checkConfig ();
180
+
177
181
protected :
178
182
BlinkerSharer * _sharers[BLINKER_MQTT_MAX_SHARERS_NUM];
179
183
uint8_t _sharerCount = 0 ;
180
184
uint8_t _sharerFrom = BLINKER_MQTT_FROM_AUTHER;
181
185
bool _isWiFiInit = false ;
182
186
bool _isBegin = false ;
183
187
b_config_t _configType = COMM;
184
- b_configStatus_t _configStatus = SMART_BEGIN ;
188
+ b_configStatus_t _configStatus = AUTO_INIT ;
185
189
uint32_t _connectTime = 0 ;
186
190
uint8_t _connectTimes = 0 ;
187
191
// const char* _authKey;
@@ -2674,6 +2678,8 @@ int BlinkerMQTT::isJson(const String & data)
2674
2678
2675
2679
bool BlinkerMQTT::checkInit ()
2676
2680
{
2681
+ char ok[2 + 1 ] = " OK" ;
2682
+
2677
2683
if (!_isWiFiInit)
2678
2684
{
2679
2685
switch (_configType)
@@ -2707,6 +2713,25 @@ bool BlinkerMQTT::checkInit()
2707
2713
case BLINKER_SMART_CONFIG :
2708
2714
switch (_configStatus)
2709
2715
{
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
+ }
2710
2735
case SMART_BEGIN :
2711
2736
if (WiFi.smartConfigDone ())
2712
2737
{
@@ -2739,7 +2764,14 @@ bool BlinkerMQTT::checkInit()
2739
2764
BLINKER_LOG (BLINKER_F (" IP Address: " ));
2740
2765
BLINKER_LOG (WiFi.localIP ());
2741
2766
_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" ));
2743
2775
2744
2776
// begin();
2745
2777
@@ -2752,12 +2784,32 @@ bool BlinkerMQTT::checkInit()
2752
2784
BLINKER_LOG (BLINKER_F (" Waiting for SmartConfig." ));
2753
2785
return false ;
2754
2786
default :
2787
+ yield ();
2755
2788
return false ;
2756
2789
}
2757
2790
case BLINKER_AP_CONFIG :
2758
2791
#if defined(BLINKER_APCONFIG)
2759
2792
switch (_configStatus)
2760
2793
{
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
+ }
2761
2813
case APCFG_BEGIN :
2762
2814
checkAPCFG ();
2763
2815
return false ;
@@ -2780,6 +2832,11 @@ bool BlinkerMQTT::checkInit()
2780
2832
_connectTime = 0 ;
2781
2833
2782
2834
// begin();
2835
+
2836
+ EEPROM.begin (BLINKER_EEP_SIZE);
2837
+ EEPROM.put (BLINKER_EEP_ADDR_WLAN_CHECK, ok);
2838
+ EEPROM.commit ();
2839
+ EEPROM.end ();
2783
2840
2784
2841
return false ;
2785
2842
}
@@ -2788,6 +2845,7 @@ bool BlinkerMQTT::checkInit()
2788
2845
softAPinit ();
2789
2846
return false ;
2790
2847
default :
2848
+ yield ();
2791
2849
return false ;
2792
2850
}
2793
2851
#endif
@@ -2848,7 +2906,7 @@ void BlinkerMQTT::smartconfigBegin()
2848
2906
_configType = BLINKER_SMART_CONFIG;
2849
2907
2850
2908
if (!autoInit ()) smartconfig ();
2851
- else _configStatus = SMART_DONE;
2909
+ // else _configStatus = SMART_DONE;
2852
2910
2853
2911
#if defined(ESP8266)
2854
2912
BLINKER_LOG (BLINKER_F (" ESP8266_MQTT initialized..." ));
@@ -2886,9 +2944,9 @@ void BlinkerMQTT::apconfigBegin()
2886
2944
{
2887
2945
#if defined(BLINKER_APCONFIG)
2888
2946
_configType = BLINKER_AP_CONFIG;
2889
-
2947
+
2890
2948
if (!autoInit ()) softAPinit ();
2891
- else _configStatus = APCFG_DONE;
2949
+ // else _configStatus = APCFG_DONE;
2892
2950
2893
2951
#if defined(ESP8266)
2894
2952
BLINKER_LOG (BLINKER_F (" ESP8266_MQTT initialized..." ));
@@ -2898,6 +2956,31 @@ void BlinkerMQTT::apconfigBegin()
2898
2956
#endif
2899
2957
}
2900
2958
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
+
2901
2984
bool BlinkerMQTT::autoInit ()
2902
2985
{
2903
2986
WiFi.mode (WIFI_STA);
@@ -2910,34 +2993,41 @@ bool BlinkerMQTT::autoInit()
2910
2993
WiFi.setHostname (_hostname.c_str ());
2911
2994
#endif
2912
2995
2913
- WiFi. begin ();
2914
- ::delay ( 500 );
2996
+ if ( checkConfig ())
2997
+ {
2915
2998
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 );
2920
3001
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!"));
2922
3006
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" ));
2929
3008
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;
2936
3022
2937
- // begin();
3023
+ // // begin();
2938
3024
3025
+ // return true;
3026
+ // }
2939
3027
return true ;
2940
3028
}
3029
+
3030
+ return false ;
2941
3031
}
2942
3032
2943
3033
void BlinkerMQTT::smartconfig ()
0 commit comments