web iot espressif

Dependencies:   WIZWebtInterface mbed

Files at this revision

API Documentation at this revision

Comitter:
846354866
Date:
Wed Nov 16 05:25:55 2016 +0000
Parent:
2:0523b50f44fd
Child:
4:f975091219a2
Commit message:
web iot for espressif(no chinese) version0.2

Changed in this revision

WIZnetInterface.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
wizHTML.h Show annotated file Show diff for this revision Revisions of this file
--- a/WIZnetInterface.lib	Fri Nov 04 08:11:34 2016 +0000
+++ b/WIZnetInterface.lib	Wed Nov 16 05:25:55 2016 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/teams/WIZnet/code/WIZnetInterface/#c91884bd2713
+http://developer.mbed.org/teams/WIZnet/code/WIZnetInterface/#d4ed4f463f3d
--- a/main.cpp	Fri Nov 04 08:11:34 2016 +0000
+++ b/main.cpp	Wed Nov 16 05:25:55 2016 +0000
@@ -1,17 +1,18 @@
 #include "mbed.h"
 #include "EthernetInterface.h"
+#include "wizHTML.h"
 #include <stdio.h>
 #include <string.h>
 
 
 #define MAC     "\x00\x08\xDC\x11\x34\x78"
-#define IP      "192.168.1.123"
+#define IP      "192.168.1.12"
 #define MASK    "255.255.255.0"
 #define GATEWAY "192.168.1.1"
 
 #define HTTPD_SERVER_PORT   80
-#define HTTPD_MAX_REQ_LENGTH   1023
-#define HTTPD_MAX_HDR_LENGTH   255
+#define HTTPD_MAX_REQ_LENGTH   102
+#define HTTPD_MAX_HDR_LENGTH   96
 #define HTTPD_MAX_FNAME_LENGTH   127
 #define HTTPD_MAX_DNAME_LENGTH   127
 
@@ -21,21 +22,15 @@
 DigitalIn myInD11(D11);
 DigitalOut myOutD12(D12);
 AnalogIn myInA1(A1);
-Timeout button_ledTimeout;
-Timeout receive_dht11Timeout;
+Timeout buttonLedTimeout;
+Timeout receiveDht11Timeout;
 
 unsigned char RH,RL,TH,TL;
 unsigned char data_byte;
 unsigned int  U8FLAG;
-unsigned char status_led;
-unsigned char seaver_ip[] = IP;
-char led_control[6] = "cloce";
-
-
-//SDFileSystem sd(p5, p6, p7, p8, "sd"); // LPC1768 MBD2PMD
-//SDFileSystem sd(P0_18, P0_17, P0_15, P0_16, "sd"); // Seeeduino Arch Pro SPI2SD
-//SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // K64F
-//SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "sd"); // WIZwiki-W7500 
+char seaver_ip[] = IP;
+char led_control[6];
+unsigned int loopCnt = 10000;
 
 EthernetInterface eth;
 TCPSocketServer server;
@@ -49,28 +44,10 @@
 char *eou;
 char *qrystr;
 
-
-Ticker ledTick;
-
-void receive_dht11();
-unsigned char receive_byte();
+void receiveDht11();
+unsigned char receiveByte();
 
-void delay(unsigned char ms)
-{ 
-    unsigned int y;
-    while(ms--)
-        for(y=1920; y>0; y--);
-}
-
-void Delay_us(unsigned char us)
-{
-    unsigned char y;
-    while(us--)
-        for(y=2; y>0; y--);
-}
-
-
-void button_led()
+void buttonLed()
 {  
     while(myInD11.read())
     {
@@ -78,244 +55,162 @@
         if(!myInD11.read())
         {
             myOutD12.write(!myOutD12.read());
-            status_led = myOutD12.read();
             break;
         }
     }
     
-    button_ledTimeout.attach(&button_led,0.1f);
+    buttonLedTimeout.attach(&buttonLed,0.1f);
 }
 
 void getFile(char* uri)
 {
+    static char string_c[24] = {0};
+    char* Led;
+    char* ledS;
+    int gas = myInA1.read_u16();
     uart.printf("getFile %s\n", uri);
-    strcpy(led_control, &uri[1]);
-    //receive_dht11();
+    if(memcmp(&uri[1], "favicon.ico", 11)==0){
+        return;
+    }
+    strncpy(led_control, &uri[1], 5);
     char *lstchr = strrchr(uri, NULL) -1;
-    sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
-    client.send(httpHeader,strlen(httpHeader));
-    sprintf(httpHeader,"<html><head><title>hello dfrobot</title></head><body><h1> Information State</h1><ul>");
-    client.send(httpHeader,strlen(httpHeader));
-       
-    sprintf(httpHeader,"<H1>Temperature:%d  Celsius</H1>", TH);
-    client.send(httpHeader,strlen(httpHeader));
-    sprintf(httpHeader,"<H1>Humidity:%d  %%</H1>", RH);
-    client.send(httpHeader,strlen(httpHeader));
-    sprintf(httpHeader,"<H1>Gas Value:%d</H1>", myInA1.read_u16());
-    client.send(httpHeader,strlen(httpHeader));
     
-    static char i = 0, j = 0, open_1 = 1, cloce_1 = 1, k;
-    if (strncmp(led_control, "cloce", 5)==0)
-     {uart.printf("status_led--cloce--- %d\n", status_led);
-        if(cloce_1)
-        {
-            sprintf(httpHeader,"<H1>Led state:CLOCE</H1>");
-            client.send(httpHeader,strlen(httpHeader));
+    static char open_1 = 1, cloce_1 = 1;
+    if (memcmp(led_control, "offon", 5)==0){
+        if (cloce_1){
             myOutD12.write(0);   
-            sprintf(httpHeader,"<form action=\"http://%s/open\"><input type=\"submit\" value=\"ON/OFF\"></form>", seaver_ip);
-            client.send(httpHeader,strlen(httpHeader));
-            
-            j = 1;
-            k = status_led;
             cloce_1 = 0;
         }
-         else if (k == status_led && j)
-         {
-            sprintf(httpHeader,"<H1>Led state:CLOCE</H1>");
-            client.send(httpHeader,strlen(httpHeader));
-            myOutD12.write(0);   
-            sprintf(httpHeader,"<form action=\"http://%s/open\"><input type=\"submit\" value=\"ON/OFF\"></form>", seaver_ip);
-            client.send(httpHeader,strlen(httpHeader));
-            j = 0;
-        }
-        else if(status_led && !j)
-        {
-            sprintf(httpHeader,"<H1>Led state:OPEN</H1>");
-            client.send(httpHeader,strlen(httpHeader));
-            myOutD12.write(1);
-            sprintf(httpHeader,"<form action=\"http://%s/cloce\"><input type=\"submit\" value=\"ON/OFF\"></form>", seaver_ip);
-            client.send(httpHeader,strlen(httpHeader));
-        }
-        else if(!status_led && !j)
-        {
-            sprintf(httpHeader,"<H1>Led state:CLOCE</H1>");
-            client.send(httpHeader,strlen(httpHeader));
-            myOutD12.write(0);   
-            sprintf(httpHeader,"<form action=\"http://%s/open\"><input type=\"submit\" value=\"ON/OFF\"></form>", seaver_ip);
-            client.send(httpHeader,strlen(httpHeader));
-        }
         
         open_1 = 1;
      }
-     else if (strncmp(led_control, "open", 4)==0)
-     {uart.printf("status_led--open--- %d\n", status_led);
-        if(open_1)
-        {
-            sprintf(httpHeader,"<H1>Led state:OPEN</H1>");
-            client.send(httpHeader,strlen(httpHeader));
-            myOutD12.write(1);
-            sprintf(httpHeader,"<form action=\"http://%s/cloce\"><input type=\"submit\" value=\"ON/OFF\"></form>", seaver_ip);
-            client.send(httpHeader,strlen(httpHeader));
-        
-            i = 1;
-            k = status_led;
-            open_1 = 0;
-        }
-        else if(k == status_led && i)
-        {
-            sprintf(httpHeader,"<H1>Led state:OPEN</H1>");
-            client.send(httpHeader,strlen(httpHeader));
+     else if (memcmp(led_control, "onoff", 5)==0){
+        if (open_1){
             myOutD12.write(1);
-            sprintf(httpHeader,"<form action=\"http://%s/cloce\"><input type=\"submit\" value=\"ON/OFF\"></form>", seaver_ip);
-            client.send(httpHeader,strlen(httpHeader));
-            i = 0;
-        }
-        else if (status_led && !i)// 0
-         {
-            sprintf(httpHeader,"<H1>Led state:OPEN</H1>");
-            client.send(httpHeader,strlen(httpHeader));
-            myOutD12.write(1);
-            sprintf(httpHeader,"<form action=\"http://%s/cloce\"><input type=\"submit\" value=\"ON/OFF\"></form>", seaver_ip);
-            client.send(httpHeader,strlen(httpHeader));
-        }
-        else if (!status_led && !i)
-        {
-            sprintf(httpHeader,"<H1>Led state:CLOCE</H1>");
-            client.send(httpHeader,strlen(httpHeader));
-            myOutD12.write(0);   
-            sprintf(httpHeader,"<form action=\"http://%s/open\"><input type=\"submit\" value=\"ON/OFF\"></form>", seaver_ip);
-            client.send(httpHeader,strlen(httpHeader));
+            open_1 = 0;
         }
         
         cloce_1 = 1;
-     }
+    }
+    if(myOutD12.read()){
+        Led  = ledSc[0];
+        ledS = ledSc[1];
+        sprintf(string_c,"%s/offon", seaver_ip);
+    }else{
+        Led  = ledSc[2];
+        ledS = ledSc[3];  
+        sprintf(string_c,"%s/onoff", seaver_ip);  
+    }
+    htmlWeb(TH, TH*5, RH, RH*5, gas, gas*0.05f, Led, ledS, string_c);
+    sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
+    client.send(httpHeader,strlen(httpHeader));
     
-    if (strncmp(led_control, "", 1)==0)
-    {
-        if(myOutD12.read())
-        {
-            sprintf(httpHeader,"<H1>Led state:OPEN</H1>");
-            client.send(httpHeader,strlen(httpHeader));
-            myOutD12.write(1);
-        
-            sprintf(httpHeader,"<form action=\"http://%s/cloce\"><input type=\"submit\" value=\"ON/OFF\"></form>", seaver_ip);
-            client.send(httpHeader,strlen(httpHeader));
-            status_led = 1;
-        }
-        else
-        {
-            sprintf(httpHeader,"<H1>Led state:CLOCE</H1>");
-            client.send(httpHeader,strlen(httpHeader));
-            myOutD12.write(0);   
-        
-            sprintf(httpHeader,"<form action=\"http://%s/open\"><input type=\"submit\" value=\"ON/OFF\"></form>", seaver_ip);
-            client.send(httpHeader,strlen(httpHeader));
-            status_led = 0;
-        }    
+    for (int i=0; i<sizeof(html); i += 96){
+        memcpy(httpHeader, &html[i], 96);
+        client.send(httpHeader,strlen(httpHeader));
+        //wait(0.00001);
     }
     
-    sprintf(httpHeader,"<meta http-equiv=\"refresh\" content=\"1\">");
-    client.send(httpHeader,strlen(httpHeader));
-    
-    sprintf(httpHeader,"</ul></body></html>");
-    client.send(httpHeader,strlen(httpHeader));
+    memset(string_c, 0, sizeof(string_c));
+    memset(html, 0, sizeof(html));
 }
 
 int main (void)
 {
     uart.baud(115200);
     uart.printf("Initializing\r\n");
-//    EthernetInterface eth;
     uart.printf("Initializing Ethernet\r\n");
-    //eth.init(); //Use DHCP
-    //eth.init((uint8_t*)MAC);    // Use DHCP for WIZnetInterface
-    eth.init((uint8_t*)MAC,IP,MASK,GATEWAY);  //IP,mask,Gateway
-    uart.printf("Connecting\r\n");
-    eth.connect();
-    uart.printf("IP Address is %s\r\n", eth.getIPAddress());
+    buttonLedTimeout.attach(&buttonLed,0.1f);
+    receiveDht11Timeout.attach(&receiveDht11,0.2f);
+    while (true) {
+        //eth.init(); //Use DHCP
+        eth.init((uint8_t*)MAC,IP,MASK,GATEWAY);  //IP,mask,Gateway
+        uart.printf("Connecting\r\n");
+        eth.connect();
+        uart.printf("IP Address is %s\r\n", eth.getIPAddress());
 
-//    TCPSocketServer server;
-    server.bind(HTTPD_SERVER_PORT);
-    server.listen();
-    uart.printf("Server Listening\r\n");
-    
-    button_ledTimeout.attach(&button_led,0.1f);
-    receive_dht11Timeout.attach(&receive_dht11,0.2f);
-
-    while (true) {
-        uart.printf("\nWait for new connection...\r\n");
-        server.accept(client);
-        client.set_blocking(false, 1500); // Timeout after (1.5)s
+        server.bind(HTTPD_SERVER_PORT);
+        server.listen();
+        uart.printf("Server Listening\r\n");
 
-        uart.printf("Connection from: %s\r\n", client.get_address());
-        uart.printf("%s():%d\n",__FUNCTION__, __LINE__);
         while (true) {
-            //led2 = true;
-            int n = client.receive(buffer, sizeof(buffer));
-            if (n <= 0) break;
-            uart.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer);
-            if (n >= 1024) {
-                sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
-                client.send(httpHeader,strlen(httpHeader));
-                client.send(buffer,n);
-                break;
-            } else {
-                uart.printf("%s():%d\n",__FUNCTION__, __LINE__);
-                buffer[n]=0;
-            }
-            if (!strncmp(buffer, "GET ", 4)) {
-                uart.printf("%s():%d\n",__FUNCTION__, __LINE__);
-                uristr = buffer + 4;
-                eou = strstr(uristr, " ");
-                if (eou == NULL) {
-                            uart.printf("%s():%d\n",__FUNCTION__, __LINE__);
-                    sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
+            uart.printf("\nWait for new connection...\r\n");
+        
+            client.set_blocking(false, 500); // Timeout after (0.5)s
+            server.accept(client);
+
+            uart.printf("Connection from: %s\r\n", client.get_address());
+            while (true) {
+                int n = client.receive(buffer, sizeof(buffer));
+                if (n <= 0) {
+                    uart.printf("return value n<=0\n");
+                    break;
+                }
+                uart.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,buffer);
+                if (n >= 1024) {
+                    sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
                     client.send(httpHeader,strlen(httpHeader));
                     client.send(buffer,n);
+                    break;
                 } else {
-                            uart.printf("%s():%d\n",__FUNCTION__, __LINE__);
-                    *eou = 0;
-                    getFile(uristr);
+                    buffer[n]=0;
+                }
+                if (!memcmp(buffer, "GET ", 4)) {
+                    uristr = buffer + 4;
+                    eou = strstr(uristr, " ");
+                    if (eou == NULL) {
+                        sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
+                        client.send(httpHeader,strlen(httpHeader));
+                        client.send(buffer,n);
+                    } else {
+                        *eou = 0;
+                        getFile(uristr);
+                    }
                 }
             }
+            client.close();
+            break;
         }
-        //led2 = false;
-        client.close();
     }
 }
 
-unsigned char receive_byte()
+unsigned char receiveByte()
 {
     unsigned char i,temp;
     for(i=0;i<8;i++)
     {
         U8FLAG = 2;
-        while(!myIOD10.read() && U8FLAG++);
-        Delay_us(30);
+        loopCnt = 10000;
+        while(!myIOD10.read() && U8FLAG++){
+            if (loopCnt-- == 0) return 2;    
+        }
+        wait(0.00003f);
         temp=0;
         if(myIOD10.read()==1)
         temp=1;
         U8FLAG = 2;
-        while(myIOD10.read() && U8FLAG++);
+        loopCnt = 10000;
+        while(myIOD10.read() && U8FLAG++){
+            if (loopCnt-- == 0) return 2;    
+        }
         
         if(U8FLAG==1)break;
-        data_byte<<=1;//
+        data_byte<<=1;
         data_byte|=temp;
     }
     return data_byte;
 }
 
-void receive_dht11()
+void receiveDht11()
 {
     unsigned char T_H,T_L,R_H,R_L,check,num_check;
     
-   
     myIOD10.output();
     myIOD10.write(0);
-    delay(20);
+    wait(0.020f);
     
     myIOD10.write(1);
-    Delay_us(25);
+    wait(0.00002f);
     myIOD10.write(1);
     
     myIOD10.input();
@@ -323,14 +218,13 @@
     {
         while(!myIOD10.read());
         while(myIOD10.read());
-        R_H=receive_byte();
-        R_L=receive_byte();
-        T_H=receive_byte();
-        T_L=receive_byte();
-        check=receive_byte();
-        uart.printf("--R_H--%d---R_L---%d--T_H--%d---T_L---%d\r\n", R_H,R_L,T_H,T_L);
-        Delay_us(20);
-        num_check=R_H+R_L+T_H+T_L;uart.printf("--num_check--%d---check---%d\r\n", num_check,check);
+        R_H=receiveByte();
+        R_L=receiveByte();
+        T_H=receiveByte();
+        T_L=receiveByte();
+        check=receiveByte();
+        wait(0.00004f);
+        num_check=R_H+R_L+T_H+T_L;
         if(num_check==check)
         {
             RH=R_H;
@@ -341,10 +235,5 @@
         }
     }
     
-    uart.printf("humidity [H] -out--%d\r\n", RH);
-    uart.printf("humidity [L] -out--%d\r\n", RL);
-    uart.printf("temperature [H] -out--%d\r\n", TH);
-    uart.printf("temperature [L] -out--%d\r\n", TL);
-    
-    receive_dht11Timeout.attach(&receive_dht11,0.2f);
+    receiveDht11Timeout.attach(&receiveDht11,0.2f);
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wizHTML.h	Wed Nov 16 05:25:55 2016 +0000
@@ -0,0 +1,78 @@
+static char ledSc[4][7] = {"open.", "FF0000", "close", "000000"};
+static char html[3072]={0};
+void htmlWeb(int Temp,int tempW,int Hum,int humW,int Gas,float gasW,char* Led,char* ledS,char* ip)
+{
+    sprintf(html, "<!DOCTYPE html PUBLIC '-//DTD HTML 4.01//EN' 'http://www.dfrobot.com.cn'>\
+        <html>\
+        <body bgcolor='#cccc99'></body>\
+        <head><title>Hi DFrobot</title></head>\
+        <h1>W7500P Data Monitor</h1>\
+        <hr style='width: 300; height: 2px;'>\
+        \
+        <table style='width: 500px; height: 30px;' border='1' cellpadding='2' cellspacing='2'>\
+        <tbody>\
+        <tr>\
+        <td style='font-family: Verdana; font-weight: bold; font-style: italic; background-color: rgb(100, 100, 255); text-align: center;'><small>\
+        <span style='color: white;'>Data&Control\
+        </span></small></td>\
+        </tr>\
+        </tbody>\
+        </table>\
+        \
+        <tbody>\
+        <table bgcolor='#cccccc' border='1' cellpadding='0' cellspacing='0' width='500' height='22';>Temperature:%d Celsius\
+        <tbody><tr><td>\
+        <table border='0' cellpadding='0' cellspacing='0' width='%d'>\
+        <td bgcolor='#33ff00'>&nbsp;\
+        </td></tr></tbody>\
+        </table>\
+        </tbody>\
+        \
+        <tbody>\
+        <table bgcolor='#cccccc' border='1' cellpadding='0' cellspacing='0' width='500' height='22';><p></p>Humidity:%d%%\
+        <tbody><tr><td>\
+        <table border='0' cellpadding='0' cellspacing='0' width='%d'>\
+        <td bgcolor='#33ff00'>&nbsp;\
+        </td></tr></tbody>\
+        </table>\
+        </tbody>\
+        \
+        <tbody>\
+        <table bgcolor='#cccccc' border='1' cellpadding='0' cellspacing='0' width='500' height='22';><p></p>Gas Value:%d\
+        <tbody><tr><td>\
+        <table border='0' cellpadding='0' cellspacing='0' width='%.0f'>\
+        <td bgcolor='#33ff00'>&nbsp;\
+        </td></tr></tbody>\
+        </table>\
+        </tbody>\
+        \
+        <tbody>\
+        <table bgcolor='#cccccc' border='1' cellpadding='0' cellspacing='0' height='20';><p></p>Led state:%s\
+        <tbody><tr><td>\
+        <table border='0' cellpadding='0' cellspacing='0' width='115'>\
+        <td bgcolor='#%s'>&nbsp;\
+        </td></tr></tbody>\
+        </table>\
+        </tbody>\
+        \
+        <tbody>\
+        <table border='0' width='600'>\
+        <tbody><tr>\
+        <td width='0'></td>\
+        <hr />\
+        </tr></tbody>\
+        </table>\
+        </tbody>\
+        \
+        <tbody>\
+        <table style='width: 130px; color: #873422; height: 30px;'>\
+        <tbody><tr>\
+        <td ><big><big><font size='-1'><big><big><strong>LED control</font></big></big></td>\
+        </tr></tbody>\
+        </table>\
+        \
+        <form action='http://%s'><H3>Led:<input type='submit' value='ON/OFF'><H3></form>\
+        </tbody>\
+        <meta http-equiv='refresh' content='2'>\
+        </html>\0", Temp, tempW, Hum, humW, Gas, gasW, Led, ledS, ip);
+}
\ No newline at end of file