Sensor with Web Server
Dependencies: EthernetInterface mbed-rpc mbed-rtos mbed
main.cpp@0:c385e589a779, 2014-04-08 (annotated)
- Committer:
- afilipem
- Date:
- Tue Apr 08 12:13:32 2014 +0000
- Revision:
- 0:c385e589a779
1 version;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
afilipem | 0:c385e589a779 | 1 | //*********************************** |
afilipem | 0:c385e589a779 | 2 | //Blend of Ethernet Code with OneWire |
afilipem | 0:c385e589a779 | 3 | //*********************************** |
afilipem | 0:c385e589a779 | 4 | |
afilipem | 0:c385e589a779 | 5 | #include "mbed.h" |
afilipem | 0:c385e589a779 | 6 | #include "EthernetInterface.h" |
afilipem | 0:c385e589a779 | 7 | #include "mbed_rpc.h" |
afilipem | 0:c385e589a779 | 8 | #include "RPCCommand.h" |
afilipem | 0:c385e589a779 | 9 | #include "HTTPServer.h" |
afilipem | 0:c385e589a779 | 10 | #include "Formatter.h" |
afilipem | 0:c385e589a779 | 11 | #include "RequestHandler.h" |
afilipem | 0:c385e589a779 | 12 | #include "RPCType.h" |
afilipem | 0:c385e589a779 | 13 | |
afilipem | 0:c385e589a779 | 14 | #define SERVER_PORT 80 |
afilipem | 0:c385e589a779 | 15 | |
afilipem | 0:c385e589a779 | 16 | #include <mbed.h> |
afilipem | 0:c385e589a779 | 17 | #include "DS18S20.h" |
afilipem | 0:c385e589a779 | 18 | #include "DS18B20.h" |
afilipem | 0:c385e589a779 | 19 | #include "OneWireDefs.h" |
afilipem | 0:c385e589a779 | 20 | //***new adding |
afilipem | 0:c385e589a779 | 21 | #include "DebugTrace.h" |
afilipem | 0:c385e589a779 | 22 | |
afilipem | 0:c385e589a779 | 23 | //#define THERMOMETER DS18S20 |
afilipem | 0:c385e589a779 | 24 | #define THERMOMETER DS18B20 |
afilipem | 0:c385e589a779 | 25 | |
afilipem | 0:c385e589a779 | 26 | //***new adding |
afilipem | 0:c385e589a779 | 27 | DigitalOut relay(p21); |
afilipem | 0:c385e589a779 | 28 | //***new adding |
afilipem | 0:c385e589a779 | 29 | float realTemp = -999; |
afilipem | 0:c385e589a779 | 30 | //***new adding |
afilipem | 0:c385e589a779 | 31 | DebugTrace pc_ds18B20B(ON, TO_SERIAL); |
afilipem | 0:c385e589a779 | 32 | |
afilipem | 0:c385e589a779 | 33 | HTTPServer create_simple_server() |
afilipem | 0:c385e589a779 | 34 | { |
afilipem | 0:c385e589a779 | 35 | HTTPServer srv; |
afilipem | 0:c385e589a779 | 36 | srv.add_request_handler("DELETE", new DeleteRequestHandler()); |
afilipem | 0:c385e589a779 | 37 | srv.add_request_handler("GET", new GetRequestHandler()); |
afilipem | 0:c385e589a779 | 38 | srv.add_request_handler("PUT", new PutRequestHandler()); |
afilipem | 0:c385e589a779 | 39 | return srv; |
afilipem | 0:c385e589a779 | 40 | } |
afilipem | 0:c385e589a779 | 41 | |
afilipem | 0:c385e589a779 | 42 | HTTPServer create_interactive_server() |
afilipem | 0:c385e589a779 | 43 | { |
afilipem | 0:c385e589a779 | 44 | HTTPServer srv(new InteractiveHTMLFormatter()); |
afilipem | 0:c385e589a779 | 45 | srv.add_request_handler("GET", new ComplexRequestHandler()); |
afilipem | 0:c385e589a779 | 46 | return srv; |
afilipem | 0:c385e589a779 | 47 | } |
afilipem | 0:c385e589a779 | 48 | |
afilipem | 0:c385e589a779 | 49 | int main(void) |
afilipem | 0:c385e589a779 | 50 | { |
afilipem | 0:c385e589a779 | 51 | // device( crcOn, useAddress, parasitic, mbed pin ) |
afilipem | 0:c385e589a779 | 52 | THERMOMETER device(true, true, false, p25); |
afilipem | 0:c385e589a779 | 53 | |
afilipem | 0:c385e589a779 | 54 | while (!device.initialize()); // keep calling until it works |
afilipem | 0:c385e589a779 | 55 | |
afilipem | 0:c385e589a779 | 56 | while (true) |
afilipem | 0:c385e589a779 | 57 | { |
afilipem | 0:c385e589a779 | 58 | RPCType::instance().register_types(); |
afilipem | 0:c385e589a779 | 59 | device.setResolution(twelveBit); |
afilipem | 0:c385e589a779 | 60 | realTemp=device.readTemperature(); |
afilipem | 0:c385e589a779 | 61 | |
afilipem | 0:c385e589a779 | 62 | if (realTemp<24) |
afilipem | 0:c385e589a779 | 63 | { |
afilipem | 0:c385e589a779 | 64 | relay=1; |
afilipem | 0:c385e589a779 | 65 | pc_ds18B20B.traceOut("Less than 24"); |
afilipem | 0:c385e589a779 | 66 | EthernetInterface eth; |
afilipem | 0:c385e589a779 | 67 | if(eth.init()) |
afilipem | 0:c385e589a779 | 68 | { |
afilipem | 0:c385e589a779 | 69 | printf("Error while initializing the ethernet interface.\n"); |
afilipem | 0:c385e589a779 | 70 | return -1; |
afilipem | 0:c385e589a779 | 71 | } |
afilipem | 0:c385e589a779 | 72 | if(eth.connect()) |
afilipem | 0:c385e589a779 | 73 | { |
afilipem | 0:c385e589a779 | 74 | printf("Error while starting the ethernet interface.\n"); |
afilipem | 0:c385e589a779 | 75 | return -1; |
afilipem | 0:c385e589a779 | 76 | } |
afilipem | 0:c385e589a779 | 77 | |
afilipem | 0:c385e589a779 | 78 | printf("IP Address is %s\n", eth.getIPAddress()); |
afilipem | 0:c385e589a779 | 79 | |
afilipem | 0:c385e589a779 | 80 | HTTPServer srv = create_interactive_server(); |
afilipem | 0:c385e589a779 | 81 | |
afilipem | 0:c385e589a779 | 82 | if(!srv.init(SERVER_PORT)) |
afilipem | 0:c385e589a779 | 83 | { |
afilipem | 0:c385e589a779 | 84 | eth.disconnect(); |
afilipem | 0:c385e589a779 | 85 | return -1; |
afilipem | 0:c385e589a779 | 86 | } |
afilipem | 0:c385e589a779 | 87 | |
afilipem | 0:c385e589a779 | 88 | srv.run(); |
afilipem | 0:c385e589a779 | 89 | } |
afilipem | 0:c385e589a779 | 90 | if (realTemp>=24) |
afilipem | 0:c385e589a779 | 91 | { |
afilipem | 0:c385e589a779 | 92 | relay=0; |
afilipem | 0:c385e589a779 | 93 | pc_ds18B20B.traceOut("More than 24"); |
afilipem | 0:c385e589a779 | 94 | EthernetInterface eth; |
afilipem | 0:c385e589a779 | 95 | if(eth.init()) |
afilipem | 0:c385e589a779 | 96 | { |
afilipem | 0:c385e589a779 | 97 | printf("Error while initializing the ethernet interface.\n"); |
afilipem | 0:c385e589a779 | 98 | return -1; |
afilipem | 0:c385e589a779 | 99 | } |
afilipem | 0:c385e589a779 | 100 | if(eth.connect()) |
afilipem | 0:c385e589a779 | 101 | { |
afilipem | 0:c385e589a779 | 102 | printf("Error while starting the ethernet interface.\n"); |
afilipem | 0:c385e589a779 | 103 | return -1; |
afilipem | 0:c385e589a779 | 104 | } |
afilipem | 0:c385e589a779 | 105 | |
afilipem | 0:c385e589a779 | 106 | printf("IP Address is %s\n", eth.getIPAddress()); |
afilipem | 0:c385e589a779 | 107 | |
afilipem | 0:c385e589a779 | 108 | HTTPServer srv = create_interactive_server(); |
afilipem | 0:c385e589a779 | 109 | |
afilipem | 0:c385e589a779 | 110 | if(!srv.init(SERVER_PORT)) |
afilipem | 0:c385e589a779 | 111 | { |
afilipem | 0:c385e589a779 | 112 | eth.disconnect(); |
afilipem | 0:c385e589a779 | 113 | return -1; |
afilipem | 0:c385e589a779 | 114 | } |
afilipem | 0:c385e589a779 | 115 | |
afilipem | 0:c385e589a779 | 116 | srv.run(); |
afilipem | 0:c385e589a779 | 117 | } |
afilipem | 0:c385e589a779 | 118 | wait(5); |
afilipem | 0:c385e589a779 | 119 | } |
afilipem | 0:c385e589a779 | 120 | |
afilipem | 0:c385e589a779 | 121 | return EXIT_SUCCESS; |
afilipem | 0:c385e589a779 | 122 | } |
afilipem | 0:c385e589a779 | 123 | |
afilipem | 0:c385e589a779 | 124 |