HTTP server is created by connecting an ENC28J60 module to the mbed board. It is serving a webpage which enables remotely turn on/off LED1 (or other device). Compile, download, run and type 192.168.0.170/secret/ into your web browser and Flot Interactivity Graphique

Dependencies:   UIPEthernet mbed FCT_WEB hebergement

Fork of WebSwitch_ENC28J60 by Zoltan Hudak

Page généré : /media/uploads/Fo170/webservernucleo.png

P.S : 1ère mise en fonctionnement de la carte NUCLEO STM32F411RET6 Instruction pour la mise en fonctionnement : https://developer.mbed.org/users/Fo170/notebook/the-stm32-nucleo-64-board/

Vue d'ensemble : /media/uploads/Fo170/vue_d_ensemble_1.jpg

/media/uploads/Fo170/vue_d_ensemble_2.jpg

Vue de la carte ENC28J60 : /media/uploads/Fo170/carte_enc28j60_a.jpg

/media/uploads/Fo170/carte_enc28j60_b.jpg

Carte Nucléo : /media/uploads/Fo170/nucleo_stm32f411re.jpg

Revision:
14:671fa04504c2
Parent:
13:c8b148c37fc4
Child:
15:b241b1fccd1f
--- a/main.cpp	Wed Aug 05 12:45:44 2015 +0000
+++ b/main.cpp	Wed Aug 19 11:05:49 2015 +0000
@@ -19,9 +19,9 @@
 // PB_3 (connectique Morpho) : SCK
 // PB_6 (connectique Morpho) : CS
 
-DigitalOut myled(LED1);
+DigitalOut  myled(LED1);
 AnalogIn    a_in(PC_5); // PC_5 au lieu de PC_0
-DigitalIn boton(USER_BUTTON);
+DigitalIn   button_usr(USER_BUTTON);
 
 using namespace std;
 
@@ -53,31 +53,24 @@
 // or instead of using LED1 pin, select a free pin (not used by SPI port) and connect to it an external LED which is connected to a resitor that is connected to the groud.
 // In the second case remember to replace LED1 in sw(LED1) constructor (see below).
 
-
 // MAC number must be unique within the connected network. Modify as appropriate.
 const uint8_t    MY_MAC[6] = {0x00,0x01,0x02,0x03,0x04,0x06};
 
 // IP address must be also unique and compatible with your network. Change as appropriate.
-const IPAddress  MY_IP(192,168,0,170);
-const string __IP_LOCAL__                             =  "192.168.0.170";
-const string __hebergement__                          =  "http://olivier.fournet.free.fr/electronique/e/WebServerNucleo/js/";
-const string __Time_between_page_refresh__            =  "1";
-
-
-// Logo Test d'image en base64 :
-// http://webcodertools.com/imagetobase64converter/Create
-const string __Logo_image__ =  "<img alt='' src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAOwwAADsMBx2+oZAAAABp0RVh0U29mdHdhcmUAUGFpbnQuTkVUIHYzLjUuMTAw9HKhAAAAUklEQVQ4T2NggIEGMIBzCTDgSh0cHAjrIcFgiGFoGoCWELYQrgLIgGsg4CtkaTibWNfu378fq2tpHx6EbcAMQ8J6iPU3cpIhbCqaisFpCTwVAwB5lit+0ltbrgAAAABJRU5ErkJggg=='>";
+const IPAddress  MY_IP(10,0,0,170);
+#define  __IP_LOCAL__                     "10.0.0.170"
 
-const string __image_Password_Folder__  = "<img alt='' src='http://olivier.fournet.free.fr/jpg/password_folder.jpg'>";
+/* Maison
+const IPAddress  MY_IP(192,168,0,170);
+#define  __IP_LOCAL__                     "192.168.0.170"
+*/
+const string PASSWORD     = "secret";   // change as you like
 
-//const string __image_301_Moved_Permanently__  = "<img alt='' src='http://olivier.fournet.free.fr/jpg/301_moved_permanently.jpg'>";
-#define __image_301_Moved_Permanently__           "<img alt='' src='http://olivier.fournet.free.fr/jpg/301_moved_permanently.jpg'>"
-const string str_moved_perm   = "<h1>301 Moved Permanently " __image_301_Moved_Permanently__ "</h1>\r\n";
+// In this example we are turning on/off LED1.
+DigitalOut       sw(LED1);  // Change LED1 to a pin of your choice. However, make sure that it does not collide with any of the SPI pins already used in the UIPEthernet(...) constructor above!
 
-//const string __image_401_Unauthorized__       = "<img alt='' src='http://olivier.fournet.free.fr/png/401_Unauthorized.png'>";
-#define __image_401_Unauthorized__                "<img alt='' src='http://olivier.fournet.free.fr/png/401_Unauthorized.png'>"
-const string str_Unauthorized = "<h1>401 Unauthorized " __image_401_Unauthorized__ "</h1>\r\n";
-
+#include <hebergement.h>
+#include <Fct_Web.h>
 
 #define NB_SAMPLES    10
 unsigned long int Sample = 0;
@@ -120,71 +113,7 @@
     time_samples[NB_SAMPLES-1] = Seconds;
 }
 //------------
-const uint16_t   MY_PORT = 80;      // for HTTP connection
-EthernetServer   myServer = EthernetServer(MY_PORT);
-// In this example we are turning on/off LED1.
-DigitalOut       sw(LED1);  // Change LED1 to a pin of your choice. However, make sure that it does not collide with any of the SPI pins already used in the UIPEthernet(...) constructor above!
 
-const string PASSWORD     = "secret";   // change as you like
-const string HTTP_OK      = "HTTP/1.0 200 OK";
-const string MOVED_PERM   = "HTTP/1.0 301 Moved Permanently\r\nLocation: ";
-const string UNAUTHORIZED = "HTTP/1.0 401 Unauthorized";
-string       httpHeader;     // HTTP header
-string       httpContent;    // HTTP content
-
-
-// analyse the url given
-// return values: -1 invalid password
-//                -2 no command given but password valid
-//                -3 just refresh page
-//                 0 switch off
-//                 1 switch on
-//
-//                The string passed to this function will look like this:
-//                GET /password HTTP/1.....
-//                GET /password/ HTTP/1.....
-//                GET /password/?sw=1 HTTP/1.....
-//                GET /password/?sw=0 HTTP/1.....
-
-int8_t analyse_get_url(string& str)
-{
-    if(str.substr(5, PASSWORD.size()) != PASSWORD)
-        return(-1);
-
-    uint8_t pos = 5 + PASSWORD.size();
-
-    if(str.substr(pos, 1) == " ") return(-2);
-
-    if(str.substr(pos, 1) != "/") return(-1);
-
-    pos++;
-
-    string cmd(str.substr(pos, 5));
-
-    if(cmd == "?sw=0")  return(0);
-
-    if(cmd == "?sw=1")  return(1);
-
-    return(-3);
-}
-
-
-    
-string& moved_perm(uint8_t flag)
-{
-    if(flag == 1)
-        httpContent = "/" +  PASSWORD + "/";
-    else
-        httpContent = "";
- /*
-    httpContent += "<h1>301 Moved Permanently ";
-    httpContent += __image_301_Moved_Permanently__;
-    httpContent += "</h1>\r\n";
-    */
-    httpContent += str_moved_perm;
-    
-    return (httpContent);
-}
 
 string& page(uint8_t status)
 {
@@ -193,17 +122,12 @@
     //char time_stamp[32];
 
     //-------------
-    httpContent = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\r\n";
-    httpContent += "<meta http-equiv=\"refresh\" content=\"";
-    httpContent += __Time_between_page_refresh__;
-    httpContent += ";url=http://";
-    httpContent += __IP_LOCAL__;
-    httpContent += "/\">\r\n";
-    httpContent += "<HTML><HEAD>\r\n";
+    httpContent = str_DOCTYPE;
+    httpContent += str_meta_refresh;
+    httpContent += "\r\n<HTML><HEAD>\r\n";
     httpContent += "<title>WEB Server Nucleo F411RE - ENC28J60 - Flot Examples: Interactivity</title>\r\n";
-    httpContent += "<script language=\"javascript\" type=\"text/javascript\" src=\"";
-    httpContent += __hebergement__;
-    httpContent += "WebServerNucleo_Interactivity_init.js\"></script>\r\n";
+    httpContent += str_favicon;
+    httpContent += str_JavaScript;
     httpContent += "<script language=\"javascript\" type=\"text/javascript\">init_WebServerNucleo_Interactivity();</script>\r\n";
     // Variables JavaScript
     httpContent += "<script language=\"javascript\" type=\"text/javascript\">\r\n";
@@ -225,13 +149,13 @@
     }
     Sample++;
     httpContent += "</script>\r\n";
- // Fin Variable JavaScript
+    // Fin Variable JavaScript
     httpContent += "</HEAD><BODY>\r\n";
     httpContent += "<center><h2>WEB Server Nucleo F411RE</h2>\r\n";
     httpContent += "<p>Designed for STM32F411RE & ENC28J60 (RTC, ADC - <a href=\"http://www.flotcharts.org/flot/examples/interacting/index.html\">Flot Examples: Interactivity</a>),\r\n";
-    httpContent += "<p>Compilation avec mBED &agrave; " __TIME__ " le " __DATE__" \r\n";   
-           
-    // httpContent += __Logo_image__ ;
+     
+    httpContent += str_Compilation_DATE_AND_TIME;       
+    // httpContent += str_Logo_image;
     
     httpContent += "<p></center>\r\n<hr><p>\r\n";
     
@@ -247,7 +171,7 @@
     httpContent += "<hr>\r\n";
     //-------------
     httpContent += "(Contact repos) \r\n";
-    if(boton)
+    if(button_usr)
     {
         httpContent += "<font color=#00FF00>BUTTON ON</font>\r\n";
     } 
@@ -281,52 +205,6 @@
     return httpContent;
 }
 
-string& page_toggle_switch(uint8_t status)
-{
-    //-------------
-    httpContent = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\r\n";
-    httpContent += "<HTML><HEAD>\r\n";
-    httpContent += "<title>WEB Server Nucleo F411RE - ENC28J60 - Password Page</title>\r\n";
-    httpContent += "</HEAD><BODY>\r\n";
-    httpContent += "<center><h2>WEB Server Nucleo F411RE - ENC28J60 - Password Page</h2></center>\r\n<p>";
-    httpContent += __image_Password_Folder__;
-    httpContent += "<p>\r\n";
-    
-    if(status == 1)
-    {
-        httpContent += "<hr><pre>\r\n  <font color=#00FF00>ON</font>";
-        httpContent += " <a href=\"./?sw=0\">[switch off]</a>\r\n";
-    }
-    else
-    {
-        httpContent += "<hr><pre>\r\n  <font color=#FF0000>OFF</font>";
-        httpContent += " <a href=\"./?sw=1\">[switch on]</a>\r\n";
-    }
-
-    httpContent += "  <a href=\".\">[refresh status]</a>\r\n";
-    httpContent += "</pre>\r\n<hr>\r\n";
-    
-    httpContent += "</BODY></HTML>";
-    //-----------
-    //wait(1);
-    return httpContent;
-}
-
-void http_send(EthernetClient& client, string& header, string& content)
-{
-    char content_length[5] = {};
-
-    header += "\r\nContent-Type: text/html\r\n";
-    header += "Content-Length: ";
-    sprintf(content_length, "%d", content.length());
-    header += string(content_length) + "\r\n";
-    header += "Pragma: no-cache\r\n";
-    header += "Connection: About to close\r\n";
-    header += "\r\n";
-    string webpage = header + content;
-    client.write((uint8_t*)webpage.c_str(),webpage.length());
-}
-
 int main()
 {
  // RTC
@@ -347,81 +225,7 @@
  // interval: 200 micro seconds chaques samples
  //time_measurement_ADC.attach_us(&measurement_ADC, TIME_SAMPLES_us);
  
- //-----------------      
-    UIPEthernet.begin(MY_MAC,MY_IP);
-    myServer.begin();
-    while(1)
-    {
-        EthernetClient client = myServer.available();
-        if(client)
-        {
-            size_t size = client.available();
-            if(size > 0)
-            {
-                uint8_t* buf = (uint8_t*)malloc(size);
-                size = client.read(buf, size);
-                string received((char*)buf);
-                free(buf);
-                if(received.substr(0, 3) != "GET")
-                {
-                    // head, post or other method
-                    // for possible status codes see:
-                    // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
-                    httpHeader = HTTP_OK;
-                    httpContent = "<h1>200 OK</h1>";
-                    http_send(client, httpHeader, httpContent);
-                    continue;
-                }
-
-                if(received.substr(0, 6) == "GET / ")
-                {
-                    httpHeader = HTTP_OK;
-                    /*
-                    httpContent = "<p>Usage: http://host_or_ip/password</p>\r\n";
-                    http_send(client, httpHeader, httpContent);
-                    */
-                    http_send(client, httpHeader, page(sw));
-                    continue;
-                }
-
-                int cmd = analyse_get_url(received);
-
-                if(cmd == -2)
-                {
-                    // redirect to the right base url
-                    httpHeader = MOVED_PERM;
-                    http_send(client, httpHeader, moved_perm(1));
-                    continue;
-                }
-
-                if(cmd == -1)
-                {
-                    httpHeader = UNAUTHORIZED;
-                    /*
-                    httpContent = "<h1>401 Unauthorized ";
-                    httpContent += __image_401_Unauthorized__;
-                    httpContent += "</h1>\r\n";
-                    */
-                    httpContent = str_Unauthorized;
-                    http_send(client, httpHeader, httpContent);
-                    continue;
-                }
-
-                if(cmd == 1)
-                { 
-                 sw = 1;     // switch on
-                }
-
-                if(cmd == 0)
-                {
-                 sw = 0;    // switch off
-                }
-
-                httpHeader = HTTP_OK;
-                http_send(client, httpHeader, page_toggle_switch(sw));
-            }
-        }
-    }
+ HTTP_LOOP();
 }