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?
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?