Web radiation monitoring system.
Dependencies: EthernetNetIf TextLCD mbed HTTPServer
main.cpp@0:de8fcaf0fc9b, 2011-05-24 (annotated)
- Committer:
- moritams
- Date:
- Tue May 24 10:53:04 2011 +0000
- Revision:
- 0:de8fcaf0fc9b
prototype. ver0.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
moritams | 0:de8fcaf0fc9b | 1 | // Hello World! for the TextLCD |
moritams | 0:de8fcaf0fc9b | 2 | |
moritams | 0:de8fcaf0fc9b | 3 | #include "mbed.h" |
moritams | 0:de8fcaf0fc9b | 4 | #include "TextLCD.h" |
moritams | 0:de8fcaf0fc9b | 5 | #include "EthernetNetIf.h" |
moritams | 0:de8fcaf0fc9b | 6 | #include "HTTPServer.h" |
moritams | 0:de8fcaf0fc9b | 7 | #include "ipaddr.h" |
moritams | 0:de8fcaf0fc9b | 8 | #include "RPCVariable.h" |
moritams | 0:de8fcaf0fc9b | 9 | |
moritams | 0:de8fcaf0fc9b | 10 | IpAddr ip(192,168,1,121); |
moritams | 0:de8fcaf0fc9b | 11 | IpAddr netmask(255,255,255,0); |
moritams | 0:de8fcaf0fc9b | 12 | IpAddr gateway(192,168,1,1); |
moritams | 0:de8fcaf0fc9b | 13 | IpAddr dns(192,168,1,1); |
moritams | 0:de8fcaf0fc9b | 14 | |
moritams | 0:de8fcaf0fc9b | 15 | EthernetNetIf eth(ip,netmask,gateway,dns); |
moritams | 0:de8fcaf0fc9b | 16 | HTTPServer svr; |
moritams | 0:de8fcaf0fc9b | 17 | |
moritams | 0:de8fcaf0fc9b | 18 | DigitalOut led1(LED1); |
moritams | 0:de8fcaf0fc9b | 19 | |
moritams | 0:de8fcaf0fc9b | 20 | InterruptIn geigerIn(p5); |
moritams | 0:de8fcaf0fc9b | 21 | |
moritams | 0:de8fcaf0fc9b | 22 | TextLCD lcd(p24, p26, p27, p28, p29, p30); // rs, e, d4-d7 |
moritams | 0:de8fcaf0fc9b | 23 | |
moritams | 0:de8fcaf0fc9b | 24 | unsigned int count = 0; |
moritams | 0:de8fcaf0fc9b | 25 | unsigned int prev_count = 0; |
moritams | 0:de8fcaf0fc9b | 26 | unsigned int i = 0; |
moritams | 0:de8fcaf0fc9b | 27 | |
moritams | 0:de8fcaf0fc9b | 28 | |
moritams | 0:de8fcaf0fc9b | 29 | class MyHandler : public HTTPRequestHandler |
moritams | 0:de8fcaf0fc9b | 30 | { |
moritams | 0:de8fcaf0fc9b | 31 | public: |
moritams | 0:de8fcaf0fc9b | 32 | MyHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket); |
moritams | 0:de8fcaf0fc9b | 33 | virtual ~MyHandler(); |
moritams | 0:de8fcaf0fc9b | 34 | |
moritams | 0:de8fcaf0fc9b | 35 | //protected: |
moritams | 0:de8fcaf0fc9b | 36 | static inline HTTPRequestHandler* inst(const char* rootPath, const char* path, TCPSocket* pTCPSocket) { return new MyHandler(rootPath, path, pTCPSocket); } //if we ever could do static virtual functions, this would be one |
moritams | 0:de8fcaf0fc9b | 37 | |
moritams | 0:de8fcaf0fc9b | 38 | virtual void doGet(); |
moritams | 0:de8fcaf0fc9b | 39 | virtual void doPost(); |
moritams | 0:de8fcaf0fc9b | 40 | virtual void doHead(); |
moritams | 0:de8fcaf0fc9b | 41 | |
moritams | 0:de8fcaf0fc9b | 42 | virtual void onReadable(); //Data has been read |
moritams | 0:de8fcaf0fc9b | 43 | virtual void onWriteable(); //Data has been written & buf is free |
moritams | 0:de8fcaf0fc9b | 44 | virtual void onClose(); //Connection is closing |
moritams | 0:de8fcaf0fc9b | 45 | }; |
moritams | 0:de8fcaf0fc9b | 46 | |
moritams | 0:de8fcaf0fc9b | 47 | MyHandler::MyHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket) |
moritams | 0:de8fcaf0fc9b | 48 | { |
moritams | 0:de8fcaf0fc9b | 49 | } |
moritams | 0:de8fcaf0fc9b | 50 | |
moritams | 0:de8fcaf0fc9b | 51 | MyHandler::~MyHandler() |
moritams | 0:de8fcaf0fc9b | 52 | { |
moritams | 0:de8fcaf0fc9b | 53 | } |
moritams | 0:de8fcaf0fc9b | 54 | |
moritams | 0:de8fcaf0fc9b | 55 | void MyHandler::doGet() |
moritams | 0:de8fcaf0fc9b | 56 | { |
moritams | 0:de8fcaf0fc9b | 57 | const char* resp = "Hello world !"; |
moritams | 0:de8fcaf0fc9b | 58 | char a[64]; |
moritams | 0:de8fcaf0fc9b | 59 | sprintf(a, "%d CPM.\n\0", ::prev_count); |
moritams | 0:de8fcaf0fc9b | 60 | //setContentLen( strlen(resp) ); |
moritams | 0:de8fcaf0fc9b | 61 | setContentLen( strlen(a) ); |
moritams | 0:de8fcaf0fc9b | 62 | |
moritams | 0:de8fcaf0fc9b | 63 | respHeaders()["Connection"] = "close"; |
moritams | 0:de8fcaf0fc9b | 64 | |
moritams | 0:de8fcaf0fc9b | 65 | //writeData(resp, strlen(resp)); |
moritams | 0:de8fcaf0fc9b | 66 | writeData(a, strlen(a)); |
moritams | 0:de8fcaf0fc9b | 67 | |
moritams | 0:de8fcaf0fc9b | 68 | } |
moritams | 0:de8fcaf0fc9b | 69 | |
moritams | 0:de8fcaf0fc9b | 70 | void MyHandler::doPost() |
moritams | 0:de8fcaf0fc9b | 71 | { |
moritams | 0:de8fcaf0fc9b | 72 | |
moritams | 0:de8fcaf0fc9b | 73 | } |
moritams | 0:de8fcaf0fc9b | 74 | |
moritams | 0:de8fcaf0fc9b | 75 | void MyHandler::doHead() |
moritams | 0:de8fcaf0fc9b | 76 | { |
moritams | 0:de8fcaf0fc9b | 77 | |
moritams | 0:de8fcaf0fc9b | 78 | } |
moritams | 0:de8fcaf0fc9b | 79 | |
moritams | 0:de8fcaf0fc9b | 80 | |
moritams | 0:de8fcaf0fc9b | 81 | void MyHandler::onReadable() //Data has been read |
moritams | 0:de8fcaf0fc9b | 82 | { |
moritams | 0:de8fcaf0fc9b | 83 | |
moritams | 0:de8fcaf0fc9b | 84 | } |
moritams | 0:de8fcaf0fc9b | 85 | |
moritams | 0:de8fcaf0fc9b | 86 | void MyHandler::onWriteable() //Data has been written & buf is free |
moritams | 0:de8fcaf0fc9b | 87 | { |
moritams | 0:de8fcaf0fc9b | 88 | close(); //Data written, we can close the connection |
moritams | 0:de8fcaf0fc9b | 89 | } |
moritams | 0:de8fcaf0fc9b | 90 | |
moritams | 0:de8fcaf0fc9b | 91 | void MyHandler::onClose() //Connection is closing |
moritams | 0:de8fcaf0fc9b | 92 | { |
moritams | 0:de8fcaf0fc9b | 93 | //Nothing to do |
moritams | 0:de8fcaf0fc9b | 94 | } |
moritams | 0:de8fcaf0fc9b | 95 | |
moritams | 0:de8fcaf0fc9b | 96 | |
moritams | 0:de8fcaf0fc9b | 97 | void radioactive() |
moritams | 0:de8fcaf0fc9b | 98 | { |
moritams | 0:de8fcaf0fc9b | 99 | count = count + 1; |
moritams | 0:de8fcaf0fc9b | 100 | } |
moritams | 0:de8fcaf0fc9b | 101 | |
moritams | 0:de8fcaf0fc9b | 102 | void count_minutes() |
moritams | 0:de8fcaf0fc9b | 103 | { |
moritams | 0:de8fcaf0fc9b | 104 | lcd.locate(0, 0); |
moritams | 0:de8fcaf0fc9b | 105 | lcd.printf("%04dCPM ", count); lcd.printf("%02dsec", i); |
moritams | 0:de8fcaf0fc9b | 106 | lcd.locate(0, 1); |
moritams | 0:de8fcaf0fc9b | 107 | lcd.printf("Prev. %04dCPM ", prev_count); |
moritams | 0:de8fcaf0fc9b | 108 | |
moritams | 0:de8fcaf0fc9b | 109 | i = i + 1; |
moritams | 0:de8fcaf0fc9b | 110 | |
moritams | 0:de8fcaf0fc9b | 111 | if(i > 59) |
moritams | 0:de8fcaf0fc9b | 112 | { |
moritams | 0:de8fcaf0fc9b | 113 | i = 0; |
moritams | 0:de8fcaf0fc9b | 114 | prev_count = count; |
moritams | 0:de8fcaf0fc9b | 115 | count = 0; |
moritams | 0:de8fcaf0fc9b | 116 | } |
moritams | 0:de8fcaf0fc9b | 117 | } |
moritams | 0:de8fcaf0fc9b | 118 | |
moritams | 0:de8fcaf0fc9b | 119 | int main() { |
moritams | 0:de8fcaf0fc9b | 120 | |
moritams | 0:de8fcaf0fc9b | 121 | // Count variables clear. |
moritams | 0:de8fcaf0fc9b | 122 | count = 0; |
moritams | 0:de8fcaf0fc9b | 123 | prev_count = 0; |
moritams | 0:de8fcaf0fc9b | 124 | |
moritams | 0:de8fcaf0fc9b | 125 | lcd.cls(); |
moritams | 0:de8fcaf0fc9b | 126 | lcd.locate(0, 0); |
moritams | 0:de8fcaf0fc9b | 127 | |
moritams | 0:de8fcaf0fc9b | 128 | geigerIn.fall(&radioactive); |
moritams | 0:de8fcaf0fc9b | 129 | |
moritams | 0:de8fcaf0fc9b | 130 | EthernetErr ethErr = eth.setup(); |
moritams | 0:de8fcaf0fc9b | 131 | if(ethErr) |
moritams | 0:de8fcaf0fc9b | 132 | { |
moritams | 0:de8fcaf0fc9b | 133 | return -1; |
moritams | 0:de8fcaf0fc9b | 134 | } |
moritams | 0:de8fcaf0fc9b | 135 | |
moritams | 0:de8fcaf0fc9b | 136 | svr.addHandler<SimpleHandler>("/"); //Default handler |
moritams | 0:de8fcaf0fc9b | 137 | svr.addHandler<MyHandler>("/geiger"); |
moritams | 0:de8fcaf0fc9b | 138 | |
moritams | 0:de8fcaf0fc9b | 139 | svr.bind(80); |
moritams | 0:de8fcaf0fc9b | 140 | |
moritams | 0:de8fcaf0fc9b | 141 | Ticker tick; |
moritams | 0:de8fcaf0fc9b | 142 | tick.attach(count_minutes, 1); |
moritams | 0:de8fcaf0fc9b | 143 | |
moritams | 0:de8fcaf0fc9b | 144 | Timer tm; |
moritams | 0:de8fcaf0fc9b | 145 | tm.start(); |
moritams | 0:de8fcaf0fc9b | 146 | //Listen indefinitely |
moritams | 0:de8fcaf0fc9b | 147 | while(true) |
moritams | 0:de8fcaf0fc9b | 148 | { |
moritams | 0:de8fcaf0fc9b | 149 | Net::poll(); |
moritams | 0:de8fcaf0fc9b | 150 | if(tm.read()>.5) |
moritams | 0:de8fcaf0fc9b | 151 | { |
moritams | 0:de8fcaf0fc9b | 152 | led1=!led1; //Show that we are alive |
moritams | 0:de8fcaf0fc9b | 153 | tm.start(); |
moritams | 0:de8fcaf0fc9b | 154 | } |
moritams | 0:de8fcaf0fc9b | 155 | } |
moritams | 0:de8fcaf0fc9b | 156 | |
moritams | 0:de8fcaf0fc9b | 157 | } |