Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Arduino + Ethernet Shield Help (http://www.chiefdelphi.com/forums/showthread.php?t=118069)

jwallace15 07-28-2013 11:15 PM

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):

Code:

#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("\n\r");
              while(client.findUntil("PinD", "\n\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.

tr6scott 07-29-2013 06:30 AM

Re: Arduino + Ethernet Shield Help
 
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.

jwallace15 07-29-2013 10:08 AM

Re: Arduino + Ethernet Shield Help
 
Quote:

Originally Posted by tr6scott (Post 1285099)
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.

tr6scott 07-29-2013 10:51 AM

Re: Arduino + Ethernet Shield Help
 
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/u...-55-33_765.jpg

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

jwallace15 07-29-2013 11:03 AM

Re: Arduino + Ethernet Shield Help
 
1 Attachment(s)
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.

techhelpbb 07-29-2013 02:49 PM

Re: Arduino + Ethernet Shield Help
 
Quote:

Originally Posted by jwallace15 (Post 1285130)
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.

jwallace15 07-29-2013 02:51 PM

Re: Arduino + Ethernet Shield Help
 
The relay board has a VCC and GND pin. The Arduino outputs drive transistors on the board that power the relays.

tr6scott 07-29-2013 03:42 PM

Re: Arduino + Ethernet Shield Help
 
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/tutor...r-LED-control/

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

ajlapp 07-30-2013 12:20 AM

Re: Arduino + Ethernet Shield Help
 
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.

techhelpbb 07-31-2013 04:13 PM

Re: Arduino + Ethernet Shield Help
 
Quote:

Originally Posted by jwallace15 (Post 1285130)
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.

Is the relay board pictured?
Just curious.

http://www.dhgate.com/product/new-5v...114459520.html

jwallace15 07-31-2013 04:21 PM

Re: Arduino + Ethernet Shield Help
 
Quote:

Originally Posted by ajlapp (Post 1285274)
One simple option for testing...

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!

Quote:

Originally Posted by techhelpbb (Post 1285498)
Is the relay board pictured? Just curious.

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-Ch.../dp/B0057OC5WK

jwallace15 07-31-2013 10:03 PM

Re: Arduino + Ethernet Shield Help
 
Quote:

Originally Posted by techhelpbb (Post 1285539)
So you need to put something between your Arduino and that board if those numbers are even close to correct.

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? ;) )

techhelpbb 07-31-2013 10:07 PM

Re: Arduino + Ethernet Shield Help
 
Quote:

Originally Posted by jwallace15 (Post 1285540)
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?

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:

Code:

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).

BornaE 08-01-2013 02:37 AM

Re: Arduino + Ethernet Shield Help
 
Back to the code, what do you get printed on the serial port when you press the buttons?

jwallace15 08-01-2013 11:08 AM

Re: Arduino + Ethernet Shield Help
 
Quote:

Originally Posted by BornaE (Post 1285557)
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...

Code:

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.


All times are GMT -5. The time now is 12:44 PM.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi