forked from khoih-prog/EthernetWebServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
360db83
commit 32864cc
Showing
1 changed file
with
177 additions
and
0 deletions.
There are no files selected for viewing
177 changes: 177 additions & 0 deletions
177
examples/SimpleWebServer_NativeEthernet/SimpleWebServer_NativeEthernet.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
/**************************************************************************************************************************** | ||
SimpleWebServer_NativeEthernet.ino - Dead simple web-server for Ethernet shields | ||
EthernetWebServer is a library for the Ethernet shields to run WebServer | ||
Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases | ||
Built by Khoi Hoang https://github.com/khoih-prog/EthernetWebServer | ||
Licensed under MIT 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. | ||
*****************************************************************************************************************************/ | ||
|
||
#include <SPI.h> | ||
#include <NativeEthernet.h> | ||
|
||
#define USING_STATIC_IP true | ||
|
||
byte mac[] = { 0xFE, 0xED, 0xDE, 0xAD, 0xBE, 0xEF }; //physical mac address | ||
|
||
#if USING_STATIC_IP | ||
IPAddress ip (192, 168, 2, 127); | ||
IPAddress gateway (192, 168, 2, 1); | ||
IPAddress subnet (255, 255, 255, 0); | ||
#endif | ||
|
||
EthernetServer server(80); //server port | ||
|
||
EthernetClient client; | ||
|
||
String readString; | ||
|
||
////////////////////// | ||
|
||
#define BOARD_NAME "Teensy 4.1 with NativeEthernet" | ||
|
||
#define ETHERNET_SS_PIN 10 | ||
|
||
#define BUFFER_SIZE 400 | ||
char htmlBuffer[BUFFER_SIZE]; | ||
|
||
void handleRoot() | ||
{ | ||
int sec = millis() / 1000; | ||
int min = sec / 60; | ||
int hr = min / 60; | ||
int day = hr / 24; | ||
|
||
hr = hr % 24; | ||
|
||
Serial.print(F("R")); | ||
|
||
snprintf(htmlBuffer, 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 EthernetWebServer!</h2>\ | ||
<h3>on %s</h3>\ | ||
<p>Uptime: %d d %02d:%02d:%02d</p>\ | ||
</body>\ | ||
</html>", BOARD_NAME, BOARD_NAME, day, hr % 24, min % 60, sec % 60); | ||
|
||
//server.send(200, F("text/html"), htmlBuffer); | ||
Serial.println(htmlBuffer); | ||
|
||
client.println(htmlBuffer); | ||
} | ||
|
||
////////////////////// | ||
|
||
void drawGraph() | ||
{ | ||
String out; | ||
out.reserve(3000); | ||
char temp[70]; | ||
|
||
Serial.print(F("D")); | ||
|
||
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=\"1\" stroke=\"rgb(0, 0, 0)\" />\n"); | ||
out += F("<g stroke=\"black\">\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=\"1\" />\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); | ||
Serial.println(out); | ||
|
||
client.println(out); | ||
} | ||
|
||
////////////////////// | ||
|
||
void setup() | ||
{ | ||
//enable serial data print | ||
Serial.begin(115200); | ||
while (!Serial); | ||
|
||
Serial.print("SimpleWebServer_NativeEthernet on "); | ||
Serial.println(BOARD_NAME); | ||
|
||
Ethernet.init(ETHERNET_SS_PIN); | ||
|
||
#if USING_STATIC_IP | ||
// Static IP | ||
//Ethernet.begin(mac, ip, gateway, subnet); | ||
Ethernet.begin(mac, ip); | ||
#else | ||
// DHCP | ||
Ethernet.begin(mac); | ||
#endif | ||
|
||
server.begin(); | ||
|
||
Serial.print(F("Connected! IP address: ")); | ||
Serial.println(Ethernet.localIP()); | ||
} | ||
|
||
void loop() | ||
{ | ||
// Create a client connection | ||
client = server.available(); | ||
|
||
if (client) | ||
{ | ||
while (client.connected()) | ||
{ | ||
if (client.available()) | ||
{ | ||
handleRoot(); | ||
drawGraph(); | ||
|
||
//stopping client | ||
client.stop(); | ||
} | ||
} | ||
} | ||
} |