monitor sensor and control relay via web server
Dependencies: WIZWebtInterface mbed
Fork of HTTP_Test_Server_Web by
main.cpp@4:f975091219a2, 2016-11-16 (annotated)
- Committer:
- 846354866
- Date:
- Wed Nov 16 06:16:59 2016 +0000
- Revision:
- 4:f975091219a2
- Parent:
- 3:dc786e394e82
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
846354866 | 0:25d52c6a3448 | 1 | #include "mbed.h" |
846354866 | 0:25d52c6a3448 | 2 | #include "EthernetInterface.h" |
846354866 | 3:dc786e394e82 | 3 | #include "wizHTML.h" |
846354866 | 0:25d52c6a3448 | 4 | #include <stdio.h> |
846354866 | 0:25d52c6a3448 | 5 | #include <string.h> |
846354866 | 0:25d52c6a3448 | 6 | |
846354866 | 0:25d52c6a3448 | 7 | |
846354866 | 0:25d52c6a3448 | 8 | #define MAC "\x00\x08\xDC\x11\x34\x78" |
846354866 | 3:dc786e394e82 | 9 | #define IP "192.168.1.12" |
846354866 | 0:25d52c6a3448 | 10 | #define MASK "255.255.255.0" |
846354866 | 0:25d52c6a3448 | 11 | #define GATEWAY "192.168.1.1" |
846354866 | 0:25d52c6a3448 | 12 | |
846354866 | 0:25d52c6a3448 | 13 | #define HTTPD_SERVER_PORT 80 |
846354866 | 3:dc786e394e82 | 14 | #define HTTPD_MAX_REQ_LENGTH 102 |
846354866 | 3:dc786e394e82 | 15 | #define HTTPD_MAX_HDR_LENGTH 96 |
846354866 | 0:25d52c6a3448 | 16 | #define HTTPD_MAX_FNAME_LENGTH 127 |
846354866 | 0:25d52c6a3448 | 17 | #define HTTPD_MAX_DNAME_LENGTH 127 |
846354866 | 0:25d52c6a3448 | 18 | |
846354866 | 0:25d52c6a3448 | 19 | |
846354866 | 0:25d52c6a3448 | 20 | Serial uart(PA_13,PA_14); |
846354866 | 0:25d52c6a3448 | 21 | DigitalInOut myIOD10(D10); |
846354866 | 0:25d52c6a3448 | 22 | DigitalIn myInD11(D11); |
846354866 | 0:25d52c6a3448 | 23 | DigitalOut myOutD12(D12); |
846354866 | 0:25d52c6a3448 | 24 | AnalogIn myInA1(A1); |
846354866 | 3:dc786e394e82 | 25 | Timeout buttonLedTimeout; |
846354866 | 3:dc786e394e82 | 26 | Timeout receiveDht11Timeout; |
846354866 | 0:25d52c6a3448 | 27 | |
846354866 | 0:25d52c6a3448 | 28 | unsigned char RH,RL,TH,TL; |
846354866 | 0:25d52c6a3448 | 29 | unsigned char data_byte; |
846354866 | 0:25d52c6a3448 | 30 | unsigned int U8FLAG; |
846354866 | 3:dc786e394e82 | 31 | char seaver_ip[] = IP; |
846354866 | 3:dc786e394e82 | 32 | char led_control[6]; |
846354866 | 3:dc786e394e82 | 33 | unsigned int loopCnt = 10000; |
846354866 | 0:25d52c6a3448 | 34 | |
846354866 | 0:25d52c6a3448 | 35 | EthernetInterface eth; |
846354866 | 0:25d52c6a3448 | 36 | TCPSocketServer server; |
846354866 | 0:25d52c6a3448 | 37 | TCPSocketConnection client; |
846354866 | 0:25d52c6a3448 | 38 | |
846354866 | 0:25d52c6a3448 | 39 | char buffer[HTTPD_MAX_REQ_LENGTH+1]; |
846354866 | 0:25d52c6a3448 | 40 | char httpHeader[HTTPD_MAX_HDR_LENGTH+1]; |
846354866 | 0:25d52c6a3448 | 41 | char fileName[HTTPD_MAX_FNAME_LENGTH+1]; |
846354866 | 0:25d52c6a3448 | 42 | char dirName[HTTPD_MAX_DNAME_LENGTH+1]; |
846354866 | 0:25d52c6a3448 | 43 | char *uristr; |
846354866 | 0:25d52c6a3448 | 44 | char *eou; |
846354866 | 0:25d52c6a3448 | 45 | char *qrystr; |
846354866 | 0:25d52c6a3448 | 46 | |
846354866 | 3:dc786e394e82 | 47 | void receiveDht11(); |
846354866 | 3:dc786e394e82 | 48 | unsigned char receiveByte(); |
846354866 | 0:25d52c6a3448 | 49 | |
846354866 | 3:dc786e394e82 | 50 | void buttonLed() |
846354866 | 0:25d52c6a3448 | 51 | { |
846354866 | 0:25d52c6a3448 | 52 | while(myInD11.read()) |
846354866 | 0:25d52c6a3448 | 53 | { |
846354866 | 0:25d52c6a3448 | 54 | wait(0.01f); |
846354866 | 0:25d52c6a3448 | 55 | if(!myInD11.read()) |
846354866 | 0:25d52c6a3448 | 56 | { |
846354866 | 0:25d52c6a3448 | 57 | myOutD12.write(!myOutD12.read()); |
846354866 | 0:25d52c6a3448 | 58 | break; |
846354866 | 0:25d52c6a3448 | 59 | } |
846354866 | 0:25d52c6a3448 | 60 | } |
846354866 | 0:25d52c6a3448 | 61 | |
846354866 | 3:dc786e394e82 | 62 | buttonLedTimeout.attach(&buttonLed,0.1f); |
846354866 | 0:25d52c6a3448 | 63 | } |
846354866 | 0:25d52c6a3448 | 64 | |
846354866 | 0:25d52c6a3448 | 65 | void getFile(char* uri) |
846354866 | 0:25d52c6a3448 | 66 | { |
846354866 | 3:dc786e394e82 | 67 | static char string_c[24] = {0}; |
846354866 | 3:dc786e394e82 | 68 | char* Led; |
846354866 | 3:dc786e394e82 | 69 | char* ledS; |
846354866 | 3:dc786e394e82 | 70 | int gas = myInA1.read_u16(); |
846354866 | 0:25d52c6a3448 | 71 | uart.printf("getFile %s\n", uri); |
846354866 | 3:dc786e394e82 | 72 | if(memcmp(&uri[1], "favicon.ico", 11)==0){ |
846354866 | 3:dc786e394e82 | 73 | return; |
846354866 | 3:dc786e394e82 | 74 | } |
846354866 | 3:dc786e394e82 | 75 | strncpy(led_control, &uri[1], 5); |
846354866 | 0:25d52c6a3448 | 76 | char *lstchr = strrchr(uri, NULL) -1; |
846354866 | 0:25d52c6a3448 | 77 | |
846354866 | 3:dc786e394e82 | 78 | static char open_1 = 1, cloce_1 = 1; |
846354866 | 3:dc786e394e82 | 79 | if (memcmp(led_control, "offon", 5)==0){ |
846354866 | 3:dc786e394e82 | 80 | if (cloce_1){ |
846354866 | 0:25d52c6a3448 | 81 | myOutD12.write(0); |
846354866 | 0:25d52c6a3448 | 82 | cloce_1 = 0; |
846354866 | 0:25d52c6a3448 | 83 | } |
846354866 | 0:25d52c6a3448 | 84 | |
846354866 | 0:25d52c6a3448 | 85 | open_1 = 1; |
846354866 | 0:25d52c6a3448 | 86 | } |
846354866 | 3:dc786e394e82 | 87 | else if (memcmp(led_control, "onoff", 5)==0){ |
846354866 | 3:dc786e394e82 | 88 | if (open_1){ |
846354866 | 0:25d52c6a3448 | 89 | myOutD12.write(1); |
846354866 | 3:dc786e394e82 | 90 | open_1 = 0; |
846354866 | 0:25d52c6a3448 | 91 | } |
846354866 | 0:25d52c6a3448 | 92 | |
846354866 | 0:25d52c6a3448 | 93 | cloce_1 = 1; |
846354866 | 3:dc786e394e82 | 94 | } |
846354866 | 3:dc786e394e82 | 95 | if(myOutD12.read()){ |
846354866 | 3:dc786e394e82 | 96 | Led = ledSc[0]; |
846354866 | 3:dc786e394e82 | 97 | ledS = ledSc[1]; |
846354866 | 3:dc786e394e82 | 98 | sprintf(string_c,"%s/offon", seaver_ip); |
846354866 | 3:dc786e394e82 | 99 | }else{ |
846354866 | 3:dc786e394e82 | 100 | Led = ledSc[2]; |
846354866 | 3:dc786e394e82 | 101 | ledS = ledSc[3]; |
846354866 | 3:dc786e394e82 | 102 | sprintf(string_c,"%s/onoff", seaver_ip); |
846354866 | 3:dc786e394e82 | 103 | } |
846354866 | 3:dc786e394e82 | 104 | htmlWeb(TH, TH*5, RH, RH*5, gas, gas*0.05f, Led, ledS, string_c); |
846354866 | 3:dc786e394e82 | 105 | sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n"); |
846354866 | 3:dc786e394e82 | 106 | client.send(httpHeader,strlen(httpHeader)); |
846354866 | 0:25d52c6a3448 | 107 | |
846354866 | 3:dc786e394e82 | 108 | for (int i=0; i<sizeof(html); i += 96){ |
846354866 | 3:dc786e394e82 | 109 | memcpy(httpHeader, &html[i], 96); |
846354866 | 3:dc786e394e82 | 110 | client.send(httpHeader,strlen(httpHeader)); |
846354866 | 3:dc786e394e82 | 111 | //wait(0.00001); |
846354866 | 0:25d52c6a3448 | 112 | } |
846354866 | 0:25d52c6a3448 | 113 | |
846354866 | 3:dc786e394e82 | 114 | memset(string_c, 0, sizeof(string_c)); |
846354866 | 3:dc786e394e82 | 115 | memset(html, 0, sizeof(html)); |
846354866 | 0:25d52c6a3448 | 116 | } |
846354866 | 0:25d52c6a3448 | 117 | |
846354866 | 0:25d52c6a3448 | 118 | int main (void) |
846354866 | 0:25d52c6a3448 | 119 | { |
846354866 | 0:25d52c6a3448 | 120 | uart.baud(115200); |
846354866 | 0:25d52c6a3448 | 121 | uart.printf("Initializing\r\n"); |
846354866 | 0:25d52c6a3448 | 122 | uart.printf("Initializing Ethernet\r\n"); |
846354866 | 3:dc786e394e82 | 123 | buttonLedTimeout.attach(&buttonLed,0.1f); |
846354866 | 3:dc786e394e82 | 124 | receiveDht11Timeout.attach(&receiveDht11,0.2f); |
846354866 | 3:dc786e394e82 | 125 | while (true) { |
846354866 | 3:dc786e394e82 | 126 | //eth.init(); //Use DHCP |
846354866 | 3:dc786e394e82 | 127 | eth.init((uint8_t*)MAC,IP,MASK,GATEWAY); //IP,mask,Gateway |
846354866 | 3:dc786e394e82 | 128 | uart.printf("Connecting\r\n"); |
846354866 | 3:dc786e394e82 | 129 | eth.connect(); |
846354866 | 3:dc786e394e82 | 130 | uart.printf("IP Address is %s\r\n", eth.getIPAddress()); |
846354866 | 0:25d52c6a3448 | 131 | |
846354866 | 3:dc786e394e82 | 132 | server.bind(HTTPD_SERVER_PORT); |
846354866 | 3:dc786e394e82 | 133 | server.listen(); |
846354866 | 3:dc786e394e82 | 134 | uart.printf("Server Listening\r\n"); |
846354866 | 0:25d52c6a3448 | 135 | |
846354866 | 0:25d52c6a3448 | 136 | while (true) { |
846354866 | 3:dc786e394e82 | 137 | uart.printf("\nWait for new connection...\r\n"); |
846354866 | 3:dc786e394e82 | 138 | |
846354866 | 3:dc786e394e82 | 139 | client.set_blocking(false, 500); // Timeout after (0.5)s |
846354866 | 3:dc786e394e82 | 140 | server.accept(client); |
846354866 | 3:dc786e394e82 | 141 | |
846354866 | 3:dc786e394e82 | 142 | uart.printf("Connection from: %s\r\n", client.get_address()); |
846354866 | 3:dc786e394e82 | 143 | while (true) { |
846354866 | 3:dc786e394e82 | 144 | int n = client.receive(buffer, sizeof(buffer)); |
846354866 | 3:dc786e394e82 | 145 | if (n <= 0) { |
846354866 | 3:dc786e394e82 | 146 | uart.printf("return value n<=0\n"); |
846354866 | 3:dc786e394e82 | 147 | break; |
846354866 | 3:dc786e394e82 | 148 | } |
846354866 | 3:dc786e394e82 | 149 | uart.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,buffer); |
846354866 | 3:dc786e394e82 | 150 | if (n >= 1024) { |
846354866 | 3:dc786e394e82 | 151 | sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); |
846354866 | 0:25d52c6a3448 | 152 | client.send(httpHeader,strlen(httpHeader)); |
846354866 | 0:25d52c6a3448 | 153 | client.send(buffer,n); |
846354866 | 3:dc786e394e82 | 154 | break; |
846354866 | 0:25d52c6a3448 | 155 | } else { |
846354866 | 3:dc786e394e82 | 156 | buffer[n]=0; |
846354866 | 3:dc786e394e82 | 157 | } |
846354866 | 3:dc786e394e82 | 158 | if (!memcmp(buffer, "GET ", 4)) { |
846354866 | 3:dc786e394e82 | 159 | uristr = buffer + 4; |
846354866 | 3:dc786e394e82 | 160 | eou = strstr(uristr, " "); |
846354866 | 3:dc786e394e82 | 161 | if (eou == NULL) { |
846354866 | 3:dc786e394e82 | 162 | sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); |
846354866 | 3:dc786e394e82 | 163 | client.send(httpHeader,strlen(httpHeader)); |
846354866 | 3:dc786e394e82 | 164 | client.send(buffer,n); |
846354866 | 3:dc786e394e82 | 165 | } else { |
846354866 | 3:dc786e394e82 | 166 | *eou = 0; |
846354866 | 3:dc786e394e82 | 167 | getFile(uristr); |
846354866 | 3:dc786e394e82 | 168 | } |
846354866 | 0:25d52c6a3448 | 169 | } |
846354866 | 0:25d52c6a3448 | 170 | } |
846354866 | 3:dc786e394e82 | 171 | client.close(); |
846354866 | 3:dc786e394e82 | 172 | break; |
846354866 | 0:25d52c6a3448 | 173 | } |
846354866 | 0:25d52c6a3448 | 174 | } |
846354866 | 0:25d52c6a3448 | 175 | } |
846354866 | 0:25d52c6a3448 | 176 | |
846354866 | 3:dc786e394e82 | 177 | unsigned char receiveByte() |
846354866 | 0:25d52c6a3448 | 178 | { |
846354866 | 0:25d52c6a3448 | 179 | unsigned char i,temp; |
846354866 | 1:900e3ba27ddb | 180 | for(i=0;i<8;i++) |
846354866 | 0:25d52c6a3448 | 181 | { |
846354866 | 0:25d52c6a3448 | 182 | U8FLAG = 2; |
846354866 | 3:dc786e394e82 | 183 | loopCnt = 10000; |
846354866 | 3:dc786e394e82 | 184 | while(!myIOD10.read() && U8FLAG++){ |
846354866 | 3:dc786e394e82 | 185 | if (loopCnt-- == 0) return 2; |
846354866 | 3:dc786e394e82 | 186 | } |
846354866 | 3:dc786e394e82 | 187 | wait(0.00003f); |
846354866 | 1:900e3ba27ddb | 188 | temp=0; |
846354866 | 0:25d52c6a3448 | 189 | if(myIOD10.read()==1) |
846354866 | 1:900e3ba27ddb | 190 | temp=1; |
846354866 | 0:25d52c6a3448 | 191 | U8FLAG = 2; |
846354866 | 3:dc786e394e82 | 192 | loopCnt = 10000; |
846354866 | 3:dc786e394e82 | 193 | while(myIOD10.read() && U8FLAG++){ |
846354866 | 3:dc786e394e82 | 194 | if (loopCnt-- == 0) return 2; |
846354866 | 3:dc786e394e82 | 195 | } |
846354866 | 1:900e3ba27ddb | 196 | |
846354866 | 0:25d52c6a3448 | 197 | if(U8FLAG==1)break; |
846354866 | 3:dc786e394e82 | 198 | data_byte<<=1; |
846354866 | 0:25d52c6a3448 | 199 | data_byte|=temp; |
846354866 | 0:25d52c6a3448 | 200 | } |
846354866 | 0:25d52c6a3448 | 201 | return data_byte; |
846354866 | 0:25d52c6a3448 | 202 | } |
846354866 | 0:25d52c6a3448 | 203 | |
846354866 | 3:dc786e394e82 | 204 | void receiveDht11() |
846354866 | 0:25d52c6a3448 | 205 | { |
846354866 | 0:25d52c6a3448 | 206 | unsigned char T_H,T_L,R_H,R_L,check,num_check; |
846354866 | 0:25d52c6a3448 | 207 | |
846354866 | 0:25d52c6a3448 | 208 | myIOD10.output(); |
846354866 | 0:25d52c6a3448 | 209 | myIOD10.write(0); |
846354866 | 3:dc786e394e82 | 210 | wait(0.020f); |
846354866 | 0:25d52c6a3448 | 211 | |
846354866 | 0:25d52c6a3448 | 212 | myIOD10.write(1); |
846354866 | 3:dc786e394e82 | 213 | wait(0.00002f); |
846354866 | 0:25d52c6a3448 | 214 | myIOD10.write(1); |
846354866 | 0:25d52c6a3448 | 215 | |
846354866 | 0:25d52c6a3448 | 216 | myIOD10.input(); |
846354866 | 1:900e3ba27ddb | 217 | if(!myIOD10.read()) |
846354866 | 0:25d52c6a3448 | 218 | { |
846354866 | 1:900e3ba27ddb | 219 | while(!myIOD10.read()); |
846354866 | 1:900e3ba27ddb | 220 | while(myIOD10.read()); |
846354866 | 3:dc786e394e82 | 221 | R_H=receiveByte(); |
846354866 | 3:dc786e394e82 | 222 | R_L=receiveByte(); |
846354866 | 3:dc786e394e82 | 223 | T_H=receiveByte(); |
846354866 | 3:dc786e394e82 | 224 | T_L=receiveByte(); |
846354866 | 3:dc786e394e82 | 225 | check=receiveByte(); |
846354866 | 3:dc786e394e82 | 226 | wait(0.00004f); |
846354866 | 3:dc786e394e82 | 227 | num_check=R_H+R_L+T_H+T_L; |
846354866 | 1:900e3ba27ddb | 228 | if(num_check==check) |
846354866 | 0:25d52c6a3448 | 229 | { |
846354866 | 0:25d52c6a3448 | 230 | RH=R_H; |
846354866 | 0:25d52c6a3448 | 231 | RL=R_L; |
846354866 | 0:25d52c6a3448 | 232 | TH=T_H; |
846354866 | 0:25d52c6a3448 | 233 | TL=T_L; |
846354866 | 0:25d52c6a3448 | 234 | check=num_check; |
846354866 | 0:25d52c6a3448 | 235 | } |
846354866 | 0:25d52c6a3448 | 236 | } |
846354866 | 0:25d52c6a3448 | 237 | |
846354866 | 3:dc786e394e82 | 238 | receiveDht11Timeout.attach(&receiveDht11,0.2f); |
846354866 | 0:25d52c6a3448 | 239 | } |