ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
wificonfig.cpp
Go to the documentation of this file.
1 /*
2  wificonfig.cpp - wifi 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 #include "../../include/esp3d_config.h"
22 #if defined (WIFI_FEATURE)
23 #ifdef ARDUINO_ARCH_ESP32
24 #include <esp_wifi.h>
25 #endif //ARDUINO_ARCH_ESP32
26 #ifdef ARDUINO_ARCH_ESP8266
27 #endif //ARDUINO_ARCH_ESP8266
28 #include "../wifi/wificonfig.h"
29 #include "../network/netconfig.h"
30 #include "../../core/esp3doutput.h"
31 #include "../../core/settings_esp3d.h"
32 
33 const uint8_t DEFAULT_AP_MASK_VALUE[] = {255, 255, 255, 0};
34 
36 {
37 }
38 
40 {
41  end();
42 }
43 
47 bool WiFiConfig::isSSIDValid (const char * ssid)
48 {
49  //limited size
50  //char c;
51  if (strlen (ssid) > MAX_SSID_LENGTH || strlen (ssid) < MIN_SSID_LENGTH) {
52  return false;
53  }
54  //only printable
55  for (uint i = 0; i < strlen (ssid); i++) {
56  if (!isPrintable (ssid[i]) ) {
57  return false;
58  }
59  }
60  return true;
61 }
62 
63 const char * WiFiConfig::hostname()
64 {
65  static String tmp;
66 #if defined (ARDUINO_ARCH_ESP8266)
67  if (WiFi.getMode() == WIFI_AP) {
68  //No API for AP
69  tmp = NetConfig::hostname(true);
70  } else {
71  tmp = WiFi.hostname();
72 
73  }
74 #endif //ARDUINO_ARCH_ESP8266
75 #if defined (ARDUINO_ARCH_ESP32)
76  if (WiFi.getMode() == WIFI_AP) {
77  //tmp = NetConfig::hostname(true);
78  //Set API is not working so far
79  tmp = WiFi.softAPgetHostname();
80  } else {
81  tmp = WiFi.getHostname();
82  }
83 #endif //ARDUINO_ARCH_ESP8266
84  return tmp.c_str();
85 }
86 
91 bool WiFiConfig::isPasswordValid (const char * password)
92 {
93  if (strlen (password) == 0) {
94  return true; //open network
95  }
96  //limited size
97  if ((strlen (password) > MAX_PASSWORD_LENGTH) || (strlen (password) < MIN_PASSWORD_LENGTH)) {
98  return false;
99  }
100  return true;
101 }
102 
103 /*
104  * Get WiFi signal strength
105  */
106 
107 int32_t WiFiConfig::getSignal (int32_t RSSI)
108 {
109  if (RSSI <= -100) {
110  return 0;
111  }
112  if (RSSI >= -50) {
113  return 100;
114  }
115  return (2 * (RSSI + 100) );
116 }
117 
118 /*
119  * Connect client to AP
120  */
121 
122 bool WiFiConfig::ConnectSTA2AP()
123 {
124  String msg, msg_out;
125  uint8_t count = 0;
126  uint8_t dot = 0;
127  wl_status_t status = WiFi.status();
129  while (status != WL_CONNECTED && count < 40) {
130 
131  switch (status) {
132  case WL_NO_SSID_AVAIL:
133  msg="No SSID";
134  break;
135  case WL_CONNECT_FAILED:
136  msg="Connection failed";
137  break;
138  case WL_CONNECTED:
139  break;
140  default:
141  if ((dot>3) || (dot==0) ) {
142  dot=0;
143  msg_out = "Connecting";
144  }
145  msg_out+=".";
146  msg= msg_out;
147  dot++;
148  break;
149  }
150  ESP3DGlobalOutput::SetStatus(msg.c_str());
151  output.printMSG(msg.c_str());
152  output.flush();
153  Hal::wait (500);
154  count++;
155  status = WiFi.status();
156  }
157  return (status == WL_CONNECTED);
158 }
159 
160 /*
161  * Start client mode (Station)
162  */
164 {
165  //Sanity check
166  if((WiFi.getMode() == WIFI_STA) || (WiFi.getMode() == WIFI_AP_STA)) {
167  WiFi.disconnect();
168  }
169  if((WiFi.getMode() == WIFI_AP) || (WiFi.getMode() == WIFI_AP_STA)) {
170  WiFi.softAPdisconnect();
171  }
172  WiFi.enableAP (false);
173  WiFi.mode(WIFI_STA);
174  //Get parameters for STA
177 
182  IPAddress ip(IP), mask(MK), gateway(GW);
183  WiFi.config(ip, gateway,mask);
184  }
186  String stmp;
187  stmp = "Connecting to '" + SSID + "'";;
188  output.printMSG(stmp.c_str());
189  if (WiFi.begin(SSID.c_str(), (password.length() > 0)?password.c_str():nullptr)) {
190 #if defined (ARDUINO_ARCH_ESP8266)
191  WiFi.setSleepMode(WIFI_NONE_SLEEP);
192  if (!WiFi.hostname(NetConfig::hostname(true))) {
193  output.printERROR("Set hostname STA failed");
194  }
195 #endif //ARDUINO_ARCH_ESP8266
196 #if defined (ARDUINO_ARCH_ESP32)
197  WiFi.setSleep(false);
198  WiFi.setHostname(NetConfig::hostname(true));
199 #endif //ARDUINO_ARCH_ESP32
200  return ConnectSTA2AP();
201  } else {
202  output.printERROR("Starting client failed");
203  return false;
204  }
205 }
206 
212 {
214  //Sanity check
215  if((WiFi.getMode() == WIFI_STA) || (WiFi.getMode() == WIFI_AP_STA)) {
216  WiFi.disconnect();
217  }
218  if((WiFi.getMode() == WIFI_AP) || (WiFi.getMode() == WIFI_AP_STA)) {
219  WiFi.softAPdisconnect();
220  }
221  WiFi.enableSTA (false);
222  WiFi.mode(WIFI_AP);
223  //Set Sleep Mode to none
224 #if defined (ARDUINO_ARCH_ESP8266)
225  WiFi.setSleepMode(WIFI_NONE_SLEEP);
226  //No API for it
227  //if(!WiFi.hostname(NetConfig::hostname(true))){
228  // output.printERROR("Set hostname AP failed");
229  //}
230 #endif //ARDUINO_ARCH_ESP8266
231 
233  String password = Settings_ESP3D::read_string(ESP_AP_PASSWORD);
234  //channel
235  int8_t channel = Settings_ESP3D::read_byte (ESP_AP_CHANNEL);
236  //IP
238  IPAddress ip(IP);
239  IPAddress mask(DEFAULT_AP_MASK_VALUE);
240  //Set static IP
241 
242  if (!WiFi.softAPConfig(ip, ip, mask)) {
243  output.printERROR("Set IP to AP failed");
244  } else {
245  output.printMSG(ip.toString().c_str());
246  }
247  //Start AP
248  if(WiFi.softAP(SSID.c_str(), (password.length() > 0)?password.c_str():nullptr, channel)) {
249  String stmp = "AP SSID: '" + SSID;
250  if (password.length() > 0) {
251  stmp +="' is started and protected by password";
252  } else {
253  stmp +=" is started not protected by passord";
254  }
255  output.printMSG(stmp.c_str());
256  //must be done after starting AP not before
257 #if defined (ARDUINO_ARCH_ESP32)
258  WiFi.setSleep(false);
259  if (!WiFi.softAPsetHostname(NetConfig::hostname(true))) {
260  output.printERROR("Set hostname AP failed");
261  }
262 #endif //ARDUINO_ARCH_ESP32
263  return true;
264  } else {
265  output.printERROR("Starting AP failed");
266  return false;
267  }
268 }
269 
271 {
272  return (WiFi.getMode() != WIFI_OFF);
273 }
274 
279 {
280  bool res = false;
281  end();
283  output.printMSG("Starting WiFi");
284  int8_t wifiMode =Settings_ESP3D::read_byte(ESP_RADIO_MODE);
285  if (wifiMode == ESP_WIFI_AP) {
286  res = StartAP();
287  } else if (wifiMode == ESP_WIFI_STA) {
288  res = StartSTA();
289  //AP is backup mode
290  if(!res) {
291  res = StartAP();
292  }
293  }
294  return res;
295 }
296 
302 {
303  //Sanity check
304  if((WiFi.getMode() == WIFI_STA) || (WiFi.getMode() == WIFI_AP_STA)) {
305  WiFi.disconnect(true);
306  }
307  if((WiFi.getMode() == WIFI_AP) || (WiFi.getMode() == WIFI_AP_STA)) {
308  WiFi.softAPdisconnect(true);
309  }
310  WiFi.mode(WIFI_OFF);
312  output.printMSG("WiFi Off");
313 }
314 
320 {
321  //to avoid mixed mode
322  if (WiFi.getMode() == WIFI_AP_STA) {
323  if (WiFi.scanComplete() != WIFI_SCAN_RUNNING) {
324  WiFi.enableSTA (false);
325  }
326  }
327 }
328 
330 {
331 #ifdef ARDUINO_ARCH_ESP32
332  if (WiFi.getSleep()) {
333  return "Modem";
334  } else {
335  return "None";
336  }
337 #endif //ARDUINO_ARCH_ESP32
338 #ifdef ARDUINO_ARCH_ESP8266
339  WiFiSleepType_t ps_type = WiFi.getSleepMode();
340  if (ps_type == WIFI_NONE_SLEEP) {
341  return "None";
342  } else if (ps_type == WIFI_LIGHT_SLEEP) {
343  return "Light";
344  } else if (ps_type == WIFI_MODEM_SLEEP) {
345  return "Modem";
346  } else {
347  return "???";
348  }
349 #endif //ARDUINO_ARCH_ESP8266
350 }
351 
352 const char* WiFiConfig::getPHYModeString (uint8_t wifimode)
353 {
354 #ifdef ARDUINO_ARCH_ESP32
355  uint8_t PhyMode;
356  esp_wifi_get_protocol ((wifimode == WIFI_STA)?ESP_IF_WIFI_STA:ESP_IF_WIFI_AP, &PhyMode);
357 #endif //ARDUINO_ARCH_ESP32
358 #ifdef ARDUINO_ARCH_ESP8266
359  (void)wifimode;
360  WiFiPhyMode_t PhyMode = WiFi.getPhyMode();
361 #endif //ARDUINO_ARCH_ESP8266
362  if (PhyMode == (WIFI_PHY_MODE_11G) ) {
363  return "11g";
364  } else if (PhyMode == (WIFI_PHY_MODE_11B) ) {
365  return "11b";
366  } else if (PhyMode == (WIFI_PHY_MODE_11N) ) {
367  return "11n";
368  } else {
369  return "???";
370  }
371 }
372 
374 {
375 #ifdef ARDUINO_ARCH_ESP32
376  wifi_config_t conf;
377  esp_wifi_get_config (ESP_IF_WIFI_AP, &conf);
378  return (conf.ap.ssid_hidden == 0);
379 #endif //ARDUINO_ARCH_ESP32
380 #ifdef ARDUINO_ARCH_ESP8266
381  struct softap_config apconfig;
382  wifi_softap_get_config (&apconfig);
383  return (apconfig.ssid_hidden == 0);
384 #endif //ARDUINO_ARCH_ESP8266
385 }
386 
387 const char * WiFiConfig::AP_SSID()
388 {
389  static String ssid;
390 #ifdef ARDUINO_ARCH_ESP32
391  wifi_config_t conf;
392  esp_wifi_get_config (ESP_IF_WIFI_AP, &conf);
393  ssid = (const char*) conf.ap.ssid;
394 #endif //ARDUINO_ARCH_ESP32
395 #ifdef ARDUINO_ARCH_ESP8266
396  struct softap_config apconfig;
397  wifi_softap_get_config (&apconfig);
398  ssid = (const char*) apconfig.ssid;
399 #endif //ARDUINO_ARCH_ESP8266
400  return ssid.c_str();
401 }
402 
404 {
405  uint8_t mode = 0;
406 #ifdef ARDUINO_ARCH_ESP32
407  wifi_config_t conf;
408  esp_wifi_get_config (ESP_IF_WIFI_AP, &conf);
409  mode = conf.ap.authmode;
410 
411 #endif //ARDUINO_ARCH_ESP32
412 #ifdef ARDUINO_ARCH_ESP8266
413  struct softap_config apconfig;
414  wifi_softap_get_config (&apconfig);
415  mode = apconfig.authmode;
416 #endif //ARDUINO_ARCH_ESP8266
417  if (mode == AUTH_OPEN) {
418  return "None";
419  } else if (mode == AUTH_WEP) {
420  return "WEP";
421  } else if (mode == AUTH_WPA_PSK) {
422  return "WPA";
423  } else if (mode == AUTH_WPA2_PSK) {
424  return "WPA2";
425  } else {
426  return "WPA/WPA2";
427  }
428 }
429 
431 {
432  static String tmp;
433 #ifdef ARDUINO_ARCH_ESP32
434  tcpip_adapter_ip_info_t ip_AP;
435  tcpip_adapter_get_ip_info (TCPIP_ADAPTER_IF_AP, &ip_AP);
436  tmp = IPAddress (ip_AP.gw.addr).toString();
437 #endif //ARDUINO_ARCH_ESP32
438 #ifdef ARDUINO_ARCH_ESP8266
439  struct ip_info ip_AP;
440  if (!wifi_get_ip_info (SOFTAP_IF, &ip_AP)) {
441  log_esp3d("Error getting gateway ip");
442  }
443  tmp = IPAddress (ip_AP.gw).toString();
444 #endif //ARDUINO_ARCH_ESP8266
445  return tmp.c_str();
446 }
447 
449 {
450  static String tmp;
451 #ifdef ARDUINO_ARCH_ESP32
452  tcpip_adapter_ip_info_t ip_AP;
453  tcpip_adapter_get_ip_info (TCPIP_ADAPTER_IF_AP, &ip_AP);
454  tmp = IPAddress (ip_AP.netmask.addr).toString();
455 #endif //ARDUINO_ARCH_ESP32
456 #ifdef ARDUINO_ARCH_ESP8266
457  struct ip_info ip_AP;
458  if (!wifi_get_ip_info (SOFTAP_IF, &ip_AP)) {
459  log_esp3d("Error getting mask ip");
460  }
461  tmp = IPAddress (ip_AP.netmask).toString();
462 #endif //ARDUINO_ARCH_ESP8266
463  return tmp.c_str();
464 }
465 
466 const char * WiFiConfig::getConnectedSTA(uint8_t * totalcount, bool reset)
467 {
468  static uint8_t count = 0;
469  static uint8_t current = 0;
470  static String data;
471  data = "";
472 
473 #ifdef ARDUINO_ARCH_ESP32
474  if (current > count) {
475  current =0;
476  }
477  static wifi_sta_list_t station;
478  static tcpip_adapter_sta_list_t tcpip_sta_list;
479  if (reset) {
480  count = 0;
481  }
482  if (count == 0) {
483  current = 0;
484  esp_wifi_ap_get_sta_list (&station);
485  tcpip_adapter_get_sta_list (&station, &tcpip_sta_list);
486  count = station.num;
487  }
488  if (count > 0) {
489  data = IPAddress (tcpip_sta_list.sta[current].ip.addr).toString();
490  data += "(";
491  data += NetConfig::mac2str(tcpip_sta_list.sta[current].mac);
492  data += ")";
493  current++;
494  }
495 
496 #endif //ARDUINO_ARCH_ESP32
497 #ifdef ARDUINO_ARCH_ESP8266
498  static struct station_info * station;
499  if (current > count) {
500  current = 0;
501  count = 0;
502  }
503  if (reset) {
504  count = 0;
505  }
506  if (count == 0) {
507  current = 0;
508  station = wifi_softap_get_station_info();
509  struct station_info * station_tmp = station;
510  while (station) {
511  //go next record
512  count++;
513  station = STAILQ_NEXT (station, next);
514  }
515  station = station_tmp;
516  }
517  if ((count > 0) && station) {
518  data = IPAddress ((const uint8_t *) &station->ip).toString();
519  data += "(";
520  data += NetConfig::mac2str(station->bssid);
521  data += ")";
522  current++;
523  station = STAILQ_NEXT (station, next);
524  if ((current == count) || !station) {
525  wifi_softap_free_station_info();
526  }
527  }
528 #endif //ARDUINO_ARCH_ESP8266
529  if(totalcount) {
530  *totalcount = count;
531  }
532  return data.c_str();
533 }
534 
535 #endif // WIFI_FEATURE
536 
WiFiConfig::is_AP_visible
static bool is_AP_visible()
Definition: wificonfig.cpp:373
DHCP_MODE
#define DHCP_MODE
Definition: netconfig.h:36
WiFiConfig::AP_Mask_String
static const char * AP_Mask_String()
Definition: wificonfig.cpp:448
Settings_ESP3D::read_string
static const char * read_string(int pos, bool *haserror=NULL)
Definition: settings_esp3d.cpp:794
Hal::wait
static void wait(uint32_t milliseconds)
Definition: hal.cpp:226
MAX_SSID_LENGTH
#define MAX_SSID_LENGTH
Definition: wificonfig.h:22
WiFiConfig::isPasswordValid
static bool isPasswordValid(const char *password)
Definition: wificonfig.cpp:91
WiFiConfig::begin
static bool begin()
Definition: wificonfig.cpp:278
ESP_WIFI_STA
#define ESP_WIFI_STA
Definition: netconfig.h:41
WiFiConfig::isSSIDValid
static bool isSSIDValid(const char *ssid)
Definition: wificonfig.cpp:47
ESP_AP_IP_VALUE
#define ESP_AP_IP_VALUE
Definition: settings_esp3d.h:62
ESP_STA_PASSWORD
#define ESP_STA_PASSWORD
Definition: settings_esp3d.h:41
ESP_AP_PASSWORD
#define ESP_AP_PASSWORD
Definition: settings_esp3d.h:61
ESP_STA_IP_MODE
#define ESP_STA_IP_MODE
Definition: settings_esp3d.h:42
WiFiConfig::end
static void end()
Definition: wificonfig.cpp:301
ESP_STA_SSID
#define ESP_STA_SSID
Definition: settings_esp3d.h:40
NetConfig::hostname
static const char * hostname(bool fromsettings=false)
Definition: netconfig.cpp:331
WiFiConfig::getConnectedSTA
static const char * getConnectedSTA(uint8_t *totalcount=NULL, bool reset=false)
Definition: wificonfig.cpp:466
ESP_AP_SSID
#define ESP_AP_SSID
Definition: settings_esp3d.h:60
WiFiConfig::getSignal
static int32_t getSignal(int32_t RSSI)
Definition: wificonfig.cpp:107
DEFAULT_AP_MASK_VALUE
const uint8_t DEFAULT_AP_MASK_VALUE[]
Definition: wificonfig.cpp:33
ESP_STA_IP_VALUE
#define ESP_STA_IP_VALUE
Definition: settings_esp3d.h:43
WiFiConfig::StartAP
static bool StartAP()
Definition: wificonfig.cpp:211
Settings_ESP3D::read_IP
static uint32_t read_IP(int pos, bool *haserror=NULL)
Definition: settings_esp3d.cpp:958
WiFiConfig::~WiFiConfig
~WiFiConfig()
Definition: wificonfig.cpp:39
ESP3DOutput::printMSG
size_t printMSG(const char *s, bool withNL=true)
Definition: esp3doutput.cpp:190
WiFiConfig::WiFiConfig
WiFiConfig()
Definition: wificonfig.cpp:35
MIN_SSID_LENGTH
#define MIN_SSID_LENGTH
Definition: wificonfig.h:23
MIN_PASSWORD_LENGTH
#define MIN_PASSWORD_LENGTH
Definition: wificonfig.h:29
WiFiConfig::AP_Auth_String
static const char * AP_Auth_String()
Definition: wificonfig.cpp:403
WiFiConfig::getPHYModeString
static const char * getPHYModeString(uint8_t wifimode)
Definition: wificonfig.cpp:352
ESP3DOutput::printERROR
size_t printERROR(const char *s, int code_error=200)
Definition: esp3doutput.cpp:247
log_esp3d
#define log_esp3d(format,...)
Definition: debug_esp3d.h:29
WiFiConfig::getSleepModeString
static const char * getSleepModeString()
Definition: wificonfig.cpp:329
ESP_STA_GATEWAY_VALUE
#define ESP_STA_GATEWAY_VALUE
Definition: settings_esp3d.h:45
Settings_ESP3D::read_byte
static uint8_t read_byte(int pos, bool *haserror=NULL)
Definition: settings_esp3d.cpp:715
ESP_ALL_CLIENTS
#define ESP_ALL_CLIENTS
Definition: esp3doutput.h:30
ESP_STA_MASK_VALUE
#define ESP_STA_MASK_VALUE
Definition: settings_esp3d.h:44
ESP_WIFI_AP
#define ESP_WIFI_AP
Definition: netconfig.h:42
WiFiConfig::hostname
static const char * hostname()
Definition: wificonfig.cpp:63
NetConfig::mac2str
static char * mac2str(uint8_t mac[8])
Definition: netconfig.cpp:60
WiFiConfig::started
static bool started()
Definition: wificonfig.cpp:270
WiFiConfig::AP_SSID
static const char * AP_SSID()
Definition: wificonfig.cpp:387
MAX_PASSWORD_LENGTH
#define MAX_PASSWORD_LENGTH
Definition: wificonfig.h:26
WiFiConfig::handle
static void handle()
Definition: wificonfig.cpp:319
ESP3DOutput
Definition: esp3doutput.h:48
ESP_RADIO_MODE
#define ESP_RADIO_MODE
Definition: settings_esp3d.h:39
WiFiConfig::StartSTA
static bool StartSTA()
Definition: wificonfig.cpp:163
ESP3DGlobalOutput::SetStatus
static void SetStatus(const char *status)
Definition: esp3doutput.cpp:428
ESP_AP_CHANNEL
#define ESP_AP_CHANNEL
Definition: settings_esp3d.h:49
WiFiConfig::AP_Gateway_String
static const char * AP_Gateway_String()
Definition: wificonfig.cpp:430