Pages: [1]   Go Down
Author Topic: Mega 2560 with Ethernet shield  (Read 955 times)
0 Members and 1 Guest are viewing this topic.
Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have the Arduino Mega 2560 and the official Ethernet shield with the WIZnet iEthernet W5100 chip, witch I'm trying to use it to control some relays in order to control some higher voltage. There will be a total of 32 relays.

I want to control them by Ethernet (as I will be about 50m away from the card).
So I was wondering if anyone had any idea how I should do this. I'm not very experienced with the programming language, but I know the basics. I just need some help to to get at the right track. (at first at least)

Thank you for your time
StreamCyper
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Simple web page control of a pin.

Code:
//zoomkat 1-10-11
//routerbot code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html
//address will look like http://192.168.1.102:84/ when submited
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString;

//////////////////////

void setup(){

  pinMode(4, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  //enable serial data print
  Serial.begin(9600);
  Serial.println("servertest1"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  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); //uncomment to see in serial monitor
        }

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString);

          //now output HTML data header

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>HTML form GET example</H1>");

          client.println("<FORM ACTION=\"http://192.168.1.102:84\" method=get >");

          client.println("Pin 4 \"on\" or \"off\": <INPUT TYPE=TEXT NAME=\"LED\" VALUE=\"\" SIZE=\"25\" MAXLENGTH=\"50\"><BR>");

          client.println("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"Change Pin 4!\">");

          client.println("</FORM>");

          client.println("<BR>");

          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client
          client.stop();

          /////////////////////
          if(readString.indexOf("on") >0)//checks for on
          {
            digitalWrite(4, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(4, LOW);    // set pin 4 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thank you very much for your help and fast reply   smiley-grin  hopefully I will be able to work out what I need from this smiley
Logged

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've tried using the code I got from Zoomkat, but no matter what I try I can't connect to my Arduino. anyone has any suggestions for what I should do?
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
anyone has any suggestions for what I should do?

First question is have you ever gotten any ethernet code to work on your arduino? Second, you may need to provide information as to the lan network you are connecting to. Below is some simple client code you can try to see if it works.

Code:
//zoomkat 12-08-11
//simple client test
//for use with IDE 1.0
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical 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
byte myserver[] = { 208, 104, 2, 86 }; // zoomkat web page server IP address
EthernetClient client;
//////////////////////

void setup(){

  Ethernet.begin(mac, ip);
  //Ethernet.begin(mac, ip, subnet, gateway);
  Serial.begin(9600);
  Serial.println("Better client test 12/01/11"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  } 
}

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(myserver, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
    client.println(); //end of get request
  }
  else {
    Serial.println("connection failed"); //error message if no client connect
    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(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No I have not tried any other Ethernet programs on it as it is brand new.
when I try to run the test program I just get connection fail. so it's obvious I'm doing something wrong... 
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'm doing something wrong... 

Yes, the biggest thing you are doing wrong if you want help is not supplying the requested info on your network hardware and connections.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

sorry about that.... I got the the arduino connected to my computer with normal USB cable, and also it's connected to a network switch. my computer is also connected to the switch by cable,  that's all that is connected to the switch. I don't need to be able to connect to the arduino through Internet only through LAN.

the switch I use is this one:
http://us.zyxel.com/Products/details.aspx?PC1IndexFlag=20040520161143&CategoryGroupNo=5BAD15B7-2A64-43F2-9C39-8C5C912AE745


I'm using the arduino 1.0 programming software, it's set up like this:
Board: Arduino Mega 2560 or Mega ADK
Serial Port: COM 4
Programmer: AVRISP mkII
Logged

Belgium
Offline Offline
Sr. Member
****
Karma: 1
Posts: 281
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you tried the sketch that zoomkat posted? What does the serial monitor say? Have you tried pinging the ethernet shield from your computer? Is the IP that you used in the sketch in the same range as your computer?
Logged


Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

when I use sketch Zoomkat gave me, (just copy, paste and change mac and IP) and I get connection failed, Disconnecting.

I use IP: 169.254.216.120 for my PC, and IP: 169.254.216.121 for arduino.

When I try to ping the IP address of my arduino it responds.
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
when I use sketch Zoomkat gave me, (just copy, paste and change mac and IP) and I get connection failed, Disconnecting.

why did you change the mac address?

Quote
I use IP: 169.254.216.120 for my PC, and IP: 169.254.216.121 for arduino.

What does the ip address of your pc have to do with the arduino code? These may well be valid routable internet ip addresses and you may be pinging a computer "out there". 192.168.*.* are usual ip addresses used on a lan. Making random changes to working code usually results in failure.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what IP I use on the computer have nothing to do with the arduino code, but you complained that I didn't give enough info, so I give all info that just far out be useful.

Reason for changing the mac address is only because I changed it to the mac address that what is written on the Ethernet shied, no other reason.

Now I got it working smiley-grin I couldn't get anything working so I tried to really get into the code and understand it... then I found the problem with the test code. it tried to connect to "my server" with the IP: 208, 104, 2, 86 and then it failed. then I connected it to my home network and used on of my other computers as server and it worked. Then I went back to the other program for controlling the pins. and I found out that I had messed up the IP's there as well and then it didn't work of course...

Sorry for all the trouble  smiley-sweat  and thank you very much for all the help!!!
Logged

Pages: [1]   Go Up
Jump to: