|
| 1 | +/**************************************************************************************************************************** |
| 2 | + Async_HttpBasicAuth.ino |
| 3 | +
|
| 4 | + For ENC28J60 Ethernet in ESP32 (ESP32 + ENC28J60) |
| 5 | +
|
| 6 | + AsyncWebServer_ESP32_ENC is a library for the Ethernet ENC28J60 in ESSP32 to run AsyncWebServer |
| 7 | +
|
| 8 | + Based on and modified from ESPAsyncWebServer (https://github.com/me-no-dev/ESPAsyncWebServer) |
| 9 | + Built by Khoi Hoang https://github.com/khoih-prog/AsyncWebServer_ESP32_ENC |
| 10 | + Licensed under GPLv3 license |
| 11 | + *****************************************************************************************************************************/ |
| 12 | + |
| 13 | +#if !( defined(ESP32) ) |
| 14 | + #error This code is designed for (ESP32 + ENC28J60) to run on ESP32 platform! Please check your Tools->Board setting. |
| 15 | +#endif |
| 16 | + |
| 17 | +#include <Arduino.h> |
| 18 | + |
| 19 | +#define _ASYNC_WEBSERVER_LOGLEVEL_ 4 |
| 20 | + |
| 21 | +// Enter a MAC address and IP address for your controller below. |
| 22 | +#define NUMBER_OF_MAC 20 |
| 23 | + |
| 24 | +byte mac[][NUMBER_OF_MAC] = |
| 25 | +{ |
| 26 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 }, |
| 27 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 }, |
| 28 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 }, |
| 29 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 }, |
| 30 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 }, |
| 31 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 }, |
| 32 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 }, |
| 33 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 }, |
| 34 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 }, |
| 35 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A }, |
| 36 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B }, |
| 37 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C }, |
| 38 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D }, |
| 39 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E }, |
| 40 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F }, |
| 41 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 }, |
| 42 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 }, |
| 43 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 }, |
| 44 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 }, |
| 45 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 }, |
| 46 | +}; |
| 47 | + |
| 48 | +// Select the IP address according to your local network |
| 49 | +IPAddress myIP(192, 168, 2, 232); |
| 50 | +IPAddress myGW(192, 168, 2, 1); |
| 51 | +IPAddress mySN(255, 255, 255, 0); |
| 52 | + |
| 53 | +// Google DNS Server IP |
| 54 | +IPAddress myDNS(8, 8, 8, 8); |
| 55 | + |
| 56 | +////////////////////////////////////////////////////////// |
| 57 | + |
| 58 | +// Optional values to override default settings |
| 59 | +//#define SPI_HOST 1 |
| 60 | +//#define SPI_CLOCK_MHZ 8 |
| 61 | + |
| 62 | +// Must connect INT to GPIOxx or not working |
| 63 | +//#define INT_GPIO 4 |
| 64 | + |
| 65 | +//#define MISO_GPIO 19 |
| 66 | +//#define MOSI_GPIO 23 |
| 67 | +//#define SCK_GPIO 18 |
| 68 | +//#define CS_GPIO 5 |
| 69 | +// Optional values to override default settings |
| 70 | + |
| 71 | +////////////////////////////////////////////////////////// |
| 72 | + |
| 73 | +#include <AsyncWebServer_ESP32_ENC.h> |
| 74 | + |
| 75 | +AsyncWebServer server(80); |
| 76 | + |
| 77 | +const char* www_username = "admin"; |
| 78 | +const char* www_password = "esp32_enc28j60"; |
| 79 | + |
| 80 | +void setup() |
| 81 | +{ |
| 82 | + Serial.begin(115200); |
| 83 | + |
| 84 | + while (!Serial && millis() < 5000); |
| 85 | + |
| 86 | + delay(200); |
| 87 | + |
| 88 | + Serial.print("\nStart Async_HTTPBasicAuth on "); |
| 89 | + Serial.print(ARDUINO_BOARD); |
| 90 | + Serial.print(" with "); |
| 91 | + Serial.println(SHIELD_TYPE); |
| 92 | + Serial.println(ASYNC_WEBSERVER_ESP32_ENC_VERSION); |
| 93 | + |
| 94 | + AWS_LOGWARN(F("Default SPI pinout:")); |
| 95 | + AWS_LOGWARN1(F("MOSI:"), MOSI_GPIO); |
| 96 | + AWS_LOGWARN1(F("MISO:"), MISO_GPIO); |
| 97 | + AWS_LOGWARN1(F("SCK:"), SCK_GPIO); |
| 98 | + AWS_LOGWARN1(F("CS:"), CS_GPIO); |
| 99 | + AWS_LOGWARN1(F("INT:"), INT_GPIO); |
| 100 | + AWS_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ); |
| 101 | + AWS_LOGWARN(F("=========================")); |
| 102 | + |
| 103 | + /////////////////////////////////// |
| 104 | + |
| 105 | + // To be called before ETH.begin() |
| 106 | + ESP32_ENC_onEvent(); |
| 107 | + |
| 108 | + // start the ethernet connection and the server: |
| 109 | + // Use DHCP dynamic IP and random mac |
| 110 | + uint16_t index = millis() % NUMBER_OF_MAC; |
| 111 | + |
| 112 | + //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ, |
| 113 | + // int SPI_HOST, uint8_t *ENC28J60_Mac = ENC28J60_Default_Mac); |
| 114 | + //ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, SPI_HOST ); |
| 115 | + ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, SPI_HOST, mac[index] ); |
| 116 | + |
| 117 | + // Static IP, leave without this line to get IP via DHCP |
| 118 | + //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0); |
| 119 | + //ETH.config(myIP, myGW, mySN, myDNS); |
| 120 | + |
| 121 | + ESP32_ENC_waitForConnect(); |
| 122 | + |
| 123 | + /////////////////////////////////// |
| 124 | + |
| 125 | + server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) |
| 126 | + { |
| 127 | + if (!request->authenticate(www_username, www_password)) |
| 128 | + { |
| 129 | + return request->requestAuthentication(); |
| 130 | + } |
| 131 | + |
| 132 | + request->send(200, "text/plain", "Login OK"); |
| 133 | + }); |
| 134 | + |
| 135 | + server.begin(); |
| 136 | + |
| 137 | + Serial.print(F("Async_HttpBasicAuth started @ IP : ")); |
| 138 | + Serial.println(ETH.localIP()); |
| 139 | + |
| 140 | + Serial.print(F("Open http://")); |
| 141 | + Serial.print(ETH.localIP()); |
| 142 | + Serial.println(F("/ in your browser to see it working")); |
| 143 | + |
| 144 | + Serial.print(F("Login using username = ")); |
| 145 | + Serial.print(www_username); |
| 146 | + Serial.print(F(" and password = ")); |
| 147 | + Serial.println(www_password); |
| 148 | +} |
| 149 | + |
| 150 | +void heartBeatPrint() |
| 151 | +{ |
| 152 | + static int num = 1; |
| 153 | + |
| 154 | + Serial.print(F(".")); |
| 155 | + |
| 156 | + if (num == 80) |
| 157 | + { |
| 158 | + Serial.println(); |
| 159 | + num = 1; |
| 160 | + } |
| 161 | + else if (num++ % 10 == 0) |
| 162 | + { |
| 163 | + Serial.print(F(" ")); |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +void check_status() |
| 168 | +{ |
| 169 | + static unsigned long checkstatus_timeout = 0; |
| 170 | + |
| 171 | +#define STATUS_CHECK_INTERVAL 10000L |
| 172 | + |
| 173 | + // Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to send updates frequently if there is no status change. |
| 174 | + if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0)) |
| 175 | + { |
| 176 | + heartBeatPrint(); |
| 177 | + checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL; |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +void loop() |
| 182 | +{ |
| 183 | + check_status(); |
| 184 | +} |
0 commit comments