Go Down

Topic: How to get substrings from char pointer array (Read 3 times) previous topic - next topic

winkleink

I'm using the ethercard library (https://github.com/jcw/ethercard) to set the Arduino up as a web client so I can read data from a website.
All this is working correctly and the demo webClient sketch (https://github.com/jcw/ethercard/blob/master/examples/webClient/webClient.ino) works perfectly.
I have even change the web site and page to be access and the correct output is sent to the serial monitor.

My problem is that my knowledge of C and pointers is very limited.

I know that Ethernet::buffer is an array of bytes using pointers.

What I am trying to do is read the Google XML weather data from http://www.google.com//ig/api?weather=London
Extract the current temp_c, humidity, icon and wind_condition data and use them to create an output.

Where I am failing is how to parsing a char.  Any advice or pointers (sorry for pun) on how to do this would be appreciated.

Below is the webclient code I am working from

// Demo using DHCP and DNS to perform a web client request.
// 2011-06-08 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php

#include <EtherCard.h>

// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

byte Ethernet::buffer[700];
static uint32_t timer;

char website[] PROGMEM = "www.google.com";

// called when the client request is complete
static void my_callback (byte status, word off, word len) {
  Serial.println(">>>");
  Ethernet::buffer[off+300] = 0;
  Serial.print((const char*) Ethernet::buffer + off);
  Serial.println("...");
}

void setup () {
  Serial.begin(57600);
  Serial.println("\n[webClient]");

  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
    Serial.println( "Failed to access Ethernet controller");
  if (!ether.dhcpSetup())
    Serial.println("DHCP failed");

  ether.printIp("IP: ", ether.myip);
  ether.printIp("GW: ", ether.gwip);
  ether.printIp("DNS: ", ether.dnsip);

  if (!ether.dnsLookup(website))
    Serial.println("DNS failed");
   
  ether.printIp("SRV: ", ether.hisip);
}

void loop () {
  ether.packetLoop(ether.packetReceive());
 
  if (millis() > timer) {
    timer = millis() + 5000;
    Serial.println();
    Serial.print("<<< REQ ");
    ether.browseUrl(PSTR("/foo/"), "bar", website, my_callback);
  }
}

dc42

Please use the # button when posting code, it makes it easier to read.

I found a library for parsing XML on arduino at http://john.crouchley.com/blog/archives/454. Alternatively, you can search for strings and characters inside a null terminated string using the strstr and strchr functions from the standard C library.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Dominic_Connor


pravas

I generally use strstr() function:
char buffer[]="+CBC: 0,99,4190" ; // this is a return string by GSM modem  0,charge level, millivolt
char *ptr;  //temp pointer variable for pointer artihmetic
char *fieldPtr; //temp pointer  variable for pointer artihmetic
:
:
:
:
ptr=strstr(buffer,"+CBC:"); // if substring found in buffer it return address ofstart character of matched string
                      // if not matched it returned NULL
if(ptr!=NULL)
{
   ptr=strstr(ptr,",");
   Serial.println(ptr);         // will print :",99,4190"
                       // you can use pointer arithmetic to get the field
   while(ptr!=NULL)
   {
    ptr++;
    fieldPtr=ptr;
    ptr=strstr(ptr,",");
    *ptr='\0';
    Serial.println(fieldPtr);
   }
}
--------Output---------------------------------
,99,4190
99
4190

winkleink


I generally use strstr() function:
char buffer[]="+CBC: 0,99,4190" ; // this is a return string by GSM modem  0,charge level, millivolt
char *ptr;  //temp pointer variable for pointer artihmetic
char *fieldPtr; //temp pointer  variable for pointer artihmetic
:
:
:
:
ptr=strstr(buffer,"+CBC:"); // if substring found in buffer it return address ofstart character of matched string
                      // if not matched it returned NULL
if(ptr!=NULL)
{
   ptr=strstr(ptr,",");
   Serial.println(ptr);         // will print :",99,4190"
                       // you can use pointer arithmetic to get the field
   while(ptr!=NULL)
   {
    ptr++;
    fieldPtr=ptr;
    ptr=strstr(ptr,",");
    *ptr='\0';
    Serial.println(fieldPtr);
   }
}
--------Output---------------------------------
,99,4190
99
4190



pravas - Thanks.
This looks like the kind of thing I'm trying to do but for a web page rather than GPS.
I think some experimenting over the weekend will see if I understand what you are saying.

Albert.


Go Up