Ethernet relay project

Hello,

i'm new in arduino world. I would like to start my first project using arduino, but before I buy arduino boards I would like to find out some things, I hope some one will help me.

So the questions about arduino:

  1. What kind of arduiono bords should I buy to create Ethernet controlled relay projet ? The thing I found out already that I should buy Arduino Ethernet Shield, but what kind of main board is compatible with Arduino Ethernet Shield?
  2. Is there any VB samples for arduino control over ethernet ?
  3. What kind of relay boards is comatible with arduino ? I would like to use some factory boards.

I will wait for answer. Thanks.

indoor
Welcome to arduino
to answer your questions:

  1. What kind of arduiono bords should I buy to create Ethernet controlled relay projet ? The thing I found out already that I should buy Arduino Ethernet Shield, but what kind of main board is compatible with Arduino Ethernet Shield?

You can add the ethernet shield to all boards that have the headers. That is the uno, mega 256, mega ADK, but not the lilpad, or fio.
Next to having a ethernet shield you can also buy the Aduino ethernet. And there are plenty of clones. See this page on the avilable official hardware http://arduino.cc/en/Main/Hardware

  1. Is there any VB samples for arduino control over ethernet ?

There is no VB with arduino. Code is written in a C C++ like language.

  1. What kind of relay boards is comatible with arduino ? I would like to use some factory boards.

I can't help you with this one.

Best regards
Jantje

Thanks for reply.

I decided to buy Arduino Mega and Arduino Ethernet Shield.

C++ is OK for me, but there I have another questions:

  1. Are there any samples/documentation how to control arduiono over ethernet from web page or from Visual Studio ?
  2. How to Drive AC relays with Arduino ?

Hellooo Someone !!!

Hello,

did you install the arduino IDE on your computer? This is necessary to write code and to transport it to your arduino. And it is also necessary to learn how the whole thing works. With the IDE come some ethernet examples. Additionally google for webduino.

Best regards

Elektrix

Here is some example code that will help you turn pins on and of from your browser.

Taken from my book: 'Programming Arduino: Getting Started with Sketches'

// sketch 10-02 Internet Pins

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

// MAC address just has to be unique. This should work
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// The IP address will be dependent on your local network:
byte ip[] = { 192, 168, 1, 30 };
EthernetServer server(80);

int numPins = 5;
int pins[] = {3, 4, 5, 6, 7};
int pinState[] = {0, 0, 0, 0, 0};
char line1[100];

void setup()
{
  for (int i = 0; i < numPins; i++)
  {
     pinMode(pins[i], OUTPUT);
  }
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  server.begin();
}

void loop()
{
  EthernetClient client = server.available();
  if (client) 
  {
    while (client.connected()) 
    {
      readHeader(client);
      if (! pageNameIs("/"))
      {
        client.stop();  
        return;
      }
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println();

      // send the body
      client.println("<html><body>");
      client.println("<h1>Output Pins</h1>");
      client.println("<form method='GET'>");  
      setValuesFromParams();
      setPinStates();
      for (int i = 0; i < numPins; i++)
      {
         writeHTMLforPin(client, i);
      }
      client.println("<input type='submit' value='Update'/>");
      client.println("</form>");
      client.println("</body></html>");

      client.stop();            
    }
  }
}

void writeHTMLforPin(EthernetClient client, int i)
{
  client.print("<p>Pin ");
  client.print(pins[i]);  
  client.print("<select name='");
  client.print(i);
  client.println("'>");
  client.print("<option value='0'");
  if (pinState[i] == 0)
  {
    client.print(" selected");
  }
  client.println(">Off</option>");
  client.print("<option value='1'");
  if (pinState[i] == 1)
  {
    client.print(" selected");
  }
  client.println(">On</option>");
  client.println("</select></p>");  
}

void setPinStates()
{
  for (int i = 0; i < numPins; i++)
  {
     digitalWrite(pins[i], pinState[i]);
  }
}

void setValuesFromParams()
{
  for (int i = 0; i < numPins; i++)
  {
    pinState[i] = valueOfParam(i + '0');
  }
}

void readHeader(EthernetClient client)
{
  // read first line of header
  char ch;
  int i = 0;
  while (ch != '\n')
  {
    if (client.available())
    {
      ch = client.read();
      line1[i] = ch;
      i ++;
    }
  }
  line1[i] = '\0'; 
  Serial.println(line1);
}

boolean pageNameIs(char* name)
{
   // page name starts at char pos 4
   // ends with space
   int i = 4;
   char ch = line1[i];
   while (ch != ' ' && ch != '\n' && ch != '?')
   {
     if (name[i-4] != line1[i])
     {
       return false;
     }
     i++;
     ch = line1[i];
   }
   return true;
}

int valueOfParam(char param)
{
  for (int i = 0; i < strlen(line1); i++)
  {
    if (line1[i] == param && line1[i+1] == '=')
    {
      return (line1[i+2] - '0');
    }
  }
  return 0;
}

works great! But I can not access through my phones data network. Any suggestions?

jdmaag:
works great! But I can not access through my phones data network. Any suggestions?

Get a static IP, forward port 80 to your Arduino and hope that your ISP is not blocking said port.

For furture reference, you're better off posting full descriptions of your project in the "Project Guidance" section, if you want more help; this sub-forum isn't nearly as active as that one.

Hey guys... This is a very nice piece of code. I run it locally on my machine and while the LED works properly, the client side of the code isn't working and always says "connection failed". That means that I can't read the state of the LED, right? What am I possibly doing wrong? Can I expand the code for 8 on-off buttons? Any help would be greatly appreciated. Have a nice day :slight_smile:

FaTaL:
Hey guys... This is a very nice piece of code. I run it locally on my machine and while the LED works properly, the client side of the code isn't working and always says "connection failed". That means that I can't read the state of the LED, right? What am I possibly doing wrong? Can I expand the code for 8 on-off buttons? Any help would be greatly appreciated. Have a nice day :slight_smile:

You need to set the port in your code i.e.:

EthernetServer server(54321);

After doing this you need to forward port 54321 to your arduino device on your router/modem. After doing this you can connect remotely by going to http://yourexternalIP:54321

Hope this helps.

No... My problem is when I have 2 leds on for example, and then I try to hit the 192.168.1.30 again in the URL bar. All the leds turn off again. I see that there is a void setValuesFromParams() function but doesn't seem to work properly... That means that if I have 5 lights on for two hours for example, and then try to open the browser and hit the ip, the lights will turn off, regardless of their state at the time :frowning: Any help would be appreciated. Thanks

hallo
@Si:
is it possible to implement your skatch to FHEM? i want to switch 4 relays but everyone for it own...
i tried with http://arduinoip/?cmd=set 3=0 but all of the relays will change also with /?3=0 or /?cmd=set_3_0...

is there a way for an webadress-based change for only one of the pins???

thanks for answeres...

Mike

FaTal, I believe when you connect to the IP address the Arduino resets the LED states. You could possibly change the code to make them stay the same.