HTTP/Ethernet experiments all just hang :( can anyone help :)

30 May 2012

After reading the forum post "Stumped on HTTP client..." http://mbed.org/forum/mbed/topic/3546/ the answer might be "wait for the official networking support from mbed" but I thought I would post in case someone can offer the vital clue to fix my issues ;)

I'm really excited about the internet of things and want to connect my mbed to the web for pachube (now cosm) and NTP. I've bought a TMP102 and got it working with printf within minutes with the excellent apis available here, and I've also been experimenting with a LED matrix display. Next task is to make it internet connected.

For ethernet, I've tried some of the examples out there already on mbed but all seem to do the following: - setup ethernet - get DHCP address OK (and appear on my router IP list) - try and post or get something from the web but just hang, no timeout, no error, nothing.

The successful DHCP should indicate my ethernet breakout is wired ok.

Is there a way to enable extra debug messages to understand why this is hanging?

The device is a LPC2368.

The code I've tried include: - NTP Client: http://mbed.org/cookbook/NTP-Client

printf output:

Quote:

==============================
Start
Setting up ethernet...
[..\fwk\if\eth\EthernetNetIf.cpp:setup@86] HW Addr is : e2:10:31:a9:d7:2f.
[..\fwk\if\eth\EthernetNetIf.cpp:setup@99] DHCP Started, waiting for IP...
[..\fwk\if\eth\EthernetNetIf.cpp:setup@142] Connected, IP : 192.168.0.150
Setup ethernet OK
Reset time to NULL
Current time is (UTC): Thu Jan  1 00:12:26 1970

Access NTP...

From this code:


#include "mbed.h"
#include "EthernetNetIf.h"
#include "NTPClient.h"

EthernetNetIf eth; 
NTPClient ntp;
  
int main() {

  printf("==============================\n");
  printf("Start\n");

  printf("Setting up ethernet...\n");
  EthernetErr ethErr = eth.setup();
  if(ethErr)
  {
    printf("Error %d in setup.\n", ethErr);
    return -1;
  }
  printf("Setup ethernet OK\n");

  printf("Reset time to NULL\n");
  time_t ctTime;
  ctTime = time(NULL);  
  
  printf("Current time is (UTC): %s\n", ctime(&ctTime));  

  printf("Access NTP...\n");
    
  Host server(IpAddr(), 123, "0.uk.pool.ntp.org");
  
  NTPResult NTPerror;
  //Read time from NTP server
  NTPerror = ntp.setTime(server);

  printf("Result...\n");
  if (NTPerror == NTP_OK) printf("NTP - Time set");
  else { //an NTPerror, but which one
      switch (NTPerror) {
          case NTP_TIMEOUT:
              printf("NTP - timeout\n");
              break;
          case NTP_DNS:
              printf("NTP - DNS error\n");
              break;
          case NTP_PRTCL:
              printf("NTP - Protocol error\n");
              break;
          default:
              printf("Unknown error\n");
      }
      printf("END.\n");
      return -1;
  }

    
  ctTime = time(NULL);  
  printf("\nTime is now (UTC): %s\n", ctime(&ctTime)); 
  
  while(1)
  {
    printf("\nTime is now (UTC): %s\n", ctime(&ctTime)); 
    wait(5);
  }
  
}

The other one I tried was this pachube example (with my own api key and feedid, and pachube replaced with 'cosm'. Output:

Quote:

Setting up...

Setup OK
Json: {   "title":"TITLE HERE", "version":"1.0.0",  "datastreams":[    { "id":"STREAM ID", "current_value":"9"},    { "id":"STREAM ID", "current_value":"67"}  ]}
client.post HTTPResult...

Here's the relevant section of the code from this example: http://mbed.org/users/takashikojo/programs/Pachube-v2_MyFeed/m0l93r/docs/main_8cpp_source.html

..cut..
   // for authentication, API key is set in client header
    HTTPClient client;
    client.setRequestHeader("X-PachubeApiKey", apiKey);

    // text object holds data to be posted
    HTTPText jsonContent("text/json");
    
    while (1) {
    
        sprintf(content, contentTemplate, rand()%40, rand()%100) ;
        printf("Json: %s\n", content) ;
        jsonContent.set(content) ;

        // uri for post includes feed ID
        string uri = "http://api.cosm.com/v2/feeds/" + environmentID + ".json?_method=put";

        printf("client.post HTTPResult...\n");
        // result should be 0 and response should be 200 for successful post
        HTTPResult result = client.post(uri.c_str(), jsonContent, NULL);

        if (result==HTTP_OK) {
            printf(" OK: \"%s\"\n", txt.gets());
        } else {
            printf(" Error: %d\n", result);
        }
..cut...

Am I missing something fundamental here? I've tried searching through mbed posts and comments etc but not solved my issue :) I've also tried playing with my router to put the mbed ethernet address on DMZ in case of firewall issues.

If anyone can give any pointers to help me solve this I'll be very happy!

Thanks!

Neil.

31 May 2012

have you run one of the demos successfully before your own program? ahead of your "cut...", is there a DHCP or LAN IP setup?

I have found a hangup when I use netServices and client.get with HTTPText.

Lots of folks here say hat NetServices library is quite buggy. Some have shown that web client and web server work fine if you don't use that library. This means more code. There are some examples of doing so.

31 May 2012

You need to be periodically calling Net::Poll() to "pump" the ethernet traffic processing.

It's not very obvious from the examples, but if you look, it's always there somewhere.

I often set up a Ticker that just calls it every 0.1 seconds or so.

01 Jun 2012

Steve - no, the demos don't run. The only changes I've made are to add extra printf's to see where it's locking up. Are there any other debug settings you can enable to get the libraries to printf more info about what they are doing?

Dave - do the demos like the http://mbed.org/cookbook/NTP-Client work for you out of the box or did you need to add the poll you mention?

The main thing I don't understand is why there is not even a time out happening in either case.

Thanks!

Neil.

01 Jun 2012

Neil, I have the ethernet connector removed from my mbed breadboard at the moment, so I can't test that code.

Try putting these lines in your main anywhere early on:

Ticker tkr;
tkr.attach(Net::poll, 0.05);
01 Jun 2012

Some chance that router on the LAN is blocking outgoing ports to NIST (or other) time server? Assuming DHCP succeeded - meaning the LAN connectivity is OK, etc.

Sometimes the first NTP attempt, on any computer, fails and a different server needs to be tried.

01 Jun 2012

Install Wireshark on your PC and see what's happening.

01 Jun 2012

agilent mbed wrote:

Neil, I have the ethernet connector removed from my mbed breadboard at the moment, so I can't test that code.

Try putting these lines in your main anywhere early on:

Ticker tkr;
tkr.attach(Net::poll, 0.05);

Dave - I tried this for teh NTP and pachube demos but no luck. I tried placing it in a few different places, e.g. at the start and after the DHCP setup but no difference.

I'll give wireshark a go, however if my laptop is on wifi and the mbed connected direct to the router would I see the traffic between mbed and router? (e.g. nothing except broadcast packets?)

Steve - it's possible, hence I tried placing it on the DMZ though it's possible that doesn't work with a DHCP device.

Are there other ethernet demos I should be trying?

Thanks for the ideas so far!

01 Jun 2012

Just tried the ethernet tester: http://mbed.org/users/no2chem/programs/EthernetTester/6095m and it works great! I can connect from my laptop to the simple webserver on the mbed. Clearly everything in the mbed interface must be working. Maybe there's something strange with my router and virgin media broadband connection. Anyone else suffer similar issues?

Neil.