Setting Arduino Pins over the Network from Programming Arduino book

Hello,

I am having trouble at connecting arduino pins over network. The code uploads fine but when trying to open web page i get an error that "The page isn't working right now, 192...* didn't send any data"

Project before this works fine, web page opens, so i guess it's the code not the connections,..

Do you see anything in the code that i should fix?

Bets regards.

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

byte mac [] = {0xA8, 0x61, 0x0A, 0xAE, 0xA8, 0xD1};

EthernetServer server (80);

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

void setup ()
{
  Ethernet.begin (mac);
  server.begin ();
  Serial.begin (115200);
  pinMode (4, OUTPUT);
  digitalWrite (4, HIGH);
  for (int i = 0; i < numPins; i++)
  {
    pinMode (pins [i], OUTPUT);
  }
  if (pageNameIs ("/"))
    Serial.print ("It is /");
  else
    Serial.println ("It is not. ");
}

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 ();
    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)
{
  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(const char * name)
{
  int i = 4;
  char ch = line1 [i];
  while (ch != ' ' && ch != '\n' && ch != '?' && i < LINE_LEN)
  {
    ch = line1 [i];
    if (name [i-4] != line1 [i])
    {
      return false;
    }
    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;
}

what do you really see in the terminal? how do you know the IP of the server?

Should this code be in setup() ?

Terminal? I get IP from my DHCP IPv4. As i did on a project before which was "Arduino as web server"

Well, the code that was in the book didn't work so that other guy fixed it for me. Those lines were added by him. Now it has no errors. But still doesn't work.

I should be getting this:
This

But i get that:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.