Ethernet shield and receiver problem

Hi. i need some help about ethernet shield and receiver. I use 2xArduino uno, trassmitter, reciever, sensor BMP080, sensor DHT11 and LCD dispslay with I^2C interface. I take the information from transmitter and want to record it in mysql database.
This is my transsmitter code:

#include <VirtualWire.h>
  #include "LowPower.h"
  #include "DHT.h"
  #include "Adafruit_BMP085.h"
  #include "Wire.h"
  
  
  
  #define DHTPIN 8     // what pin we're connected to
  #define DHTTYPE DHT11   // DHT 11 
  DHT dht(DHTPIN, DHTTYPE);
  Adafruit_BMP085 bmp;
  int ledPin = 13;
  char Msg[30];// The string that we are going to send trought rf transmitter 
   
  void setup() 
  {
    Serial.begin(115200);
    dht.begin();  // initialing the DHT sensor
    bmp.begin();  
    pinMode(ledPin,OUTPUT);
    // VirtualWire setup
    vw_setup(2000); // Bits per sec
    vw_set_tx_pin(12);// Set the Tx pin. Default is 12
  }
   
  void loop() 
  {
      Serial.print("Pressure = ");
      Serial.println(bmp.readPressure());
      
       Serial.print("Temperature = ");
       Serial.println(bmp.readTemperature());
       
       Serial.print("Humidity = "); 
       Serial.println(dht.readHumidity());
      
  
    // Read and store Sensor Data
    int humidity = dht.readHumidity();
    int temp = bmp.readTemperature();
    unsigned long pressure = bmp.readPressure()/100;
     Serial.print("Pressure = "); 
     Serial.println(pressure );
   
    sprintf(Msg, "%d,%d,%d", humidity,temp ,pressure);
   // Turn on a light to show transmitting
    digitalWrite(ledPin, HIGH); 
   //LowPower.powerDown(SLEEP_250MS, ADC_OFF, BOD_OFF);  
    delay(100); 
    vw_send((uint8_t *)Msg, strlen(Msg));
    vw_wait_tx(); // Wait until the whole message is gone
   // Turn off a light after transmission
    digitalWrite(ledPin, LOW); 
    // put 5 mins sleep mode
    // As lowpower library support maximam 8s ,we use for loop to take longer (5mins) sleep
    // 5x60=300
    //300/4=75
    for(int i=0;i<75;i++)
    {
    LowPower.powerDown(SLEEP_4S, ADC_OFF, BOD_OFF);    // Instead of delay(4000); 
   //delay(4000);
    }
  }

reciever code:

  #include <Wire.h> // use Wire library for protocol i2c (A4 = SDA & A5 = SCL)
  #include <LiquidCrystal_I2C.h> // use LiquidCrystal_I2C library for control LCD on i2c protocol
  #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.101";
  
  LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); //change the address as per your I2C module
  
  int humidity=100;
  int temp=200;
  unsigned long pressure=0;
  char MsgReceived[21]; 
  int led = 13; //pin for LED
  
  
  byte thermometer[8] = //icon for thermometer
  {
    B00100,
    B01010,
    B01010,
    B01110,
    B01110,
    B11111,
    B11111,
    B01110
  };
  
  byte droplet[8] = //icon for droplet
  {
    B00100,
    B00100,
    B01010,
    B01010,
    B10001,
    B10001,
    B10001,
    B01110,
  };
  byte hi[8]=    //icon for heat index
  {
    0B00000,
    0B00000,
    0B10101,
    0B01110,
    0B11111,
    0B01110,
    0B10101,
    0B00000,
  };
  
  void setup() 
  {
    Ethernet.begin(mac);
    Serial.begin(115200);
    
    lcd.begin(20,4); // set up the LCD's number of columns and rows: 
    lcd.backlight(); //backlight is now ON
    pinMode(led, OUTPUT); 
  
//    // VirtualWire 
//    // Bits per sec
    vw_setup(2000);
    // set pin for connect receiver module 
    vw_set_rx_pin(11);  
    // Start the receiver PLL running
    vw_rx_start();       
  
    lcd.begin(20,4);   // initialize the lcd for 20 chars 4 lines, turn on backlight
    lcd.backlight(); // finish with backlight on  
    lcd.createChar(1, thermometer);
    lcd.createChar(2, droplet);
    lcd.createChar(3, hi);
    lcd.clear(); // clear the screen
  
  }
  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)) 
    {
      digitalWrite(led, HIGH);
      delay(100);
      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]);
      }
    connection();
    sscanf(MsgReceived, "%d,%d,%d",&humidity, &temp, &pressure); // Converts a string to an array
    digitalWrite(led, LOW); 
    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(" %");
    
    delay(10000);  

}
    
   
    
  }
  
  void connection()
  {
    if(client.connect(server, 80)) {
        
      client.print( "GET /diplomna/Arduino/add_data.php?");
      client.print("temperature=");
      client.print( temp );
      client.print("&&");
      client.print("humidity=");
      client.print( humidity );
      client.print("&&");
      client.print("pressure=");
      client.println( pressure );
      client.println( "HTTP/1.1");
      client.println( "Connection: close" );
      client.stop();
      Serial.println("CONNECTED");
    }
    else {
      // you didn’t get a connection to the server:
      Serial.println("-> connection failed");
    }
    
  }
  void lcd_display()
  { lcd.setCursor(1,0);
    lcd.print(" WEATHER STATION ");
    lcd.setCursor(4,1);
    lcd.print("TEMP");
    lcd.setCursor(9, 1);
    lcd.write(1);
    lcd.setCursor(11, 1);
    lcd.print(temp);
    lcd.write(0b11011111);
    lcd.print("C");
    lcd.setCursor(4,2);
    lcd.print("HUM");
    lcd.setCursor(9, 2);
    lcd.write(2);
    lcd.setCursor(11, 2);
    lcd.print(humidity);
    lcd.print("%");
    lcd.setCursor(4,3);
    lcd.print("PRS");
    lcd.setCursor(9, 3);
    lcd.write(3);
    lcd.setCursor(11, 3);
    lcd.print(pressure);
    lcd.print("Pa");
  }

receiver serial monitor output:
→ connection failed
Humidity = 73%
tempreture = 23 *C
pressure = 1101 hPa

if i don’t use Rf receiver i can connect to database and make a record (i remove #include <VirtualWire.h> and all rf function) . My display works properly in both cases.

I think that I have a conflict between the receiver and the shield. Can you help me :?
Thanks in advance :slight_smile:

I think you have a pin conflict. SPI uses pins 10,11,12,13 and VirtualWire uses 11. You will have to change that with vw_set_rx_pin(pin).

i change vw_set_rx_pin(5), but connecation failed again :frowning:

One more change - the function connection() is out of the is statement:

   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)) 
   {
     digitalWrite(led, HIGH);
     delay(100);
     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]);
     }
   }
   
   connection();

The first argument to the connect() method is either a string that is a server name, OR an IP address as 4 bytes. It is NOT an IP address as a string.

Don't use D13 for the LED. That is the SPI clock pin on the Uno.

I removed the LED and changed ip address: byte server = { 192, 168, 0, 101 }; and still doesn't work :frowning:

Add some debug stuff. Start with this in the receiver code.

// replace this
    Ethernet.begin(mac);

// with this
    while(!Ethernet.begin(mac)) {
      Serial.println("DHCP failed. Try again in 5 seconds");
      delay(5000);
    }

    Serial.println(Ethernet.localIP());

Does it get an IP?

Can you connect to the server with a PC on the localnet? Don't use the server for this test. Try from a separate PC.

I replace the code, but i can't get ip.

when i tried to connect to the server with other pc, this is the message:

Access forbidden!

New XAMPP security concept:

Access to the requested object is only available from the local network.

This setting can be configured in the file "httpd-xampp.conf".

Then you have two problems.

  1. You are not getting an IP.
  2. The server will not let you connect.

I can’t understand, why when I use this code below, everything works fine, but when added receiver and its functions i can’t connect to database. I think there is a conflict between the shield and receiver, but I can’t understand where exactly the things go wrong:

this is the code that make records in database:

 #include <SPI.h>
 #include <Ethernet.h>
 #include <Wire.h> // use Wire library for protocol i2c (A4 = SDA & A5 = SCL)
 #include <LiquidCrystal_I2C.h> // use LiquidCrystal_I2C library for control LCD on i2c protocol
 EthernetClient client;
 
 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
 char server[] = "192.168.0.101";
 int interval = 5000; // Wait between dumps
 
 byte thermometer[8] = //icon for thermometer
 {
   B00100,
   B01010,
   B01010,
   B01110,
   B01110,
   B11111,
   B11111,
   B01110
 };
 
 byte droplet[8] = //icon for droplet
 {
   B00100,
   B00100,
   B01010,
   B01010,
   B10001,
   B10001,
   B10001,
   B01110,
 };
 byte hi[8]=    //icon for heat index
 {
   0B00000,
   0B00000,
   0B10101,
   0B01110,
   0B11111,
   0B01110,
   0B10101,
   0B00000,
 };                 //(addr, EN,RW,RS,D4,D5,D6,D7,BL,BLpol)
 LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); //change the address as per your I2C module
 // Sensors 
 int humidity=0;
 int temp=0;
 int pressure=0;

 
 void setup() 
 {
   Ethernet.begin(mac);
   Serial.begin(115200);
   
   lcd.begin(20,4); // set up the LCD's number of columns and rows: 
   lcd.backlight(); //backlight is now ON
   lcd.createChar(1, thermometer);
   lcd.createChar(2, droplet);
   lcd.createChar(3, hi);
   lcd.clear(); // clear the screen
 
 } // END void setup
 
 void loop()
 {
   if (client.connect(server, 80)) {
   
   client.print( "GET /diplomna/Arduino/add_data.php?");
   client.print("temperature=");;
   client.print( temp);
   client.print("&&");
   client.print("humidity=");
   client.println( humidity );
   client.println( "HTTP/1.1");
   client.println( "Connection: close" );
   client.stop();
   Serial.println("CONNECTED");
   }
 else {
   // you didn’t get a connection to the server:
   Serial.println("–> connection failed/n");
 }
   
 lcd_display();
 delay(interval);
    
 }
 void lcd_display()
 { lcd.setCursor(1,0);
   lcd.print(" WEATHER STATION ");
   lcd.setCursor(4,1);
   lcd.print("TEMP");
   lcd.setCursor(9, 1);
   lcd.write(1);
   lcd.setCursor(11, 1);
   lcd.print(temp);
   lcd.write(0b11011111);
   lcd.print("C");
   lcd.setCursor(4,2);
   lcd.print("HUM");
   lcd.setCursor(9, 2);
   lcd.write(2);
   lcd.setCursor(11, 2);
   lcd.print(humidity);
   lcd.print("%");
   lcd.setCursor(4,3);
   lcd.print("PRS");
   lcd.setCursor(9, 3);
   lcd.write(3);
   lcd.setCursor(11, 3);
   lcd.print(pressure);
   lcd.print("Pa");
 }

Serial monitor:

  • CONNECTED
  • CONNECTED
  • CONNECTED

I don't see where it is working fine.
You are not reading and displaying the response from the server.
You are calling the client.stop function before you get a response from the server.
The shield won't get an IP using dhcp.

What part of that do you see as working fine?

Make these changes and try it again.

   if (client.connect(server, 80)) {
// add this line here
   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( "Connection: close\r\n" );

// add this
   while(client.connected()) {
      while(client.available()) {
        char ch = client.read();
        Serial.write(ch);
      }
    }
// end of add

   client.stop();
// add this line here
   Serial.println("DISCONNECTED");
   }
 else {
   // you didn't get a connection to the server:
   Serial.println("-> connection failed/n");
 }

edit: Corrected a bunch of errors in you request send. Look for comments.

When I use this code, the arduino takes the values for humidity and temperature and makes records in MySQL database. Thats all i want.
When i use your code:

while(!Ethernet.begin(mac)) {
     Serial.println("DHCP failed. Try again in 5 seconds");
     delay(5000);
   }
 Serial.begin(115200);
 Serial.println(Ethernet.localIP());

in the second case, i get the ip. Everything working fine.

When i replace your code in second case, this is the output:

192.168.0.102

CONNECTED
HTTP/1.1 400 Bad Request
Date: Wed, 03 Jun 2015 14:29:32 GMT
Server: Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.5.15
Vary: accept-language,accept-charset
Accept-Ranges: bytes
Connection: close
Content-Type: text/html; charset=utf-8
Content-Language: en

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Bad request!</title>
<link rev="made" href="mailto:postmaster@localhost" />
<style type="text/css"><!--/*--><![CDATA[/*><!--*/ 
    body { color: #000000; background-color: #FFFFFF; }
    a:link { color: #0000CC; }
    p, address {margin-left: 3em;}
    span {font-size: smaller;}
/*]]>*/--></style>
</head>

<body>
<h1>Bad request!</h1>
<p>


    Your browser (or proxy) sent a request that
    this server could not understand.

</p>
<p>
If you think this is a server error, please contact
the <a href="mailto:postmaster@localhost">webmaster</a>.

</p>

<h2>Error 400</h2>
<address>
  <a href="/">localhost</a>

  <span>Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.5.15</span>
</address>
</body>
</html>

DISCONNECTED

This is ouput, when i use RF receiver + your code:
→ connection failed/n
Temperature = 200 *C
Pressure = 0 Pa

This reply will be caused by any of the errors I corrected above in reply#10.

HTTP/1.1 400 Bad Request

Insure you correct them all.