Created
May 16, 2025 19:05
-
-
Save cyrusmeh/289d8f5f9a3c3ecb65751c8ed632979b to your computer and use it in GitHub Desktop.
In this comprehensive tutorial, we'll show you how to connect your Arduino Nano to the internet or a local network using the W5500 Ethernet Module.
This file contains hidden or 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
| #include <SPI.h> | |
| #include <Ethernet2.h> | |
| // Network configuration | |
| byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; | |
| IPAddress ip(192, 168, 137, 100); | |
| IPAddress gateway(192, 168, 137, 1); | |
| IPAddress subnet(255, 255, 255, 0); | |
| EthernetServer server(80); // HTTP port | |
| // LDR pin | |
| const int LDR_PIN = A0; | |
| int lightValue = 0; | |
| unsigned long lastSensorRead = 0; | |
| void setup() { | |
| Serial.begin(9600); | |
| while (!Serial); // Wait for serial | |
| // Start Ethernet | |
| Ethernet.begin(mac, ip, gateway, subnet); | |
| server.begin(); | |
| Serial.print("Server is at "); | |
| Serial.println(Ethernet.localIP()); | |
| Serial.println("Ready to receive requests and send sensor data..."); | |
| } | |
| void loop() { | |
| // Read sensor every 2 seconds | |
| if (millis() - lastSensorRead > 2000) { | |
| lightValue = analogRead(LDR_PIN); | |
| Serial.print("LDR Value: "); | |
| Serial.print(lightValue); | |
| Serial.print(" ("); | |
| Serial.print(map(lightValue, 0, 1023, 0, 100)); | |
| Serial.println("% brightness)"); | |
| lastSensorRead = millis(); | |
| } | |
| // Handle client connections | |
| EthernetClient client = server.available(); | |
| if (client) { | |
| Serial.println("\nNew client connected"); | |
| boolean currentLineIsBlank = true; | |
| String request = ""; | |
| while (client.connected()) { | |
| if (client.available()) { | |
| char c = client.read(); | |
| request += c; | |
| Serial.write(c); | |
| if (c == '\n' && currentLineIsBlank) { | |
| // Send HTTP response | |
| sendResponse(client); | |
| break; | |
| } | |
| if (c == '\n') { | |
| currentLineIsBlank = true; | |
| } else if (c != '\r') { | |
| currentLineIsBlank = false; | |
| } | |
| } | |
| } | |
| // Log the request | |
| Serial.println("Full request:"); | |
| Serial.println(request); | |
| delay(1); // Allow time for browser to receive data | |
| client.stop(); | |
| Serial.println("Client disconnected"); | |
| } | |
| } | |
| void sendResponse(EthernetClient &client) { | |
| // Read sensor right before sending response for fresh data | |
| lightValue = analogRead(LDR_PIN); | |
| client.println("HTTP/1.1 200 OK"); | |
| client.println("Content-Type: text/html"); | |
| client.println("Connection: close"); | |
| client.println(); | |
| client.println("<!DOCTYPE HTML>"); | |
| client.println("<html>"); | |
| client.println("<head>"); | |
| client.println("<title>Arduino LDR Monitor</title>"); | |
| client.println("<meta http-equiv=\"refresh\" content=\"5\">"); // Auto-refresh every 5 sec | |
| client.println("<style>"); | |
| client.println("body { font-family: Arial, sans-serif; text-align: center; margin-top: 50px; }"); | |
| client.println(".sensor-value { font-size: 2em; color: #0066cc; margin: 20px; }"); | |
| client.println("</style>"); | |
| client.println("</head>"); | |
| client.println("<body>"); | |
| client.println("<h1>Arduino LDR Monitor</h1>"); | |
| client.print("<div class=\"sensor-value\">"); | |
| client.print("Light Level: "); | |
| client.print(lightValue); | |
| client.print(" ("); | |
| client.print(map(lightValue, 0, 1023, 0, 100)); | |
| client.println("%)</div>"); | |
| client.println("<p>Page refreshes every 5 seconds</p>"); | |
| client.println("<p>Last update: "); | |
| client.print(millis() / 1000); | |
| client.println(" seconds after startup</p>"); | |
| client.println("</body>"); | |
| client.println("</html>"); | |
| Serial.println("Sent sensor data to client"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment