Hello, I am putting together a device, and one aspect of it needs to be able to connect to an outside web server and display the information from the site when the push button on the device is pressed. However, I am getting several different errors when I try to make it work. Sometimes the device hangs up until my watchdog timer resets the system. Other times the Serial monitor will say:
Pressed
connecting...
connection failed
While other times the Serial monitor displays:
Pressed
connecting...
connected
passed the GET command
at which point the program has entered the block of code where I use a GET statement to retrieve the information from the server, but the program does not display it for me.
I am using an Arduino Ethernet board (not a shield). I know that none of the libraries are in conflict here, as I've uploaded examples with all of them added and had no problems. Here is the code below dealing with only the internet aspects of the program. Thanks for any help.
#include <stdint.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <Time.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(XX,XX,XX,XX); // IP Address of site I wish to connect to
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(XXX,XXX,X,XXX); //my computer's IP Address
// 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;
boolean confirmed = false;
void setup()
{
Serial.begin(9600);
Serial.println("Serial begin:");
confirmed = false;
}
void loop()
{
if(confirmed == true) // Indicates button was pushed. Device should GET information from the server
{
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.1"); // Just the site from the WebClient library example code
client.println();
Serial.println("passed the GET command");
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
You might try the below and see if you get a connection.
//zoomkat 9-22-12
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//remove SD card if inserted
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
char serverName[] = "web.comporium.net"; // zoomkat's test web page server
EthernetClient client;
//////////////////////
void setup(){
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
Serial.begin(9600);
Serial.println("Better client test 9/22/12"); // so I can keep track of what is loaded
Serial.println("Send an e in serial monitor to test"); // what to do to test
}
void loop(){
// check for serial input
if (Serial.available() > 0) //if something in serial buffer
{
byte inChar; // sets inChar as a byte
inChar = Serial.read(); //gets byte from buffer
if(inChar == 'e') // checks to see byte is an e
{
sendGET(); // call sendGET function below when byte is an e
}
}
}
//////////////////////////
void sendGET() //client function to send/receive GET request data.
{
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
client.println("Host: web.comporium.net");
client.println(); //end of get request
}
else {
Serial.println("connection failed"); //error message if no client connect
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) { //connected or data available
char c = client.read(); //gets byte from ethernet buffer
Serial.print(c); //prints byte to serial monitor
}
Serial.println();
Serial.println("disconnecting.");
Serial.println("==================");
Serial.println();
client.stop(); //stop client
}
Is this the code that causes the problem? If not, post that code.
Through a series of commenting line after line I have determined that the line that causes the hangups is :
if (client.connect(server, 80)) { ...
After trying out a RAM memory library, it showed that I had approximately 700 bytes of RAM left. I will start going back through the rest of the code and trying to clean it up so I use less RAM.
Zoomkat, for some reason my Ethernet board will not make a connection to the web server using your code. I get two lines showing up on my Serial monitor:
Better client test 9/22/12
Send an e in serial monitor to test
After those, nothing happens. But I do get a connection on other servers when I use some other programs.
I incorporated Zoomkat's example into the code I'm using and everytime I push the button on my device I am able to successfully connect to the website. Thank you for that
But when I substitute the: char serverName[] = "web.comporium.net"; which is used in the example code for my: IPAddress server(XX,XX,XX,XX); code, and make the necessary changes in my GET line to pull the information I want from the website, it almost always hangs up.
I say "almost always" hangs up because sometimes on my first attempt after uploading the program it will run a successful GET, but after that I keep getting hangups that activate my Watchdog Timer. Here's the revised section of code that deals with the internet aspect:
#include <stdint.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <Time.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //mac address
//char serverName[] = "web.comporium.net"; // zoomkat's test web page server
IPAddress server(XX,XX,XX,XX); // IP to the GET site
EthernetClient client;
void setup()
{
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
Serial.begin(9600);
Serial.println("Serial begin:");
void loop(){
if(confirmed == true) // Push button is pressed
{
byte inChar; // sets inChar as a byte
inChar = Serial.read(); //gets byte from buffer
sendGET(); // call sendGET
}
}
void sendGET() //client function to send/receive GET request data.
{
// if (client.connect(serverName, 80)) { //starts client connection, checks for connection
if (client.connect(server, 80)) {
Serial.println("connected");
// delay(500); I tried adding a delay
client.println("GET /guardmon_report.php"); // The site I'm trying to connect to
// client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text from Zoomkat example
// client.println("Host: web.comporium.net");
delay(500); // I tried adding another delay
client.println(); //end of get request
}
else {
Serial.println("connection failed"); //error message if no client connect
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) { //connected or data available
char c = client.read(); //gets byte from ethernet buffer
Serial.print(c); //prints byte to serial monitor
}
Serial.println();
Serial.println("disconnecting.");
Serial.println("==================");
Serial.println();
client.stop(); //stop client
}
I know it's not a network problem, but is there something about using an IP Address instead of a DNS above the void setup() that would cause my program to hang up?
But when I substitute the: char serverName[] = "web.comporium.net"; which is used in the example code for my: IPAddress server(XX,XX,XX,XX); code, and make the necessary changes in my GET line to pull the information I want from the website, it almost always hangs up.
Hangs up where? What is the last thing you see on the serial monitor when it hangs up?
Code that uses an ip address instead of a web url.
//zoomkat 4-04-12
//simple client test
//for use with IDE 1.0
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605
//the arduino lan IP address { 192, 168, 1, 102 } may need
//to be modified modified to work with your router.
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
//byte ip[] = { 192, 168, 1, 102 }; // ip in lan assigned to arduino
//byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
//byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
byte myserver[] = { 208, 104, 2, 86 }; // zoomkat web page server IP address
EthernetClient client;
//////////////////////
void setup(){
Ethernet.begin(mac);
//Ethernet.begin(mac, ip, gateway, gateway, subnet);
Serial.begin(9600);
Serial.println("Better client test 4/04/12"); // so I can keep track of what is loaded
Serial.println("Send an e in serial monitor to test"); // what to do to test
}
void loop(){
// check for serial input
if (Serial.available() > 0) //if something in serial buffer
{
byte inChar; // sets inChar as a byte
inChar = Serial.read(); //gets byte from buffer
if(inChar == 'e') // checks to see byte is an e
{
sendGET(); // call sendGET function below when byte is an e
}
}
}
//////////////////////////
void sendGET() //client function to send/receive GET request data.
{
if (client.connect(myserver, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
client.println(); //end of get request
}
else {
Serial.println("connection failed"); //error message if no client connect
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) { //connected or data available
char c = client.read(); //gets byte from ethernet buffer
Serial.print(c); //prints byte to serial monitor
}
Serial.println();
Serial.println("disconnecting.");
Serial.println("==================");
Serial.println();
client.stop(); //stop client
}
When I use the IP address example code provided by Zoomkat on it's own, it works, but then when I incorporate it into the code my device hangs up.
But when I declare my IP address using: IPAddress server(XX,XX,XX,XX); and add the GET functions the program works properly the first few times that I press my button. But eventually it hangs up, and always at the point when the program reads: if (client.connect(server, 80)) . After that, resetting the board and/or reuploading the program wont even allow the program to get through one loop cycle before crashing. If I unplug the device and restart the Arduino IDE, the program usually works for a little bit before hanging up.
Here's the relevant code (very similar to the previous codes I've posted):
#include <stdint.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <Time.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x3B, 0x8E }; //physical mac address
IPAddress server(XX,XX,XX,XX); // Guard Alarm IP
EthernetClient client;
void setup()
{
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
Serial.begin(9600);
Serial.println("Serial begin:");
}
void loop()
{
if(confirmed == true) // Confirms that the button was pushed
{
byte inChar; // sets inChar as a byte
inChar = Serial.read(); //gets byte from buffer
sendGET(); // call sendGET function to confirm button was pushed
}
}
void sendGET() //client function to send/receive GET request data.
{
if (client.connect(server, 80)) {
Serial.println("connected");
delay(500);
client.println("GET /guardmon_report.php"); // The site I'm trying to connect to
delay(500);
client.println(); //end of get request
}
else {
Serial.println("connection failed"); //error message if no client connect
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) { //connected or data available
char c = client.read(); //gets byte from ethernet buffer
Serial.print(c); //prints byte to serial monitor
}
Serial.println();
Serial.println("disconnecting.");
Serial.println("==================");
Serial.println();
client.stop(); //stop client
}
// replace this
client.println("GET /guardmon_report.php");
// with this.
client.println("GET /guardmon_report.php HTTP/1.0");
The rest of your code is "perfect world" programming. It will work as long as nothing goes wrong. Here is where it will fail if the connection breaks. It will never exit these loops
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) { //connected or data available
char c = client.read(); //gets byte from ethernet buffer
Serial.print(c); //prints byte to serial monitor
}