Dependencies:   NetServices mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetNetIf.h"
00003 #include "HTTPClient.h"
00004 #include "NTPClient.h"
00005 #include "HTTPServer.h"
00006 #include "RPCFunction.h"
00007 
00008 #define HOSTNAME "mbedSE"
00009 
00010 EthernetNetIf eth(HOSTNAME);
00011 HTTPClient http;
00012 NTPClient ntp;
00013 HTTPServer svr;
00014 
00015 DigitalOut led1(LED1, "led1");
00016 DigitalOut led2(LED2, "led2");
00017 DigitalOut led3(LED3, "led3");
00018 DigitalOut led4(LED4, "led4");
00019 
00020 void DoEcho(char* input, char* output);
00021 RPCFunction Echo(&DoEcho, "echo");
00022 
00023 void DoEcho(char* input, char* output) {
00024     printf("%s\n", input);
00025     strcpy(output, input);
00026 }
00027 
00028 int main() {
00029 
00030     printf("\n\n/* Segundo Equipo NetServices library demonstration */\n");
00031     printf("Try starting the program with the network disconnected and then connect after a few timeouts reported\n\n");
00032     EthernetErr ethErr;
00033     int count = 0;
00034     do {
00035         printf("Setting up %d...\n", ++count);
00036         ethErr = eth.setup();
00037         if (ethErr) printf("Timeout\n", ethErr);
00038     } while (ethErr != ETH_OK);
00039 
00040     printf("Connected OK\n");
00041     const char* hwAddr = eth.getHwAddr();
00042     printf("HW address : %02x:%02x:%02x:%02x:%02x:%02x\n",
00043            hwAddr[0], hwAddr[1], hwAddr[2],
00044            hwAddr[3], hwAddr[4], hwAddr[5]);
00045 
00046     IpAddr ethIp = eth.getIp();
00047     printf("IP address : %d.%d.%d.%d\n", ethIp[0], ethIp[1], ethIp[2], ethIp[3]);
00048     printf("Check router DHCP table for name : %s\n", eth.getHostname());
00049 
00050     printf("\nHTTPClient get...\n");
00051     HTTPText txt;
00052     HTTPResult r = http.get("http://mbed.org/media/uploads/donatien/hello.txt", &txt);
00053     if (r==HTTP_OK) {
00054         printf("Result ok : %s\n", txt.gets());
00055     } else {
00056         printf("Error %d\n", r);
00057     }
00058 
00059     time_t ctTime;
00060     ctTime = time(NULL);
00061     printf("\nCurrent time is (UTC): %d %s\n", ctTime, ctime(&ctTime));
00062     printf("NTP setTime...\n");
00063     Host server(IpAddr(), 123, "pool.ntp.org");
00064     printf("Result : %d\n", ntp.setTime(server));
00065 
00066     ctTime = time(NULL);
00067     printf("\nTime is now (UTC): %d %s\n", ctTime, ctime(&ctTime));
00068 
00069     printf("NTP setTime to a deliberately incorrect server...\n");
00070     server.setName("www.google.com");
00071     printf("Result : %d\n", ntp.setTime(server));
00072 
00073     ctTime = time(NULL);
00074     printf("\nTime should still be correct (UTC): %d %s\n", ctTime, ctime(&ctTime));
00075     
00076     char ip[16];
00077     sprintf(ip, "%d.%d.%d.%d", ethIp[0], ethIp[1], ethIp[2], ethIp[3]);
00078     
00079 #define PORT 80
00080     svr.addHandler<RPCHandler>("/rpc");
00081     svr.bind(PORT);
00082     printf("Server listening (port %d)\n", PORT);
00083     printf("- LED1 should flash\n");
00084     printf("- type into browser to test url decode : %s/rpc/echo/run \"quoted string with <>\" or http://192.168.1.103/rpc/echo/run%%20%%22quoted%%20string%%20with%%20%%3C%%3E%%22\n", ip);
00085 
00086     Timer tm;
00087     tm.start();
00088 
00089     while (true) {
00090         Net::poll();
00091         if (tm.read() > 0.5) {
00092             led1 = !led1;
00093             tm.start();
00094         }
00095     }
00096 }