web iot espressif
Dependencies: WIZWebtInterface mbed
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 #include "wizHTML.h" 00004 #include <stdio.h> 00005 #include <string.h> 00006 00007 00008 #define MAC "\x00\x08\xDC\x11\x34\x78" 00009 #define IP "192.168.1.12" 00010 #define MASK "255.255.255.0" 00011 #define GATEWAY "192.168.1.1" 00012 00013 #define HTTPD_SERVER_PORT 80 00014 #define HTTPD_MAX_REQ_LENGTH 102 00015 #define HTTPD_MAX_HDR_LENGTH 96 00016 #define HTTPD_MAX_FNAME_LENGTH 127 00017 #define HTTPD_MAX_DNAME_LENGTH 127 00018 00019 00020 Serial uart(PA_13,PA_14); 00021 DigitalInOut myIOD10(D10); 00022 DigitalIn myInD11(D11); 00023 DigitalOut myOutD12(D12); 00024 AnalogIn myInA1(A1); 00025 Timeout buttonLedTimeout; 00026 Timeout receiveDht11Timeout; 00027 00028 unsigned char RH,RL,TH,TL; 00029 unsigned char data_byte; 00030 unsigned int U8FLAG; 00031 char seaver_ip[] = IP; 00032 char led_control[6]; 00033 unsigned int loopCnt = 10000; 00034 00035 EthernetInterface eth; 00036 TCPSocketServer server; 00037 TCPSocketConnection client; 00038 00039 char buffer[HTTPD_MAX_REQ_LENGTH+1]; 00040 char httpHeader[HTTPD_MAX_HDR_LENGTH+1]; 00041 char fileName[HTTPD_MAX_FNAME_LENGTH+1]; 00042 char dirName[HTTPD_MAX_DNAME_LENGTH+1]; 00043 char *uristr; 00044 char *eou; 00045 char *qrystr; 00046 00047 void receiveDht11(); 00048 unsigned char receiveByte(); 00049 00050 void buttonLed() 00051 { 00052 while(myInD11.read()) 00053 { 00054 wait(0.01f); 00055 if(!myInD11.read()) 00056 { 00057 myOutD12.write(!myOutD12.read()); 00058 break; 00059 } 00060 } 00061 00062 buttonLedTimeout.attach(&buttonLed,0.1f); 00063 } 00064 00065 void getFile(char* uri) 00066 { 00067 static char string_c[24] = {0}; 00068 char* Led; 00069 char* ledS; 00070 int gas = myInA1.read_u16(); 00071 uart.printf("getFile %s\n", uri); 00072 if(memcmp(&uri[1], "favicon.ico", 11)==0){ 00073 return; 00074 } 00075 strncpy(led_control, &uri[1], 5); 00076 char *lstchr = strrchr(uri, NULL) -1; 00077 00078 static char open_1 = 1, cloce_1 = 1; 00079 if (memcmp(led_control, "offon", 5)==0){ 00080 if (cloce_1){ 00081 myOutD12.write(0); 00082 cloce_1 = 0; 00083 } 00084 00085 open_1 = 1; 00086 } 00087 else if (memcmp(led_control, "onoff", 5)==0){ 00088 if (open_1){ 00089 myOutD12.write(1); 00090 open_1 = 0; 00091 } 00092 00093 cloce_1 = 1; 00094 } 00095 if(myOutD12.read()){ 00096 Led = ledSc[0]; 00097 ledS = ledSc[1]; 00098 sprintf(string_c,"%s/offon", seaver_ip); 00099 }else{ 00100 Led = ledSc[2]; 00101 ledS = ledSc[3]; 00102 sprintf(string_c,"%s/onoff", seaver_ip); 00103 } 00104 htmlWeb(TH, TH*5, RH, RH*5, gas, gas*0.05f, Led, ledS, string_c); 00105 sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n"); 00106 client.send(httpHeader,strlen(httpHeader)); 00107 00108 for (int i=0; i<sizeof(html); i += 96){ 00109 memcpy(httpHeader, &html[i], 96); 00110 client.send(httpHeader,strlen(httpHeader)); 00111 //wait(0.00001); 00112 } 00113 00114 memset(string_c, 0, sizeof(string_c)); 00115 memset(html, 0, sizeof(html)); 00116 } 00117 00118 int main (void) 00119 { 00120 uart.baud(115200); 00121 uart.printf("Initializing\r\n"); 00122 uart.printf("Initializing Ethernet\r\n"); 00123 buttonLedTimeout.attach(&buttonLed,0.1f); 00124 receiveDht11Timeout.attach(&receiveDht11,0.2f); 00125 while (true) { 00126 //eth.init(); //Use DHCP 00127 eth.init((uint8_t*)MAC,IP,MASK,GATEWAY); //IP,mask,Gateway 00128 uart.printf("Connecting\r\n"); 00129 eth.connect(); 00130 uart.printf("IP Address is %s\r\n", eth.getIPAddress()); 00131 00132 server.bind(HTTPD_SERVER_PORT); 00133 server.listen(); 00134 uart.printf("Server Listening\r\n"); 00135 00136 while (true) { 00137 uart.printf("\nWait for new connection...\r\n"); 00138 00139 client.set_blocking(false, 500); // Timeout after (0.5)s 00140 server.accept(client); 00141 00142 uart.printf("Connection from: %s\r\n", client.get_address()); 00143 while (true) { 00144 int n = client.receive(buffer, sizeof(buffer)); 00145 if (n <= 0) { 00146 uart.printf("return value n<=0\n"); 00147 break; 00148 } 00149 uart.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,buffer); 00150 if (n >= 1024) { 00151 sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); 00152 client.send(httpHeader,strlen(httpHeader)); 00153 client.send(buffer,n); 00154 break; 00155 } else { 00156 buffer[n]=0; 00157 } 00158 if (!memcmp(buffer, "GET ", 4)) { 00159 uristr = buffer + 4; 00160 eou = strstr(uristr, " "); 00161 if (eou == NULL) { 00162 sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); 00163 client.send(httpHeader,strlen(httpHeader)); 00164 client.send(buffer,n); 00165 } else { 00166 *eou = 0; 00167 getFile(uristr); 00168 } 00169 } 00170 } 00171 client.close(); 00172 break; 00173 } 00174 } 00175 } 00176 00177 unsigned char receiveByte() 00178 { 00179 unsigned char i,temp; 00180 for(i=0;i<8;i++) 00181 { 00182 U8FLAG = 2; 00183 loopCnt = 10000; 00184 while(!myIOD10.read() && U8FLAG++){ 00185 if (loopCnt-- == 0) return 2; 00186 } 00187 wait(0.00003f); 00188 temp=0; 00189 if(myIOD10.read()==1) 00190 temp=1; 00191 U8FLAG = 2; 00192 loopCnt = 10000; 00193 while(myIOD10.read() && U8FLAG++){ 00194 if (loopCnt-- == 0) return 2; 00195 } 00196 00197 if(U8FLAG==1)break; 00198 data_byte<<=1; 00199 data_byte|=temp; 00200 } 00201 return data_byte; 00202 } 00203 00204 void receiveDht11() 00205 { 00206 unsigned char T_H,T_L,R_H,R_L,check,num_check; 00207 00208 myIOD10.output(); 00209 myIOD10.write(0); 00210 wait(0.020f); 00211 00212 myIOD10.write(1); 00213 wait(0.00002f); 00214 myIOD10.write(1); 00215 00216 myIOD10.input(); 00217 if(!myIOD10.read()) 00218 { 00219 while(!myIOD10.read()); 00220 while(myIOD10.read()); 00221 R_H=receiveByte(); 00222 R_L=receiveByte(); 00223 T_H=receiveByte(); 00224 T_L=receiveByte(); 00225 check=receiveByte(); 00226 wait(0.00004f); 00227 num_check=R_H+R_L+T_H+T_L; 00228 if(num_check==check) 00229 { 00230 RH=R_H; 00231 RL=R_L; 00232 TH=T_H; 00233 TL=T_L; 00234 check=num_check; 00235 } 00236 } 00237 00238 receiveDht11Timeout.attach(&receiveDht11,0.2f); 00239 }
Generated on Wed Jul 13 2022 10:50:26 by 1.7.2