Arduino + Ethernet Shield Help

Alright, so I bought a book to help teach me about some Arduino programming. My Dad bought me an ethernet shield when I got my R3 and I never really knew what to do with it until I got the book, which dedicates a whole chapter to using Arduino with an ethernet shield.

I’m trying to hook this up to my train set and be able to turn things on and off from my upstairs computer (train set is in the basement). We took our old router and made it into a bridge to communicate to our current router to communicate to our computer. Pinging my Arduino’s IP address works, so I know the connection is fine.

In the book it included code to be able to turn digital pin 8 on and off through a webpage. This is the code it had (that I copied into an Arduino Sketch):

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

byte mac] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip] = { 192,168,1,130 };

const int MAX_PAGENAME_LEN = 8;
char buffer[MAX_PAGENAME_LEN+1];

EthernetServer server(80);

void setup()
{
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  server.begin();
  delay(2000);
}

void loop()
{
  EthernetClient client = server.available();
  if (client)
  {
    int type = 0;
    while (client.connected())
    {
      if (client.available())
      {
        memset(buffer,0, sizeof(buffer));
        if(client.find("/"))
          if(client.readBytesUntil('/', buffer,sizeof(buffer)))
          {
            Serial.println(buffer);
            if(strcmp(buffer,"POST ") == 0)
            {
              client.find("
\r");
              while(client.findUntil("PinD", "
\r"))
              {
                int pin = client.parseInt();
                int val = client.parseInt();
                pinMode(pin, OUTPUT);
                digitalWrite(pin, val);
              }
            }
            sendHeader(client,"Post example");
            client.println("<h2>Click buttons to turn pin 8 on or off</h2>");
            client.print(
            "<form action='/' method='POST'><p><input type='hidden' name='pinD8'");
            client.println(" value='0'><input type='submit' value='Off'/></form>");
            client.print(
            "<form action='/' method='POST'><p><input type='hidden' name='pinD8'");
            client.println(" value='1'><input type='submit' value='On'/></form>");
            client.println("</body></html>");
            client.stop();
          }
          break;
        }
      }
      delay(1);
      client.stop();
    }
  }
void sendHeader(EthernetClient client, char *title)
{
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println();
  client.print("<html><head><title>");
  client.print(title);
  client.println("</title><body>");
}

I understand that the 2 HTML buttons assign the value of on or off (1 or 0) to the function digitalWrite(pin, val). However when I click either of the buttons on the webpage generated on the local IP address 130 nothing changes to the LED I have on digital pin 8. (and there are no electrical issues, I checked)

Can anyone help me? I’ve been stuck on this pretty much all day.

Thanks in advance.

Did not look at code, but can you control the led with the simple blink stater program to prove hardware functioning? If a board that has the led onboard for pin 13, maybe try that, just to prove hardware function.

This simple program to turn on the LED through ethernet is testing to see if I’ll be able to control my much larger train set, which has different outputs and inputs (digital and analog). I just want to see if it can be done.

Well you need to look at what the arduino can source power at, and look at what you are trying to control on the train set to see if they are compatible, both volts and amp wise. Typically, to do anything but drive a led, or ttl level components, which I doubt the train set is, you will need to develop either isolation relay or triac interface between the arduino and the real world devices on the train. On the input side, again you are working with 0-5v analog on the arduino, so you may need some voltage division resistor network to get the voltage down to a range you can read.

We have our breakaway bot running on an arduino, and if you look at the picture, on the left you will see an eight relay interface board that we use to control the solenoids at 24vdc and the compressor at 12vdc.

http://www.mcbride4.org/wp-content/uploads/2013/03/2013-03-26_18-55-33_765.jpg

Also some trains run on ac, so you may have an issue there too.

I have this relay board that I was planning on using to turn things on and off. There are miscellaneous things all over my layout that I’m going to turn on and off with the relay board (blue thing).

I understand how everything will work electrically, I’m worried about the programming.





Make sure that board takes TTL input levels and provides the drive circuitry and bypass diodes for that circuitry to drive the relay coils in those relays.

You do not want to put an inductive load on your Arduino pins directly. Use the pins to drive a transistor or suitable IC to drive those relay coils. The picture does not show any drivers but they might be on the other side of the PCB.

The relay board has a VCC and GND pin. The Arduino outputs drive transistors on the board that power the relays.

Ok, I was just trying to see if you can control something with a basic sketch, to confirm it was working and wired, then add the ethernet control to the system.

I don’t have the ethernet shield to play with, so I won’t be of much help. Googling around, there appears to be a significant rewrite to the ethernet library, from the .002x to the 1.0.x versions, so it may be that code in the book, is worked with the pre 1.0 release of the IDE.

That being said, I did a search online, for some example code that was written post 1.0 and found the following examples.

http://bildr.org/2011/06/arduino-ethernet-pin-control//

and

http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/web-server-LED-control/

both look to be good examples of what you are trying to accomplish.

One simple option for testing…

Download and install the RobotOpen Driver Station App…

www.robotopen.biz

Then download the RobotOpen Library from the same area.

There are many example programs.

This code base will let you talk to the Arduino via wifi and control the outputs from a game pad.

It’s a good place to start without actually understanding the underlying code.

Is the relay board pictured?
Just curious.

http://www.dhgate.com/product/new-5v-8-channel-relay-shield-for-arduino/114459520.html

I’ll look into that. My Dad won’t be too thrilled because he worked for ages getting out old router to be a bridge (or something like that). But thanks for the option!

Judging by the URL it seems like it. That website is blocked on my computer under the category of “Ecommerce”.

This is it: http://www.amazon.com/SainSmart-8-Channel-Relay-Module-Arduino/dp/B0057OC5WK

What sort of thing are you talking about? Resistor? Transistor? 1000µf capacitor?

(ok the capacitor was a joke but still, do you mean a transistor?)

I did notice when I first started using it that strange things would happen… Such as when I’d try to close the circuit for all 8 relays that the LED’s would dim and only 4 relays would click. I also had trouble with the digital input pins (I have emergency stop buttons on the control panels, and was trying to get it so that if the buttons were pressed it would turn on a relay that closed the circuit for a very bright red LED matrix to simulate a warning light) when the relay was activated.

I got my R3 from a kit off of Sparkfun and it included a toy motor to switched on by a transistor (but run off of the Arduino +5v output). It also had a temperature sensor. One day I was trying to simulate a thermostat by entering in a selected temperature into the Serial Monitor, and then if the current temperature was above the entered temperature it would turn on the motor. I’m not sure what happened but the temperature sensor started giving me bogus readings of 500+ degrees (it was not a programming error as I tested it with a code I had written before that worked fine). I think possibly I burnt out the sensor?

/tangent

Anyways, are you suggesting adding a transistor between the Arduino and the relay?

(and by the way for any of you wondering, “In that picture… Is that an air pressure gauge on the left? And pneumatic tubing in the background?” well yes. I have pnuematics on my train set. Problem? :wink: )

Sorry I am quite tired.
I deleted my post above because it was going off on a tangent of it’s own.

http://www.amazon.com/SainSmart-8-Ch.../dp/B0057OC5WK
The PCB layout image at your Amazon link says something like:

Input control signal LOW state current:
2.5V at  0.1mA
3.3V at 0.18mA
  5V at 0.35mA

The Arduino use Atmel AVR MCU and they can sink and source a maximum of 40mA per I/O and generally a chip total of 200mA.
Look at the specification sheets for the Atmel AVR chips in question and this link:
http://forums.adafruit.com/viewtopic.php?f=24&t=17802

Here’s the thing…it seems unlikely that board uses so little control current as it appears that the input goes through a resistor into the opto-isolator if you look at the blurry schematic.

From the pictures that board appears to have 817C opto-isolators (the close up shows B1312 and 817C).
The datasheet for that is likely here:
http://www.futurlec.com/LED/PC817.shtml

That part lists 50mA maximum forward current and is setup for a nominal 20mA current.

I suspect having thought about this that the numbers as shown on the layout provided are wrong.
How does one operate a 20mA LED with 20x less current and expect that to work?
So either I am wrong and those transistors are the buffer or there is no buffer and those specifications are wrong.

  1. Wire that thing up.
  2. Disconnect the Arduino.
  3. Take a DC ammeter or a suitably configured DMM and measure the current when you close the circuit between one control input pin and the system ground.

If that current exceeds 30mA you do not want to drive it with the Arduino directly.
I suspect the inputs source more than that which is why the reviewers are complaining they can only turn on 5 with the Raspberry Pi.

The safe bet here, given how easy it is to test, is to test this.
If the measured current exceeds say 30mA greatly then drive the 8 relays with a ULN2803 (8 Darlington transistors) or 8 discrete transistors. Just remember to account for the junction voltage drops.
If the measured current is between 30mA and 40mA look at the TTL 7407 or 7417 chips.
Just keep in mind that TTL 7417s are rarely available from any company except Texas Instruments.
7407, 74LS07 and even 74HC07 (open-drain CMOS equivalent) are a bit easier to find these days.
If the measured current is around 20mA or less then it should be fine but just remember 20mA x 8 circuits = 160mA.
If you make all 8 Arduino outputs low then that 160mA will consume a large portion of total maximum drive the chip can handle. So be careful about what, besides that board, you put on there while it’s like that.
If the potential for 160mA concerns you, then again, look at the 7407 because that will dramatically reduce the current demand on the Arduino (roughly 1/60 the current for all 8 circuits).

Back to the code, what do you get printed on the serial port when you press the buttons?

Allow me to test (it did say something I just can’t remember)

Alright testing now.

Accessed the webpage, serial printed…


 HTTP
favicon.i,,

Pressing “On”

Prints the same. No LED.

Pressing “Off”

Same thing. And no LED.

When I press the buttons, the TX and RX leds blink on both the ethernet shield and the R3, and an LED I connected to pin 13 (it is not in the code though, it’s just there to show me if the Arduino LED blinks) gets a little bit brighter when the TX and RX leds blink.

What are you powering this assembly with?
What voltage and current is the power source rated for?
Are you using the USB port for power?

The potential code issues aside make sure that you are not fooling yourself by powering that assembly with a power source that is not sufficient.

It is powered over my USB port.

I know that it is sufficient, I recently made a sketch with my MEGA that had 15+ LED’s that would scroll in a marquee and the delay could be adjusted from a trim pot (all the way down to a ~10ms delay).

I also ran a servo off of the mega while doing that, and 3 more trim pots that could adjust the colour of an RGB LED.

Can you add some print statements after the conditional logic steps in your code, and tell us what they give you?

I wonder if favicon.i is an icon? I hope not, because that would not make any sense.

It’s good to see another person that loves trains.

Do you mean add like an if-then statement, as in if the On button is pressed, serial.print(“The Led is on”);

and say the led is off if the Off button is pressed?

EDIT: ^ I added that sort of statement and when I press either the on of off buttons nothing is printed.

:smiley:

http://www.chiefdelphi.com/forums/attachment.php?attachmentid=12959&d=1346106255 (as of last year. Much has changed since then. MUCH.)

I put my Arduino Ethernet Shield R3 onto my Arduino Duo R3.

I cut a network cable in half.
Made an ethernet ‘cross-over cable’ like so:
Orange/White (one half) - Green/White (the other half)
White/Orange (one half) - White/Green (the other half)
Vice-versa
I used Scotch Tape as an insulator after twisting wires together.
MacGyvering during lunch…check.
Good thing that was a high quality dollar store cable ($1.34).

I put my ‘cross-over cable’ into my laptop (Dell E6400…yes I have several laptops) ethernet port.
I put the other end into the Arduino Ethernet Shield.

I disabled IPV6 (blah) on my laptop wired ethernet interface.
Set my wired connection in Windows 7 to: 192.168.1.131
Looked at the supplied code and it uses: 192.168.1.130
Set the gateway as: 192.168.1.1
Set the netmask to: 255.255.255.0

Loaded up your stored page in Firefox.
Regardless of what webpage button I press (On/Off) I get “HTTP” out the serial monitor.

When I get a chance I’ll check the output pins.

However I do not get this:

Care to clarify if you are using Internet Exploder…I mean Internet Explorer :slight_smile:

I have version 1.05 stable of the Arduino software.