ESP3D Lib  1.0
3D Printer WiFi Library
wifiservices.cpp
Go to the documentation of this file.
1 /*
2  wifiservices.cpp - wifi services functions class
3 
4  Copyright (c) 2014 Luc Lebosse. All rights reserved.
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
21 #ifdef ARDUINO_ARCH_ESP32
22 
23 #include "esplibconfig.h"
24 
25 #if ENABLED(ESP3D_WIFISUPPORT)
26 
27 #include <WiFi.h>
28 #include <FS.h>
29 #include <SPIFFS.h>
30 #include <Preferences.h>
31 #include "wificonfig.h"
32 #include "wifiservices.h"
33 #ifdef ENABLE_MDNS
34 #include <ESPmDNS.h>
35 #endif
36 #ifdef ENABLE_OTA
37 #include <ArduinoOTA.h>
38 #endif
39 #ifdef ENABLE_HTTP
40 #include "web_server.h"
41 #endif
42 
44 
46 }
48  end();
49 }
50 
51 bool WiFiServices::begin(){
52  bool no_error = true;
53  //Sanity check
54  if(WiFi.getMode() == WIFI_OFF) return false;
55  String h;
56  Preferences prefs;
57  //Get hostname
58  String defV = DEFAULT_HOSTNAME;
59  prefs.begin(NAMESPACE, true);
60  h = prefs.getString(HOSTNAME_ENTRY, defV);
61  prefs.end();
62  //Start SPIFFS
63  SPIFFS.begin(true);
64 
65 #ifdef ENABLE_OTA
66  ArduinoOTA
67  .onStart([]() {
68  String type;
69  if (ArduinoOTA.getCommand() == U_FLASH)
70  type = "sketch";
71  else {// U_SPIFFS
72  // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
73  type = "filesystem";
74  SPIFFS.end();
75  }
76  MYSERIAL0.printf("OTA:Start OTA updating %s]\r\n", type.c_str());
77  })
78  .onEnd([]() {
79  MYSERIAL0.println("OTA:End");
80 
81  })
82  .onProgress([](unsigned int progress, unsigned int total) {
83  MYSERIAL0.printf("OTA:OTA Progress: %u%%]\r\n", (progress / (total / 100)));
84  })
85  .onError([](ota_error_t error) {
86  MYSERIAL0.printf("OTA: Error(%u)\r\n", error);
87  if (error == OTA_AUTH_ERROR) MYSERIAL0.println("OTA:Auth Failed]");
88  else if (error == OTA_BEGIN_ERROR) MYSERIAL0.println("OTA:Begin Failed");
89  else if (error == OTA_CONNECT_ERROR) MYSERIAL0.println("OTA:Connect Failed");
90  else if (error == OTA_RECEIVE_ERROR) MYSERIAL0.println("OTA:Receive Failed");
91  else if (error == OTA_END_ERROR) MYSERIAL0.println("OTA:End Failed]");
92  });
93  ArduinoOTA.begin();
94 #endif
95 #ifdef ENABLE_MDNS
96  //no need in AP mode
97  if(WiFi.getMode() == WIFI_STA){
98  //start mDns
99  if (!MDNS.begin(h.c_str())) {
100  MYSERIAL0.println("Cannot start mDNS");
101  no_error = false;
102  } else {
103  MYSERIAL0.printf("Start mDNS with hostname:%s\r\n",h.c_str());
104  }
105  }
106 #endif
107 #ifdef ENABLE_HTTP
108  web_server.begin();
109 #endif
110  return no_error;
111 }
112 void WiFiServices::end(){
113 #ifdef ENABLE_HTTP
114  web_server.end();
115 #endif
116  //stop OTA
117 #ifdef ENABLE_OTA
118  ArduinoOTA.end();
119 #endif
120  //Stop SPIFFS
121  SPIFFS.end();
122 
123 #ifdef ENABLE_MDNS
124  //Stop mDNS
125  //MDNS.end();
126 #endif
127 }
128 
129 void WiFiServices::handle(){
130 #ifdef ENABLE_OTA
131  ArduinoOTA.handle();
132 #endif
133 #ifdef ENABLE_HTTP
134  web_server.handle();
135 #endif
136 }
137 
138 #endif // ENABLE_WIFI
139 
140 #endif // ARDUINO_ARCH_ESP32
Web_Server::handle
static void handle()
web_server.h
WiFiServices::end
static void end()
Web_Server::begin
bool begin()
WiFiServices::begin
static bool begin()
Web_Server::end
void end()
WiFiServices::WiFiServices
WiFiServices()
wifiservices.h
WiFiServices::handle
static void handle()
MYSERIAL0
#define MYSERIAL0
Definition: esplibconfig.h:29
esplibconfig.h
wifi_services
WiFiServices wifi_services
wificonfig.h
NAMESPACE
#define NAMESPACE
Definition: wificonfig.h:36
WiFiServices
Definition: wifiservices.h:27
DEFAULT_HOSTNAME
#define DEFAULT_HOSTNAME
Definition: wificonfig.h:67
WiFiServices::~WiFiServices
~WiFiServices()
HOSTNAME_ENTRY
#define HOSTNAME_ENTRY
Definition: wificonfig.h:37
web_server
Web_Server web_server