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

main.cpp

Committer:
Fo170
Date:
2015-07-25
Revision:
5:a46a2512e17e
Parent:
4:d34811deedab
Child:
6:2ce163810c2f

File content as of revision 5:a46a2512e17e:

/* In this example LED1 is switched on/off using a web browser connected to this HTTP server.
 * The example is based on the Tuxgraphics Web Switch <http://www.tuxgraphics.org/>.
 * This HTTP server is built around the the ENC28J60 chip 
 * driven by the UIPEthernet library <https://github.com/ntruchsess/arduino_uip>
 * ported to mbed.
 */

#include <mbed.h>
#include <UIPEthernet.h>
#include <UIPServer.h>
#include <UIPClient.h>
#include <string>

DigitalOut myled(LED1);
AnalogIn    a_in(PC_0);

using namespace std;

// UIPEthernet is the name of a global instance of UIPEthernetClass.
// Do not change the name! It is used within the UIPEthernet library.
#if defined(TARGET_LPC1768)
UIPEthernetClass UIPEthernet(p11, p12, p13, p8);        // mosi, miso, sck, cs
#elif defined(TARGET_LPC1114)
UIPEthernetClass UIPEthernet(dp2, dp1, dp6, dp25);      // mosi, miso, sck, cs
#elif defined(TARGET_LPC11U68)
UIPEthernetClass UIPEthernet(P0_9, P0_8, P1_29, P0_2);  // mosi, miso, sck, cs
#elif defined (TARGET_NUCLEO_F103RB)
UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6);   // mosi, miso, sck, cs
#elif defined (TARGET_NUCLEO_F401RE)
UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6);   // mosi, miso, sck, cs
#elif defined (TARGET_NUCLEO_F411RE)
UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6);   // mosi, miso, sck, cs

// If your board/plaform is not present yet then uncomment the following two lines and replace TARGET_YOUR_BOARD as appropriate.

//#elif defined (TARGET_YOUR_BOARD)
//UIPEthernetClass UIPEthernet(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS);   // mosi, miso, sck, cs

#endif

// Note:
// If it happends that any of the SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS pins collide with LED1 pin
// then either use different SPI port (if available on the board) and change the pin names in the constructor UIPEthernet(...) accordingly
// 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);
#define IPAdress_String "192.168.0.170"

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</h1>\r\n";

    return (httpContent);
}

string& page(uint8_t status)
{
    char buffer[32];
    char time_stamp[32];//as our lcd is of 16 character it is so
    //-------------
    httpContent = "<h2>Web Switch Nucleo-F411RE & ENC28J60 (RTC et ADC) ";
    httpContent += IPAdress_String "\r\n";
    
    // Test d'image en base64 :
    // http://webcodertools.com/imagetobase64converter/Create
    httpContent += "<img alt='' src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAOwwAADsMBx2+oZAAAABp0RVh0U29mdHdhcmUAUGFpbnQuTkVUIHYzLjUuMTAw9HKhAAAAUklEQVQ4T2NggIEGMIBzCTDgSh0cHAjrIcFgiGFoGoCWELYQrgLIgGsg4CtkaTibWNfu378fq2tpHx6EbcAMQ8J6iPU3cpIhbCqaisFpCTwVAwB5lit+0ltbrgAAAABJRU5ErkJggg=='>";
    httpContent += "</h2>\r\n";

    if(status == 1) {
        httpContent += "<pre>\r\n  <font color=#00FF00>ON</font>";
        httpContent += " <a href=\"./?sw=0\">[switch off]</a>\r\n";
    } else {
        httpContent += "<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";
    httpContent += "<hr>\r\n";
    //-------------
    time_t seconds = time(NULL)+ 19800; // time(null) gives the GMT time .
    // printf("Time as seconds since January 1, 1970 = %d\n", seconds);
    strftime(time_stamp, 32, "%y %m %d, %H:%M:%Ss", localtime(&seconds)); 
    // this converts the value in seconds obtained above to human readable format and assigns it to the timestamp   
    sprintf(buffer, "%s", time_stamp);// diplays the human readable time
    httpContent += buffer;
    httpContent += "\r\n";    
    httpContent += "<hr>\r\n";
    //----------------
    httpContent += "Temperature: ";
    sprintf(buffer, "%4.3f", a_in * 3.3);
    httpContent += buffer;
    httpContent += "&deg;C\r\n";
    httpContent += "<hr>\r\n";
    //-----------
    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
 set_time(1387188323); // Set RTC time to 16 December 2013 10:05:23 UTC
 // Date and time are set.
    
    
    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);
                    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</h1>";
                    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(sw));
            }
        }
    }
}