Uploading sensor data (voltage divider, MAX4172, INA219) over Ethernet to Thing Speak service. Uses old mbed revision that is compatible with NetServices library. I2C communication is made with I2CR library.

Dependencies:   C12832 I2CR INA219 NetServices mbed

Fork of NetServices_HelloWorld by Segundo Equipo

Committer:
segundo
Date:
Fri Nov 19 21:54:02 2010 +0000
Revision:
2:16857d9ab50d
Parent:
1:57f922fe8fb5
Child:
3:aeaa9fa8cda0

        

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 2:16857d9ab50d 30 printf("\n\n/* NetServices tribute 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 2:16857d9ab50d 33 do {
segundo 2:16857d9ab50d 34 printf("Setting up...\n");
segundo 2:16857d9ab50d 35 ethErr = eth.setup();
segundo 2:16857d9ab50d 36 if (ethErr) printf("Timeout\n", ethErr);
segundo 2:16857d9ab50d 37 } while (ethErr != ETH_OK);
segundo 2:16857d9ab50d 38
segundo 2:16857d9ab50d 39 printf("Connected OK\n");
segundo 2:16857d9ab50d 40 const char* hwAddr = eth.getHwAddr();
segundo 2:16857d9ab50d 41 printf("HW address : %02x:%02x:%02x:%02x:%02x:%02x\n",
segundo 2:16857d9ab50d 42 hwAddr[0], hwAddr[1], hwAddr[2],
segundo 2:16857d9ab50d 43 hwAddr[3], hwAddr[4], hwAddr[5]);
segundo 0:2419d81ee03d 44
segundo 1:57f922fe8fb5 45 IpAddr ethIp = eth.getIp();
segundo 2:16857d9ab50d 46 printf("IP address : %d.%d.%d.%d\n", ethIp[0], ethIp[1], ethIp[2], ethIp[3]);
segundo 2:16857d9ab50d 47 printf("Check router DHCP table for name : %s\n", HOSTNAME);
segundo 0:2419d81ee03d 48
segundo 1:57f922fe8fb5 49 printf("\nHTTPClient get...\n");
segundo 0:2419d81ee03d 50 HTTPText txt;
segundo 0:2419d81ee03d 51 HTTPResult r = http.get("http://mbed.org/media/uploads/donatien/hello.txt", &txt);
segundo 0:2419d81ee03d 52 if (r==HTTP_OK) {
segundo 0:2419d81ee03d 53 printf("Result ok : %s\n", txt.gets());
segundo 0:2419d81ee03d 54 } else {
segundo 0:2419d81ee03d 55 printf("Error %d\n", r);
segundo 0:2419d81ee03d 56 }
segundo 0:2419d81ee03d 57
segundo 1:57f922fe8fb5 58 time_t ctTime;
segundo 1:57f922fe8fb5 59 ctTime = time(NULL);
segundo 1:57f922fe8fb5 60 printf("\nCurrent time is (UTC): %d %s\n", ctTime, ctime(&ctTime));
segundo 1:57f922fe8fb5 61 printf("NTP setTime...\n");
segundo 1:57f922fe8fb5 62 Host server(IpAddr(), 123, "pool.ntp.org");
segundo 1:57f922fe8fb5 63 printf("Result : %d\n", ntp.setTime(server));
segundo 1:57f922fe8fb5 64
segundo 1:57f922fe8fb5 65 ctTime = time(NULL);
segundo 1:57f922fe8fb5 66 printf("\nTime is now (UTC): %d %s\n", ctTime, ctime(&ctTime));
segundo 1:57f922fe8fb5 67
segundo 2:16857d9ab50d 68 printf("NTP setTime to a deliberately incorrect server...\n");
segundo 2:16857d9ab50d 69 server.setName("www.google.com");
segundo 2:16857d9ab50d 70 printf("Result : %d\n", ntp.setTime(server));
segundo 2:16857d9ab50d 71
segundo 2:16857d9ab50d 72 ctTime = time(NULL);
segundo 2:16857d9ab50d 73 printf("\nTime should still be correct (UTC): %d %s\n", ctTime, ctime(&ctTime));
segundo 2:16857d9ab50d 74
segundo 2:16857d9ab50d 75 char ip[16];
segundo 2:16857d9ab50d 76 sprintf(ip, "%d.%d.%d.%d", ethIp[0], ethIp[1], ethIp[2], ethIp[3]);
segundo 2:16857d9ab50d 77
segundo 2:16857d9ab50d 78 #define PORT 80
segundo 2:16857d9ab50d 79 svr.addHandler<RPCHandler>("/rpc");
segundo 2:16857d9ab50d 80 svr.bind(PORT);
segundo 2:16857d9ab50d 81 printf("Server listening (port %d)\n", PORT);
segundo 2:16857d9ab50d 82 printf("- LED1 should flash\n");
segundo 2:16857d9ab50d 83 printf("- type into browser to test url decode : %s/rpc/echo/run \"quoted string with <>\"\n", ip);
segundo 2:16857d9ab50d 84
segundo 2:16857d9ab50d 85 Timer tm;
segundo 2:16857d9ab50d 86 tm.start();
segundo 2:16857d9ab50d 87
segundo 2:16857d9ab50d 88 while (true) {
segundo 2:16857d9ab50d 89 Net::poll();
segundo 2:16857d9ab50d 90 if (tm.read() > 0.5) {
segundo 2:16857d9ab50d 91 led1 = !led1;
segundo 2:16857d9ab50d 92 tm.start();
segundo 2:16857d9ab50d 93 }
segundo 2:16857d9ab50d 94 }
segundo 0:2419d81ee03d 95 }