Hi all,
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 };
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