Code for an ethernet-enabled mbed that calls to a web api, and reasons with JSON resonses

Dependencies:   NetServices RPCInterface mbed picojson xbee_api

main.cpp

Committer:
ammanvedi
Date:
2014-01-25
Revision:
0:d956c5c1bdd6
Child:
1:7485d913aaf5

File content as of revision 0:d956c5c1bdd6:

#include "mbed.h"
#include "EthernetNetIf.h"
#include "HTTPClient.h"
#include "NTPClient.h"
#include "HTTPServer.h"
#include "RPCFunction.h"

#define HOSTNAME "HUB-01"

EthernetNetIf eth(HOSTNAME);
HTTPClient http;
NTPClient ntp;



int main() {

   
    printf("Try starting the program with the network disconnected and then connect after a few timeouts reported\n\n");
    EthernetErr ethErr;
    int count = 0;
    do {
        printf("Setting up %d...\n", ++count);
        ethErr = eth.setup();
        if (ethErr) printf("Timeout\n", ethErr);
    } while (ethErr != ETH_OK);

    printf("Connected OK\n");
    const char* hwAddr = eth.getHwAddr();
    printf("HW address : %02x:%02x:%02x:%02x:%02x:%02x\n",
           hwAddr[0], hwAddr[1], hwAddr[2],
           hwAddr[3], hwAddr[4], hwAddr[5]);

    IpAddr ethIp = eth.getIp();
    printf("IP address : %d.%d.%d.%d\n", ethIp[0], ethIp[1], ethIp[2], ethIp[3]);
    printf("Check router DHCP table for name : %s\n", eth.getHostname());

    printf("\nHTTPClient get...\n");
    HTTPText txt;
    HTTPResult r = http.get("192.168.0.4:3000/devices", &txt);
    if (r==HTTP_OK) {
        printf("Result ok : %s\n", txt.gets());
    } else {
        printf("Error %d\n", r);
    }


}