Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////////////////////////////////////////////////////////
- //Hello World on an Arduino Every with a Robotdyn Nano V3 Ethernet Shield - W5500 (V2) //
- // //
- //Arduino Every: https://store-usa.arduino.cc/products/arduino-nano-every //
- //Ethernet Shield: https://www.aliexpress.com/item/32966745493.html //
- //////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////
- // Initialize //
- //////////////////////////////////////////////////////////////////////////////////////////
- //Ethernet Varirable Setup
- //install ethernet2 library from https://www.arduino.cc/reference/en/libraries/ethernet2/
- //or by using the library manager within the arduino IDE
- //ethernet code based on web server sample that comes with the library
- #include <SPI.h>
- #include <Ethernet2.h>
- byte mac[] = { 0x74, 0x69, 0x69, 0x2D, 0x33, 0x36 }; // ethernet mac address - must be unique on your network
- //set ip, gateway, and subnet to match your network, you will use the ip to access the server
- IPAddress ip(192, 168, 0, 46);
- IPAddress gateway(192, 168, 0, 1);
- IPAddress subnet(255, 255, 255, 0);
- EthernetServer server(80); //set HTTP server port
- String readString; //var for url requested, this allows us to serve different pages based on the url
- //To create a favicon, first create a 16x16 png file, then convert to base64 then to 8 bit signed hex
- //http://www.xiconeditor.com/ -> create and or edit ico file
- //https://www.aconvert.com/image/ico-to-png/ -> convert ico to png
- //https://www.base64-image.de/ -> png to base64
- //https://cryptii.com/ -> hex encode
- // text -> decode 64 -> encode 8 bit hexidecimal signed -> text -> signed 8 bit for ethercard library
- //http://tomeko.net/online_tools/base64.php?lang=en -> base64 to unsigned hex with 0x prefix for ethernet2 library
- //for this example I have created a arduino green house/home icon
- //code was originally inspired by this forum post: https://jeelabs.org/forum/node/1042.html
- const char favicon[] = {
- -0x77, 0x50, 0x4e, 0x47, 0x0d, 0xa, 0x1a, 0xa, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
- 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10, 0x2, 0x3, 0x00, 0x00, 0x00, 0x62, -0x63, 0x17,
- -0x0e, 0x00, 0x00, 0x00, 0x04, 0x67, 0x41, 0x4d, 0x41, 0x00, 0x00, -0x4f, -0x71, 0x0b, -0x04, 0x61,
- 0x05, 0x00, 0x00, 0x00, 0x20, 0x63, 0x48, 0x52, 0x4d, 0x00, 0x00, 0x7a, 0x26, 0x00, 0x00, -0x80,
- -0x7c, 0x00, 0x00, -0x06, 0x00, 0x00, 0x00, -0x80, -0x18, 0x00, 0x00, 0x75, 0x30, 0x00, 0x00, -0x16,
- 0x60, 0x00, 0x00, 0x3a, -0x68, 0x00, 0x00, 0x17, 0x70, -0x64, -0x46, 0x51, 0x3c, 0x00, 0x00, 0x00,
- 0x09, 0x50, 0x4c, 0x54, 0x45, 0x00, 0x00, 0x00, 0x00, -0x6a, -0x64, -0x01, -0x01, -0x01, 0x2a, 0x0d,
- 0x43, 0x7e, 0x00, 0x00, 0x00, 0x01, 0x74, 0x52, 0x4e, 0x53, 0x00, 0x40, -0x1a, -0x28, 0x66, 0x00,
- 0x00, 0x00, 0x01, 0x62, 0x4b, 0x47, 0x44, 0x02, 0x66, 0x0b, 0x7c, 0x64, 0x00, 0x00, 0x00, 0x07,
- 0x74, 0x49, 0x4d, 0x45, 0x07, -0x1e, 0x06, 0x10, 0x09, 0x13, 0x0d, 0x58, -0x72, -0x4c, -0x4a, 0x00,
- 0x00, 0x00, 0x34, 0x49, 0x44, 0x41, 0x54, 0x08, -0x29, 0x63, 0x60, 0x0c, 0x0d, 0x75, 0x60, 0x10,
- 0x0d, 0x0d, 0x0d, -0x7f, 0x10, -0x5f, 0x61, 0x53, -0x7f, 0x44, -0x2c, -0x2e, 0x50, -0x7a, -0x30, -0x54,
- -0x6b, 0x40, 0x62, -0x2b, -0x56, 0x50, -0x7a, -0x50, 0x55, -0x55, -0x5a, 0x42, 0x58, 0x50, 0x22, 0x0a,
- -0x67, 0x40, -0x18, 0x05, 0x19, 0x05, 0x00, -0x35, -0x6e, 0x18, 0x60, 0x40, 0x44, 0x39, -0x49, 0x00,
- 0x00, 0x00, 0x25, 0x74, 0x45, 0x58, 0x74, 0x64, 0x61, 0x74, 0x65, 0x3a, 0x63, 0x72, 0x65, 0x61,
- 0x74, 0x65, 0x00, 0x32, 0x30, 0x31, 0x38, 0x2d, 0x30, 0x36, 0x2d, 0x31, 0x36, 0x54, 0x30, 0x39,
- 0x3a, 0x31, 0x39, 0x3a, 0x31, 0x33, 0x2d, 0x30, 0x37, 0x3a, 0x30, 0x30, -0x7a, 0x13, -0x11, 0x3e,
- 0x00, 0x00, 0x00, 0x25, 0x74, 0x45, 0x58, 0x74, 0x64, 0x61, 0x74, 0x65, 0x3a, 0x6d, 0x6f, 0x64,
- 0x69, 0x66, 0x79, 0x00, 0x32, 0x30, 0x31, 0x38, 0x2d, 0x30, 0x36, 0x2d, 0x31, 0x36, 0x54, 0x30,
- 0x39, 0x3a, 0x31, 0x39, 0x3a, 0x31, 0x33, 0x2d, 0x30, 0x37, 0x3a, 0x30, 0x30, -0x9, 0x4e, 0x57,
- -0x7e, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, -0x52, 0x42, 0x60, -0x7e
- };
- //////////////////////////////////////////////////////////////////////////////////////////
- // Start Functions //
- //////////////////////////////////////////////////////////////////////////////////////////
- //return how long the arduino/server has been running
- //original code from Arduino forum
- //https://forum.arduino.cc/t/function-uptime/70600
- char *uptime(unsigned long milli)
- {
- static char _return[25];
- unsigned long secs = milli / 1000, mins = secs / 60;
- unsigned int hours = mins / 60, days = hours / 24;
- milli -= secs * 1000;
- secs -= mins * 60;
- mins -= hours * 60;
- hours -= days * 24;
- sprintf(_return, "%d days %2.2d:%2.2d:%2.2d.%3.3d", (byte)days, (byte)hours, (byte)mins, (byte)secs, (int)milli);
- return _return;
- }
- //////////////////////////////////////////////////////////////////////////////////////////
- // Start Setup //
- //////////////////////////////////////////////////////////////////////////////////////////
- void setup () {
- Serial.begin(9600);
- Serial.println("Setup Start.");
- // start the Ethernet connection and the server:
- Ethernet.begin(mac, ip);
- server.begin();
- Serial.print("server is at ");
- Serial.println(Ethernet.localIP());
- Serial.println("Setup Complete.");
- Serial.println("");
- }
- //////////////////////////////////////////////////////////////////////////////////////////
- // Start Main Loop //
- //////////////////////////////////////////////////////////////////////////////////////////
- void loop() {
- // listen for incoming clients
- EthernetClient client = server.available();
- if (client) {
- Serial.println("");
- Serial.println("new client");
- boolean currentLineIsBlank = true;
- boolean endofFirstLine = false;
- readString = "";
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- //first time encounter a line return say true
- if (c == '\n' && endofFirstLine == false) endofFirstLine = true;
- //Serial.print(c); //print what server receives to serial monitor
- //read char by char HTTP request, limit to checking the first 30 and stop after first line
- if ((readString.length() < 30) && (endofFirstLine == false)) {
- //store characters to string
- readString += 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) {
- //serve a text version of Hello World and server uptime
- if (readString.indexOf("/text") > 0) {
- Serial.println(F("Text Hello World Sent"));
- // send a standard http response header
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/plain");
- client.println("Pragma: no-cache");
- client.println("Connection: close"); // the connection will be closed after completion of the response
- client.println();
- client.println("Hello World!");
- client.print("Server uptime: "); client.print(uptime(millis()));
- break;
- }
- //server a html version of Hello World and the server update
- if (readString.indexOf("/html") > 0) {
- Serial.println(F("HTML Hello World Sent"));
- // send a standard http response header
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println("Pragma: no-cache");
- client.println("Connection: close"); // the connection will be closed after completion of the response
- client.println();
- client.println("<!DOCTYPE HTML>");
- client.println("<html><body>");
- client.println("<h1>Hello World!</h1>");
- client.print("<br>Server uptime: "); client.println(uptime(millis()));
- client.println("</body></html>");
- break;
- }
- //serve the favicon
- if (readString.indexOf("favicon") > 0) {
- Serial.println(F("favicon Served"));
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: image/png");
- client.println("Pragma: no-cache");
- client.println("Connection: close"); // the connection will be closed after completion of the response
- client.println();
- for (int i = 0; i < sizeof(favicon); i++) {
- client.write(favicon[i]);
- }
- break;
- }
- //all other inquiries get 404
- else {
- Serial.println(F("404 Served"));
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/plain");
- client.println("Pragma: no-cache");
- client.println("Connection: close"); // the connection will be closed after completion of the response
- client.println();
- client.println("404 Error");
- 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(1);
- // close the connection
- client.stop();
- Serial.println("client disconnected");
- Serial.print (F("Uptime: ")); Serial.print (uptime(millis())); Serial.print(F(" readString: ")); Serial.println(readString);
- }
- delay(100);
- }
Add Comment
Please, Sign In to add comment