Skip to content

Commit

Permalink
AP mode
Browse files Browse the repository at this point in the history
  • Loading branch information
vitotai committed Jan 21, 2017
1 parent 40cf581 commit f1ac241
Show file tree
Hide file tree
Showing 16 changed files with 393 additions and 96 deletions.
156 changes: 108 additions & 48 deletions BrewManiacEx/BrewManiacEx.ino
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@

extern void brewmaniac_setup();
extern void brewmaniac_loop();

#define ResponseAppleCNA true

/**************************************************************************************/
/**************************************************************************************/

Expand Down Expand Up @@ -161,6 +164,7 @@ R"END(
}
)END";

void requestRestart(bool disc);

class NetworkConfig:public AsyncWebHandler
{
Expand Down Expand Up @@ -189,6 +193,12 @@ public:
request->send(400);
return;
}
if(root.containsKey("disconnect")){
requestRestart(true);
request->send(200);
return;
}

File config=SPIFFS.open(CONFIG_FILENAME,"w+");
if(!config){
request->send(500);
Expand Down Expand Up @@ -538,16 +548,37 @@ void bmwEventHandler(BrewManiacWeb* bmw, BmwEventType event)
}
}

#if ResponseAppleCNA == true

HttpUpdateHandler httpUpdateHandler(FIRMWARE_UPDATE_URL,JS_UPDATE_URL);
unsigned long _connectionTime;
byte _wifiState;
class AppleCNAHandler: public AsyncWebHandler
{
public:
AppleCNAHandler(){}
void handleRequest(AsyncWebServerRequest *request){
request->send(200, "text/html", "<HTML><HEAD><TITLE>Success</TITLE></HEAD><BODY>Success</BODY></HTML>");
}
bool canHandle(AsyncWebServerRequest *request){
String host=request->host();
//DBG_PRINTF("Request host:");
//DBG_PRINTF(host.c_str());
//DBG_PRINTF("\n");
if(host.indexOf(String("apple")) >=0
|| host.indexOf(String("itools")) >=0
|| host.indexOf(String("ibook")) >=0
|| host.indexOf(String("airport")) >=0
|| host.indexOf(String("thinkdifferent")) >=0
|| host.indexOf(String("akamai")) >=0 ){
return true;
}
return false;
}
};

#define WiFiStateConnected 0
#define WiFiStateWaitToConnect 1
#define WiFiStateConnecting 2
#define TIME_WAIT_TO_CONNECT 10000
#define TIME_RECONNECT_TIMEOUT 10000
AppleCNAHandler appleCNAHandler;

#endif //#if ResponseAppleCNA == true

HttpUpdateHandler httpUpdateHandler(FIRMWARE_UPDATE_URL,JS_UPDATE_URL);

bool testSPIFFS(void)
{
Expand All @@ -574,12 +605,23 @@ bool testSPIFFS(void)
DebugOut(c.c_str());
return true;
}
#define PROFILING true
#define PROFILING false
#if PROFILING == true
unsigned long _profileMaximumLoop=0;
unsigned long _profileLoopBegin;
#endif

void displayIP(bool apmode){
IPV4Address ip;
if(apmode){
ip.dword = WiFi.softAPIP();
bmWeb.setIp(ip.bytes,true);
}else{
ip.dword = WiFi.localIP();
bmWeb.setIp(ip.bytes);
}
}

void setup(void){
//0. initilze debug port
#if SerialDebug == true
Expand Down Expand Up @@ -611,17 +653,18 @@ void setup(void){


//3. Start WiFi
WiFiSetup::begin(_gHostname);
_wifiState=WiFiStateConnected;
WiFiSetup.begin(_gHostname);

DebugOut("Connected! IP address: ");
DebugOut(WiFi.localIP());
if (!MDNS.begin(_gHostname)) {
DebugOut("Error setting mDNS responder");
}
// TODO: SSDP responder

TimeKeeper.begin("time.nist.gov","time.windows.com","de.pool.ntp.org");
if(WiFiSetup.isApMode())
TimeKeeper.begin(false);
else
TimeKeeper.begin("time.nist.gov","time.windows.com","de.pool.ntp.org");

//4. check version
bool forcedUpdate;
Expand Down Expand Up @@ -656,17 +699,21 @@ void setup(void){
//5.2 Normal serving pages
//5.2.1 status report through SSE
#if UseWebSocket == true
ws.onEvent(onWsEvent);
server.addHandler(&ws);
ws.onEvent(onWsEvent);
server.addHandler(&ws);
#endif

#if UseServerSideEvent == true
sse.onConnect(sseConnect);
server.addHandler(&sse);
sse.onConnect(sseConnect);
server.addHandler(&sse);
#endif
server.addHandler(&networkConfig);
server.addHandler(&bmwHandler);

#if ResponseAppleCNA == true
if(WiFiSetup.isApMode())
server.addHandler(&appleCNAHandler);
#endif
server.addHandler(&logHandler);
//5.2.2 SPIFFS is part of the serving pages
//securedAccess need additional check
Expand Down Expand Up @@ -705,12 +752,27 @@ void setup(void){
ESPUpdateServer_setup(_gUsername,_gPassword);

// 9. display IP
IPV4Address ip;
ip.dword = WiFi.localIP();
bmWeb.setIp(ip.bytes);
displayIP(WiFiSetup.isApMode());

DebugOut("End Setup\n");
}

#define SystemStateOperating 0
#define SystemStateRestartPending 1
#define SystemStateWaitRestart 2

#define TIME_RESTART_TIMEOUT 3000

bool _disconnectBeforeRestart;
static unsigned long _time;
byte _systemState=SystemStateOperating;
void requestRestart(bool disc)
{
_disconnectBeforeRestart=disc;
_systemState =SystemStateRestartPending;
}

#define IS_RESTARTING (_systemState!=SystemStateOperating)


void loop(void){
Expand All @@ -723,39 +785,37 @@ void loop(void){

brewmaniac_loop();

if(WiFi.status() != WL_CONNECTED)
{
if(_wifiState==WiFiStateConnected)
{
byte nullIp[4]={0,0,0,0};
bmWeb.setIp(nullIp);

_connectionTime=millis();
_wifiState = WiFiStateWaitToConnect;
}
else if(_wifiState==WiFiStateWaitToConnect)
{
if((millis() - _connectionTime) > TIME_WAIT_TO_CONNECT)
{
WiFi.begin();
_connectionTime=millis();
_wifiState = WiFiStateConnecting;
}
}
else if(_wifiState==WiFiStateConnecting)
{
if((millis() - _connectionTime) > TIME_RECONNECT_TIMEOUT){
ESP.restart();
if(WiFiSetup.stayConnected()){
if(WiFiSetup.isApMode()){
TimeKeeper.setInternetAccessibility(false);
displayIP(true);
}else{
if(WiFi.status() != WL_CONNECTED){
uint8_t nullip[]={0,0,0,0};
bmWeb.setIp(nullip);
}else{
displayIP(false);
}
}
}
else
{
_wifiState=WiFiStateConnected;
}
}

httpUpdateHandler.runUpdate();

if(_systemState ==SystemStateRestartPending){
_time=millis();
_systemState =SystemStateWaitRestart;
}else if(_systemState ==SystemStateWaitRestart){
if((millis() - _time) > TIME_RESTART_TIMEOUT){
if(_disconnectBeforeRestart){
WiFi.disconnect();
WiFiSetup.setAutoReconnect(false);
delay(1000);
}
// ESP.restart();
}
}


#if PROFILING == true
unsigned long thisloop = millis() - _profileLoopBegin;
if(thisloop > _profileMaximumLoop) _profileMaximumLoop = thisloop;
Expand Down
6 changes: 3 additions & 3 deletions BrewManiacEx/BrewManiacProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extern void wiReadRecipe(AutomationRecipe *recipe);
extern byte readSetting(int addr);
extern void updateSetting(int addr,byte value);

extern void wiSetDeviceAddress(byte ip[]);
extern void wiSetDeviceAddress(byte ip[],bool apmode);
extern void wiUpdateSetting(int address,byte value);

#if MaximumNumberOfSensors > 1
Expand Down Expand Up @@ -213,9 +213,9 @@ void BrewManiacProxy::setButtonLabel(byte btns)
if(_eventHandler) _eventHandler(BMNotifyButtonLabel);
}

void BrewManiacProxy::setIp(byte ip[])
void BrewManiacProxy::setIp(byte ip[], bool apmode)
{
wiSetDeviceAddress(ip);
wiSetDeviceAddress(ip,apmode);
}


Expand Down
2 changes: 1 addition & 1 deletion BrewManiacEx/BrewManiacProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class BrewManiacProxy{

// from Web
void sendButton(byte mask,bool longPressed);
void setIp(byte ip[]);
void setIp(byte ip[],bool apmode=false);

// recipe
AutomationRecipe automationRecipe;
Expand Down
4 changes: 2 additions & 2 deletions BrewManiacEx/BrewManiacWeb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ void BrewManiacWeb::updateSensorSetting(String& json)
#endif


void BrewManiacWeb::setIp(uint8_t ip[])
void BrewManiacWeb::setIp(uint8_t ip[],bool apmode)
{
bmproxy.setIp(ip);
bmproxy.setIp(ip,apmode);
}

BrewManiacWeb::BrewManiacWeb(void)
Expand Down
2 changes: 1 addition & 1 deletion BrewManiacEx/BrewManiacWeb.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class BrewManiacWeb
bool isBrewing(void);
byte brewingStage(void);
byte lastBrewEvent(void);
void setIp(uint8_t ip[]);
void setIp(uint8_t ip[],bool apmode=false);

void loop(void);
// get data
Expand Down
2 changes: 1 addition & 1 deletion BrewManiacEx/HttpUpdateHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ void HttpFileDownloader::download(void)

_http.begin(_url);

DEBUGF("[HTTP] GET...\n");
DEBUGF("[HTTP] GET %s...\n",_url.c_str());
// start connection and send HTTP header
int httpCode = _http.GET();
if(httpCode > 0) {
Expand Down
19 changes: 12 additions & 7 deletions BrewManiacEx/TimeKeeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,20 @@ void TimeKeeperClass::setCurrentTime(time_t now)
// saveTime(now);
}

void TimeKeeperClass::begin(void)
void TimeKeeperClass::begin(bool useSaved)
{
//_online = false;

_referenceSeconds=loadTime();
_referenceSeconds += 300; // add 5 minutes.
_referenceSystemTime = millis();
_lastSaved=_referenceSeconds;
DBG_PRINTF("Load saved time:%ld\n",_referenceSeconds);
if(useSaved){
/* _referenceSeconds=loadTime();
_referenceSeconds += 300; // add 5 minutes.
_referenceSystemTime = millis();
_lastSaved=_referenceSeconds;
DBG_PRINTF("Load saved time:%ld\n",_referenceSeconds);
*/
}else{
_referenceSystemTime = millis();
_referenceSeconds = 0;
}
}

void TimeKeeperClass::begin(char* server1,char* server2,char* server3)
Expand Down
2 changes: 1 addition & 1 deletion BrewManiacEx/TimeKeeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class TimeKeeperClass
public:
TimeKeeperClass(void):_referenceSeconds(0),_referenceSystemTime(0){}
void begin(char* server1,char* server2,char* server3);
void begin(void);
void begin(bool useSaved=false);

time_t getTimeSeconds(void); // get Epoch time
const char *getDateTimeStr(void);
Expand Down
Loading

0 comments on commit f1ac241

Please sign in to comment.