|
| 1 | +/**************************************************************************************************************************** |
| 2 | + RTL8720_WiFi.ino |
| 3 | + |
| 4 | + For RTL8720DN, RTL8722DM and RTL8722CSM WiFi shields |
| 5 | + |
| 6 | + WiFiManager_Generic_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards |
| 7 | + (https://github.com/khoih-prog/WiFiManager_Generic_Lite) to enable store Credentials in EEPROM/LittleFS for easy |
| 8 | + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. |
| 9 | + |
| 10 | + Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_Generic_Lite |
| 11 | + Licensed under MIT license |
| 12 | + *****************************************************************************************************************************/ |
| 13 | + |
| 14 | +#include "defines.h" |
| 15 | +#include "Credentials.h" |
| 16 | +#include "dynamicParams.h" |
| 17 | + |
| 18 | +WiFiManager_Generic_Lite* WiFiManager_Generic; |
| 19 | + |
| 20 | +void heartBeatPrint(void) |
| 21 | +{ |
| 22 | + static int num = 1; |
| 23 | + |
| 24 | + if (WiFiManager_Generic->isConfigMode()) |
| 25 | + Serial.print("C"); // C means in Config Mode |
| 26 | + else |
| 27 | + { |
| 28 | + if (WiFi.status() == WL_CONNECTED) |
| 29 | + { |
| 30 | + Serial.print("H"); // H means connected to WiFi |
| 31 | + // Bad bug or RTL8720 => if WiFi lost, WiFi.status() still is WL_CONNECTED and WiFi.RSSI() still OK. |
| 32 | + // Similar bug in Portenta_H7 without workaround as in Portenta_H7, WiFi.RSSI() => 0 |
| 33 | + //Serial.print(WiFi.RSSI()); |
| 34 | + } |
| 35 | + else |
| 36 | + Serial.print("F"); // F means not connected to WiFi |
| 37 | + } |
| 38 | + |
| 39 | + if (num == 80) |
| 40 | + { |
| 41 | + Serial.println(); |
| 42 | + num = 1; |
| 43 | + } |
| 44 | + else if (num++ % 10 == 0) |
| 45 | + { |
| 46 | + Serial.print(F(" ")); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +void check_status() |
| 51 | +{ |
| 52 | + static unsigned long checkstatus_timeout = 0; |
| 53 | + |
| 54 | + //KH |
| 55 | +#define HEARTBEAT_INTERVAL 20000L |
| 56 | + // Print hearbeat every HEARTBEAT_INTERVAL (20) seconds. |
| 57 | + if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0)) |
| 58 | + { |
| 59 | + heartBeatPrint(); |
| 60 | + checkstatus_timeout = millis() + HEARTBEAT_INTERVAL; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +#if USING_CUSTOMS_STYLE |
| 65 | +const char NewCustomsStyle[] /*PROGMEM*/ = "<style>div,input{padding:5px;font-size:1em;}input{width:95%;}body{text-align: center;}\ |
| 66 | +button{background-color:blue;color:white;line-height:2.4rem;font-size:1.2rem;width:100%;}fieldset{border-radius:0.3rem;margin:0px;}</style>"; |
| 67 | +#endif |
| 68 | + |
| 69 | +void setup() |
| 70 | +{ |
| 71 | + // Debug console |
| 72 | + Serial.begin(115200); |
| 73 | + while (!Serial && millis() < 5000); |
| 74 | + |
| 75 | + delay(200); |
| 76 | + |
| 77 | + Serial.print(F("\nStart RTL8720_WiFi on ")); Serial.println(BOARD_NAME); |
| 78 | + Serial.println(WIFI_MANAGER_GENERIC_LITE_VERSION); |
| 79 | + |
| 80 | + if (WiFi.status() == WL_NO_SHIELD) |
| 81 | + { |
| 82 | + Serial.println(F("WiFi shield not present")); |
| 83 | + // don't continue |
| 84 | + while (true); |
| 85 | + } |
| 86 | + |
| 87 | + String fv = WiFi.firmwareVersion(); |
| 88 | + |
| 89 | + Serial.print("Current Firmware Version = "); Serial.println(fv); |
| 90 | + |
| 91 | + if (fv != LATEST_RTL8720_FIRMWARE) |
| 92 | + { |
| 93 | + Serial.println("Please upgrade the firmware"); |
| 94 | + } |
| 95 | + |
| 96 | + WiFiManager_Generic = new WiFiManager_Generic_Lite(); |
| 97 | + |
| 98 | + // Optional to change default AP IP(192.168.4.1) and channel(10) |
| 99 | + //WiFiManager_Generic->setConfigPortalIP(IPAddress(192, 168, 120, 1)); |
| 100 | + WiFiManager_Generic->setConfigPortalChannel(0); |
| 101 | + |
| 102 | +#if USING_CUSTOMS_STYLE |
| 103 | + WiFiManager_Generic->setCustomsStyle(NewCustomsStyle); |
| 104 | +#endif |
| 105 | + |
| 106 | +#if USING_CUSTOMS_HEAD_ELEMENT |
| 107 | + WiFiManager_Generic->setCustomsHeadElement("<style>html{filter: invert(10%);}</style>"); |
| 108 | +#endif |
| 109 | + |
| 110 | +#if USING_CORS_FEATURE |
| 111 | + WiFiManager_Generic->setCORSHeader("Your Access-Control-Allow-Origin"); |
| 112 | +#endif |
| 113 | + |
| 114 | + // Set customized DHCP HostName |
| 115 | + WiFiManager_Generic->begin(HOST_NAME); |
| 116 | + //Or use default Hostname "SAMD-WIFI-XXXXXX" |
| 117 | + //WiFiManager_Generic->begin(); |
| 118 | + |
| 119 | +} |
| 120 | + |
| 121 | +#if USE_DYNAMIC_PARAMETERS |
| 122 | +void displayCredentials() |
| 123 | +{ |
| 124 | + Serial.println(F("\nYour stored Credentials :")); |
| 125 | + |
| 126 | + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) |
| 127 | + { |
| 128 | + Serial.print(myMenuItems[i].displayName); |
| 129 | + Serial.print(F(" = ")); |
| 130 | + Serial.println(myMenuItems[i].pdata); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +void displayCredentialsInLoop() |
| 135 | +{ |
| 136 | + static bool displayedCredentials = false; |
| 137 | + |
| 138 | + if (!displayedCredentials) |
| 139 | + { |
| 140 | + for (int i = 0; i < NUM_MENU_ITEMS; i++) |
| 141 | + { |
| 142 | + if (!strlen(myMenuItems[i].pdata)) |
| 143 | + { |
| 144 | + break; |
| 145 | + } |
| 146 | + |
| 147 | + if ( i == (NUM_MENU_ITEMS - 1) ) |
| 148 | + { |
| 149 | + displayedCredentials = true; |
| 150 | + displayCredentials(); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +#endif |
| 157 | + |
| 158 | +void loop() |
| 159 | +{ |
| 160 | + WiFiManager_Generic->run(); |
| 161 | + check_status(); |
| 162 | + |
| 163 | +#if USE_DYNAMIC_PARAMETERS |
| 164 | + displayCredentialsInLoop(); |
| 165 | +#endif |
| 166 | +} |
0 commit comments