Go Down

Topic: Ethernet connection issue (Read 240 times) previous topic - next topic

msg56

I want to use an Arduino UNO with Ethernet shield above the cooktop in my kitchen as part of a way to detect whether or not the gas is on.  I'm just starting the process of figuring out how to connect it to my home network and following chapter 14 of Jeremy Blum's first book "Exploring Arduino".  I downloaded his code (to prevent typos!).  When I first tried it by using my existing house cabling I could not configure Ethernet with DHCP, so I started trying all kind of things.  My Ethernet switch and router are in the basement about 40 feet away from my office.  I have an identical switch  (TP-Link  TL-SG108) and thought I would insert it between the Ethernet shield and the 4-port Ethernet outlet in my office to see if the LEDs on the switch would give me any indication that I had a good connection.  To my surprise, I was now able to successfully configure DHCP and everything works fine.  When I remove the switch in my office and plug the cable directly into the shield it doesn't work.  I'm trying to figure out why I need the switch in my office (close the the shield).  I don's want to have to put an Ethernet switch above my cooktop to get a network connection.  Any ideas? 
Code: [Select]
/*
Exploring Arduino - Code Listing 14-2: Web Server Code
http://www.exploringarduino.com/content/ch13

Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com )
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License v3 as published by
the Free Software Foundation.
*/

//Arduino Web Server
//Some code adapted under MIT License from
//http://bildr.org/2011/06/arduino-ethernet-pin-control/

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

const int BLUE    =5;
const int GREEN   =6;
const int RED     =7;
const int SPEAKER =3;

//For controlling LEDS and the speaker
//If you want to control additional things, add variables to control them here.
int freq = 0;
int pin;

//Set to your MAC address!
//It should be on your sticker. If you can't find it,
//make one up, or use this one.
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x94, 0xF6 };

//Start the server on port 80
EthernetServer server = EthernetServer(80); //port 80

boolean receiving = false; //To keep track of whether we are getting data.

void setup()
{
  Serial.begin(9600);

  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);

  //Connect with DHCP
  if (!Ethernet.begin(mac))
  {
    Serial.println("Could not Configure Ethernet with DHCP.");
    return;
  }
  else
  {
    Serial.println("Ethernet Configured!");
  }

  //Start the server
  server.begin();
  Serial.print("Server Started.\nLocal IP: ");
  Serial.println(Ethernet.localIP());
 
}

void loop()
{
  EthernetClient client = server.available();

  if (client)
  {

   //An HTTP request ends with a blank line
   boolean currentLineIsBlank = true;
   boolean sentHeader = false;

   while (client.connected())
   {
     if (client.available())
     {
       char c = client.read(); //Read from the incoming buffer

       if(receiving && c == ' ') receiving = false; //done receiving
       if(c == '?') receiving = true; //found arguments

       //This looks at the GET requests
       if(receiving)
       {
         //An LED command is specified with an L
         if (c == 'L')
         {
           Serial.print("Toggling Pin ");
           pin = client.parseInt();
           Serial.println(pin);
           digitalWrite(pin, !digitalRead(pin));
           break;
         }
         //A speaker command is specified with an S
         else if (c == 'S')
         {
           Serial.print("Setting Frequency to ");
           freq = client.parseInt();
           Serial.println(freq);
           if (freq == 0)
             noTone(SPEAKER);
           else
             tone(SPEAKER, freq);
           break;
         }
         //Add similarly formatted else if statements here
         //TO CONTROL ADDITIONAL THINGS
       }
   
       //Print out the response header and the HTML page
       if(!sentHeader)
       {
          //Send a standard HTTP response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html\n");
     
          //Red toggle button
          client.println("<form action='' method='get'>");
          client.println("<input type='hidden' name='L' value='7' />");
          client.println("<input type='submit' value='Toggle Red' />");
          client.println("</form>");
     
          //Green toggle button
          client.println("<form action='' method='get'>");
          client.println("<input type='hidden' name='L' value='6' />");
          client.println("<input type='submit' value='Toggle Green' />");
          client.println("</form>");
     
          //Blue toggle button
          client.println("<form action='' method='get'>");
          client.println("<input type='hidden' name='L' value='5' />");
          client.println("<input type='submit' value='Toggle Blue' />");
          client.println("</form>");
     
          //Speaker frequency slider
          client.println("<form action='' method='get'>");
          client.print("<input type='range' name='S' min='0' max='1000' step='100' value='0'/>");
          client.println("<input type='submit' value='Set Frequency' />");
          client.println("</form>");
     
          //Add additional forms forms for controlling more things here.
     
          sentHeader = true;
        }

        if (c == '\n' && currentLineIsBlank) break;

        if (c == '\n')
        {
          currentLineIsBlank = true;
        }
        else if (c != '\r')
        {
          currentLineIsBlank = false;
        }
      }
    }
    delay(5); //Give the web browser time to receive the data
    client.stop(); //Close the connection:
  }
}




steve_mcdonald

Is the router in the basement issuing addresses with DHCP?

msg56

Yes, it is.  I know the cables are good because I'm using the same cables for both setups.  In fact, the arrangement that works includes and extra cable and switch.

msg56

#3
Oct 22, 2020, 08:49 pm Last Edit: Oct 22, 2020, 08:58 pm by msg56
SOLVED!  But now I'm wondering how the bottom setup in the diagram ever worked.  Earlier, when I said I knew the cable was good, violated a rule I learned early in my career, don't assume anything.  Since I don't own a $2000 fluke analyzer, I couldn't test the quality of the cable feeding the four-port wall plate. Our house is only a year old and I never got around to connecting the other three cables on that four-port wall plate to my router in the basement.  I did that today and found that two of them allowed the shield to communicate fine.  The third one had a fault, which I could visible see just by looking at the RJ45 connector.  Replaced it and voila!  So, since I know the builder didn't always do a good job making up the connectors, I simply cut off the RJ45 connector (on the original line that was giving me a problem) and replaced it, and low and behold, it now works great.  I'm back in business.

Go Up