Ethernet shield + Arduino uno

Hi,

I am using the sample source code to make it multiple connection to google.com with arduino uno R3 staking on top ethernet shield. The result i got from serial monitoring is managed to get 1 connection successful and the rest was printed disconnecting. I had tried with arduino 1.0.5, arduino 1.5.3 and arduino nightly build but still no luck.
Kindly advise me how to make it connect repeating.

/*
Web client

This sketch connects to a website (http://www.google.com)
using an Arduino Wiznet Ethernet shield.

Circuit:

  • Ethernet shield attached to pins 10, 11, 12, 13

created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe, based on work by Adrian McEwen

*/

#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[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = "www.google.com"; // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192,168,0,177);

// 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);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo 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:
// 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");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
}

void loop()
{
// 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();

}

}

Try this sketch. It is set to download Google's home page every 30 seconds. You must change the network settings to yours.
http://playground.arduino.cc/Code/WebClient

SurferTim:
Try this sketch. It is set to download Google's home page every 30 seconds. You must change the network settings to yours.
Arduino Playground - HomePage

Its worked. Thanks so much :slight_smile:

When i added search the content and just get a line of content with assigned it to string variable and print it out.

if (client.find("<span id="lblMobile""))
{
client.find(">");

while(client.available())
{
//inChar = client.read();
char outChar = client.read();
txtKey += outChar;

connectLoop = 0;

}

Serial.print(txtKey);
// clientMobile = splitString(txtKey,'|',0);
// Serial.println(clientMobile);
delay(500);
}

However nothing to print or sometime printed strange characters. Seem gimme very unstable output.
Could you advise me how to assign to string and able to print the result
"testing|0163240222|test"

This is the output from website

Get Gsm Message
testing|0163240222|test

You should save each of the body lines in a character array and examine the contents of each line when you get a carriage return or line feed. Then reset the array index and get the next line.

The web server code in the playground uses this to store the GET request line with tCount and tBuf.
http://playground.arduino.cc/Code/WebServerST

That is the way I do it. Someone else may have a better idea.

The below textfinder application might be an ~easy approach. There are other code examples of capturing desired text from html streams, particularly capturing weather data. Bottom is somebody else's weather code.

http://playground.arduino.cc/Code/TextFinder

// Include description files for other libraries used (if any)
#include <SPI.h>
#include <Ethernet.h>

// Define Constants
// Max string length may have to be adjusted depending on data to be extracted
#define MAX_STRING_LEN  20

// Setup vars
char tagStr[MAX_STRING_LEN] = "";
char dataStr[MAX_STRING_LEN] = "";
char tmpStr[MAX_STRING_LEN] = "";
char endTag[3] = {'<', '/', '\0'};
int len;

// Flags to differentiate XML tags from document elements (ie. data)
boolean tagFlag = false;
boolean dataFlag = false;

// Ethernet vars
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 102 };
byte server[] = { 140, 90, 113, 200 }; // www.weather.gov

// Start ethernet client
EthernetClient client;

void setup()
{
  Serial.begin(9600);
  Serial.println("Starting WebWx");
  Serial.println("connecting...");
  Ethernet.begin(mac, ip);
  delay(1000);

  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.println("GET /xml/current_obs/KRDU.xml HTTP/1.0");    
    client.println();
    delay(2000);
  } else {
    Serial.println("connection failed");
  }  
}

void loop() {

  // Read serial data in from web:
  while (client.available()) {
    serialEvent();
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("Disconnected");
    Serial.println("==================================");
    Serial.println("");
    client.stop();

    // Time until next update
    //Serial.println("Waiting");
    for (int t = 1; t <= 15; t++) {
      delay(60000); // 1 minute
    }

    if (client.connect(server, 80)) {
      //Serial.println("Reconnected");
      client.println("GET /xml/current_obs/KRDU.xml HTTP/1.0");    
      client.println();
      delay(2000);
    } else {
      Serial.println("Reconnect failed");
    }      
  }
}

// Process each char from web
void serialEvent() {

   // Read a char
	 char inChar = client.read();
   //Serial.print(".");
  
   if (inChar == '<') {
      addChar(inChar, tmpStr);
      tagFlag = true;
      dataFlag = false;

   } else if (inChar == '>') {
      addChar(inChar, tmpStr);

      if (tagFlag) {      
         strncpy(tagStr, tmpStr, strlen(tmpStr)+1);
      }

      // Clear tmp
      clearStr(tmpStr);

      tagFlag = false;
      dataFlag = true;      
      
   } else if (inChar != 10) {
      if (tagFlag) {
         // Add tag char to string
         addChar(inChar, tmpStr);

         // Check for </XML> end tag, ignore it
         if ( tagFlag && strcmp(tmpStr, endTag) == 0 ) {
            clearStr(tmpStr);
            tagFlag = false;
            dataFlag = false;
         }
      }
      
      if (dataFlag) {
         // Add data char to string
         addChar(inChar, dataStr);
      }
   }  
  
   // If a LF, process the line
   if (inChar == 10 ) {

/*
      Serial.print("tagStr: ");
      Serial.println(tagStr);
      Serial.print("dataStr: ");
      Serial.println(dataStr);
*/

      // Find specific tags and print data
      if (matchTag("<temp_f>")) {
	      Serial.print("Temp: ");
         Serial.print(dataStr);
      }
      if (matchTag("<relative_humidity>")) {
	      Serial.print(", Humidity: ");
         Serial.print(dataStr);
      }
      if (matchTag("<pressure_in>")) {
	      Serial.print(", Pressure: ");
         Serial.print(dataStr);
         Serial.println("");
      }

      // Clear all strings
      clearStr(tmpStr);
      clearStr(tagStr);
      clearStr(dataStr);

      // Clear Flags
      tagFlag = false;
      dataFlag = false;
   }
}

/////////////////////
// Other Functions //
/////////////////////

// Function to clear a string
void clearStr (char* str) {
   int len = strlen(str);
   for (int c = 0; c < len; c++) {
      str[c] = 0;
   }
}

//Function to add a char to a string and check its length
void addChar (char ch, char* str) {
   char *tagMsg  = "<TRUNCATED_TAG>";
   char *dataMsg = "-TRUNCATED_DATA-";

   // Check the max size of the string to make sure it doesn't grow too
   // big.  If string is beyond MAX_STRING_LEN assume it is unimportant
   // and replace it with a warning message.
   if (strlen(str) > MAX_STRING_LEN - 2) {
      if (tagFlag) {
         clearStr(tagStr);
         strcpy(tagStr,tagMsg);
      }
      if (dataFlag) {
         clearStr(dataStr);
         strcpy(dataStr,dataMsg);
      }

      // Clear the temp buffer and flags to stop current processing
      clearStr(tmpStr);
      tagFlag = false;
      dataFlag = false;

   } else {
      // Add char to string
      str[strlen(str)] = ch;
   }
}

// Function to check the current tag for a specific string
boolean matchTag (char* searchTag) {
   if ( strcmp(tagStr, searchTag) == 0 ) {
      return true;
   } else {
      return false;
   }
}

zoomkat:
The below textfinder application might be an ~easy approach. There are other code examples of capturing desired text from html streams, particularly capturing weather data. Bottom is somebody else's weather code.

Arduino Playground - HomePage

// Include description files for other libraries used (if any)

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

// Define Constants
// Max string length may have to be adjusted depending on data to be extracted
#define MAX_STRING_LEN  20

// Setup vars
char tagStr[MAX_STRING_LEN] = "";
char dataStr[MAX_STRING_LEN] = "";
char tmpStr[MAX_STRING_LEN] = "";
char endTag[3] = {'<', '/', '\0'};
int len;

// Flags to differentiate XML tags from document elements (ie. data)
boolean tagFlag = false;
boolean dataFlag = false;

// Ethernet vars
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 102 };
byte server[] = { 140, 90, 113, 200 }; // www.weather.gov

// Start ethernet client
EthernetClient client;

void setup()
{
  Serial.begin(9600);
  Serial.println("Starting WebWx");
  Serial.println("connecting...");
  Ethernet.begin(mac, ip);
  delay(1000);

if (client.connect(server, 80)) {
    Serial.println("connected");
    client.println("GET /xml/current_obs/KRDU.xml HTTP/1.0");   
    client.println();
    delay(2000);
  } else {
    Serial.println("connection failed");
  } 
}

void loop() {

// Read serial data in from web:
  while (client.available()) {
    serialEvent();
  }

if (!client.connected()) {
    Serial.println();
    Serial.println("Disconnected");
    Serial.println("==================================");
    Serial.println("");
    client.stop();

// Time until next update
    //Serial.println("Waiting");
    for (int t = 1; t <= 15; t++) {
      delay(60000); // 1 minute
    }

if (client.connect(server, 80)) {
      //Serial.println("Reconnected");
      client.println("GET /xml/current_obs/KRDU.xml HTTP/1.0");   
      client.println();
      delay(2000);
    } else {
      Serial.println("Reconnect failed");
    }     
  }
}

// Process each char from web
void serialEvent() {

// Read a char
char inChar = client.read();
   //Serial.print(".");
 
   if (inChar == '<') {
      addChar(inChar, tmpStr);
      tagFlag = true;
      dataFlag = false;

} else if (inChar == '>') {
      addChar(inChar, tmpStr);

if (tagFlag) {     
         strncpy(tagStr, tmpStr, strlen(tmpStr)+1);
      }

// Clear tmp
      clearStr(tmpStr);

tagFlag = false;
      dataFlag = true;     
     
   } else if (inChar != 10) {
      if (tagFlag) {
         // Add tag char to string
         addChar(inChar, tmpStr);

// Check for end tag, ignore it
         if ( tagFlag && strcmp(tmpStr, endTag) == 0 ) {
            clearStr(tmpStr);
            tagFlag = false;
            dataFlag = false;
         }
      }
     
      if (dataFlag) {
         // Add data char to string
         addChar(inChar, dataStr);
      }
   } 
 
   // If a LF, process the line
   if (inChar == 10 ) {

/*
      Serial.print("tagStr: ");
      Serial.println(tagStr);
      Serial.print("dataStr: ");
      Serial.println(dataStr);
*/

// Find specific tags and print data
      if (matchTag("<temp_f>")) {
      Serial.print("Temp: ");
         Serial.print(dataStr);
      }
      if (matchTag("<relative_humidity>")) {
      Serial.print(", Humidity: ");
         Serial.print(dataStr);
      }
      if (matchTag("<pressure_in>")) {
      Serial.print(", Pressure: ");
         Serial.print(dataStr);
         Serial.println("");
      }

// Clear all strings
      clearStr(tmpStr);
      clearStr(tagStr);
      clearStr(dataStr);

// Clear Flags
      tagFlag = false;
      dataFlag = false;
   }
}

/////////////////////
// Other Functions //
/////////////////////

// Function to clear a string
void clearStr (char* str) {
   int len = strlen(str);
   for (int c = 0; c < len; c++) {
      str[c] = 0;
   }
}

//Function to add a char to a string and check its length
void addChar (char ch, char* str) {
   char *tagMsg  = "<TRUNCATED_TAG>";
   char *dataMsg = "-TRUNCATED_DATA-";

// Check the max size of the string to make sure it doesn't grow too
   // big.  If string is beyond MAX_STRING_LEN assume it is unimportant
   // and replace it with a warning message.
   if (strlen(str) > MAX_STRING_LEN - 2) {
      if (tagFlag) {
         clearStr(tagStr);
         strcpy(tagStr,tagMsg);
      }
      if (dataFlag) {
         clearStr(dataStr);
         strcpy(dataStr,dataMsg);
      }

// Clear the temp buffer and flags to stop current processing
      clearStr(tmpStr);
      tagFlag = false;
      dataFlag = false;

} else {
      // Add char to string
      str[strlen(str)] = ch;
   }
}

// Function to check the current tag for a specific string
boolean matchTag (char* searchTag) {
   if ( strcmp(tagStr, searchTag) == 0 ) {
      return true;
   } else {
      return false;
   }
}

Hmm! I had tried with Mega shield with similar source code. It is worked. Just UNO R3 shield board not working.
Any reason why like that?

After i press power button at UNO board, now i got this strange result .
Why is that happened? Can i staking UNO board + Ethernet shield + GSM shield?

AT%13%
0 9>AT%13%%13%%10%OK%13%%10%
AT+CGREG?%13%
9 40>AT+CGREG?%13%%13%%10%+CGREG: 0,2%13%%10%%13%%10%OK%13%%10%
AT+CGREG?%13%
40 71>AT+CGREG?%13%%13%%10%+CGREG: 0,2%13%%10%%13%%10%OK%13%%10%
AT+CGREG?%13%
85 116>AT+CGREG?%13%%13%%10%+CGREG: 0,1%13%%10%%13%%10%OK%13%%10%
AT+IFC=1,1%13%
116 5>AT+IFC=1,1%13%%13%%10%OK%13%%10%
AT+CMGF=1%13%
5 21>AT+CMGF=1%13%%13%%10%OK%13%%10%
AT+CLIP=1%13%
21 37>AT+CLIP=1%13%%13%%10%OK%13%%10%
ATE0%13%
37 48>ATE0%13%%13%%10%OK%13%%10%
AT+COLP=1%13%
48 54>%13%%10%OK%13%%10%
GSM initialized
connecting...connected
test|01632403cè

You did not mention a GSM shield in your original post. Which GSM shield are you using? Post a link to that hardware.

SurferTim:
You did not mention a GSM shield in your original post. Which GSM shield are you using? Post a link to that hardware.

My appology for that. :slight_smile: I bought this model. Please see the attached link below.

I wonder how come my result become like that.

GSM initialized
connecting...connected
t
a{apaya»0

The printed result suppose to be
test|0163240228|test|0163240228|test|

How are you powering all that? Could be a power brownout or fail when the GSM shield transmits.

This is from page 18 of the GSM module.

It must be able to provide sufficient current in a transmitting burst which typically rises to 2A.

The ethernet shield is no lightweight when it is running. It takes over 100ma.

SurferTim:
How are you powering all that? Could be a power brownout or fail when the GSM shield transmits.

This is from page 18 of the GSM module.
http://arduino.cc/en/uploads/Main/Quectel_M10_datasheet.pdf

It must be able to provide sufficient current in a transmitting burst which typically rises to 2A.

The ethernet shield is no lightweight when it is running. It takes over 100ma.

Hi Surfer Tim,

THanks for the information.
I am connecting my arduino with USB cable plug to PC. Should i supply additional power source 5V and 4A and try it?

Now I encountered this scenario:
Scenario A:
Arduino UNO + Arduino ethernet shield + arduino GSM shield.

The suppose printed result :
test|0163240338|test|0163240338|test|

I managed get the txtKey value grab from website and debug printed below
txtKey = "test|0163240338|test|0163240338|test|";

But when i passed txtKey in splitString function. It printed empty value. It is working perfect when i switch to Arduino mega board.. Any reason why it doesnt worked for Arduino UNO R3?

value1 = splitString(txtKey,'|',0);

String splitString(String s, char parser, int index){
String rs="\0";
// int parserIndex = index;
int parserCnt=0;
int rFromIndex=0, rToIndex=-1;

while(index>=parserCnt){
rFromIndex = rToIndex+1;
rToIndex = s.indexOf(parser,rFromIndex);

if(index == parserCnt){
if(rToIndex == 0 || rToIndex == -1){
return "\0";
}
return s.substring(rFromIndex,rToIndex);
}
else{
parserCnt++;
}

}

delay(200);

return rs;
}

The Actual result:

I don't know about the external power. Have you tried the GSM shield without the ethernet shield connected?

I do know that most usb ports will provide a maximum of 500ma.

I don't know about using the Mega setting. That sounds like a bad idea to me. Maybe you are running out of SRAM in the Uno?

SurferTim:
I don't know about the external power. Have you tried the GSM shield without the ethernet shield connected?

I do know that most usb ports will provide a maximum of 500ma.

I don't know about using the Mega setting. That sounds like a bad idea to me. Maybe you are running out of SRAM in the Uno?

I had checked the sketch size hit 87% and memory usage hit 77% for UNO.
I had try external power source but still the same result.
The Gsm shield can send message out it just that i cannot get the get / print the value i wanted .
Any idea? I had spent whole day but donno why i cannot get the actual result printed. =(

yoonpoh:

SurferTim:
I don't know about the external power. Have you tried the GSM shield without the ethernet shield connected?

I do know that most usb ports will provide a maximum of 500ma.

I don't know about using the Mega setting. That sounds like a bad idea to me. Maybe you are running out of SRAM in the Uno?

I had checked the sketch size hit 87% and memory usage hit 77% for UNO.
I had try external power source but still the same result.
The Gsm shield can send message out it just that i cannot get the get / print the value i wanted .
Any idea? I had spent whole day but donno why i cannot get the actual result printed. =(

Hi SurferTim ,

I figure out the problem, I guess memory cannot hit more than 70% or else it will printed the inaccurate result. Again thanks for the great information and support. XD