Dependencies:   NetServices mbed

Committer:
segundo
Date:
Wed Dec 15 18:03:19 2010 +0000
Revision:
5:1e77bdd1a639
Parent:
4:26f346102b1a

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
segundo 0:2419d81ee03d 1 #include "mbed.h"
segundo 0:2419d81ee03d 2 #include "EthernetNetIf.h"
segundo 0:2419d81ee03d 3 #include "HTTPClient.h"
segundo 1:57f922fe8fb5 4 #include "NTPClient.h"
segundo 2:16857d9ab50d 5 #include "HTTPServer.h"
segundo 2:16857d9ab50d 6 #include "RPCFunction.h"
segundo 0:2419d81ee03d 7
segundo 2:16857d9ab50d 8 #define HOSTNAME "mbedSE"
segundo 2:16857d9ab50d 9
segundo 2:16857d9ab50d 10 EthernetNetIf eth(HOSTNAME);
segundo 0:2419d81ee03d 11 HTTPClient http;
segundo 1:57f922fe8fb5 12 NTPClient ntp;
segundo 2:16857d9ab50d 13 HTTPServer svr;
segundo 2:16857d9ab50d 14
segundo 2:16857d9ab50d 15 DigitalOut led1(LED1, "led1");
segundo 2:16857d9ab50d 16 DigitalOut led2(LED2, "led2");
segundo 2:16857d9ab50d 17 DigitalOut led3(LED3, "led3");
segundo 2:16857d9ab50d 18 DigitalOut led4(LED4, "led4");
segundo 2:16857d9ab50d 19
segundo 2:16857d9ab50d 20 void DoEcho(char* input, char* output);
segundo 2:16857d9ab50d 21 RPCFunction Echo(&DoEcho, "echo");
segundo 2:16857d9ab50d 22
segundo 2:16857d9ab50d 23 void DoEcho(char* input, char* output) {
segundo 2:16857d9ab50d 24 printf("%s\n", input);
segundo 2:16857d9ab50d 25 strcpy(output, input);
segundo 2:16857d9ab50d 26 }
segundo 0:2419d81ee03d 27
segundo 0:2419d81ee03d 28 int main() {
segundo 0:2419d81ee03d 29
segundo 4:26f346102b1a 30 printf("\n\n/* Segundo Equipo NetServices library demonstration */\n");
segundo 2:16857d9ab50d 31 printf("Try starting the program with the network disconnected and then connect after a few timeouts reported\n\n");
segundo 2:16857d9ab50d 32 EthernetErr ethErr;
segundo 3:aeaa9fa8cda0 33 int count = 0;
segundo 2:16857d9ab50d 34 do {
segundo 3:aeaa9fa8cda0 35 printf("Setting up %d...\n", ++count);
segundo 2:16857d9ab50d 36 ethErr = eth.setup();
segundo 2:16857d9ab50d 37 if (ethErr) printf("Timeout\n", ethErr);
segundo 2:16857d9ab50d 38 } while (ethErr != ETH_OK);
segundo 2:16857d9ab50d 39
segundo 2:16857d9ab50d 40 printf("Connected OK\n");
segundo 2:16857d9ab50d 41 const char* hwAddr = eth.getHwAddr();
segundo 2:16857d9ab50d 42 printf("HW address : %02x:%02x:%02x:%02x:%02x:%02x\n",
segundo 2:16857d9ab50d 43 hwAddr[0], hwAddr[1], hwAddr[2],
segundo 2:16857d9ab50d 44 hwAddr[3], hwAddr[4], hwAddr[5]);
segundo 0:2419d81ee03d 45
segundo 1:57f922fe8fb5 46 IpAddr ethIp = eth.getIp();
segundo 2:16857d9ab50d 47 printf("IP address : %d.%d.%d.%d\n", ethIp[0], ethIp[1], ethIp[2], ethIp[3]);
segundo 5:1e77bdd1a639 48 printf("Check router DHCP table for name : %s\n", eth.getHostname());
segundo 0:2419d81ee03d 49
segundo 1:57f922fe8fb5 50 printf("\nHTTPClient get...\n");
segundo 0:2419d81ee03d 51 HTTPText txt;
segundo 0:2419d81ee03d 52 HTTPResult r = http.get("http://mbed.org/media/uploads/donatien/hello.txt", &txt);
segundo 0:2419d81ee03d 53 if (r==HTTP_OK) {
segundo 0:2419d81ee03d 54 printf("Result ok : %s\n", txt.gets());
segundo 0:2419d81ee03d 55 } else {
segundo 0:2419d81ee03d 56 printf("Error %d\n", r);
segundo 0:2419d81ee03d 57 }
segundo 0:2419d81ee03d 58
segundo 1:57f922fe8fb5 59 time_t ctTime;
segundo 1:57f922fe8fb5 60 ctTime = time(NULL);
segundo 1:57f922fe8fb5 61 printf("\nCurrent time is (UTC): %d %s\n", ctTime, ctime(&ctTime));
segundo 1:57f922fe8fb5 62 printf("NTP setTime...\n");
segundo 1:57f922fe8fb5 63 Host server(IpAddr(), 123, "pool.ntp.org");
segundo 1:57f922fe8fb5 64 printf("Result : %d\n", ntp.setTime(server));
segundo 1:57f922fe8fb5 65
segundo 1:57f922fe8fb5 66 ctTime = time(NULL);
segundo 1:57f922fe8fb5 67 printf("\nTime is now (UTC): %d %s\n", ctTime, ctime(&ctTime));
segundo 1:57f922fe8fb5 68
segundo 2:16857d9ab50d 69 printf("NTP setTime to a deliberately incorrect server...\n");
segundo 2:16857d9ab50d 70 server.setName("www.google.com");
segundo 2:16857d9ab50d 71 printf("Result : %d\n", ntp.setTime(server));
segundo 2:16857d9ab50d 72
segundo 2:16857d9ab50d 73 ctTime = time(NULL);
segundo 2:16857d9ab50d 74 printf("\nTime should still be correct (UTC): %d %s\n", ctTime, ctime(&ctTime));
segundo 2:16857d9ab50d 75
segundo 2:16857d9ab50d 76 char ip[16];
segundo 2:16857d9ab50d 77 sprintf(ip, "%d.%d.%d.%d", ethIp[0], ethIp[1], ethIp[2], ethIp[3]);
segundo 2:16857d9ab50d 78
segundo 2:16857d9ab50d 79 #define PORT 80
segundo 2:16857d9ab50d 80 svr.addHandler<RPCHandler>("/rpc");
segundo 2:16857d9ab50d 81 svr.bind(PORT);
segundo 2:16857d9ab50d 82 printf("Server listening (port %d)\n", PORT);
segundo 2:16857d9ab50d 83 printf("- LED1 should flash\n");
segundo 4:26f346102b1a 84 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);
segundo 2:16857d9ab50d 85
segundo 2:16857d9ab50d 86 Timer tm;
segundo 2:16857d9ab50d 87 tm.start();
segundo 2:16857d9ab50d 88
segundo 2:16857d9ab50d 89 while (true) {
segundo 2:16857d9ab50d 90 Net::poll();
segundo 2:16857d9ab50d 91 if (tm.read() > 0.5) {
segundo 2:16857d9ab50d 92 led1 = !led1;
segundo 2:16857d9ab50d 93 tm.start();
segundo 2:16857d9ab50d 94 }
segundo 2:16857d9ab50d 95 }
segundo 0:2419d81ee03d 96 }