Displaying connectivity

30 May 2010

I'm trying to display on an LED simply if my mbed can connect to google.com or not, and loop every 30 seconds or so.

I've got it connecting ok, and I'm displaying the source on my terminal as per the purehttpc example. (I tried the simplehttpc example, but it couldn't find "ipv4addr" - I think the library has been changed and the example not updated?)

I just don't understand how to use the lwip library to get whether it has connected successfully or not - there are lots of variables and pointers and structures and my head hurts.

Can anyone give me somewhere to start?

30 May 2010

Hi Alex,
try this:

 

struct ip_addr  ipa;
IP4_ADDR(&ipa,209,85,229,99);

 

30 May 2010

Hi Mindaugas,

Thanks for that - I'm sure that'll fix the simplehttpc example, but I still don't know how to apply it to my use.

Any ideas?

Alex

02 Jul 2010

I want to know three things - if the device is physically connected to my router, if it can connect to my router and get an IP address, and if it can connect to a remote server on the internet. This is shown with ledLink, led2 and led3.

Here's what I've got so far:

 

#include "mbed.h"
#include "EthernetNetIf.h"
#include "HTTPClient.h"

Ethernet link;
HTTPClient http;
EthernetNetIf eth; 

Ticker linkcheck;

Serial pc(USBTX, USBRX);    // debugging

PwmOut led2 (LED2); //router assign ip
PwmOut led3 (LED3); //internet connect

DigitalOut ledLink(p30);
DigitalOut ledActivity(p29);

bool connected; //physically connected

void linkCheck(){

   connected = link.link();
   ledLink = connected;

}

int main() {
    
    linkcheck.attach(&linkCheck,1);
    
    int ethErrorCount = 0;
    int textErrorCount = 0;
    
    while(1) {
        
        //if physically connected
        if(connected){
        
            //try to connect to dhcp
            EthernetErr ethErr = eth.setup(5000); //5 second timeout
            
            //on error
            if(ethErr){
                led2 = 0;
                                
            } else { //dhcp ok
                led2 = 1;
            
				//continue with downloading txt file
                HTTPText txt;
                
                http.setTimeout(5000);
                                
                HTTPResult r = http.get("http://mbed.org/media/uploads/donatien/hello.txt", &txt);
            
                if(r!=HTTP_OK){
					led3 = 0

                } else {
                    //downloaded file ok
                    led3 = 1; 
                }
            }
        }
    
        wait(1);
   
        led4 = 1;
        wait(0.2);
        led4 = 0;
        wait(0.2);

    }
}

The first loop goes fine - it gets an IP and downloads the "Hello World" text file ok. The second loop around it gets stuck on "DHCP Started, waiting for IP...", and doesn't even time out. It sends a flood of connections to my router, which gets overwhelmed and shuts down.

 

Is there a way to remove/drop/disconnect from eth, so setup will work again? Or is there a way to check if it's still connected ok?

How can I get my statusLed to show activity?

08 Jul 2010

Shameless self-bump. Anyone?

08 Jul 2010

Hi,

I'm a noobie to C++ and ethernet but am working on a somewhat similar issue.  Should you be deleting the objects at the end of each loop so you don't keep creating them since they don't appear to go out of scope?  If so, you should create your objects through the use of pointers which places them on the heap so they can be deleted at will:

As a global:

                  EthernetErr * pEthErr;

At the beginning of the loop:

                 pEthErr = new EthernetErr;

At the end of the loop:

               delete pEthErr;

 

This should be done for each object created in the loop???