1

I need some help to connect my arduino uno R3 and ethernet shield. I don't have a mac address with my shield so I'm using this one: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

The Ethernet controller on the Ethernet shield is W5100 and I don't use SD card.

I have receiver module wich receives data from transmitter and I want to save it in database, but there is issue related to the connection. When I print the Ethernet.localIP i observed only 0.0.0.0.

This is the IP address of my local network - char server[] = "192.168.0.106"

You can find my code below:

  #include <Wire.h> // use Wire library for protocol i2c (A4 = SDA & A5 = SCL)
  #include <VirtualWire.h> // use Virtual library for decode signal from Rx module
  #include <SPI.h>
  #include <Ethernet.h>


  EthernetClient client;
  byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 
  char server[] = "192.168.0.106";

  int humidity=0;
  int temp=0;
  int pressure=0;
  char MsgReceived[21]; 


  void setup() 
  {
     Serial.begin(115200);
     Ethernet.begin(mac);

     Serial.println(Ethernet.localIP());

    // Bits per sec
    vw_setup(2000);
    // set pin for connect receiver module 
    vw_set_rx_pin(3);  
    // Start the receiver PLL running
    vw_rx_start();       

  }
  void loop()
  {    
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  //Taking the data from the control base
  if (vw_get_message(buf, &buflen)) 
  {
    int i;
    // Message with a good checksum received, dump it. 
    for (i = 0; i < buflen; i++)
    {            
      // Fill Msg Char array with corresponding 
      // chars from buffer.   
      MsgReceived[i] = char(buf[i]);
      //Serial.print(MsgReceived[i]);
    }

    sscanf(MsgReceived, "%d,%d,%d",&humidity, &temp,&pressure); // Converts a string to an array 
    lcd_display();
    memset( MsgReceived, 0, sizeof(MsgReceived));// This line is for reset the StringReceived

    Serial.print("Temperature = ");
    Serial.print(temp);
    Serial.println(" *C");
    Serial.print("Pressure = ");
    Serial.print(pressure);
    Serial.println(" Pa");
    Serial.print("humidity = ");
    Serial.print(humidity);
    Serial.println(" %");

if (client.connect(server, 80)) {
   Serial.println("CONNECTED");
   client.print( "GET /diplomna/Arduino/add_data.php?");
   client.print("temperature=");;
   client.print( temp);
   client.print("&&");
   client.print("humidity=");
// change this to a print rather than println
   client.print( humidity );
// add a leading space in this println
   client.println( " HTTP/1.1");
// add a second cr/lf here to denote end of header
   client.println();
   client.println( "Connection: close\r\n" );

   while(client.connected()) {
      while(client.available()) {
        char ch = client.read();
        Serial.write(ch);
      }
    }
   client.stop();
   Serial.println("DISCONNECTED");
   }
 else {
   // you didn't get a connection to the server:
   Serial.println("-> connection failed/n");
 }
   delay(10000);
  }
  } 

You can find the serial monitor output below:

0.0.0.0 Temperature = 26 *C Pressure = 997 Pa humidity = 57 % -> connection failed/n

I also tried to execute the following example code (DhcpAddressPrinter) and the results are the same - 0.0.0.0

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

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
};

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // this check is only needed on the Leonardo:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for (;;)
      ;
  }
  // print your local IP address:
  printIPAddress();
}

void loop() {

  switch (Ethernet.maintain())
  {
    case 1:
      //renewed fail
      Serial.println("Error: renewed fail");
      break;

    case 2:
      //renewed success
      Serial.println("Renewed success");

      //print your local IP address:
      printIPAddress();
      break;

    case 3:
      //rebind fail
      Serial.println("Error: rebind fail");
      break;

    case 4:
      //rebind success
      Serial.println("Rebind success");

      //print your local IP address:
      printIPAddress();
      break;

    default:
      //nothing happened
      break;

  }
}

void printIPAddress()
{
  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print(".");
  }

  Serial.println();
}

Serial monitor output: Failed to configure Ethernet using DHCP

I also tried to set the ip address of the shield to be 192.168.137.1 and made the following update: Ethernet.begin(mac); ===> Ethernet.begin(mac,ip); The results are the same.

Could you please help me to debug this issue. Thanks in advance!

Regards, SK

9
  • If you're not using one of the peripherals in your program, you'll need to explicitly deselect it. To do this with the SD card, set pin 4 as an output and write a high to it. store.arduino.cc/arduino-ethernet-shield-2
    – Juraj
    Jun 10, 2018 at 21:12
  • what research have you done? .... did you try the example sketch?
    – jsotola
    Jun 10, 2018 at 21:32
  • How is your Ethernet shield wired to your network? What does your network consist of?
    – Majenko
    Jun 11, 2018 at 5:28
  • Hi Juraj, thank you for the feedback. I will try this today.
    – Supreme
    Jun 11, 2018 at 6:08
  • Hi jstola, Yes I made reasearch. I read many thames and reviwed a lot of videos regarding the issues related to the connections. Did you have a chance to read about my issue before to comment? There I explain that I have tried an example sketch, but the results are not as expected.
    – Supreme
    Jun 11, 2018 at 6:11

2 Answers 2

2

from comments: " I use straight connection cable to connect the ethernet shield to my laptop and I use wifi network. "

This is the problem. Connect to router.

The direct Ethernet connection may require the use of a cross-over cable and the PC probably doesn't have a DHCP server for the Ethernet interface, so you should set an IP address for the network interface and in the Arduino sketch.

see this SO question

1
  • Hi Juraj, you are right. I have connected the ethernet with the router and now the example scratch works.
    – Supreme
    Jun 11, 2018 at 18:49
1

The first thing you should do is disconnect or disable all devices from the Arduino except the network card then run your tests again, if they work (which I doubt) then the problem is a hardware resource clash, for which you will need to read the datasheets.

I suspect the problem you have is more fundamental, do you have a DHCP server on your network?

The first example doesn't specifically mention DHCP, but the second example makes the same call top Ethernet.begin() and seems to expect the call to have configured the NIC via DHCP.

So my advice would be to look at the API for the NIC and see what you need to do to statically configure you IPv4 settings. Also you might need to bone up on IPv4 addresses and net masks, because the addresses you mentioned won't be able to communicate unless you have some special routing gear (it could be I read the question wrong).

Hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.