Skip to content

Commit

Permalink
update codes, upgrade BLINKER AP/SMARTCONFIG codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
i3water committed Feb 19, 2021
1 parent 639a30e commit 6225702
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 27 deletions.
142 changes: 116 additions & 26 deletions src/Adapters/BlinkerMQTT.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
WiFiMulti wifiMulti;
#endif

// #include <EEPROM.h>
#include <EEPROM.h>

#include "../modules/WebSockets/WebSocketsServer.h"
#include "../modules/mqtt/Adafruit_MQTT.h"
Expand All @@ -47,6 +47,8 @@ enum b_config_t {
};

enum b_configStatus_t {
AUTO_INIT,
AUTO_DONE,
SMART_BEGIN,
SMART_DONE,
SMART_TIMEOUT,
Expand Down Expand Up @@ -174,14 +176,16 @@ class BlinkerMQTT : public BlinkerStream
int checkPrintLimit();
void parseData(const char* data);

bool checkConfig();

protected :
BlinkerSharer * _sharers[BLINKER_MQTT_MAX_SHARERS_NUM];
uint8_t _sharerCount = 0;
uint8_t _sharerFrom = BLINKER_MQTT_FROM_AUTHER;
bool _isWiFiInit = false;
bool _isBegin = false;
b_config_t _configType = COMM;
b_configStatus_t _configStatus = SMART_BEGIN;
b_configStatus_t _configStatus = AUTO_INIT;
uint32_t _connectTime = 0;
uint8_t _connectTimes = 0;
// const char* _authKey;
Expand Down Expand Up @@ -2674,6 +2678,8 @@ int BlinkerMQTT::isJson(const String & data)

bool BlinkerMQTT::checkInit()
{
char ok[2 + 1] = "OK";

if (!_isWiFiInit)
{
switch (_configType)
Expand Down Expand Up @@ -2707,6 +2713,25 @@ bool BlinkerMQTT::checkInit()
case BLINKER_SMART_CONFIG :
switch (_configStatus)
{
case AUTO_INIT :
if (WiFi.status() != WL_CONNECTED) {
::delay(500);
return false;
}
else {
BLINKER_LOG(BLINKER_F("WiFi Connected."));
BLINKER_LOG(BLINKER_F("IP Address: "));
BLINKER_LOG(WiFi.localIP());

_isWiFiInit = true;
_connectTime = 0;
// _isWiFiInit = true;

// begin();
_configStatus = AUTO_DONE;

return false;
}
case SMART_BEGIN :
if (WiFi.smartConfigDone())
{
Expand Down Expand Up @@ -2739,7 +2764,14 @@ bool BlinkerMQTT::checkInit()
BLINKER_LOG(BLINKER_F("IP Address: "));
BLINKER_LOG(WiFi.localIP());
_isWiFiInit = true;
_connectTime = 0;
_connectTime = 0;

EEPROM.begin(BLINKER_EEP_SIZE);
EEPROM.put(BLINKER_EEP_ADDR_WLAN_CHECK, ok);
EEPROM.commit();
EEPROM.end();

BLINKER_LOG(BLINKER_F("Save wlan config"));

// begin();

Expand All @@ -2752,12 +2784,32 @@ bool BlinkerMQTT::checkInit()
BLINKER_LOG(BLINKER_F("Waiting for SmartConfig."));
return false;
default :
yield();
return false;
}
case BLINKER_AP_CONFIG :
#if defined(BLINKER_APCONFIG)
switch (_configStatus)
{
case AUTO_INIT :
if (WiFi.status() != WL_CONNECTED) {
::delay(500);
return false;
}
else {
BLINKER_LOG(BLINKER_F("WiFi Connected."));
BLINKER_LOG(BLINKER_F("IP Address: "));
BLINKER_LOG(WiFi.localIP());

_isWiFiInit = true;
_connectTime = 0;
// _isWiFiInit = true;

// begin();
_configStatus = AUTO_DONE;

return false;
}
case APCFG_BEGIN :
checkAPCFG();
return false;
Expand All @@ -2780,6 +2832,11 @@ bool BlinkerMQTT::checkInit()
_connectTime = 0;

// begin();

EEPROM.begin(BLINKER_EEP_SIZE);
EEPROM.put(BLINKER_EEP_ADDR_WLAN_CHECK, ok);
EEPROM.commit();
EEPROM.end();

return false;
}
Expand All @@ -2788,6 +2845,7 @@ bool BlinkerMQTT::checkInit()
softAPinit();
return false;
default :
yield();
return false;
}
#endif
Expand Down Expand Up @@ -2848,7 +2906,7 @@ void BlinkerMQTT::smartconfigBegin()
_configType = BLINKER_SMART_CONFIG;

if (!autoInit()) smartconfig();
else _configStatus = SMART_DONE;
// else _configStatus = SMART_DONE;

#if defined(ESP8266)
BLINKER_LOG(BLINKER_F("ESP8266_MQTT initialized..."));
Expand Down Expand Up @@ -2886,9 +2944,9 @@ void BlinkerMQTT::apconfigBegin()
{
#if defined(BLINKER_APCONFIG)
_configType = BLINKER_AP_CONFIG;

if (!autoInit()) softAPinit();
else _configStatus = APCFG_DONE;
// else _configStatus = APCFG_DONE;

#if defined(ESP8266)
BLINKER_LOG(BLINKER_F("ESP8266_MQTT initialized..."));
Expand All @@ -2898,6 +2956,31 @@ void BlinkerMQTT::apconfigBegin()
#endif
}

bool BlinkerMQTT::checkConfig() {
BLINKER_LOG_ALL(BLINKER_F("check wlan config"));

char ok[2 + 1];
EEPROM.begin(BLINKER_EEP_SIZE);
EEPROM.get(BLINKER_EEP_ADDR_WLAN_CHECK, ok);
EEPROM.commit();
EEPROM.end();

if (String(ok) != String("OK")) {

BLINKER_LOG(BLINKER_F("wlan config check,fail"));

// _status = BWL_CONFIG_FAIL;
return false;
}
else {

BLINKER_LOG(BLINKER_F("wlan config check,success"));

// _status = BWL_CONFIG_SUCCESS;
return true;
}
}

bool BlinkerMQTT::autoInit()
{
WiFi.mode(WIFI_STA);
Expand All @@ -2910,34 +2993,41 @@ bool BlinkerMQTT::autoInit()
WiFi.setHostname(_hostname.c_str());
#endif

WiFi.begin();
::delay(500);
if (checkConfig())
{

// BLINKER_LOG(BLINKER_F("Waiting for WiFi "),
// BLINKER_WIFI_AUTO_INIT_TIMEOUT / 1000,
// BLINKER_F("s, will enter SMARTCONFIG or "),
// BLINKER_F("APCONFIG while WiFi not connect!"));
WiFi.begin();
::delay(500);

BLINKER_LOG(BLINKER_F("Connecting to WiFi"));
// BLINKER_LOG(BLINKER_F("Waiting for WiFi "),
// BLINKER_WIFI_AUTO_INIT_TIMEOUT / 1000,
// BLINKER_F("s, will enter SMARTCONFIG or "),
// BLINKER_F("APCONFIG while WiFi not connect!"));

uint8_t _times = 0;
while (WiFi.status() != WL_CONNECTED) {
::delay(500);
// if (_times > BLINKER_WIFI_AUTO_INIT_TIMEOUT / 500) break;
// _times++;
}
BLINKER_LOG(BLINKER_F("Connecting to WiFi"));

if (WiFi.status() != WL_CONNECTED) return false;
else {
// BLINKER_LOG(BLINKER_F("WiFi Connected."));
// BLINKER_LOG(BLINKER_F("IP Address: "));
// BLINKER_LOG(WiFi.localIP());
// _isWiFiInit = true;
// uint8_t _times = 0;
// while (WiFi.status() != WL_CONNECTED) {
// ::delay(500);
// // if (_times > BLINKER_WIFI_AUTO_INIT_TIMEOUT / 500) break;
// // _times++;
// }

// if (WiFi.status() != WL_CONNECTED) return false;
// else {
// // BLINKER_LOG(BLINKER_F("WiFi Connected."));
// // BLINKER_LOG(BLINKER_F("IP Address: "));
// // BLINKER_LOG(WiFi.localIP());
// // _isWiFiInit = true;

// begin();
// // begin();

// return true;
// }
return true;
}

return false;
}

void BlinkerMQTT::smartconfig()
Expand Down
14 changes: 14 additions & 0 deletions src/Blinker/BlinkerApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,20 @@ class BlinkerApi : public BlinkerProtocol
// void initCheck(uint32_t timeout = BLINKER_STREAM_TIMEOUT*10);
#endif

#if defined(BLINKER_WIFI) || defined(BLINKER_MQTT)
void reset()
{
BLINKER_LOG(BLINKER_F("Blinker reset..."));
char config_check[3] = {0};
EEPROM.begin(BLINKER_EEP_SIZE);
EEPROM.put(BLINKER_EEP_ADDR_WLAN_CHECK, config_check);
EEPROM.commit();
EEPROM.end();

ESP.restart();
}
#endif

#if defined(BLINKER_MQTT_AT)
void initCheck(const String & _data, uint32_t timeout = BLINKER_STREAM_TIMEOUT*10);
int analogRead();
Expand Down
3 changes: 2 additions & 1 deletion src/Blinker/BlinkerConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -1638,7 +1638,8 @@ Success--[AT+MIPLDISCOVERRSP=0,22903,1,24,"5850;5851;5852;5853;5750"]
defined(BLINKER_PRO_ESP) || defined(BLINKER_LOWPOWER_AIR202) || \
defined(BLINKER_WIFI_GATEWAY) || defined(BLINKER_WIFI_SUBDEVICE) || \
defined(BLINKER_QRCODE_NBIOT_SIM7020) || defined(BLINKER_NBIOT_SIM7000) || \
defined(BLINKER_QRCODE_NBIOT_SIM7000)
defined(BLINKER_QRCODE_NBIOT_SIM7000) || defined(BLINKER_WIFI) || \
defined(BLINKER_MQTT)

#ifndef BLINKER_BUTTON_PIN
#define BLINKER_BUTTON_PIN 2
Expand Down

0 comments on commit 6225702

Please sign in to comment.