v2.9
This commit is contained in:
@ -0,0 +1,275 @@
|
||||
/****************************************************************************************************************************
|
||||
AdvancedWebServer.h
|
||||
|
||||
For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
|
||||
|
||||
WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
|
||||
|
||||
Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
|
||||
Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
|
||||
Licensed under GPLv3 license
|
||||
|
||||
Copyright (c) 2015, Majenko Technologies
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
Redistributions in binary form must reproduce the above copyright notice, this
|
||||
list of conditions and the following disclaimer in the documentation and/or
|
||||
other materials provided with the distribution.
|
||||
|
||||
Neither the name of Majenko Technologies nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#if !( defined(ESP32) )
|
||||
#error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
|
||||
#endif
|
||||
|
||||
#define DEBUG_ETHERNET_WEBSERVER_PORT Serial
|
||||
|
||||
// Debug Level from 0 to 4
|
||||
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
// Optional values to override default settings
|
||||
// Don't change unless you know what you're doing
|
||||
//#define ETH_SPI_HOST SPI3_HOST
|
||||
//#define SPI_CLOCK_MHZ 25
|
||||
|
||||
// Must connect INT to GPIOxx or not working
|
||||
//#define INT_GPIO 4
|
||||
|
||||
//#define MISO_GPIO 19
|
||||
//#define MOSI_GPIO 23
|
||||
//#define SCK_GPIO 18
|
||||
//#define CS_GPIO 5
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
#include <WebServer_ESP32_W5500.h>
|
||||
|
||||
WebServer server(80);
|
||||
|
||||
// Enter a MAC address and IP address for your controller below.
|
||||
#define NUMBER_OF_MAC 20
|
||||
|
||||
byte mac[][NUMBER_OF_MAC] =
|
||||
{
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
|
||||
};
|
||||
|
||||
// Select the IP address according to your local network
|
||||
IPAddress myIP(192, 168, 2, 232);
|
||||
IPAddress myGW(192, 168, 2, 1);
|
||||
IPAddress mySN(255, 255, 255, 0);
|
||||
|
||||
// Google DNS Server IP
|
||||
IPAddress myDNS(8, 8, 8, 8);
|
||||
|
||||
int reqCount = 0; // number of requests received
|
||||
|
||||
void handleRoot()
|
||||
{
|
||||
#define BUFFER_SIZE 400
|
||||
|
||||
char temp[BUFFER_SIZE];
|
||||
int sec = millis() / 1000;
|
||||
int min = sec / 60;
|
||||
int hr = min / 60;
|
||||
int day = hr / 24;
|
||||
|
||||
hr = hr % 24;
|
||||
|
||||
snprintf(temp, BUFFER_SIZE - 1,
|
||||
"<html>\
|
||||
<head>\
|
||||
<meta http-equiv='refresh' content='5'/>\
|
||||
<title>AdvancedWebServer %s</title>\
|
||||
<style>\
|
||||
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
|
||||
</style>\
|
||||
</head>\
|
||||
<body>\
|
||||
<h2>Hi from WebServer_ESP32_W5500!</h2>\
|
||||
<h3>on %s</h3>\
|
||||
<p>Uptime: %d d %02d:%02d:%02d</p>\
|
||||
<img src=\"/test.svg\" />\
|
||||
</body>\
|
||||
</html>", BOARD_NAME, BOARD_NAME, day, hr % 24, min % 60, sec % 60);
|
||||
|
||||
server.send(200, F("text/html"), temp);
|
||||
}
|
||||
|
||||
void handleNotFound()
|
||||
{
|
||||
String message = F("File Not Found\n\n");
|
||||
|
||||
message += F("URI: ");
|
||||
message += server.uri();
|
||||
message += F("\nMethod: ");
|
||||
message += (server.method() == HTTP_GET) ? F("GET") : F("POST");
|
||||
message += F("\nArguments: ");
|
||||
message += server.args();
|
||||
message += F("\n");
|
||||
|
||||
for (uint8_t i = 0; i < server.args(); i++)
|
||||
{
|
||||
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
|
||||
}
|
||||
|
||||
server.send(404, F("text/plain"), message);
|
||||
}
|
||||
|
||||
void drawGraph()
|
||||
{
|
||||
String out;
|
||||
out.reserve(3000);
|
||||
char temp[70];
|
||||
|
||||
out += F("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"310\" height=\"150\">\n");
|
||||
out += F("<rect width=\"310\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"2\" stroke=\"rgb(0, 0, 0)\" />\n");
|
||||
out += F("<g stroke=\"blue\">\n");
|
||||
int y = rand() % 130;
|
||||
|
||||
for (int x = 10; x < 300; x += 10)
|
||||
{
|
||||
int y2 = rand() % 130;
|
||||
sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"2\" />\n", x, 140 - y, x + 10, 140 - y2);
|
||||
out += temp;
|
||||
y = y2;
|
||||
}
|
||||
|
||||
out += F("</g>\n</svg>\n");
|
||||
|
||||
server.send(200, F("image/svg+xml"), out);
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
while (!Serial && (millis() < 5000));
|
||||
|
||||
Serial.print(F("\nStart AdvancedWebServer on "));
|
||||
Serial.print(ARDUINO_BOARD);
|
||||
Serial.print(F(" with "));
|
||||
Serial.println(SHIELD_TYPE);
|
||||
Serial.println(WEBSERVER_ESP32_W5500_VERSION);
|
||||
|
||||
ET_LOGWARN(F("Default SPI pinout:"));
|
||||
ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
|
||||
ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
|
||||
ET_LOGWARN1(F("MISO:"), MISO_GPIO);
|
||||
ET_LOGWARN1(F("SCK:"), SCK_GPIO);
|
||||
ET_LOGWARN1(F("CS:"), CS_GPIO);
|
||||
ET_LOGWARN1(F("INT:"), INT_GPIO);
|
||||
ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
|
||||
ET_LOGWARN(F("========================="));
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
// To be called before ETH.begin()
|
||||
ESP32_W5500_onEvent();
|
||||
|
||||
// start the ethernet connection and the server:
|
||||
// Use DHCP dynamic IP and random mac
|
||||
//bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
|
||||
// int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
|
||||
ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
|
||||
//ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
|
||||
|
||||
// Static IP, leave without this line to get IP via DHCP
|
||||
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
|
||||
//ETH.config(myIP, myGW, mySN, myDNS);
|
||||
|
||||
ESP32_W5500_waitForConnect();
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
server.on(F("/"), handleRoot);
|
||||
server.on(F("/test.svg"), drawGraph);
|
||||
server.on(F("/inline"), []()
|
||||
{
|
||||
server.send(200, F("text/plain"), F("This works as well"));
|
||||
});
|
||||
|
||||
server.onNotFound(handleNotFound);
|
||||
server.begin();
|
||||
|
||||
Serial.print(F("HTTP EthernetWebServer is @ IP : "));
|
||||
Serial.println(ETH.localIP());
|
||||
}
|
||||
|
||||
void heartBeatPrint()
|
||||
{
|
||||
static int num = 1;
|
||||
|
||||
Serial.print(F("."));
|
||||
|
||||
if (num == 80)
|
||||
{
|
||||
Serial.println();
|
||||
num = 1;
|
||||
}
|
||||
else if (num++ % 10 == 0)
|
||||
{
|
||||
Serial.print(F(" "));
|
||||
}
|
||||
}
|
||||
|
||||
void check_status()
|
||||
{
|
||||
static unsigned long checkstatus_timeout = 0;
|
||||
|
||||
#define STATUS_CHECK_INTERVAL 10000L
|
||||
|
||||
// Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to send updates frequently if there is no status change.
|
||||
if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
|
||||
{
|
||||
heartBeatPrint();
|
||||
checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL;
|
||||
}
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
server.handleClient();
|
||||
check_status();
|
||||
}
|
167
lib/WebServer_ESP32_W5500/examples/HelloServer/HelloServer.ino
Normal file
167
lib/WebServer_ESP32_W5500/examples/HelloServer/HelloServer.ino
Normal file
@ -0,0 +1,167 @@
|
||||
/****************************************************************************************************************************
|
||||
HelloServer.ino - Dead simple web-server for Ethernet shields
|
||||
|
||||
For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
|
||||
|
||||
WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
|
||||
|
||||
Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
|
||||
Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
|
||||
Licensed under GPLv3 license
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#if !( defined(ESP32) )
|
||||
#error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
|
||||
#endif
|
||||
|
||||
#define DEBUG_ETHERNET_WEBSERVER_PORT Serial
|
||||
|
||||
// Debug Level from 0 to 4
|
||||
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
// Optional values to override default settings
|
||||
// Don't change unless you know what you're doing
|
||||
//#define ETH_SPI_HOST SPI3_HOST
|
||||
//#define SPI_CLOCK_MHZ 25
|
||||
|
||||
// Must connect INT to GPIOxx or not working
|
||||
//#define INT_GPIO 4
|
||||
|
||||
//#define MISO_GPIO 19
|
||||
//#define MOSI_GPIO 23
|
||||
//#define SCK_GPIO 18
|
||||
//#define CS_GPIO 5
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
#include <WebServer_ESP32_W5500.h>
|
||||
|
||||
WebServer server(80);
|
||||
|
||||
// Enter a MAC address and IP address for your controller below.
|
||||
#define NUMBER_OF_MAC 20
|
||||
|
||||
byte mac[][NUMBER_OF_MAC] =
|
||||
{
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
|
||||
};
|
||||
|
||||
// Select the IP address according to your local network
|
||||
IPAddress myIP(192, 168, 2, 232);
|
||||
IPAddress myGW(192, 168, 2, 1);
|
||||
IPAddress mySN(255, 255, 255, 0);
|
||||
|
||||
// Google DNS Server IP
|
||||
IPAddress myDNS(8, 8, 8, 8);
|
||||
|
||||
void handleRoot()
|
||||
{
|
||||
String html = F("Hello from HelloServer running on ");
|
||||
|
||||
html += String(BOARD_NAME);
|
||||
|
||||
server.send(200, F("text/plain"), html);
|
||||
}
|
||||
|
||||
void handleNotFound()
|
||||
{
|
||||
String message = F("File Not Found\n\n");
|
||||
|
||||
message += F("URI: ");
|
||||
message += server.uri();
|
||||
message += F("\nMethod: ");
|
||||
message += (server.method() == HTTP_GET) ? F("GET") : F("POST");
|
||||
message += F("\nArguments: ");
|
||||
message += server.args();
|
||||
message += F("\n");
|
||||
|
||||
for (uint8_t i = 0; i < server.args(); i++)
|
||||
{
|
||||
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
|
||||
}
|
||||
|
||||
server.send(404, F("text/plain"), message);
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
while (!Serial && (millis() < 5000));
|
||||
|
||||
Serial.print(F("\nStart HelloServer on "));
|
||||
Serial.print(ARDUINO_BOARD);
|
||||
Serial.print(F(" with "));
|
||||
Serial.println(SHIELD_TYPE);
|
||||
Serial.println(WEBSERVER_ESP32_W5500_VERSION);
|
||||
|
||||
ET_LOGWARN(F("Default SPI pinout:"));
|
||||
ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
|
||||
ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
|
||||
ET_LOGWARN1(F("MISO:"), MISO_GPIO);
|
||||
ET_LOGWARN1(F("SCK:"), SCK_GPIO);
|
||||
ET_LOGWARN1(F("CS:"), CS_GPIO);
|
||||
ET_LOGWARN1(F("INT:"), INT_GPIO);
|
||||
ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
|
||||
ET_LOGWARN(F("========================="));
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
// To be called before ETH.begin()
|
||||
ESP32_W5500_onEvent();
|
||||
|
||||
// start the ethernet connection and the server:
|
||||
// Use DHCP dynamic IP and random mac
|
||||
//bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
|
||||
// int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
|
||||
ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
|
||||
//ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
|
||||
|
||||
// Static IP, leave without this line to get IP via DHCP
|
||||
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
|
||||
//ETH.config(myIP, myGW, mySN, myDNS);
|
||||
|
||||
ESP32_W5500_waitForConnect();
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
server.on(F("/"), handleRoot);
|
||||
|
||||
server.on(F("/inline"), []()
|
||||
{
|
||||
server.send(200, F("text/plain"), F("This works as well"));
|
||||
});
|
||||
|
||||
server.onNotFound(handleNotFound);
|
||||
|
||||
server.begin();
|
||||
|
||||
Serial.print(F("HTTP EthernetWebServer is @ IP : "));
|
||||
Serial.println(ETH.localIP());
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
server.handleClient();
|
||||
}
|
190
lib/WebServer_ESP32_W5500/examples/HelloServer2/HelloServer2.ino
Normal file
190
lib/WebServer_ESP32_W5500/examples/HelloServer2/HelloServer2.ino
Normal file
@ -0,0 +1,190 @@
|
||||
/****************************************************************************************************************************
|
||||
HelloServer2.h - Dead simple web-server for Ethernet shields
|
||||
|
||||
For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
|
||||
|
||||
WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
|
||||
|
||||
Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
|
||||
Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
|
||||
Licensed under GPLv3 license
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#if !( defined(ESP32) )
|
||||
#error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
|
||||
#endif
|
||||
|
||||
#define DEBUG_ETHERNET_WEBSERVER_PORT Serial
|
||||
|
||||
// Debug Level from 0 to 4
|
||||
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
// Optional values to override default settings
|
||||
// Don't change unless you know what you're doing
|
||||
//#define ETH_SPI_HOST SPI3_HOST
|
||||
//#define SPI_CLOCK_MHZ 25
|
||||
|
||||
// Must connect INT to GPIOxx or not working
|
||||
//#define INT_GPIO 4
|
||||
|
||||
//#define MISO_GPIO 19
|
||||
//#define MOSI_GPIO 23
|
||||
//#define SCK_GPIO 18
|
||||
//#define CS_GPIO 5
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
#include <WebServer_ESP32_W5500.h>
|
||||
|
||||
WebServer server(80);
|
||||
|
||||
// Enter a MAC address and IP address for your controller below.
|
||||
#define NUMBER_OF_MAC 20
|
||||
|
||||
byte mac[][NUMBER_OF_MAC] =
|
||||
{
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
|
||||
};
|
||||
|
||||
// Select the IP address according to your local network
|
||||
IPAddress myIP(192, 168, 2, 232);
|
||||
IPAddress myGW(192, 168, 2, 1);
|
||||
IPAddress mySN(255, 255, 255, 0);
|
||||
|
||||
// Google DNS Server IP
|
||||
IPAddress myDNS(8, 8, 8, 8);
|
||||
|
||||
void handleRoot()
|
||||
{
|
||||
String html = F("Hello from HelloServer2 running on ");
|
||||
|
||||
html += String(BOARD_NAME);
|
||||
|
||||
server.send(200, F("text/plain"), html);
|
||||
}
|
||||
|
||||
void handleNotFound()
|
||||
{
|
||||
String message = F("File Not Found\n\n");
|
||||
|
||||
message += F("URI: ");
|
||||
message += server.uri();
|
||||
message += F("\nMethod: ");
|
||||
message += (server.method() == HTTP_GET) ? F("GET") : F("POST");
|
||||
message += F("\nArguments: ");
|
||||
message += server.args();
|
||||
message += F("\n");
|
||||
|
||||
for (uint8_t i = 0; i < server.args(); i++)
|
||||
{
|
||||
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
|
||||
}
|
||||
|
||||
server.send(404, F("text/plain"), message);
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
while (!Serial && (millis() < 5000));
|
||||
|
||||
Serial.print(F("\nStart HelloServer2 on "));
|
||||
Serial.print(ARDUINO_BOARD);
|
||||
Serial.print(F(" with "));
|
||||
Serial.println(SHIELD_TYPE);
|
||||
Serial.println(WEBSERVER_ESP32_W5500_VERSION);
|
||||
|
||||
ET_LOGWARN(F("Default SPI pinout:"));
|
||||
ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
|
||||
ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
|
||||
ET_LOGWARN1(F("MISO:"), MISO_GPIO);
|
||||
ET_LOGWARN1(F("SCK:"), SCK_GPIO);
|
||||
ET_LOGWARN1(F("CS:"), CS_GPIO);
|
||||
ET_LOGWARN1(F("INT:"), INT_GPIO);
|
||||
ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
|
||||
ET_LOGWARN(F("========================="));
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
// To be called before ETH.begin()
|
||||
ESP32_W5500_onEvent();
|
||||
|
||||
// start the ethernet connection and the server:
|
||||
// Use DHCP dynamic IP and random mac
|
||||
//bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
|
||||
// int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
|
||||
ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
|
||||
//ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
|
||||
|
||||
// Static IP, leave without this line to get IP via DHCP
|
||||
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
|
||||
//ETH.config(myIP, myGW, mySN, myDNS);
|
||||
|
||||
ESP32_W5500_waitForConnect();
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
server.on(F("/"), handleRoot);
|
||||
|
||||
server.on(F("/inline"), []()
|
||||
{
|
||||
server.send(200, F("text/plain"), F("This works as well"));
|
||||
});
|
||||
|
||||
server.on(F("/gif"), []()
|
||||
{
|
||||
static const uint8_t gif[] PROGMEM =
|
||||
{
|
||||
0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x10, 0x00, 0x10, 0x00, 0x80, 0x01,
|
||||
0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00, 0x00, 0x00,
|
||||
0x10, 0x00, 0x10, 0x00, 0x00, 0x02, 0x19, 0x8c, 0x8f, 0xa9, 0xcb, 0x9d,
|
||||
0x00, 0x5f, 0x74, 0xb4, 0x56, 0xb0, 0xb0, 0xd2, 0xf2, 0x35, 0x1e, 0x4c,
|
||||
0x0c, 0x24, 0x5a, 0xe6, 0x89, 0xa6, 0x4d, 0x01, 0x00, 0x3b
|
||||
};
|
||||
|
||||
char gif_colored[sizeof(gif)];
|
||||
|
||||
memcpy_P(gif_colored, gif, sizeof(gif));
|
||||
|
||||
// Set the background to a random set of colors
|
||||
gif_colored[16] = millis() % 256;
|
||||
gif_colored[17] = millis() % 256;
|
||||
gif_colored[18] = millis() % 256;
|
||||
|
||||
server.send_P(200, "image/gif", gif_colored, sizeof(gif_colored));
|
||||
});
|
||||
|
||||
server.onNotFound(handleNotFound);
|
||||
|
||||
server.begin();
|
||||
|
||||
Serial.print(F("HTTP HelloServer2 started @ IP : "));
|
||||
Serial.println(ETH.localIP());
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
server.handleClient();
|
||||
}
|
@ -0,0 +1,150 @@
|
||||
/****************************************************************************************************************************
|
||||
HTTPBasicAuth.h - Dead simple web-server for Ethernet shields
|
||||
|
||||
For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
|
||||
|
||||
WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
|
||||
|
||||
Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
|
||||
Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
|
||||
Licensed under GPLv3 license
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#if !( defined(ESP32) )
|
||||
#error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
|
||||
#endif
|
||||
|
||||
#define DEBUG_ETHERNET_WEBSERVER_PORT Serial
|
||||
|
||||
// Debug Level from 0 to 4
|
||||
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
// Optional values to override default settings
|
||||
// Don't change unless you know what you're doing
|
||||
//#define ETH_SPI_HOST SPI3_HOST
|
||||
//#define SPI_CLOCK_MHZ 25
|
||||
|
||||
// Must connect INT to GPIOxx or not working
|
||||
//#define INT_GPIO 4
|
||||
|
||||
//#define MISO_GPIO 19
|
||||
//#define MOSI_GPIO 23
|
||||
//#define SCK_GPIO 18
|
||||
//#define CS_GPIO 5
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
#include <WebServer_ESP32_W5500.h>
|
||||
|
||||
WebServer server(80);
|
||||
|
||||
// Enter a MAC address and IP address for your controller below.
|
||||
#define NUMBER_OF_MAC 20
|
||||
|
||||
byte mac[][NUMBER_OF_MAC] =
|
||||
{
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
|
||||
};
|
||||
|
||||
// Select the IP address according to your local network
|
||||
IPAddress myIP(192, 168, 2, 232);
|
||||
IPAddress myGW(192, 168, 2, 1);
|
||||
IPAddress mySN(255, 255, 255, 0);
|
||||
|
||||
// Google DNS Server IP
|
||||
IPAddress myDNS(8, 8, 8, 8);
|
||||
|
||||
const char* www_username = "admin";
|
||||
const char* www_password = "esp32_w5500";
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
while (!Serial && (millis() < 5000));
|
||||
|
||||
Serial.print(F("\nStart HTTPBasicAuth on "));
|
||||
Serial.print(ARDUINO_BOARD);
|
||||
Serial.print(F(" with "));
|
||||
Serial.println(SHIELD_TYPE);
|
||||
Serial.println(WEBSERVER_ESP32_W5500_VERSION);
|
||||
|
||||
ET_LOGWARN(F("Default SPI pinout:"));
|
||||
ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
|
||||
ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
|
||||
ET_LOGWARN1(F("MISO:"), MISO_GPIO);
|
||||
ET_LOGWARN1(F("SCK:"), SCK_GPIO);
|
||||
ET_LOGWARN1(F("CS:"), CS_GPIO);
|
||||
ET_LOGWARN1(F("INT:"), INT_GPIO);
|
||||
ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
|
||||
ET_LOGWARN(F("========================="));
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
// To be called before ETH.begin()
|
||||
ESP32_W5500_onEvent();
|
||||
|
||||
// start the ethernet connection and the server:
|
||||
// Use DHCP dynamic IP and random mac
|
||||
//bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
|
||||
// int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
|
||||
ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
|
||||
//ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
|
||||
|
||||
// Static IP, leave without this line to get IP via DHCP
|
||||
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
|
||||
//ETH.config(myIP, myGW, mySN, myDNS);
|
||||
|
||||
ESP32_W5500_waitForConnect();
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
server.on(F("/"), []()
|
||||
{
|
||||
if (!server.authenticate(www_username, www_password))
|
||||
{
|
||||
return server.requestAuthentication();
|
||||
}
|
||||
|
||||
server.send(200, F("text/plain"), F("Login OK"));
|
||||
});
|
||||
|
||||
server.begin();
|
||||
|
||||
Serial.print(F("HTTP HTTPBasicAuth started @ IP : "));
|
||||
Serial.println(ETH.localIP());
|
||||
|
||||
Serial.print(F("Open http://"));
|
||||
Serial.print(ETH.localIP());
|
||||
Serial.println(F("/ in your browser to see it working"));
|
||||
Serial.print(F("Using username : "));
|
||||
Serial.print(www_username);
|
||||
Serial.print(F(" and password : "));
|
||||
Serial.println(www_password);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
server.handleClient();
|
||||
}
|
@ -0,0 +1,245 @@
|
||||
/****************************************************************************************************************************
|
||||
MQTTClient_Auth.ino - Dead simple MQTT Client for Ethernet shields
|
||||
|
||||
For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
|
||||
|
||||
WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
|
||||
|
||||
Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
|
||||
Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
|
||||
Licensed under GPLv3 license
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
/*
|
||||
Basic MQTT example (without SSL!) with Authentication
|
||||
This sketch demonstrates the basic capabilities of the library.
|
||||
It connects to an MQTT server then:
|
||||
- providing username and password
|
||||
- publishes "hello world" to the topic "outTopic"
|
||||
- subscribes to the topic "inTopic", printing out any messages
|
||||
it receives. NB - it assumes the received payloads are strings not binary
|
||||
It will reconnect to the server if the connection is lost using a blocking
|
||||
reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
|
||||
achieve the same result without blocking the main loop.
|
||||
*/
|
||||
|
||||
#if !( defined(ESP32) )
|
||||
#error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
|
||||
#endif
|
||||
|
||||
#define DEBUG_ETHERNET_WEBSERVER_PORT Serial
|
||||
|
||||
// Debug Level from 0 to 4
|
||||
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
// Optional values to override default settings
|
||||
// Don't change unless you know what you're doing
|
||||
//#define ETH_SPI_HOST SPI3_HOST
|
||||
//#define SPI_CLOCK_MHZ 25
|
||||
|
||||
// Must connect INT to GPIOxx or not working
|
||||
//#define INT_GPIO 4
|
||||
|
||||
//#define MISO_GPIO 19
|
||||
//#define MOSI_GPIO 23
|
||||
//#define SCK_GPIO 18
|
||||
//#define CS_GPIO 5
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
#include <WebServer_ESP32_W5500.h>
|
||||
|
||||
WebServer server(80);
|
||||
|
||||
// Enter a MAC address and IP address for your controller below.
|
||||
#define NUMBER_OF_MAC 20
|
||||
|
||||
byte mac[][NUMBER_OF_MAC] =
|
||||
{
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
|
||||
};
|
||||
|
||||
// Select the IP address according to your local network
|
||||
IPAddress myIP(192, 168, 2, 232);
|
||||
IPAddress myGW(192, 168, 2, 1);
|
||||
IPAddress mySN(255, 255, 255, 0);
|
||||
|
||||
// Google DNS Server IP
|
||||
IPAddress myDNS(8, 8, 8, 8);
|
||||
|
||||
#include <PubSubClient.h>
|
||||
|
||||
// Update these with values suitable for your network.
|
||||
//const char* mqttServer = "broker.example"; // Broker address
|
||||
const char* mqttServer = "broker.emqx.io"; // Broker address
|
||||
//const char* mqttServer = "broker.shiftr.io"; // Broker address
|
||||
|
||||
const char *ID = "MQTTClient_SSL-Client"; // Name of our device, must be unique
|
||||
const char *TOPIC = "MQTT_Pub"; // Topic to subscribe to
|
||||
const char *subTopic = "MQTT_Sub"; // Topic to subscribe to
|
||||
|
||||
//IPAddress mqttServer(172, 16, 0, 2);
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length)
|
||||
{
|
||||
Serial.print("Message arrived [");
|
||||
Serial.print(topic);
|
||||
Serial.print("] ");
|
||||
|
||||
for (unsigned int i = 0; i < length; i++)
|
||||
{
|
||||
Serial.print((char)payload[i]);
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
WiFiClient ethClient;
|
||||
PubSubClient client(mqttServer, 1883, callback, ethClient);
|
||||
|
||||
void reconnect()
|
||||
{
|
||||
// Loop until we're reconnected
|
||||
while (!client.connected())
|
||||
{
|
||||
Serial.print("Attempting MQTT connection to ");
|
||||
Serial.print(mqttServer);
|
||||
|
||||
// Attempt to connect
|
||||
if (client.connect("arduino", "try", "try"))
|
||||
{
|
||||
Serial.println("...connected");
|
||||
|
||||
// Once connected, publish an announcement...
|
||||
String data = "Hello from MQTTClient_SSL on " + String(BOARD_NAME);
|
||||
|
||||
client.publish(TOPIC, data.c_str());
|
||||
|
||||
//Serial.println("Published connection message successfully!");
|
||||
//Serial.print("Subscribed to: ");
|
||||
//Serial.println(subTopic);
|
||||
|
||||
// This is a workaround to address https://github.com/OPEnSLab-OSU/SSLClient/issues/9
|
||||
//ethClientSSL.flush();
|
||||
// ... and resubscribe
|
||||
client.subscribe(subTopic);
|
||||
// for loopback testing
|
||||
client.subscribe(TOPIC);
|
||||
// This is a workaround to address https://github.com/OPEnSLab-OSU/SSLClient/issues/9
|
||||
//ethClientSSL.flush();
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print("...failed, rc=");
|
||||
Serial.print(client.state());
|
||||
Serial.println(" try again in 5 seconds");
|
||||
|
||||
// Wait 5 seconds before retrying
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
while (!Serial && (millis() < 5000));
|
||||
|
||||
Serial.print(F("\nStart MQTTClient_Auth on "));
|
||||
Serial.print(ARDUINO_BOARD);
|
||||
Serial.print(F(" with "));
|
||||
Serial.println(SHIELD_TYPE);
|
||||
Serial.println(WEBSERVER_ESP32_W5500_VERSION);
|
||||
|
||||
ET_LOGWARN(F("Default SPI pinout:"));
|
||||
ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
|
||||
ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
|
||||
ET_LOGWARN1(F("MISO:"), MISO_GPIO);
|
||||
ET_LOGWARN1(F("SCK:"), SCK_GPIO);
|
||||
ET_LOGWARN1(F("CS:"), CS_GPIO);
|
||||
ET_LOGWARN1(F("INT:"), INT_GPIO);
|
||||
ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
|
||||
ET_LOGWARN(F("========================="));
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
// To be called before ETH.begin()
|
||||
ESP32_W5500_onEvent();
|
||||
|
||||
// start the ethernet connection and the server:
|
||||
// Use DHCP dynamic IP and random mac
|
||||
//bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
|
||||
// int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
|
||||
ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
|
||||
//ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
|
||||
|
||||
// Static IP, leave without this line to get IP via DHCP
|
||||
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
|
||||
//ETH.config(myIP, myGW, mySN, myDNS);
|
||||
|
||||
ESP32_W5500_waitForConnect();
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
// Note - the default maximum packet size is 128 bytes. If the
|
||||
// combined length of clientId, username and password exceed this use the
|
||||
// following to increase the buffer size:
|
||||
// client.setBufferSize(255);
|
||||
}
|
||||
|
||||
#define MQTT_PUBLISH_INTERVAL_MS 5000L
|
||||
|
||||
String data = "Hello from MQTTClient_Auth on " + String(ARDUINO_BOARD) + " with " + String(SHIELD_TYPE);
|
||||
const char *pubData = data.c_str();
|
||||
|
||||
unsigned long lastMsg = 0;
|
||||
|
||||
void loop()
|
||||
{
|
||||
static unsigned long now;
|
||||
|
||||
if (!client.connected())
|
||||
{
|
||||
reconnect();
|
||||
}
|
||||
|
||||
// Sending Data
|
||||
now = millis();
|
||||
|
||||
if (now - lastMsg > MQTT_PUBLISH_INTERVAL_MS)
|
||||
{
|
||||
lastMsg = now;
|
||||
|
||||
if (!client.publish(TOPIC, pubData))
|
||||
{
|
||||
Serial.println("Message failed to send.");
|
||||
}
|
||||
|
||||
Serial.print("Message Send : " + String(TOPIC) + " => ");
|
||||
Serial.println(data);
|
||||
}
|
||||
|
||||
client.loop();
|
||||
}
|
@ -0,0 +1,247 @@
|
||||
/****************************************************************************************************************************
|
||||
MQTTClient_Basic.ino - Dead simple MQTT Client for Ethernet shields
|
||||
|
||||
For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
|
||||
|
||||
WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
|
||||
|
||||
Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
|
||||
Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
|
||||
Licensed under GPLv3 license
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
/*
|
||||
Basic MQTT example (without SSL!) with Authentication
|
||||
This sketch demonstrates the basic capabilities of the library.
|
||||
It connects to an MQTT server then:
|
||||
- providing username and password
|
||||
- publishes "hello world" to the topic "outTopic"
|
||||
- subscribes to the topic "inTopic", printing out any messages
|
||||
it receives. NB - it assumes the received payloads are strings not binary
|
||||
|
||||
It will reconnect to the server if the connection is lost using a blocking
|
||||
reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
|
||||
achieve the same result without blocking the main loop.
|
||||
*/
|
||||
|
||||
#if !( defined(ESP32) )
|
||||
#error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
|
||||
#endif
|
||||
|
||||
#define DEBUG_ETHERNET_WEBSERVER_PORT Serial
|
||||
|
||||
// Debug Level from 0 to 4
|
||||
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
// Optional values to override default settings
|
||||
// Don't change unless you know what you're doing
|
||||
//#define ETH_SPI_HOST SPI3_HOST
|
||||
//#define SPI_CLOCK_MHZ 25
|
||||
|
||||
// Must connect INT to GPIOxx or not working
|
||||
//#define INT_GPIO 4
|
||||
|
||||
//#define MISO_GPIO 19
|
||||
//#define MOSI_GPIO 23
|
||||
//#define SCK_GPIO 18
|
||||
//#define CS_GPIO 5
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
#include <WebServer_ESP32_W5500.h>
|
||||
|
||||
WebServer server(80);
|
||||
|
||||
// Enter a MAC address and IP address for your controller below.
|
||||
#define NUMBER_OF_MAC 20
|
||||
|
||||
byte mac[][NUMBER_OF_MAC] =
|
||||
{
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
|
||||
};
|
||||
|
||||
// Select the IP address according to your local network
|
||||
IPAddress myIP(192, 168, 2, 232);
|
||||
IPAddress myGW(192, 168, 2, 1);
|
||||
IPAddress mySN(255, 255, 255, 0);
|
||||
|
||||
// Google DNS Server IP
|
||||
IPAddress myDNS(8, 8, 8, 8);
|
||||
|
||||
#include <PubSubClient.h>
|
||||
|
||||
// Update these with values suitable for your network.
|
||||
//const char* mqttServer = "broker.example"; // Broker address
|
||||
const char* mqttServer = "broker.emqx.io"; // Broker address
|
||||
//const char* mqttServer = "broker.shiftr.io"; // Broker address
|
||||
|
||||
const char *ID = "MQTTClient_SSL-Client"; // Name of our device, must be unique
|
||||
const char *TOPIC = "MQTT_Pub"; // Topic to subscribe to
|
||||
const char *subTopic = "MQTT_Sub"; // Topic to subscribe to
|
||||
|
||||
//IPAddress mqttServer(172, 16, 0, 2);
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length)
|
||||
{
|
||||
Serial.print("Message arrived [");
|
||||
Serial.print(topic);
|
||||
Serial.print("] ");
|
||||
|
||||
for (unsigned int i = 0; i < length; i++)
|
||||
{
|
||||
Serial.print((char)payload[i]);
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
WiFiClient ethClient;
|
||||
PubSubClient client(mqttServer, 1883, callback, ethClient);
|
||||
|
||||
void reconnect()
|
||||
{
|
||||
// Loop until we're reconnected
|
||||
while (!client.connected())
|
||||
{
|
||||
//Serial.print("Attempting MQTT connection to ");
|
||||
//Serial.print(mqttServer);
|
||||
|
||||
// Attempt to connect
|
||||
if (client.connect(ID, "try", "try"))
|
||||
{
|
||||
//Serial.println("...connected");
|
||||
|
||||
// Once connected, publish an announcement...
|
||||
String data = "Hello from MQTTClient_SSL on " + String(BOARD_NAME);
|
||||
|
||||
client.publish(TOPIC, data.c_str());
|
||||
|
||||
//Serial.println("Published connection message successfully!");
|
||||
//Serial.print("Subscribed to: ");
|
||||
//Serial.println(subTopic);
|
||||
|
||||
// This is a workaround to address https://github.com/OPEnSLab-OSU/SSLClient/issues/9
|
||||
//ethClientSSL.flush();
|
||||
// ... and resubscribe
|
||||
client.subscribe(subTopic);
|
||||
// for loopback testing
|
||||
client.subscribe(TOPIC);
|
||||
// This is a workaround to address https://github.com/OPEnSLab-OSU/SSLClient/issues/9
|
||||
//ethClientSSL.flush();
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print("...failed, rc=");
|
||||
Serial.print(client.state());
|
||||
Serial.println(" try again in 5 seconds");
|
||||
|
||||
// Wait 5 seconds before retrying
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
while (!Serial && (millis() < 5000));
|
||||
|
||||
Serial.print(F("\nStart MQTTClient_Basic on "));
|
||||
Serial.print(ARDUINO_BOARD);
|
||||
Serial.print(F(" with "));
|
||||
Serial.println(SHIELD_TYPE);
|
||||
Serial.println(WEBSERVER_ESP32_W5500_VERSION);
|
||||
|
||||
ET_LOGWARN(F("Default SPI pinout:"));
|
||||
ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
|
||||
ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
|
||||
ET_LOGWARN1(F("MISO:"), MISO_GPIO);
|
||||
ET_LOGWARN1(F("SCK:"), SCK_GPIO);
|
||||
ET_LOGWARN1(F("CS:"), CS_GPIO);
|
||||
ET_LOGWARN1(F("INT:"), INT_GPIO);
|
||||
ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
|
||||
ET_LOGWARN(F("========================="));
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
// To be called before ETH.begin()
|
||||
ESP32_W5500_onEvent();
|
||||
|
||||
// start the ethernet connection and the server:
|
||||
// Use DHCP dynamic IP and random mac
|
||||
//bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
|
||||
// int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
|
||||
ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
|
||||
//ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
|
||||
|
||||
// Static IP, leave without this line to get IP via DHCP
|
||||
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
|
||||
//ETH.config(myIP, myGW, mySN, myDNS);
|
||||
|
||||
ESP32_W5500_waitForConnect();
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
client.setServer(mqttServer, 1883);
|
||||
client.setCallback(callback);
|
||||
|
||||
// Allow the hardware to sort itself out
|
||||
delay(1500);
|
||||
}
|
||||
|
||||
#define MQTT_PUBLISH_INTERVAL_MS 5000L
|
||||
|
||||
String data = "Hello from MQTTClient_Basic on " + String(ARDUINO_BOARD) + " with " + String(SHIELD_TYPE);
|
||||
const char *pubData = data.c_str();
|
||||
|
||||
unsigned long lastMsg = 0;
|
||||
|
||||
void loop()
|
||||
{
|
||||
static unsigned long now;
|
||||
|
||||
if (!client.connected())
|
||||
{
|
||||
reconnect();
|
||||
}
|
||||
|
||||
// Sending Data
|
||||
now = millis();
|
||||
|
||||
if (now - lastMsg > MQTT_PUBLISH_INTERVAL_MS)
|
||||
{
|
||||
lastMsg = now;
|
||||
|
||||
if (!client.publish(TOPIC, pubData))
|
||||
{
|
||||
Serial.println("Message failed to send.");
|
||||
}
|
||||
|
||||
Serial.print("Message Send : " + String(TOPIC) + " => ");
|
||||
Serial.println(data);
|
||||
}
|
||||
|
||||
client.loop();
|
||||
}
|
@ -0,0 +1,293 @@
|
||||
/****************************************************************************************************************************
|
||||
MQTT_ThingStream.ino - Dead simple MQTT Client for Ethernet shields
|
||||
|
||||
For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
|
||||
|
||||
WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
|
||||
|
||||
Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
|
||||
Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
|
||||
Licensed under GPLv3 license
|
||||
*****************************************************************************************************************************/
|
||||
/*
|
||||
Basic MQTT example (without SSL!)
|
||||
This sketch demonstrates the basic capabilities of the library.
|
||||
It connects to an MQTT server then:
|
||||
- publishes {Hello from MQTTClient_SSL on NUCLEO_F767ZI} to the topic [STM32_Pub]
|
||||
- subscribes to the topic [STM32_Sub], printing out any messages
|
||||
it receives. NB - it assumes the received payloads are strings not binary
|
||||
It will reconnect to the server if the connection is lost using a blocking
|
||||
reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
|
||||
achieve the same result without blocking the main loop.
|
||||
|
||||
You will need to populate "certificates.h" with your trust anchors
|
||||
(see https://github.com/OPEnSLab-OSU/SSLClient/blob/master/TrustAnchors.md)
|
||||
and my_cert/my_key with your certificate/private key pair
|
||||
(see https://github.com/OPEnSLab-OSU/SSLClient#mtls).
|
||||
*/
|
||||
|
||||
#if !( defined(ESP32) )
|
||||
#error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
|
||||
#endif
|
||||
|
||||
#define DEBUG_ETHERNET_WEBSERVER_PORT Serial
|
||||
|
||||
// Debug Level from 0 to 4
|
||||
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
// Optional values to override default settings
|
||||
// Don't change unless you know what you're doing
|
||||
//#define ETH_SPI_HOST SPI3_HOST
|
||||
//#define SPI_CLOCK_MHZ 25
|
||||
|
||||
// Must connect INT to GPIOxx or not working
|
||||
//#define INT_GPIO 4
|
||||
|
||||
//#define MISO_GPIO 19
|
||||
//#define MOSI_GPIO 23
|
||||
//#define SCK_GPIO 18
|
||||
//#define CS_GPIO 5
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
#include <WebServer_ESP32_W5500.h>
|
||||
|
||||
WebServer server(80);
|
||||
|
||||
// Enter a MAC address and IP address for your controller below.
|
||||
#define NUMBER_OF_MAC 20
|
||||
|
||||
byte mac[][NUMBER_OF_MAC] =
|
||||
{
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
|
||||
};
|
||||
|
||||
// Select the IP address according to your local network
|
||||
IPAddress myIP(192, 168, 2, 232);
|
||||
IPAddress myGW(192, 168, 2, 1);
|
||||
IPAddress mySN(255, 255, 255, 0);
|
||||
|
||||
// Google DNS Server IP
|
||||
IPAddress myDNS(8, 8, 8, 8);
|
||||
|
||||
#include <PubSubClient.h>
|
||||
|
||||
const char my_cert[] = "FIXME";
|
||||
const char my_key[] = "FIXME";
|
||||
|
||||
#define USING_THINGSTREAM_IO false //true
|
||||
|
||||
#if USING_THINGSTREAM_IO
|
||||
|
||||
const char *MQTT_PREFIX_TOPIC = "esp32-sniffer/";
|
||||
const char *MQTT_ANNOUNCE_TOPIC = "/status";
|
||||
const char *MQTT_CONTROL_TOPIC = "/control";
|
||||
const char *MQTT_BLE_TOPIC = "/ble";
|
||||
|
||||
|
||||
// GOT FROM ThingsStream!
|
||||
const char *MQTT_SERVER = "mqtt.thingstream.io";
|
||||
const char *MQTT_USER = "MQTT_USER";
|
||||
const char *MQTT_PASS = "MQTT_PASS";
|
||||
const char *MQTT_CLIENT_ID = "MQTT_CLIENT_ID";
|
||||
|
||||
String topic = MQTT_PREFIX_TOPIC + String("12345678") + MQTT_BLE_TOPIC;
|
||||
String subTopic = MQTT_PREFIX_TOPIC + String("12345678") + MQTT_BLE_TOPIC;
|
||||
|
||||
#else
|
||||
|
||||
const char* MQTT_SERVER = "broker.emqx.io"; // Broker address
|
||||
|
||||
const char* ID = "MQTTClient_SSL-Client"; // Name of our device, must be unique
|
||||
String topic = "ESP32_Pub"; // Topic to subscribe to
|
||||
String subTopic = "ESP32_Sub"; // Topic to subscribe to
|
||||
|
||||
#endif
|
||||
|
||||
void mqtt_receive_callback(char* topic, byte* payload, unsigned int length);
|
||||
|
||||
const int MQTT_PORT = 1883; //if you use SSL //1883 no SSL
|
||||
|
||||
unsigned long lastMsg = 0;
|
||||
|
||||
// Initialize the SSL client library
|
||||
// Arguments: EthernetClient, our trust anchors
|
||||
|
||||
|
||||
WiFiClient ethClient;
|
||||
|
||||
PubSubClient client(MQTT_SERVER, MQTT_PORT, mqtt_receive_callback, ethClient);
|
||||
|
||||
/*
|
||||
Called whenever a payload is received from a subscribed MQTT topic
|
||||
*/
|
||||
void mqtt_receive_callback(char* topic, byte* payload, unsigned int length)
|
||||
{
|
||||
Serial.print("MQTT Message receive [");
|
||||
Serial.print(topic);
|
||||
Serial.print("] ");
|
||||
|
||||
for (unsigned int i = 0; i < length; i++)
|
||||
{
|
||||
Serial.print((char)payload[i]);
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void reconnect()
|
||||
{
|
||||
// Loop until we're reconnected
|
||||
while (!client.connected())
|
||||
{
|
||||
//Serial.print("Attempting MQTT connection to ");
|
||||
//Serial.println(MQTT_SERVER);
|
||||
|
||||
// Attempt to connect
|
||||
|
||||
#if USING_THINGSTREAM_IO
|
||||
int connect_status = client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASS, topic.c_str(), 2, false, "");
|
||||
#else
|
||||
int connect_status = client.connect(ID);
|
||||
#endif
|
||||
|
||||
if (connect_status)
|
||||
{
|
||||
//Serial.println("...connected");
|
||||
|
||||
// Once connected, publish an announcement...
|
||||
String data = "Hello from MQTTClient_SSL on " + String(BOARD_NAME);
|
||||
|
||||
client.publish(topic.c_str(), data.c_str());
|
||||
|
||||
//Serial.println("Published connection message successfully!");
|
||||
|
||||
//Serial.print("Subscribed to: ");
|
||||
//Serial.println(subTopic);
|
||||
|
||||
// This is a workaround to address https://github.com/OPEnSLab-OSU/SSLClient/issues/9
|
||||
//ethClientSSL.flush();
|
||||
// ... and resubscribe
|
||||
client.subscribe(subTopic.c_str());
|
||||
// for loopback testing
|
||||
client.subscribe(topic.c_str());
|
||||
// This is a workaround to address https://github.com/OPEnSLab-OSU/SSLClient/issues/9
|
||||
//ethClientSSL.flush();
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print("failed, rc=");
|
||||
Serial.print(client.state());
|
||||
Serial.println(" try again in 5 seconds");
|
||||
|
||||
// Wait 5 seconds before retrying
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
while (!Serial && (millis() < 5000));
|
||||
|
||||
Serial.print(F("\nStart MQTT_ThingStream on "));
|
||||
Serial.print(ARDUINO_BOARD);
|
||||
Serial.print(F(" with "));
|
||||
Serial.println(SHIELD_TYPE);
|
||||
Serial.println(WEBSERVER_ESP32_W5500_VERSION);
|
||||
|
||||
ET_LOGWARN(F("Default SPI pinout:"));
|
||||
ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
|
||||
ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
|
||||
ET_LOGWARN1(F("MISO:"), MISO_GPIO);
|
||||
ET_LOGWARN1(F("SCK:"), SCK_GPIO);
|
||||
ET_LOGWARN1(F("CS:"), CS_GPIO);
|
||||
ET_LOGWARN1(F("INT:"), INT_GPIO);
|
||||
ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
|
||||
ET_LOGWARN(F("========================="));
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
// To be called before ETH.begin()
|
||||
ESP32_W5500_onEvent();
|
||||
|
||||
// start the ethernet connection and the server:
|
||||
// Use DHCP dynamic IP and random mac
|
||||
//bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
|
||||
// int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
|
||||
ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
|
||||
//ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
|
||||
|
||||
// Static IP, leave without this line to get IP via DHCP
|
||||
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
|
||||
//ETH.config(myIP, myGW, mySN, myDNS);
|
||||
|
||||
ESP32_W5500_waitForConnect();
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
// Note - the default maximum packet size is 256 bytes. If the
|
||||
// combined length of clientId, username and password exceed this use the
|
||||
// following to increase the buffer size:
|
||||
//client.setBufferSize(256);
|
||||
|
||||
Serial.println("***************************************");
|
||||
Serial.println(topic);
|
||||
Serial.println("***************************************");
|
||||
}
|
||||
|
||||
#define MQTT_PUBLISH_INTERVAL_MS 5000L
|
||||
|
||||
String data = "Hello from MQTT_ThingStream on " + String(BOARD_NAME) + " with " + String(SHIELD_TYPE);
|
||||
const char *pubData = data.c_str();
|
||||
|
||||
void loop()
|
||||
{
|
||||
static unsigned long now;
|
||||
|
||||
if (!client.connected())
|
||||
{
|
||||
reconnect();
|
||||
}
|
||||
|
||||
// Sending Data
|
||||
now = millis();
|
||||
|
||||
if (now - lastMsg > MQTT_PUBLISH_INTERVAL_MS)
|
||||
{
|
||||
lastMsg = now;
|
||||
|
||||
if (!client.publish(topic.c_str(), pubData))
|
||||
{
|
||||
Serial.println("Message failed to send.");
|
||||
}
|
||||
|
||||
Serial.print("MQTT Message Send : " + topic + " => ");
|
||||
Serial.println(data);
|
||||
}
|
||||
|
||||
client.loop();
|
||||
}
|
215
lib/WebServer_ESP32_W5500/examples/PostServer/PostServer.ino
Normal file
215
lib/WebServer_ESP32_W5500/examples/PostServer/PostServer.ino
Normal file
@ -0,0 +1,215 @@
|
||||
/****************************************************************************************************************************
|
||||
PostServer.h - Dead simple web-server for Ethernet shields
|
||||
|
||||
For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
|
||||
|
||||
WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
|
||||
|
||||
Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
|
||||
Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
|
||||
Licensed under GPLv3 license
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#if !( defined(ESP32) )
|
||||
#error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
|
||||
#endif
|
||||
|
||||
#define DEBUG_ETHERNET_WEBSERVER_PORT Serial
|
||||
|
||||
// Debug Level from 0 to 4
|
||||
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
// Optional values to override default settings
|
||||
// Don't change unless you know what you're doing
|
||||
//#define ETH_SPI_HOST SPI3_HOST
|
||||
//#define SPI_CLOCK_MHZ 25
|
||||
|
||||
// Must connect INT to GPIOxx or not working
|
||||
//#define INT_GPIO 4
|
||||
|
||||
//#define MISO_GPIO 19
|
||||
//#define MOSI_GPIO 23
|
||||
//#define SCK_GPIO 18
|
||||
//#define CS_GPIO 5
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
#include <WebServer_ESP32_W5500.h>
|
||||
|
||||
WebServer server(80);
|
||||
|
||||
// Enter a MAC address and IP address for your controller below.
|
||||
#define NUMBER_OF_MAC 20
|
||||
|
||||
byte mac[][NUMBER_OF_MAC] =
|
||||
{
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
|
||||
};
|
||||
|
||||
// Select the IP address according to your local network
|
||||
IPAddress myIP(192, 168, 2, 232);
|
||||
IPAddress myGW(192, 168, 2, 1);
|
||||
IPAddress mySN(255, 255, 255, 0);
|
||||
|
||||
// Google DNS Server IP
|
||||
IPAddress myDNS(8, 8, 8, 8);
|
||||
|
||||
const String postForms =
|
||||
F("<html>\
|
||||
<head>\
|
||||
<title>WebServer_ESP32_ENC POST handling</title>\
|
||||
<style>\
|
||||
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
|
||||
</style>\
|
||||
</head>\
|
||||
<body>\
|
||||
<h1>POST plain text to /postplain/</h1><br>\
|
||||
<form method=\"post\" enctype=\"text/plain\" action=\"/postplain/\">\
|
||||
<input type=\"text\" name=\'{\"hello\": \"world\", \"trash\": \"\' value=\'\"}\'><br>\
|
||||
<input type=\"submit\" value=\"Submit\">\
|
||||
</form>\
|
||||
<h1>POST form data to /postform/</h1><br>\
|
||||
<form method=\"post\" enctype=\"application/x-www-form-urlencoded\" action=\"/postform/\">\
|
||||
<input type=\"text\" name=\"hello\" value=\"world\"><br>\
|
||||
<input type=\"submit\" value=\"Submit\">\
|
||||
</form>\
|
||||
</body>\
|
||||
</html>");
|
||||
|
||||
void handleRoot()
|
||||
{
|
||||
server.send(200, F("text/html"), postForms);
|
||||
}
|
||||
|
||||
void handlePlain()
|
||||
{
|
||||
if (server.method() != HTTP_POST)
|
||||
{
|
||||
server.send(405, F("text/plain"), F("Method Not Allowed"));
|
||||
}
|
||||
else
|
||||
{
|
||||
server.send(200, F("text/plain"), "POST body was:\n" + server.arg("plain"));
|
||||
}
|
||||
}
|
||||
|
||||
void handleForm()
|
||||
{
|
||||
if (server.method() != HTTP_POST)
|
||||
{
|
||||
server.send(405, F("text/plain"), F("Method Not Allowed"));
|
||||
}
|
||||
else
|
||||
{
|
||||
String message = F("POST form was:\n");
|
||||
|
||||
for (uint8_t i = 0; i < server.args(); i++)
|
||||
{
|
||||
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
|
||||
}
|
||||
|
||||
server.send(200, F("text/plain"), message);
|
||||
}
|
||||
}
|
||||
|
||||
void handleNotFound()
|
||||
{
|
||||
String message = F("File Not Found\n\n");
|
||||
|
||||
message += F("URI: ");
|
||||
message += server.uri();
|
||||
message += F("\nMethod: ");
|
||||
message += (server.method() == HTTP_GET) ? F("GET") : F("POST");
|
||||
message += F("\nArguments: ");
|
||||
message += server.args();
|
||||
message += F("\n");
|
||||
|
||||
for (uint8_t i = 0; i < server.args(); i++)
|
||||
{
|
||||
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
|
||||
}
|
||||
|
||||
server.send(404, F("text/plain"), message);
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
while (!Serial && (millis() < 5000));
|
||||
|
||||
Serial.print(F("\nStart POSTServer on "));
|
||||
Serial.print(ARDUINO_BOARD);
|
||||
Serial.print(F(" with "));
|
||||
Serial.println(SHIELD_TYPE);
|
||||
Serial.println(WEBSERVER_ESP32_W5500_VERSION);
|
||||
|
||||
ET_LOGWARN(F("Default SPI pinout:"));
|
||||
ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
|
||||
ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
|
||||
ET_LOGWARN1(F("MISO:"), MISO_GPIO);
|
||||
ET_LOGWARN1(F("SCK:"), SCK_GPIO);
|
||||
ET_LOGWARN1(F("CS:"), CS_GPIO);
|
||||
ET_LOGWARN1(F("INT:"), INT_GPIO);
|
||||
ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
|
||||
ET_LOGWARN(F("========================="));
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
// To be called before ETH.begin()
|
||||
ESP32_W5500_onEvent();
|
||||
|
||||
// start the ethernet connection and the server:
|
||||
// Use DHCP dynamic IP and random mac
|
||||
//bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
|
||||
// int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
|
||||
ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
|
||||
//ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
|
||||
|
||||
// Static IP, leave without this line to get IP via DHCP
|
||||
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
|
||||
//ETH.config(myIP, myGW, mySN, myDNS);
|
||||
|
||||
ESP32_W5500_waitForConnect();
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
server.on(F("/"), handleRoot);
|
||||
|
||||
server.on(F("/postplain/"), handlePlain);
|
||||
|
||||
server.on(F("/postform/"), handleForm);
|
||||
|
||||
server.onNotFound(handleNotFound);
|
||||
|
||||
server.begin();
|
||||
|
||||
Serial.print(F("HTTP POSTServer started @ IP : "));
|
||||
Serial.println(ETH.localIP());
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
server.handleClient();
|
||||
}
|
@ -0,0 +1,268 @@
|
||||
/****************************************************************************************************************************
|
||||
SimpleAuthentication.ino - Dead simple web-server for Ethernet shields
|
||||
|
||||
For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
|
||||
|
||||
WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
|
||||
|
||||
Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
|
||||
Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
|
||||
Licensed under GPLv3 license
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#if !( defined(ESP32) )
|
||||
#error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
|
||||
#endif
|
||||
|
||||
#define DEBUG_ETHERNET_WEBSERVER_PORT Serial
|
||||
|
||||
// Debug Level from 0 to 4
|
||||
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
// Optional values to override default settings
|
||||
// Don't change unless you know what you're doing
|
||||
//#define ETH_SPI_HOST SPI3_HOST
|
||||
//#define SPI_CLOCK_MHZ 25
|
||||
|
||||
// Must connect INT to GPIOxx or not working
|
||||
//#define INT_GPIO 4
|
||||
|
||||
//#define MISO_GPIO 19
|
||||
//#define MOSI_GPIO 23
|
||||
//#define SCK_GPIO 18
|
||||
//#define CS_GPIO 5
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
#include <WebServer_ESP32_W5500.h>
|
||||
|
||||
WebServer server(80);
|
||||
|
||||
// Enter a MAC address and IP address for your controller below.
|
||||
#define NUMBER_OF_MAC 20
|
||||
|
||||
byte mac[][NUMBER_OF_MAC] =
|
||||
{
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
|
||||
};
|
||||
|
||||
// Select the IP address according to your local network
|
||||
IPAddress myIP(192, 168, 2, 232);
|
||||
IPAddress myGW(192, 168, 2, 1);
|
||||
IPAddress mySN(255, 255, 255, 0);
|
||||
|
||||
// Google DNS Server IP
|
||||
IPAddress myDNS(8, 8, 8, 8);
|
||||
|
||||
//Check if header is present and correct
|
||||
bool is_authenticated()
|
||||
{
|
||||
Serial.println(F("Enter is_authenticated"));
|
||||
|
||||
if (server.hasHeader(F("Cookie")))
|
||||
{
|
||||
Serial.print(F("Found cookie: "));
|
||||
String cookie = server.header(F("Cookie"));
|
||||
Serial.println(cookie);
|
||||
|
||||
if (cookie.indexOf(F("ESPSESSIONID=1")) != -1)
|
||||
{
|
||||
Serial.println(F("Authentication Successful"));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Serial.println(F("Authentication Failed"));
|
||||
return false;
|
||||
}
|
||||
|
||||
//login page, also called for disconnect
|
||||
void handleLogin()
|
||||
{
|
||||
String msg;
|
||||
|
||||
if (server.hasHeader(F("Cookie")))
|
||||
{
|
||||
Serial.print(F("Found cookie: "));
|
||||
String cookie = server.header(F("Cookie"));
|
||||
Serial.println(cookie);
|
||||
}
|
||||
|
||||
if (server.hasArg(F("DISCONNECT")))
|
||||
{
|
||||
Serial.println(F("Disconnection"));
|
||||
server.sendHeader(F("Location"), F("/login"));
|
||||
server.sendHeader(F("Cache-Control"), F("no-cache"));
|
||||
server.sendHeader(F("Set-Cookie"), F("ESPSESSIONID=0"));
|
||||
server.send(301);
|
||||
return;
|
||||
}
|
||||
|
||||
if (server.hasArg(F("USERNAME")) && server.hasArg(F("PASSWORD")))
|
||||
{
|
||||
if (server.arg(F("USERNAME")) == F("admin") && server.arg(F("PASSWORD")) == F("password"))
|
||||
{
|
||||
server.sendHeader(F("Location"), F("/"));
|
||||
server.sendHeader(F("Cache-Control"), F("no-cache"));
|
||||
server.sendHeader(F("Set-Cookie"), F("ESPSESSIONID=1"));
|
||||
server.send(301);
|
||||
Serial.println(F("Log in Successful"));
|
||||
return;
|
||||
}
|
||||
|
||||
msg = F("Wrong username/password! try again.");
|
||||
Serial.println(F("Log in Failed"));
|
||||
}
|
||||
|
||||
String content = F("<html><body><form action='/login' method='POST'>To log in, please use : admin/password<br>");
|
||||
content += F("User:<input type='text' name='USERNAME' placeholder='user name'><br>");
|
||||
content += F("Password:<input type='password' name='PASSWORD' placeholder='password'><br>");
|
||||
content += F("<input type='submit' name='SUBMIT' value='Submit'></form>");
|
||||
content += msg;
|
||||
content += F("<br>");
|
||||
content += F("You also can go <a href='/inline'>here</a></body></html>");
|
||||
server.send(200, F("text/html"), content);
|
||||
}
|
||||
|
||||
//root page can be accessed only if authentication is ok
|
||||
void handleRoot()
|
||||
{
|
||||
String header;
|
||||
|
||||
Serial.println(F("Enter handleRoot"));
|
||||
|
||||
if (!is_authenticated())
|
||||
{
|
||||
server.sendHeader(F("Location"), F("/login"));
|
||||
server.sendHeader(F("Cache-Control"), F("no-cache"));
|
||||
server.send(301);
|
||||
return;
|
||||
}
|
||||
|
||||
String content = F("<html><body><H2>Hello, you're connected to WebServer_ESP32_W5500 running on ");
|
||||
|
||||
content += String(ARDUINO_BOARD);
|
||||
content += F("!</H2><br>");
|
||||
|
||||
if (server.hasHeader(F("User-Agent")))
|
||||
{
|
||||
content += F("the user agent used is : ");
|
||||
content += server.header(F("User-Agent"));
|
||||
content += F("<br><br>");
|
||||
}
|
||||
|
||||
content += F("You can access this page until you <a href=\"/login?DISCONNECT=YES\">disconnect</a></body></html>");
|
||||
server.send(200, F("text/html"), content);
|
||||
}
|
||||
|
||||
//no need authentication
|
||||
void handleNotFound()
|
||||
{
|
||||
String message = F("File Not Found\n\n");
|
||||
|
||||
message += F("URI: ");
|
||||
message += server.uri();
|
||||
message += F("\nMethod: ");
|
||||
message += (server.method() == HTTP_GET) ? F("GET") : F("POST");
|
||||
message += F("\nArguments: ");
|
||||
message += server.args();
|
||||
message += F("\n");
|
||||
|
||||
for (uint8_t i = 0; i < server.args(); i++)
|
||||
{
|
||||
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
|
||||
}
|
||||
|
||||
server.send(404, F("text/plain"), message);
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
while (!Serial && (millis() < 5000));
|
||||
|
||||
Serial.print(F("\nStart SimpleAuthentication on "));
|
||||
Serial.print(ARDUINO_BOARD);
|
||||
Serial.print(F(" with "));
|
||||
Serial.println(SHIELD_TYPE);
|
||||
Serial.println(WEBSERVER_ESP32_W5500_VERSION);
|
||||
|
||||
ET_LOGWARN(F("Default SPI pinout:"));
|
||||
ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
|
||||
ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
|
||||
ET_LOGWARN1(F("MISO:"), MISO_GPIO);
|
||||
ET_LOGWARN1(F("SCK:"), SCK_GPIO);
|
||||
ET_LOGWARN1(F("CS:"), CS_GPIO);
|
||||
ET_LOGWARN1(F("INT:"), INT_GPIO);
|
||||
ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
|
||||
ET_LOGWARN(F("========================="));
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
// To be called before ETH.begin()
|
||||
ESP32_W5500_onEvent();
|
||||
|
||||
// start the ethernet connection and the server:
|
||||
// Use DHCP dynamic IP and random mac
|
||||
//bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
|
||||
// int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
|
||||
ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
|
||||
//ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
|
||||
|
||||
// Static IP, leave without this line to get IP via DHCP
|
||||
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
|
||||
//ETH.config(myIP, myGW, mySN, myDNS);
|
||||
|
||||
ESP32_W5500_waitForConnect();
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
server.on(F("/"), handleRoot);
|
||||
|
||||
server.on(F("/login"), handleLogin);
|
||||
|
||||
server.on(F("/inline"), []()
|
||||
{
|
||||
server.send(200, F("text/plain"), F("This works without need of authentication"));
|
||||
});
|
||||
|
||||
server.onNotFound(handleNotFound);
|
||||
|
||||
//here the list of headers to be recorded
|
||||
const char * headerkeys[] = {"User-Agent", "Cookie"} ;
|
||||
size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);
|
||||
|
||||
//ask server to track these headers
|
||||
server.collectHeaders(headerkeys, headerkeyssize);
|
||||
server.begin();
|
||||
|
||||
Serial.print(F("HTTP SimpleAuthentication is @ IP : "));
|
||||
Serial.println(ETH.localIP());
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
server.handleClient();
|
||||
}
|
236
lib/WebServer_ESP32_W5500/examples/UdpNTPClient/UdpNTPClient.ino
Normal file
236
lib/WebServer_ESP32_W5500/examples/UdpNTPClient/UdpNTPClient.ino
Normal file
@ -0,0 +1,236 @@
|
||||
/****************************************************************************************************************************
|
||||
UdpNTPClient.ino - Simple Arduino web server sample for ESP8266 AT-command shield
|
||||
|
||||
For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
|
||||
|
||||
WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
|
||||
|
||||
Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
|
||||
Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
|
||||
Licensed under GPLv3 license
|
||||
*****************************************************************************************************************************/
|
||||
/*
|
||||
The Arduino board communicates with the shield using the SPI bus. This is on digital pins 11, 12, and 13 on the Uno
|
||||
and pins 50, 51, and 52 on the Mega. On both boards, pin 10 is used as SS. On the Mega, the hardware SS pin, 53,
|
||||
is not used to select the Ethernet controller chip, but it must be kept as an output or the SPI interface won't work.
|
||||
*/
|
||||
|
||||
#if !( defined(ESP32) )
|
||||
#error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
|
||||
#endif
|
||||
|
||||
#define DEBUG_ETHERNET_WEBSERVER_PORT Serial
|
||||
|
||||
// Debug Level from 0 to 4
|
||||
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
// Optional values to override default settings
|
||||
// Don't change unless you know what you're doing
|
||||
//#define ETH_SPI_HOST SPI3_HOST
|
||||
//#define SPI_CLOCK_MHZ 25
|
||||
|
||||
// Must connect INT to GPIOxx or not working
|
||||
//#define INT_GPIO 4
|
||||
|
||||
//#define MISO_GPIO 19
|
||||
//#define MOSI_GPIO 23
|
||||
//#define SCK_GPIO 18
|
||||
//#define CS_GPIO 5
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
#include <WebServer_ESP32_W5500.h>
|
||||
|
||||
// Enter a MAC address and IP address for your controller below.
|
||||
#define NUMBER_OF_MAC 20
|
||||
|
||||
byte mac[][NUMBER_OF_MAC] =
|
||||
{
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
|
||||
};
|
||||
|
||||
// Select the IP address according to your local network
|
||||
IPAddress myIP(192, 168, 2, 232);
|
||||
IPAddress myGW(192, 168, 2, 1);
|
||||
IPAddress mySN(255, 255, 255, 0);
|
||||
|
||||
// Google DNS Server IP
|
||||
IPAddress myDNS(8, 8, 8, 8);
|
||||
|
||||
char timeServer[] = "time.nist.gov"; // NTP server
|
||||
unsigned int localPort = 2390; // local port to listen for UDP packets
|
||||
|
||||
const int NTP_PACKET_SIZE = 48; // NTP timestamp is in the first 48 bytes of the message
|
||||
const int UDP_TIMEOUT = 2000; // timeout in milliseconds to wait for an UDP packet to arrive
|
||||
|
||||
byte packetBuffer[NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets
|
||||
|
||||
// A UDP instance to let us send and receive packets over UDP
|
||||
WiFiUDP Udp;
|
||||
|
||||
// send an NTP request to the time server at the given address
|
||||
void sendNTPpacket(char *ntpSrv)
|
||||
{
|
||||
// set all bytes in the buffer to 0
|
||||
memset(packetBuffer, 0, NTP_PACKET_SIZE);
|
||||
// Initialize values needed to form NTP request
|
||||
// (see URL above for details on the packets)
|
||||
|
||||
packetBuffer[0] = 0b11100011; // LI, Version, Mode
|
||||
packetBuffer[1] = 0; // Stratum, or type of clock
|
||||
packetBuffer[2] = 6; // Polling Interval
|
||||
packetBuffer[3] = 0xEC; // Peer Clock Precision
|
||||
// 8 bytes of zero for Root Delay & Root Dispersion
|
||||
packetBuffer[12] = 49;
|
||||
packetBuffer[13] = 0x4E;
|
||||
packetBuffer[14] = 49;
|
||||
packetBuffer[15] = 52;
|
||||
|
||||
// all NTP fields have been given values, now
|
||||
// you can send a packet requesting a timestamp:
|
||||
Udp.beginPacket(ntpSrv, 123); //NTP requests are to port 123
|
||||
|
||||
Udp.write(packetBuffer, NTP_PACKET_SIZE);
|
||||
|
||||
Udp.endPacket();
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
while (!Serial && (millis() < 5000));
|
||||
|
||||
Serial.print(F("\nStart UdpNTPClient on "));
|
||||
Serial.print(ARDUINO_BOARD);
|
||||
Serial.print(F(" with "));
|
||||
Serial.println(SHIELD_TYPE);
|
||||
Serial.println(WEBSERVER_ESP32_W5500_VERSION);
|
||||
|
||||
ET_LOGWARN(F("Default SPI pinout:"));
|
||||
ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
|
||||
ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
|
||||
ET_LOGWARN1(F("MISO:"), MISO_GPIO);
|
||||
ET_LOGWARN1(F("SCK:"), SCK_GPIO);
|
||||
ET_LOGWARN1(F("CS:"), CS_GPIO);
|
||||
ET_LOGWARN1(F("INT:"), INT_GPIO);
|
||||
ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
|
||||
ET_LOGWARN(F("========================="));
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
// To be called before ETH.begin()
|
||||
ESP32_W5500_onEvent();
|
||||
|
||||
// start the ethernet connection and the server:
|
||||
// Use DHCP dynamic IP and random mac
|
||||
//bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
|
||||
// int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
|
||||
ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
|
||||
//ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
|
||||
|
||||
// Static IP, leave without this line to get IP via DHCP
|
||||
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
|
||||
//ETH.config(myIP, myGW, mySN, myDNS);
|
||||
|
||||
ESP32_W5500_waitForConnect();
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
Udp.begin(localPort);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
sendNTPpacket(timeServer); // send an NTP packet to a time server
|
||||
|
||||
// wait for a reply for UDP_TIMEOUT milliseconds
|
||||
unsigned long startMs = millis();
|
||||
|
||||
while (!Udp.available() && (millis() - startMs) < UDP_TIMEOUT) {}
|
||||
|
||||
// if there's data available, read a packet
|
||||
int packetSize = Udp.parsePacket();
|
||||
|
||||
if (packetSize)
|
||||
{
|
||||
Serial.print(F("UDP Packet received, size "));
|
||||
Serial.println(packetSize);
|
||||
Serial.print(F("From "));
|
||||
IPAddress remoteIp = Udp.remoteIP();
|
||||
Serial.print(remoteIp);
|
||||
Serial.print(F(", port "));
|
||||
Serial.println(Udp.remotePort());
|
||||
|
||||
// We've received a packet, read the data from it into the buffer
|
||||
Udp.read(packetBuffer, NTP_PACKET_SIZE);
|
||||
|
||||
// the timestamp starts at byte 40 of the received packet and is four bytes,
|
||||
// or two words, long. First, esxtract the two words:
|
||||
|
||||
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
|
||||
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
|
||||
|
||||
// combine the four bytes (two words) into a long integer
|
||||
// this is NTP time (seconds since Jan 1 1900):
|
||||
unsigned long secsSince1900 = highWord << 16 | lowWord;
|
||||
|
||||
Serial.print(F("Seconds since Jan 1 1900 = "));
|
||||
Serial.println(secsSince1900);
|
||||
|
||||
// now convert NTP time into )everyday time:
|
||||
Serial.print(F("Unix time = "));
|
||||
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
|
||||
const unsigned long seventyYears = 2208988800UL;
|
||||
// subtract seventy years:
|
||||
unsigned long epoch = secsSince1900 - seventyYears;
|
||||
// print Unix time:
|
||||
Serial.println(epoch);
|
||||
|
||||
// print the hour, minute and second:
|
||||
Serial.print(F("The UTC time is ")); // UTC is the time at Greenwich Meridian (GMT)
|
||||
Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
|
||||
Serial.print(F(":"));
|
||||
|
||||
if (((epoch % 3600) / 60) < 10)
|
||||
{
|
||||
// In the first 10 minutes of each hour, we'll want a leading '0'
|
||||
Serial.print(F("0"));
|
||||
}
|
||||
|
||||
Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
|
||||
Serial.print(F(":"));
|
||||
|
||||
if ((epoch % 60) < 10)
|
||||
{
|
||||
// In the first 10 seconds of each minute, we'll want a leading '0'
|
||||
Serial.print(F("0"));
|
||||
}
|
||||
|
||||
Serial.println(epoch % 60); // print the second
|
||||
}
|
||||
|
||||
// wait ten seconds before asking for the time again
|
||||
delay(10000);
|
||||
}
|
@ -0,0 +1,237 @@
|
||||
/****************************************************************************************************************************
|
||||
UDPSendReceive.ino - Simple Arduino web server sample for ESP8266/ESP32 AT-command shield
|
||||
|
||||
For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
|
||||
|
||||
WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
|
||||
|
||||
Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
|
||||
Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
|
||||
Licensed under GPLv3 license
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#if !( defined(ESP32) )
|
||||
#error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
|
||||
#endif
|
||||
|
||||
#define DEBUG_ETHERNET_WEBSERVER_PORT Serial
|
||||
|
||||
// Debug Level from 0 to 4
|
||||
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
// Optional values to override default settings
|
||||
// Don't change unless you know what you're doing
|
||||
//#define ETH_SPI_HOST SPI3_HOST
|
||||
//#define SPI_CLOCK_MHZ 25
|
||||
|
||||
// Must connect INT to GPIOxx or not working
|
||||
//#define INT_GPIO 4
|
||||
|
||||
//#define MISO_GPIO 19
|
||||
//#define MOSI_GPIO 23
|
||||
//#define SCK_GPIO 18
|
||||
//#define CS_GPIO 5
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
#include <WebServer_ESP32_W5500.h>
|
||||
|
||||
// Enter a MAC address and IP address for your controller below.
|
||||
#define NUMBER_OF_MAC 20
|
||||
|
||||
byte mac[][NUMBER_OF_MAC] =
|
||||
{
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
|
||||
};
|
||||
|
||||
// Select the IP address according to your local network
|
||||
IPAddress myIP(192, 168, 2, 232);
|
||||
IPAddress myGW(192, 168, 2, 1);
|
||||
IPAddress mySN(255, 255, 255, 0);
|
||||
|
||||
// Google DNS Server IP
|
||||
IPAddress myDNS(8, 8, 8, 8);
|
||||
|
||||
char timeServer[] = "time.nist.gov"; // NTP server
|
||||
unsigned int localPort = 2390; // local port to listen for UDP packets
|
||||
|
||||
const int NTP_PACKET_SIZE = 48; // NTP timestamp is in the first 48 bytes of the message
|
||||
const int UDP_TIMEOUT = 2000; // timeout in milliseconds to wait for an UDP packet to arrive
|
||||
|
||||
byte packetBuffer[NTP_PACKET_SIZE]; // buffer to hold incoming packet
|
||||
byte ReplyBuffer[] = "ACK"; // a string to send back
|
||||
|
||||
// A UDP instance to let us send and receive packets over UDP
|
||||
WiFiUDP Udp;
|
||||
|
||||
// send an NTP request to the time server at the given address
|
||||
void sendNTPpacket(char *ntpSrv)
|
||||
{
|
||||
// set all bytes in the buffer to 0
|
||||
memset(packetBuffer, 0, NTP_PACKET_SIZE);
|
||||
// Initialize values needed to form NTP request
|
||||
// (see URL above for details on the packets)
|
||||
|
||||
packetBuffer[0] = 0b11100011; // LI, Version, Mode
|
||||
packetBuffer[1] = 0; // Stratum, or type of clock
|
||||
packetBuffer[2] = 6; // Polling Interval
|
||||
packetBuffer[3] = 0xEC; // Peer Clock Precision
|
||||
// 8 bytes of zero for Root Delay & Root Dispersion
|
||||
packetBuffer[12] = 49;
|
||||
packetBuffer[13] = 0x4E;
|
||||
packetBuffer[14] = 49;
|
||||
packetBuffer[15] = 52;
|
||||
|
||||
// all NTP fields have been given values, now
|
||||
// you can send a packet requesting a timestamp:
|
||||
Udp.beginPacket(ntpSrv, 123); //NTP requests are to port 123
|
||||
|
||||
Udp.write(packetBuffer, NTP_PACKET_SIZE);
|
||||
|
||||
Udp.endPacket();
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
while (!Serial && (millis() < 5000));
|
||||
|
||||
Serial.print(F("\nStart UDPSendReceive on "));
|
||||
Serial.print(ARDUINO_BOARD);
|
||||
Serial.print(F(" with "));
|
||||
Serial.println(SHIELD_TYPE);
|
||||
Serial.println(WEBSERVER_ESP32_W5500_VERSION);
|
||||
|
||||
ET_LOGWARN(F("Default SPI pinout:"));
|
||||
ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
|
||||
ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
|
||||
ET_LOGWARN1(F("MISO:"), MISO_GPIO);
|
||||
ET_LOGWARN1(F("SCK:"), SCK_GPIO);
|
||||
ET_LOGWARN1(F("CS:"), CS_GPIO);
|
||||
ET_LOGWARN1(F("INT:"), INT_GPIO);
|
||||
ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
|
||||
ET_LOGWARN(F("========================="));
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
// To be called before ETH.begin()
|
||||
ESP32_W5500_onEvent();
|
||||
|
||||
// start the ethernet connection and the server:
|
||||
// Use DHCP dynamic IP and random mac
|
||||
//bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
|
||||
// int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
|
||||
ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
|
||||
//ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
|
||||
|
||||
// Static IP, leave without this line to get IP via DHCP
|
||||
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
|
||||
//ETH.config(myIP, myGW, mySN, myDNS);
|
||||
|
||||
ESP32_W5500_waitForConnect();
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
Serial.println(F("\nStarting connection to server..."));
|
||||
// if you get a connection, report back via serial:
|
||||
Udp.begin(localPort);
|
||||
|
||||
Serial.print(F("Listening on port "));
|
||||
Serial.println(localPort);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
sendNTPpacket(timeServer); // send an NTP packet to a time server
|
||||
|
||||
// wait for a reply for UDP_TIMEOUT milliseconds
|
||||
unsigned long startMs = millis();
|
||||
|
||||
while (!Udp.available() && (millis() - startMs) < UDP_TIMEOUT) {}
|
||||
|
||||
// if there's data available, read a packet
|
||||
int packetSize = Udp.parsePacket();
|
||||
|
||||
if (packetSize)
|
||||
{
|
||||
Serial.print(F("UDP Packet received, size "));
|
||||
Serial.println(packetSize);
|
||||
Serial.print(F("From "));
|
||||
IPAddress remoteIp = Udp.remoteIP();
|
||||
Serial.print(remoteIp);
|
||||
Serial.print(F(", port "));
|
||||
Serial.println(Udp.remotePort());
|
||||
|
||||
// We've received a packet, read the data from it into the buffer
|
||||
Udp.read(packetBuffer, NTP_PACKET_SIZE);
|
||||
|
||||
// the timestamp starts at byte 40 of the received packet and is four bytes,
|
||||
// or two words, long. First, esxtract the two words:
|
||||
|
||||
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
|
||||
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
|
||||
|
||||
// combine the four bytes (two words) into a long integer
|
||||
// this is NTP time (seconds since Jan 1 1900):
|
||||
unsigned long secsSince1900 = highWord << 16 | lowWord;
|
||||
|
||||
Serial.print(F("Seconds since Jan 1 1900 = "));
|
||||
Serial.println(secsSince1900);
|
||||
|
||||
// now convert NTP time into )everyday time:
|
||||
Serial.print(F("Unix time = "));
|
||||
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
|
||||
const unsigned long seventyYears = 2208988800UL;
|
||||
// subtract seventy years:
|
||||
unsigned long epoch = secsSince1900 - seventyYears;
|
||||
// print Unix time:
|
||||
Serial.println(epoch);
|
||||
|
||||
// print the hour, minute and second:
|
||||
Serial.print(F("The UTC time is ")); // UTC is the time at Greenwich Meridian (GMT)
|
||||
Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
|
||||
Serial.print(F(":"));
|
||||
|
||||
if (((epoch % 3600) / 60) < 10)
|
||||
{
|
||||
// In the first 10 minutes of each hour, we'll want a leading '0'
|
||||
Serial.print(F("0"));
|
||||
}
|
||||
|
||||
Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
|
||||
Serial.print(F(":"));
|
||||
|
||||
if ((epoch % 60) < 10)
|
||||
{
|
||||
// In the first 10 seconds of each minute, we'll want a leading '0'
|
||||
Serial.print(F("0"));
|
||||
}
|
||||
|
||||
Serial.println(epoch % 60); // print the second
|
||||
}
|
||||
|
||||
// wait ten seconds before asking for the time again
|
||||
delay(10000);
|
||||
}
|
166
lib/WebServer_ESP32_W5500/examples/WebClient/WebClient.ino
Normal file
166
lib/WebServer_ESP32_W5500/examples/WebClient/WebClient.ino
Normal file
@ -0,0 +1,166 @@
|
||||
/****************************************************************************************************************************
|
||||
WebClient.ino - Simple Arduino web server sample for Ethernet shield
|
||||
|
||||
For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
|
||||
|
||||
WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
|
||||
|
||||
Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
|
||||
Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
|
||||
Licensed under GPLv3 license
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#if !( defined(ESP32) )
|
||||
#error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
|
||||
#endif
|
||||
|
||||
#define DEBUG_ETHERNET_WEBSERVER_PORT Serial
|
||||
|
||||
// Debug Level from 0 to 4
|
||||
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
// Optional values to override default settings
|
||||
// Don't change unless you know what you're doing
|
||||
//#define ETH_SPI_HOST SPI3_HOST
|
||||
//#define SPI_CLOCK_MHZ 25
|
||||
|
||||
// Must connect INT to GPIOxx or not working
|
||||
//#define INT_GPIO 4
|
||||
|
||||
//#define MISO_GPIO 19
|
||||
//#define MOSI_GPIO 23
|
||||
//#define SCK_GPIO 18
|
||||
//#define CS_GPIO 5
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
#include <WebServer_ESP32_W5500.h>
|
||||
|
||||
// Enter a MAC address and IP address for your controller below.
|
||||
#define NUMBER_OF_MAC 20
|
||||
|
||||
byte mac[][NUMBER_OF_MAC] =
|
||||
{
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
|
||||
};
|
||||
|
||||
// Select the IP address according to your local network
|
||||
IPAddress myIP(192, 168, 2, 232);
|
||||
IPAddress myGW(192, 168, 2, 1);
|
||||
IPAddress mySN(255, 255, 255, 0);
|
||||
|
||||
// Google DNS Server IP
|
||||
IPAddress myDNS(8, 8, 8, 8);
|
||||
|
||||
char server[] = "arduino.tips";
|
||||
|
||||
// Initialize the Ethernet client object
|
||||
WiFiClient client;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
while (!Serial && (millis() < 5000));
|
||||
|
||||
Serial.print(F("\nStart WebClient on "));
|
||||
Serial.print(ARDUINO_BOARD);
|
||||
Serial.print(F(" with "));
|
||||
Serial.println(SHIELD_TYPE);
|
||||
Serial.println(WEBSERVER_ESP32_W5500_VERSION);
|
||||
|
||||
ET_LOGWARN(F("Default SPI pinout:"));
|
||||
ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
|
||||
ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
|
||||
ET_LOGWARN1(F("MISO:"), MISO_GPIO);
|
||||
ET_LOGWARN1(F("SCK:"), SCK_GPIO);
|
||||
ET_LOGWARN1(F("CS:"), CS_GPIO);
|
||||
ET_LOGWARN1(F("INT:"), INT_GPIO);
|
||||
ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
|
||||
ET_LOGWARN(F("========================="));
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
// To be called before ETH.begin()
|
||||
ESP32_W5500_onEvent();
|
||||
|
||||
// start the ethernet connection and the server:
|
||||
// Use DHCP dynamic IP and random mac
|
||||
//bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
|
||||
// int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
|
||||
ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
|
||||
//ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
|
||||
|
||||
// Static IP, leave without this line to get IP via DHCP
|
||||
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
|
||||
// KH, Dynamic not OK yet
|
||||
//ETH.config(myIP, myGW, mySN, myDNS);
|
||||
|
||||
ESP32_W5500_waitForConnect();
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
Serial.println();
|
||||
Serial.println(F("Starting connection to server..."));
|
||||
|
||||
// if you get a connection, report back via serial
|
||||
if (client.connect(server, 80))
|
||||
{
|
||||
Serial.println(F("Connected to server"));
|
||||
// Make a HTTP request
|
||||
client.println(F("GET /asciilogo.txt HTTP/1.1"));
|
||||
client.println(F("Host: arduino.tips"));
|
||||
client.println(F("Connection: close"));
|
||||
client.println();
|
||||
}
|
||||
}
|
||||
|
||||
void printoutData()
|
||||
{
|
||||
// if there are incoming bytes available
|
||||
// from the server, read them and print them
|
||||
while (client.available())
|
||||
{
|
||||
char c = client.read();
|
||||
Serial.write(c);
|
||||
Serial.flush();
|
||||
}
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
printoutData();
|
||||
|
||||
// if the server's disconnected, stop the client
|
||||
if (!client.connected())
|
||||
{
|
||||
Serial.println();
|
||||
Serial.println(F("Disconnecting from server..."));
|
||||
client.stop();
|
||||
|
||||
// do nothing forever
|
||||
while (true)
|
||||
yield();
|
||||
}
|
||||
}
|
@ -0,0 +1,179 @@
|
||||
/****************************************************************************************************************************
|
||||
WebClientRepeating.ino - Simple Arduino web server sample for Ethernet shield
|
||||
|
||||
For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
|
||||
|
||||
WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
|
||||
|
||||
Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
|
||||
Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
|
||||
Licensed under GPLv3 license
|
||||
*****************************************************************************************************************************/
|
||||
/*
|
||||
The Arduino board communicates with the shield using the SPI bus. This is on digital pins 11, 12, and 13 on the Uno
|
||||
and pins 50, 51, and 52 on the Mega. On both boards, pin 10 is used as SS. On the Mega, the hardware SS pin, 53,
|
||||
is not used to select the Ethernet controller chip, but it must be kept as an output or the SPI interface won't work.
|
||||
*/
|
||||
|
||||
#if !( defined(ESP32) )
|
||||
#error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
|
||||
#endif
|
||||
|
||||
#define DEBUG_ETHERNET_WEBSERVER_PORT Serial
|
||||
|
||||
// Debug Level from 0 to 4
|
||||
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
// Optional values to override default settings
|
||||
// Don't change unless you know what you're doing
|
||||
//#define ETH_SPI_HOST SPI3_HOST
|
||||
//#define SPI_CLOCK_MHZ 25
|
||||
|
||||
// Must connect INT to GPIOxx or not working
|
||||
//#define INT_GPIO 4
|
||||
|
||||
//#define MISO_GPIO 19
|
||||
//#define MOSI_GPIO 23
|
||||
//#define SCK_GPIO 18
|
||||
//#define CS_GPIO 5
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
#include <WebServer_ESP32_W5500.h>
|
||||
|
||||
// Enter a MAC address and IP address for your controller below.
|
||||
#define NUMBER_OF_MAC 20
|
||||
|
||||
byte mac[][NUMBER_OF_MAC] =
|
||||
{
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
|
||||
};
|
||||
|
||||
// Select the IP address according to your local network
|
||||
IPAddress myIP(192, 168, 2, 232);
|
||||
IPAddress myGW(192, 168, 2, 1);
|
||||
IPAddress mySN(255, 255, 255, 0);
|
||||
|
||||
// Google DNS Server IP
|
||||
IPAddress myDNS(8, 8, 8, 8);
|
||||
|
||||
char server[] = "arduino.tips";
|
||||
|
||||
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
|
||||
const unsigned long postingInterval = 10000L; // delay between updates, in milliseconds
|
||||
|
||||
// Initialize the Web client object
|
||||
WiFiClient client;
|
||||
|
||||
// this method makes a HTTP connection to the server
|
||||
void httpRequest()
|
||||
{
|
||||
Serial.println();
|
||||
|
||||
// close any connection before send a new request
|
||||
// this will free the socket on the WiFi shield
|
||||
client.stop();
|
||||
|
||||
// if there's a successful connection
|
||||
if (client.connect(server, 80))
|
||||
{
|
||||
Serial.println(F("Connecting..."));
|
||||
|
||||
// send the HTTP PUT request
|
||||
client.println(F("GET /asciilogo.txt HTTP/1.1"));
|
||||
client.println(F("Host: arduino.tips"));
|
||||
client.println(F("Connection: close"));
|
||||
client.println();
|
||||
|
||||
// note the time that the connection was made
|
||||
lastConnectionTime = millis();
|
||||
}
|
||||
else
|
||||
{
|
||||
// if you couldn't make a connection
|
||||
Serial.println(F("Connection failed"));
|
||||
}
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
while (!Serial && (millis() < 5000));
|
||||
|
||||
Serial.print(F("\nStart WebClientRepeating on "));
|
||||
Serial.print(ARDUINO_BOARD);
|
||||
Serial.print(F(" with "));
|
||||
Serial.println(SHIELD_TYPE);
|
||||
Serial.println(WEBSERVER_ESP32_W5500_VERSION);
|
||||
|
||||
ET_LOGWARN(F("Default SPI pinout:"));
|
||||
ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
|
||||
ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
|
||||
ET_LOGWARN1(F("MISO:"), MISO_GPIO);
|
||||
ET_LOGWARN1(F("SCK:"), SCK_GPIO);
|
||||
ET_LOGWARN1(F("CS:"), CS_GPIO);
|
||||
ET_LOGWARN1(F("INT:"), INT_GPIO);
|
||||
ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
|
||||
ET_LOGWARN(F("========================="));
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
// To be called before ETH.begin()
|
||||
ESP32_W5500_onEvent();
|
||||
|
||||
// start the ethernet connection and the server:
|
||||
// Use DHCP dynamic IP and random mac
|
||||
//bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
|
||||
// int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
|
||||
ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
|
||||
//ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
|
||||
|
||||
// Static IP, leave without this line to get IP via DHCP
|
||||
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
|
||||
//ETH.config(myIP, myGW, mySN, myDNS);
|
||||
|
||||
ESP32_W5500_waitForConnect();
|
||||
|
||||
///////////////////////////////////
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// if there's incoming data from the net connection send it out the serial port
|
||||
// this is for debugging purposes only
|
||||
while (client.available())
|
||||
{
|
||||
char c = client.read();
|
||||
Serial.write(c);
|
||||
Serial.flush();
|
||||
}
|
||||
|
||||
// if 10 seconds have passed since your last connection,
|
||||
// then connect again and send data
|
||||
if (millis() - lastConnectionTime > postingInterval)
|
||||
{
|
||||
httpRequest();
|
||||
}
|
||||
}
|
190
lib/WebServer_ESP32_W5500/examples/WebServer/WebServer.ino
Normal file
190
lib/WebServer_ESP32_W5500/examples/WebServer/WebServer.ino
Normal file
@ -0,0 +1,190 @@
|
||||
/****************************************************************************************************************************
|
||||
WebServer.ino - Simple Arduino web server sample for Ethernet shield
|
||||
|
||||
For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
|
||||
|
||||
WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
|
||||
|
||||
Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
|
||||
Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
|
||||
Licensed under GPLv3 license
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#if !( defined(ESP32) )
|
||||
#error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
|
||||
#endif
|
||||
|
||||
#define DEBUG_ETHERNET_WEBSERVER_PORT Serial
|
||||
|
||||
// Debug Level from 0 to 4
|
||||
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
// Optional values to override default settings
|
||||
// Don't change unless you know what you're doing
|
||||
//#define ETH_SPI_HOST SPI3_HOST
|
||||
//#define SPI_CLOCK_MHZ 25
|
||||
|
||||
// Must connect INT to GPIOxx or not working
|
||||
//#define INT_GPIO 4
|
||||
|
||||
//#define MISO_GPIO 19
|
||||
//#define MOSI_GPIO 23
|
||||
//#define SCK_GPIO 18
|
||||
//#define CS_GPIO 5
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
#include <WebServer_ESP32_W5500.h>
|
||||
|
||||
WiFiServer server(80);
|
||||
|
||||
// Enter a MAC address and IP address for your controller below.
|
||||
#define NUMBER_OF_MAC 20
|
||||
|
||||
byte mac[][NUMBER_OF_MAC] =
|
||||
{
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
|
||||
};
|
||||
|
||||
// Select the IP address according to your local network
|
||||
IPAddress myIP(192, 168, 2, 232);
|
||||
IPAddress myGW(192, 168, 2, 1);
|
||||
IPAddress mySN(255, 255, 255, 0);
|
||||
|
||||
// Google DNS Server IP
|
||||
IPAddress myDNS(8, 8, 8, 8);
|
||||
|
||||
int reqCount = 0; // number of requests received
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
while (!Serial && (millis() < 5000));
|
||||
|
||||
Serial.print(F("\nStart WebServer on "));
|
||||
Serial.print(ARDUINO_BOARD);
|
||||
Serial.print(F(" with "));
|
||||
Serial.println(SHIELD_TYPE);
|
||||
Serial.println(WEBSERVER_ESP32_W5500_VERSION);
|
||||
|
||||
ET_LOGWARN(F("Default SPI pinout:"));
|
||||
ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
|
||||
ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
|
||||
ET_LOGWARN1(F("MISO:"), MISO_GPIO);
|
||||
ET_LOGWARN1(F("SCK:"), SCK_GPIO);
|
||||
ET_LOGWARN1(F("CS:"), CS_GPIO);
|
||||
ET_LOGWARN1(F("INT:"), INT_GPIO);
|
||||
ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
|
||||
ET_LOGWARN(F("========================="));
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
// To be called before ETH.begin()
|
||||
ESP32_W5500_onEvent();
|
||||
|
||||
// start the ethernet connection and the server:
|
||||
// Use DHCP dynamic IP and random mac
|
||||
//bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
|
||||
// int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
|
||||
ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
|
||||
//ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
|
||||
|
||||
// Static IP, leave without this line to get IP via DHCP
|
||||
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
|
||||
//ETH.config(myIP, myGW, mySN, myDNS);
|
||||
|
||||
ESP32_W5500_waitForConnect();
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
// start the web server on port 80
|
||||
server.begin();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// listen for incoming clients
|
||||
WiFiClient client = server.available();
|
||||
|
||||
if (client)
|
||||
{
|
||||
Serial.println(F("New client"));
|
||||
// an http request ends with a blank line
|
||||
bool currentLineIsBlank = true;
|
||||
|
||||
while (client.connected())
|
||||
{
|
||||
if (client.available())
|
||||
{
|
||||
char c = client.read();
|
||||
Serial.write(c);
|
||||
|
||||
// if you've gotten to the end of the line (received a newline
|
||||
// character) and the line is blank, the http request has ended,
|
||||
// so you can send a reply
|
||||
if (c == '\n' && currentLineIsBlank)
|
||||
{
|
||||
Serial.println(F("Sending response"));
|
||||
|
||||
// send a standard http response header
|
||||
// use \r\n instead of many println statements to speedup data send
|
||||
client.print(
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Content-Type: text/html\r\n"
|
||||
"Connection: close\r\n" // the connection will be closed after completion of the response
|
||||
"Refresh: 20\r\n" // refresh the page automatically every 20 sec
|
||||
"\r\n");
|
||||
client.print("<!DOCTYPE HTML>\r\n");
|
||||
client.print("<html>\r\n");
|
||||
client.print(String("<h2>Hello World from ") + BOARD_NAME + "!</h2>\r\n");
|
||||
client.print("Requests received: ");
|
||||
client.print(++reqCount);
|
||||
client.print("<br>\r\n");
|
||||
client.print("<br>\r\n");
|
||||
client.print("</html>\r\n");
|
||||
break;
|
||||
}
|
||||
|
||||
if (c == '\n')
|
||||
{
|
||||
// you're starting a new line
|
||||
currentLineIsBlank = true;
|
||||
}
|
||||
else if (c != '\r')
|
||||
{
|
||||
// you've gotten a character on the current line
|
||||
currentLineIsBlank = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// give the web browser time to receive the data
|
||||
delay(10);
|
||||
|
||||
// close the connection:
|
||||
client.stop();
|
||||
Serial.println(F("Client disconnected"));
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
/****************************************************************************************************************************
|
||||
multiFileProject.cpp
|
||||
For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
|
||||
|
||||
WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
|
||||
|
||||
Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
|
||||
Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
|
||||
Licensed under GPLv3 license
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
// To demo how to include files in multi-file Projects
|
||||
|
||||
#include "multiFileProject.h"
|
@ -0,0 +1,36 @@
|
||||
/****************************************************************************************************************************
|
||||
multiFileProject.h
|
||||
For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
|
||||
|
||||
WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
|
||||
|
||||
Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
|
||||
Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
|
||||
Licensed under GPLv3 license
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
// To demo how to include files in multi-file Projects
|
||||
|
||||
#pragma once
|
||||
|
||||
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 1
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
// Optional values to override default settings
|
||||
// Don't change unless you know what you're doing
|
||||
//#define ETH_SPI_HOST SPI3_HOST
|
||||
//#define SPI_CLOCK_MHZ 25
|
||||
|
||||
// Must connect INT to GPIOxx or not working
|
||||
//#define INT_GPIO 4
|
||||
|
||||
//#define MISO_GPIO 19
|
||||
//#define MOSI_GPIO 23
|
||||
//#define SCK_GPIO 18
|
||||
//#define CS_GPIO 5
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
|
||||
#include "WebServer_ESP32_W5500.hpp"
|
@ -0,0 +1,37 @@
|
||||
/****************************************************************************************************************************
|
||||
multiFileProject.ino
|
||||
For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
|
||||
|
||||
WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
|
||||
|
||||
Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
|
||||
Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
|
||||
Licensed under GPLv3 license
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
// To demo how to include files in multi-file Projects
|
||||
|
||||
#include "multiFileProject.h"
|
||||
|
||||
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
|
||||
#include "WebServer_ESP32_W5500.h"
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
while (!Serial);
|
||||
|
||||
delay(500);
|
||||
|
||||
Serial.println("\nStart multiFileProject");
|
||||
Serial.println(WEBSERVER_ESP32_W5500_VERSION);
|
||||
|
||||
|
||||
Serial.print("You're OK now");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// put your main code here, to run repeatedly:
|
||||
}
|
Reference in New Issue
Block a user