Answer the question
In order to leave comments, you need to log in
How to exchange data between arduino nano and Ethernet Shield Module(For Arduino UNO MEGA)?
Hello. I want to run a web server on arduino. How to correctly, and in general, you can connect arduino and this is the ethernet module in the picture. Because somewhere I read that it is only for arduino MEGA and UNO.
Answer the question
In order to leave comments, you need to log in
forum.arduino.cc/index.php?topic=16325.0
Nano - Ethernet Sheild
---
TX1 - D1
RX0 - D0
D10 - D10
D11 - D11
D12 - D12
D13 - D13
REF - AREF
5V - 5V
GND - GND
D2 - RESET (see sketch above)
---
I did this:
The shield is connected to the arduino mega 2560. I don’t consider it expedient to connect to simpler ones - pages and images require a lot of memory, which is about 256K in MEGA.
sketch (gives html if the address is ip-addres or json if the request is for ip-addres/data, it's easy to attach other data, up to pictures, etc.)
#include<stdlib.h>
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);
float data[2];
uint8_t buflen = 8;
const int receive_pin = 2;
String htmlContentType = "text/html";
String jsonContentType = "application/json";
const char html[] PROGMEM = "<!DOCTYPE html>\r\n"
"<html>\r\n"
"<head>\r\n"
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n"
"</head>\r\n"
"<body>\r\n"
"</body>\r\n"
"</html>\r\n";
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void respondPage(EthernetClient client, const prog_char page[])
{
char c;
while((c = pgm_read_byte(page++)))
client.write(c);
}
void responseHeader(EthernetClient client, String contentType) {
client.println("HTTP/1.1 200 OK");
client.print("Content-Type: ");
client.println(contentType);
client.println("Connection: close");
client.println();
}
String readURL(EthernetClient client) {
if(client.find("GET /")) {
String URL = client.readStringUntil(' ');
while(client.available())
client.read();
return URL;
}
return "¬hing";
}
void loop() {
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
while (client.connected()) {
if (client.available()) {
String URL = readURL(client);
Serial.println("'"+URL+"'");
if (URL == "") {
responseHeader(client, htmlContentType);
respondPage(client, html);
}
else if (URL == "data") {
responseHeader(client, jsonContentType);
client.print("[1,1]");
}
break;
}
}
delay(1);
client.stop();
Serial.println("disconnected");
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question