Leest de waarde van een sensor binnen een maakt deze beschikbaar via internet

Dependencies:   NTPClient_NetServices mbed

Committer:
hendrikvincent
Date:
Mon Dec 02 09:01:23 2013 +0000
Revision:
0:05ccbd4f84f1
eerste programma;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hendrikvincent 0:05ccbd4f84f1 1 #include "mbed.h"
hendrikvincent 0:05ccbd4f84f1 2 #include "EthernetNetIf.h"
hendrikvincent 0:05ccbd4f84f1 3 #include "HTTPServer.h"
hendrikvincent 0:05ccbd4f84f1 4 #include "RPCFunction.h"
hendrikvincent 0:05ccbd4f84f1 5 #include "NTPClient.h"
hendrikvincent 0:05ccbd4f84f1 6
hendrikvincent 0:05ccbd4f84f1 7 EthernetNetIf eth;
hendrikvincent 0:05ccbd4f84f1 8 NTPClient ntp;
hendrikvincent 0:05ccbd4f84f1 9 HTTPServer svr;
hendrikvincent 0:05ccbd4f84f1 10
hendrikvincent 0:05ccbd4f84f1 11 DigitalOut led1(LED1, "led1");
hendrikvincent 0:05ccbd4f84f1 12 DigitalOut led2(LED2, "led2");
hendrikvincent 0:05ccbd4f84f1 13 DigitalOut led3(LED3, "led3");
hendrikvincent 0:05ccbd4f84f1 14 DigitalOut led4(LED4, "led4");
hendrikvincent 0:05ccbd4f84f1 15
hendrikvincent 0:05ccbd4f84f1 16 AnalogIn Sensor(p20); // Sensor in
hendrikvincent 0:05ccbd4f84f1 17 DigitalOut pin15(p15);
hendrikvincent 0:05ccbd4f84f1 18 DigitalOut pin16(p16);
hendrikvincent 0:05ccbd4f84f1 19 DigitalOut pin17(p17);
hendrikvincent 0:05ccbd4f84f1 20 DigitalOut pin18(p18);
hendrikvincent 0:05ccbd4f84f1 21 DigitalOut pin19(p19);
hendrikvincent 0:05ccbd4f84f1 22
hendrikvincent 0:05ccbd4f84f1 23 FILE *file;
hendrikvincent 0:05ccbd4f84f1 24
hendrikvincent 0:05ccbd4f84f1 25 int main()
hendrikvincent 0:05ccbd4f84f1 26 {
hendrikvincent 0:05ccbd4f84f1 27
hendrikvincent 0:05ccbd4f84f1 28 /************** Ethernet Setup *****************/
hendrikvincent 0:05ccbd4f84f1 29 EthernetErr ethErr;
hendrikvincent 0:05ccbd4f84f1 30 int count = 0;
hendrikvincent 0:05ccbd4f84f1 31 do {
hendrikvincent 0:05ccbd4f84f1 32 printf("Setting up %d...\n\r", ++count);
hendrikvincent 0:05ccbd4f84f1 33 ethErr = eth.setup();
hendrikvincent 0:05ccbd4f84f1 34 if (ethErr) printf("Timeout\n\r", ethErr);
hendrikvincent 0:05ccbd4f84f1 35 } while (ethErr != ETH_OK);
hendrikvincent 0:05ccbd4f84f1 36
hendrikvincent 0:05ccbd4f84f1 37 printf("Connected OK\n\r");
hendrikvincent 0:05ccbd4f84f1 38 const char* hwAddr = eth.getHwAddr();
hendrikvincent 0:05ccbd4f84f1 39
hendrikvincent 0:05ccbd4f84f1 40 IpAddr ethIp = eth.getIp();
hendrikvincent 0:05ccbd4f84f1 41 printf("IP address : %d.%d.%d.%d\n\r", ethIp[0], ethIp[1], ethIp[2], ethIp[3]);
hendrikvincent 0:05ccbd4f84f1 42
hendrikvincent 0:05ccbd4f84f1 43 /***************** FilsSystem ******************/
hendrikvincent 0:05ccbd4f84f1 44 LocalFileSystem local("local");
hendrikvincent 0:05ccbd4f84f1 45 float waarden[3][800];
hendrikvincent 0:05ccbd4f84f1 46
hendrikvincent 0:05ccbd4f84f1 47 /***************** Handlers *******************/
hendrikvincent 0:05ccbd4f84f1 48 svr.addHandler<SimpleHandler>("/hello");
hendrikvincent 0:05ccbd4f84f1 49 svr.addHandler<FSHandler>("");
hendrikvincent 0:05ccbd4f84f1 50 svr.addHandler<RPCHandler>("/rpc");
hendrikvincent 0:05ccbd4f84f1 51
hendrikvincent 0:05ccbd4f84f1 52 FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path
hendrikvincent 0:05ccbd4f84f1 53 Base::add_rpc_class<DigitalOut>();
hendrikvincent 0:05ccbd4f84f1 54 Base::add_rpc_class<AnalogIn>();
hendrikvincent 0:05ccbd4f84f1 55
hendrikvincent 0:05ccbd4f84f1 56 svr.bind(80);
hendrikvincent 0:05ccbd4f84f1 57 printf("Server listening\n\r");
hendrikvincent 0:05ccbd4f84f1 58
hendrikvincent 0:05ccbd4f84f1 59 /**************** Realtime clock ******************/
hendrikvincent 0:05ccbd4f84f1 60 time_t ctTime;
hendrikvincent 0:05ccbd4f84f1 61 ctTime = time(NULL);
hendrikvincent 0:05ccbd4f84f1 62 printf("Current time is (UTC): %s\r\n", ctime(&ctTime));
hendrikvincent 0:05ccbd4f84f1 63
hendrikvincent 0:05ccbd4f84f1 64 Host server(IpAddr(), 123, "be.pool.ntp.org");
hendrikvincent 0:05ccbd4f84f1 65 //be server: be.pool.ntp.org
hendrikvincent 0:05ccbd4f84f1 66 ntp.setTime(server);
hendrikvincent 0:05ccbd4f84f1 67
hendrikvincent 0:05ccbd4f84f1 68 ctTime = time(NULL);
hendrikvincent 0:05ccbd4f84f1 69 printf("\nTime is now (UTC): %s\r\n", ctime(&ctTime));
hendrikvincent 0:05ccbd4f84f1 70
hendrikvincent 0:05ccbd4f84f1 71 /***************** Timer ***************/
hendrikvincent 0:05ccbd4f84f1 72 Timer tm;
hendrikvincent 0:05ccbd4f84f1 73 tm.start();
hendrikvincent 0:05ccbd4f84f1 74
hendrikvincent 0:05ccbd4f84f1 75 float sensorWaarde;
hendrikvincent 0:05ccbd4f84f1 76
hendrikvincent 0:05ccbd4f84f1 77
hendrikvincent 0:05ccbd4f84f1 78 while (true) {
hendrikvincent 0:05ccbd4f84f1 79 Net::poll();
hendrikvincent 0:05ccbd4f84f1 80 if (tm.read() > 2) {
hendrikvincent 0:05ccbd4f84f1 81 file = fopen("/local/test.htm", "a"); //openen bestand.
hendrikvincent 0:05ccbd4f84f1 82 sensorWaarde = ((Sensor * 3.3 -2.78)*100); //Ijking waarde
hendrikvincent 0:05ccbd4f84f1 83 ctTime = time(NULL); //Tijd goedzetten
hendrikvincent 0:05ccbd4f84f1 84 fprintf(file, "%s %5.2f\r\n", ctime(&ctTime), sensorWaarde);
hendrikvincent 0:05ccbd4f84f1 85 printf("\nTime is now (UTC): %s\r\n", ctime(&ctTime));
hendrikvincent 0:05ccbd4f84f1 86 printf("Temperatuur: %5.2f\r\n", sensorWaarde);
hendrikvincent 0:05ccbd4f84f1 87 fclose(file);
hendrikvincent 0:05ccbd4f84f1 88 led1 = !led1;
hendrikvincent 0:05ccbd4f84f1 89 tm.start();
hendrikvincent 0:05ccbd4f84f1 90 }
hendrikvincent 0:05ccbd4f84f1 91 }
hendrikvincent 0:05ccbd4f84f1 92 }