Hi every one
I have two Arduino UNO and two ethernet shields an need to transfer data between them ,
And I am starter to ethernet library and ip and ....
so
Anyone Have codes to do this
Hi every one
I have two Arduino UNO and two ethernet shields an need to transfer data between them ,
And I am starter to ethernet library and ip and ....
so
Anyone Have codes to do this
You should look in the examples for the ethernet shield. In one Arduino you use the web client sketch, to send to the other, which uses the web server sketch. Look in those two sketches and try to figure out how it works, and you will learn a lot, and maybe get your project on the road. :). Then when you have a specific question, or you get stuck, ask again.
Hope this helps you start your journey into arduino-ethernetland ![]()
thank you but I am in a hurry and wanted ready codes
even that I will start with examples
Hi
To training on ethernet shield I decided to connect my Arduino UNO to PC first and transfer data via lan with telnet program
ther is an example about that in the library
at its name DHCP Chat Server
the code:
*/
#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.
// gateway and subnet are optional:
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
IPAddress ip(192,168,1, 177);
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 0, 0);
// telnet defaults to port 23
EthernetServer server(23);
boolean gotAMessage = false; // whether or not you got a message from the client yet
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// this check is only needed on the Leonardo:
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection:
Serial.println("Trying to get an IP address using DHCP");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// initialize the ethernet device not using DHCP:
Ethernet.begin(mac, ip, gateway, subnet);
}
// print your local IP address:
Serial.print("My IP address: ");
ip = Ethernet.localIP();
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(ip[thisByte], DEC);
Serial.print(".");
}
Serial.println();
// start listening for clients
server.begin();
}
void loop() {
// wait for a new client:
EthernetClient client = server.available();
// when the client sends the first byte, say hello:
if (client) {
if (!gotAMessage) {
Serial.println("We have a new client");
client.println("Hello, client!");
gotAMessage = true;
}
// read the bytes incoming from the client:
char thisChar = client.read();
// echo the bytes back to the client:
server.write(thisChar);
// echo the bytes to the server as well:
Serial.print(thisChar);
}
}
the problem that I use Windows 7
and need to know my own information of these lines of the code:
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
IPAddress ip(192,168,1, 177);
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 0, 0);
the tel net program I use is PuTTY
no one here
help me please;
Your ethernet shield should have come with a sticker on it telling you the MAC address. If it doesn't, just use the one in the example. Don't use the same one on both shields though.
For gateway and subnet, run IPconfig on your windows machine. Don't use the IP address of your PC though. For a normal consumer home network, the settings in the example sketch will probably do it - try it & see.
Test code that contains both client and server code that might be modified to do what you want.
//zoomkat 7-03-12, combined client and server
//simple button GET with iframe code
//for use with IDE 1.0
//open serial monitor and send an g to test client and
//see what the arduino client/server receives
//web page buttons make pin 5 high/low
//use the ' in html instead of " to prevent having to escape the "
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //assign arduino mac address
byte ip[] = {192, 168, 1, 102 }; // ip in lan assigned to arduino
byte gateway[] = {192, 168, 1, 1 }; // internet access via router
byte subnet[] = {255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port arduino server will use
EthernetClient client;
char serverName[] = "checkip.dyndns.com"; // (DNS) zoomkat's test web page server
//byte serverName[] = { 208, 104, 2, 86 }; // (IP) zoomkat web page server IP address
String readString; //used by server to capture GET request
//////////////////////
void setup(){
pinMode(5, OUTPUT); //pin selected to control
pinMode(6, OUTPUT); //pin selected to control
pinMode(7, OUTPUT); //pin selected to control
pinMode(8, OUTPUT); //pin selected to control
//pinMode(5, OUTPUT); //pin 5 selected to control
Ethernet.begin(mac,ip,gateway,gateway,subnet);
server.begin();
Serial.begin(9600);
Serial.println(F("server/client 1.0 test 7/03/12")); // keep track of what is loaded
Serial.println(F("Send an g in serial monitor to test client")); // what to do to test client
}
void loop(){
// check for serial input
if (Serial.available() > 0)
{
byte inChar;
inChar = Serial.read();
if(inChar == 'g')
{
sendGET(); // call client sendGET function
}
}
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}
//if HTTP request has ended
if (c == '\n') {
///////////////
Serial.print(readString); //print to serial monitor for debuging
//now output HTML data header
if(readString.indexOf('?') >=0) { //don't send new page
client.println(F("HTTP/1.1 204 Zoomkat"));
client.println();
client.println();
}
else {
client.println(F("HTTP/1.1 200 OK")); //send new page on browser request
client.println(F("Content-Type: text/html"));
client.println();
client.println(F("<HTML>"));
client.println(F("<HEAD>"));
client.println(F("<TITLE>Arduino GET test page</TITLE>"));
client.println(F("</HEAD>"));
client.println(F("<BODY>"));
client.println(F("<H1>Zoomkat's simple Arduino 1.0 button</H1>"));
// DIY buttons
client.println(F("Pin5"));
client.println(F("<a href=/?on2 target=inlineframe>ON</a>"));
client.println(F("<a href=/?off3 target=inlineframe>OFF</a>
"));
client.println(F("Pin6"));
client.println(F("<a href=/?on4 target=inlineframe>ON</a>"));
client.println(F("<a href=/?off5 target=inlineframe>OFF</a>
"));
client.println(F("Pin7"));
client.println(F("<a href=/?on6 target=inlineframe>ON</a>"));
client.println(F("<a href=/?off7 target=inlineframe>OFF</a>
"));
client.println(F("Pin8"));
client.println(F("<a href=/?on8 target=inlineframe>ON</a>"));
client.println(F("<a href=/?off9 target=inlineframe>OFF</a>
"));
client.println(F("Pins"));
client.println(F(" <a href=/?off2468 target=inlineframe>ALL ON</a>"));
client.println(F(" <a href=/?off3579 target=inlineframe>ALL OFF</a>
"));
// mousedown buttons
client.println(F("<input type=button value=ON onmousedown=location.href='/?on4;' target=inlineframe>"));
client.println(F("<input type=button value=OFF onmousedown=location.href='/?off5;' target=inlineframe>"));
client.println(F(" <input type=button value='ALL OFF' onmousedown=location.href='/?off3579;' target=inlineframe>
"));
// mousedown radio buttons
client.println(F("<input type=radio onmousedown=location.href='/?on6;' target=inlineframe>ON</>"));
client.println(F("<input type=radio onmousedown=location.href='/?off7; target=inlineframe'>OFF</>"));
client.println(F(" <input type=radio onmousedown=location.href='/?off3579;' target=inlineframe>ALL OFF</>
"));
// custom buttons
client.print(F("<input type=submit value=ON target=inlineframe style=width:100px;height:45px onClick=location.href='/?on8;'>"));
client.print(F("<input type=submit value=OFF target=inlineframe style=width:100px;height:45px onClick=location.href='/?off9;' target=inlineframe>"));
client.print(F(" <input type=submit value='ALL OFF' target=inlineframe style=width:100px;height:45px onClick=location.href='/?off3579;' target=inlineframe>"));
client.println(F("<IFRAME name=inlineframe style='display:none'>"));
client.println(F("</IFRAME>"));
client.println(F("</BODY>"));
client.println(F("</HTML>"));
}
delay(1);
//stopping client
client.stop();
///////////////////// control arduino pin
if(readString.indexOf('2') >0)//checks for 2
{
digitalWrite(5, HIGH); // set pin 5 high
Serial.println("Led 5 On");
Serial.println();
}
if(readString.indexOf('3') >0)//checks for 3
{
digitalWrite(5, LOW); // set pin 5 low
Serial.println("Led 5 Off");
Serial.println();
}
if(readString.indexOf('4') >0)//checks for 4
{
digitalWrite(6, HIGH); // set pin 6 high
Serial.println("Led 6 On");
Serial.println();
}
if(readString.indexOf('5') >0)//checks for 5
{
digitalWrite(6, LOW); // set pin 6 low
Serial.println("Led 6 Off");
Serial.println();
}
if(readString.indexOf('6') >0)//checks for 6
{
digitalWrite(7, HIGH); // set pin 7 high
Serial.println("Led 7 On");
Serial.println();
}
if(readString.indexOf('7') >0)//checks for 7
{
digitalWrite(7, LOW); // set pin 7 low
Serial.println("Led 7 Off");
Serial.println();
}
if(readString.indexOf('8') >0)//checks for 8
{
digitalWrite(8, HIGH); // set pin 8 high
Serial.println("Led 8 On");
Serial.println();
}
if(readString.indexOf('9') >0)//checks for 9
{
digitalWrite(8, LOW); // set pin 8 low
Serial.println("Led 8 Off");
Serial.println();
}
//clearing string for next read
readString="";
}
}
}
}
}
//////////////////////////
void sendGET() //client function to send and receive GET data from external server.
{
if (client.connect(serverName, 80)) {
Serial.println(F("connected"));
client.println(F("GET / HTTP/1.1"));
client.println(F("Host: checkip.dyndns.com"));
client.println(F("Connection: close"));
client.println();
}
else {
Serial.println(F("connection failed"));
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) { //connected or data available
char c = client.read();
Serial.print(c);
}
Serial.println();
Serial.println(F("disconnecting."));
Serial.println(F("=================="));
Serial.println();
client.stop();
}
Have you succeeded on this?