Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.3 KiB

  1. /*
  2. JustWifi
  3. Wifi Manager for ESP8266
  4. Copyright (C) 2016 by Xose Pérez <xose dot perez at gmail dot com>
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef JustWifi_h
  17. #define JustWifi_h
  18. #include <functional>
  19. #include <ESP8266WiFi.h>
  20. extern "C" {
  21. #include "user_interface.h"
  22. }
  23. #define MAX_NETWORKS 3
  24. #define WIFI_CONNECT_TIMEOUT 10000
  25. struct network_t {
  26. String ssid;
  27. String pass;
  28. };
  29. typedef enum {
  30. MODE_NONE,
  31. MODE_STATION,
  32. MODE_ACCESS_POINT
  33. } justwifi_mode_t;
  34. typedef enum {
  35. MESSAGE_AUTO_NOSSID,
  36. MESSAGE_AUTO_CONNECTING,
  37. MESSAGE_AUTO_FAILED,
  38. MESSAGE_CONNECTING,
  39. MESSAGE_CONNECT_WAITING,
  40. MESSAGE_CONNECT_FAILED,
  41. MESSAGE_CONNECTED,
  42. MESSAGE_ACCESSPOINT_CREATING,
  43. MESSAGE_ACCESSPOINT_FAILED,
  44. MESSAGE_ACCESSPOINT_CREATED,
  45. MESSAGE_DISCONNECTED
  46. } justwifi_messages_t;
  47. class JustWifi {
  48. public:
  49. typedef std::function<void(justwifi_messages_t, char *)> TMessageFunction;
  50. bool autoConnect();
  51. bool connect();
  52. bool startAP(char * ssid, char * pass);
  53. bool disconnect();
  54. bool connected();
  55. bool cleanNetworks();
  56. bool addNetwork(char * ssid, char * pass);
  57. void setConnectTimeout(unsigned long ms);
  58. justwifi_mode_t getMode();
  59. String getIP();
  60. String getNetwork();
  61. void onMessage(TMessageFunction fn);
  62. void loop();
  63. private:
  64. network_t _network[MAX_NETWORKS];
  65. String _ssid;
  66. justwifi_mode_t _mode = MODE_NONE;
  67. unsigned char _network_count = 0;
  68. unsigned long _connect_timeout = WIFI_CONNECT_TIMEOUT;
  69. TMessageFunction _callback = NULL;
  70. void _doCallback(justwifi_messages_t message, char * parameter = NULL);
  71. };
  72. #endif