21 #include "../../include/esp3d_config.h"
22 #if defined (WIFI_FEATURE) || defined (ETH_FEATURE) || defined (BLUETOOTH_FEATURE)
23 #ifdef ARDUINO_ARCH_ESP32
24 #define WIFI_EVENT_STAMODE_CONNECTED SYSTEM_EVENT_STA_CONNECTED
25 #define WIFI_EVENT_STAMODE_DISCONNECTED SYSTEM_EVENT_STA_DISCONNECTED
26 #define WIFI_EVENT_STAMODE_GOT_IP SYSTEM_EVENT_STA_GOT_IP
27 #define WIFI_EVENT_SOFTAPMODE_STACONNECTED SYSTEM_EVENT_AP_STACONNECTED
28 #endif //ARDUINO_ARCH_ESP32
29 #ifdef ARDUINO_ARCH_ESP8266
30 #endif //ARDUINO_ARCH_ESP8266
32 #if defined (WIFI_FEATURE)
33 #include "../wifi/wificonfig.h"
35 #if defined (ETH_FEATURE)
36 #include "../ethernet/ethconfig.h"
38 #if defined (BLUETOOTH_FEATURE)
39 #include "../bluetooth/BT_service.h"
40 #endif //BLUETOOTH_FEATURE
42 #include "../../core/esp3doutput.h"
43 #include "../../core/settings_esp3d.h"
45 String NetConfig::_hostname =
"";
46 bool NetConfig::_needReconnect2AP =
false;
47 bool NetConfig::_events_registered =
false;
48 bool NetConfig::_started =
false;
62 static char macstr [18];
63 if (0 > sprintf (macstr,
"%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]) ) {
64 strcpy (macstr,
"00:00:00:00:00:00");
77 if (ipaddr.fromString(s)) {
88 IPAddress ipaddr(ip_int);
89 return ipaddr.toString();
104 for (uint i = 0; i < strlen (
hostname); i++) {
106 if (! (isdigit (c) || isalpha (c) || c ==
'-') ) {
121 static String currentIP =
"";
122 #if defined( WIFI_FEATURE)
123 if (WiFi.getMode() == WIFI_STA) {
124 currentIP = WiFi.localIP().toString();
125 }
else if (WiFi.getMode() == WIFI_AP) {
126 currentIP = WiFi.softAPIP().toString();
128 #endif //WIFI_FEATURE
129 #if defined (ETH_FEATURE)
131 currentIP = ETH.localIP().toString();
134 if (currentIP.length() == 0) {
135 currentIP =
"0.0.0.0";
147 return ip.fromString(
string);
152 void NetConfig::onWiFiEvent(WiFiEvent_t event)
156 case WIFI_EVENT_STAMODE_CONNECTED:
157 _needReconnect2AP =
false;
159 case WIFI_EVENT_STAMODE_DISCONNECTED: {
161 output.printMSG (
"Disconnected");
163 _needReconnect2AP =
true;
167 case WIFI_EVENT_STAMODE_GOT_IP: {
168 output.printMSG (
"Connected");
171 output.printMSG (WiFi.localIP().toString().c_str());
174 case WIFI_EVENT_SOFTAPMODE_STACONNECTED: {
175 output.printMSG (
"New client");
179 #ifdef ARDUINO_ARCH_ESP32
180 case SYSTEM_EVENT_STA_LOST_IP:
182 _needReconnect2AP =
true;
186 case SYSTEM_EVENT_ETH_CONNECTED: {
187 output.printMSG (
"Cable connected");
191 case SYSTEM_EVENT_ETH_DISCONNECTED: {
192 output.printMSG (
"Cable disconnected");
196 case SYSTEM_EVENT_ETH_GOT_IP:
197 output.printMSG (ETH.localIP().toString().c_str());
201 #endif //ARDUINO_ARCH_ESP32
223 output.
printMSG(
"Starting Network");
226 if(!_events_registered) {
227 #ifdef ARDUINO_ARCH_ESP8266
228 #pragma GCC diagnostic push
229 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
230 WiFi.onEvent(NetConfig::onWiFiEvent, WIFI_EVENT_ANY);
231 #pragma GCC diagnostic pop
233 #ifdef ARDUINO_ARCH_ESP32
234 WiFi.onEvent(NetConfig::onWiFiEvent);
237 _events_registered =
true;
246 #if defined (WIFI_FEATURE)
250 if (WiFi.getMode() == WIFI_AP) {
254 #endif //WIFI_FEATURE
255 #if defined (ETH_FEATURE)
261 #if defined (BLUETOOTH_FEATURE)
262 if ((espMode ==
ESP_BT)) {
265 #endif //BLUETOOTH_FEATURE
269 bool start_services =
false;
270 #if defined (ETH_FEATURE)
272 start_services =
true;
275 #if defined (WIFI_FEATURE)
277 start_services =
true;
279 #endif //WIFI_FEATURE
280 if (start_services) {
286 #ifdef ARDUINO_ARCH_ESP32
287 #if defined (WIFI_FEATURE)
288 if (WiFi.getMode() == WIFI_AP) {
289 WiFi.softAPsetHostname(_hostname.c_str());
291 #endif //WIFI_FEATURE
292 #endif //ARDUINO_ARCH_ESP32
313 #if defined (WIFI_FEATURE)
315 _needReconnect2AP=
false;
318 #endif //WIFI_FEATURE
320 #if defined (ETH_FEATURE)
324 #if defined (BLUETOOTH_FEATURE)
326 #endif //BLUETOOTH_FEATURE
335 return _hostname.c_str();
337 #if defined (WIFI_FEATURE)
338 if(WiFi.getMode()!= WIFI_OFF) {
340 return _hostname.c_str();
342 #endif //WIFI_FEATURE
343 #if defined (ETH_FEATURE)
345 return ETH.getHostname();
349 #if defined (BLUETOOTH_FEATURE)
353 #endif //BLUETOOTH_FEATURE
354 return _hostname.c_str();
364 #if defined (WIFI_FEATURE)
365 if(_needReconnect2AP) {
367 if(WiFi.getMode()!= WIFI_OFF) {
372 #endif //WIFI_FEATURE
373 #if defined (ETH_FEATURE)
376 #if defined (BLUETOOTH_FEATURE)
378 #endif //BLUETOOTH_FEATURE
388 #ifdef ARDUINO_ARCH_ESP32
389 tcpip_adapter_dhcp_status_t dhcp_status;
390 tcpip_adapter_dhcpc_get_status ((mode ==
ESP_WIFI_STA)?TCPIP_ADAPTER_IF_STA:(mode ==
ESP_WIFI_AP)?TCPIP_ADAPTER_IF_AP:TCPIP_ADAPTER_IF_ETH, &dhcp_status);
391 started = (dhcp_status == TCPIP_ADAPTER_DHCP_STARTED);
392 #endif //ARDUINO_ARCH_ESP32
393 #ifdef ARDUINO_ARCH_ESP8266
395 started = (wifi_station_dhcpc_status() == DHCP_STARTED);
396 #endif //ARDUINO_ARCH_ESP8266
403 #ifdef ARDUINO_ARCH_ESP32
404 tcpip_adapter_dhcp_status_t dhcp_status;
405 tcpip_adapter_dhcps_get_status ((mode ==
ESP_WIFI_STA)?TCPIP_ADAPTER_IF_STA:(mode ==
ESP_WIFI_AP)?TCPIP_ADAPTER_IF_AP:TCPIP_ADAPTER_IF_ETH, &dhcp_status);
406 itis = (dhcp_status == TCPIP_ADAPTER_DHCP_STARTED);
407 #endif //ARDUINO_ARCH_ESP32
408 #ifdef ARDUINO_ARCH_ESP8266
410 itis = (wifi_softap_dhcps_status() == DHCP_STARTED);
411 #endif //ARDUINO_ARCH_ESP8266
415 #endif // WIFI_FEATURE || ETH_FEATURE