To access a web service using SAME70Xplained

Go To Last Post
14 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi 

 

I am working on a project to access data from a web service using  SAME70XPlained board. I am building my code using THIRDPARTY LWIP RAW BASIC HTTP EXAMPLE to set up static IP ethernet connection. That I have successfully received. For the next step to access a web service,I researched on internet about GET and POST method requests that can retrieve and post the data on a web server.

 

In my code I want to access this currency converter web service(http://currencyconverter.kowabunga.net/converter.asmx/GetCurrencyRate?Currency=CAD&RateDate=2019-02-25). So that I can pass values to its parameters like Currency=CAD and Rate Date=2019-02-05 and can get my output value as following on my web page:-

 

This XML file does not appear to have any style information associated with it. The document tree is shown below.

<decimal xmlns="http://tempuri.org/">1.4924</decimal>

 

Following are the doubts that I have:-

 

1. How can I access to another web service if my Atmel board is acting like a web server?

2. How to use POST method request in my code?

3. Is the httpserver code enough or Is there a different httpclient code that I need to implement?

 

I would really appreciate any help.

 

Last Edited: Fri. Mar 8, 2019 - 08:17 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You need a webclient! You can do this by using a plain tcp socket. What the httpclient code does is append/remove the http headers from the raw socket data. For the most part the header data is fixed, so you can create a string with the required data. How to do this? Http is well documented, so google. Or you can use tools like wireshark to look at raw data and curl to generate the request.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for your reply.

I followed your suggestions but I am still not clear on how can I post data to a web server and then get a reply back.

 

1. as Http client needs connect function to attach a socket with server's IP address. Now my question is:who is my server? Atmel board's static IP address or currencyconverter.kowabunga.net ?

2. Is the following code correct for the http initialization?

 

void httpd_init(void)
{
  
    struct tcp_pcb *client_pcb;
    struct ip_addr remoteIP;
    IP4_ADDR(&remoteIP, ETHERNET_CONF_REMOTE_IPADDR0, ETHERNET_CONF_REMOTE_IPADDR1,
    ETHERNET_CONF_REMOTE_IPADDR2, ETHERNET_CONF_REMOTE_IPADDR3);

    client_pcb= tcp_new();

    tcp_connect(client_pcb,&remoteIP,80,http_request);

}

 

3. Also please see if the http request function is missing information.

 

static err_t http_request(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
    char *string="POST /converter.asmx/GetCurrencyRate HTTP/1.1\r\nHost: currencyconverter.kowabunga.net\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 32\r\n\r\nCurrency=CAD&RateDate=2019-02-25\r\n\r\n";
    
    tcp_write(pcb, string, strlen(string), TCP_WRITE_FLAG_COPY);
     /* now send */
    tcp_output(pcb);
    tcp_recv(pcb, http_recv);

    return ERR_OK;  
}

 

 

 

If you can provide me reference to an example that is posting data to server and displaying results on a web browser that would be great. As I have spent weeks to search a post example code using atmel board and I couldn't find something relevant.

 

Thanks in advance.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

1. the server ip is www.currencyconverter.kowabunga.net

2. I have no idea. 

 

Apart from the tcp socket stuff, the rest is not specific to Atmel. I'd suggest you test your methods using python or node.js. Once you'ce got the finer points of talking to a webserver under control, then you can transfer this knowlege to the Atmel board.

 

Tools such as curl and wireshark are invaluable.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the reply.

I am still not able to set up connection with server  www.currencyconverter.kowabunga.net .

As TCP connect function tcp_connect(pcb, &resolvedIP, 80, hc_connected) requires a resolved IP address of web server. 

I want to use DNS server that can resolve IP address from a host name.

 

   dns_gethostbyname("www.currencyconverter.kowabunga.net", &resolvedIP, NULL, NULL);

 

When I use above function to get a IP address from DNS server it returns the Atmel board static IP address. 

 

The DNS server address that I am using is the one that I got from typing ipconfig /all command in command prompt of my pc

#define DNS_SERVER_ADDRESS(ipaddr)        (ip4_addr_set_u32(ipaddr, ipaddr_addr("10.28.16.5"))) /* windows DNS */

 

The default DNS server address that was given in atmel code was the following:

 

#define DNS_SERVER_ADDRESS(ipaddr)        (ip4_addr_set_u32(ipaddr, ipaddr_addr("208.67.222.222"))) /* resolver1.opendns.com */

 

What DNS server address should I use?

Please help if you have information on how to resolve IP address from a host name using DNS.

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I resolved the IP Address from a host name using DNS server 8.8.8.8 address.

It is successfully returning IP address of any host name entered.

 

Thankssmiley

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Hi

 

Now after resolving the IP address of web server, I am using that in tcp_connect function to set up a connection between web server and atmel board (client).

But the response from the web server that I received is server unavailable error 503.

 

I tried to access websites like google.com and I got the same server error response in my code.

 

Please help me if you know how can I resolve this server error.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Looks like you’ve hit a firewall. Is there something in the network you’re using?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for reply.

 

I am using my company's LAN connection. Windows firewall settings are enabled and being managed by third party software.

 

What should I do in my code to connect with web server address if firewall is enabled?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Speak to your company’s IT department. The 503 response tells you all you need to know - why did you not follow that?
You can try to fix it in your code or play with your Windows firewall settings but the problem is clearly not there.
You might be able to convince your IT people to give you a clear pipe to the outside world but I wouldn’t hold your breath. I had similar issues, so I just made my own network with a 3g router to the real world.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the reply Kartman. 

 

End result is, I have to access an internal web server so hopefully then I won't have firewall issue. 

With other external websites my connection is successful. So I will proceed further when I get that internal web server up and running.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It could be your corporate web access is via a proxy. The proxy determines if the website you want to access is allowed. Speak to your IT people about this.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you for your help Kartman.

 

I got my code working now. I am able to set up a successful connection with internal server and getting 200 OK response back. 

As destination URL is within internal network of my company, I don't need proxy server or firewall changes.

So code is able to set up a direct connection with internal destination IP address.

 

I really appreciate your time and consideration.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Excellent! You might want to look at using mongoose for your networking.
https://cesanta.com/

I’ve found it useful.