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

Committer:
Fo170
Date:
Sat Jul 25 17:43:36 2015 +0000
Revision:
5:a46a2512e17e
Parent:
4:d34811deedab
Child:
6:2ce163810c2f
petite mise ? jours du code, pour test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:68a0003c4cb8 1 /* In this example LED1 is switched on/off using a web browser connected to this HTTP server.
hudakz 4:d34811deedab 2 * The example is based on the Tuxgraphics Web Switch <http://www.tuxgraphics.org/>.
hudakz 4:d34811deedab 3 * This HTTP server is built around the the ENC28J60 chip
hudakz 4:d34811deedab 4 * driven by the UIPEthernet library <https://github.com/ntruchsess/arduino_uip>
hudakz 0:68a0003c4cb8 5 * ported to mbed.
hudakz 0:68a0003c4cb8 6 */
hudakz 0:68a0003c4cb8 7
hudakz 0:68a0003c4cb8 8 #include <mbed.h>
hudakz 0:68a0003c4cb8 9 #include <UIPEthernet.h>
hudakz 0:68a0003c4cb8 10 #include <UIPServer.h>
hudakz 0:68a0003c4cb8 11 #include <UIPClient.h>
hudakz 0:68a0003c4cb8 12 #include <string>
hudakz 0:68a0003c4cb8 13
Fo170 5:a46a2512e17e 14 DigitalOut myled(LED1);
Fo170 5:a46a2512e17e 15 AnalogIn a_in(PC_0);
Fo170 5:a46a2512e17e 16
hudakz 0:68a0003c4cb8 17 using namespace std;
hudakz 0:68a0003c4cb8 18
hudakz 0:68a0003c4cb8 19 // UIPEthernet is the name of a global instance of UIPEthernetClass.
hudakz 0:68a0003c4cb8 20 // Do not change the name! It is used within the UIPEthernet library.
hudakz 0:68a0003c4cb8 21 #if defined(TARGET_LPC1768)
hudakz 4:d34811deedab 22 UIPEthernetClass UIPEthernet(p11, p12, p13, p8); // mosi, miso, sck, cs
hudakz 0:68a0003c4cb8 23 #elif defined(TARGET_LPC1114)
hudakz 0:68a0003c4cb8 24 UIPEthernetClass UIPEthernet(dp2, dp1, dp6, dp25); // mosi, miso, sck, cs
hudakz 0:68a0003c4cb8 25 #elif defined(TARGET_LPC11U68)
hudakz 0:68a0003c4cb8 26 UIPEthernetClass UIPEthernet(P0_9, P0_8, P1_29, P0_2); // mosi, miso, sck, cs
hudakz 0:68a0003c4cb8 27 #elif defined (TARGET_NUCLEO_F103RB)
hudakz 0:68a0003c4cb8 28 UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6); // mosi, miso, sck, cs
hudakz 4:d34811deedab 29 #elif defined (TARGET_NUCLEO_F401RE)
hudakz 4:d34811deedab 30 UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6); // mosi, miso, sck, cs
hudakz 4:d34811deedab 31 #elif defined (TARGET_NUCLEO_F411RE)
hudakz 4:d34811deedab 32 UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6); // mosi, miso, sck, cs
hudakz 4:d34811deedab 33
hudakz 4:d34811deedab 34 // If your board/plaform is not present yet then uncomment the following two lines and replace TARGET_YOUR_BOARD as appropriate.
hudakz 4:d34811deedab 35
hudakz 4:d34811deedab 36 //#elif defined (TARGET_YOUR_BOARD)
hudakz 4:d34811deedab 37 //UIPEthernetClass UIPEthernet(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS); // mosi, miso, sck, cs
hudakz 4:d34811deedab 38
hudakz 0:68a0003c4cb8 39 #endif
hudakz 0:68a0003c4cb8 40
hudakz 4:d34811deedab 41 // Note:
hudakz 4:d34811deedab 42 // If it happends that any of the SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS pins collide with LED1 pin
hudakz 4:d34811deedab 43 // then either use different SPI port (if available on the board) and change the pin names in the constructor UIPEthernet(...) accordingly
hudakz 4:d34811deedab 44 // 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.
hudakz 4:d34811deedab 45 // In the second case remember to replace LED1 in sw(LED1) constructor (see below).
hudakz 4:d34811deedab 46
hudakz 0:68a0003c4cb8 47
hudakz 0:68a0003c4cb8 48 // MAC number must be unique within the connected network. Modify as appropriate.
hudakz 3:0133517ba02d 49 const uint8_t MY_MAC[6] = {0x00,0x01,0x02,0x03,0x04,0x06};
Fo170 5:a46a2512e17e 50
hudakz 0:68a0003c4cb8 51 // IP address must be also unique and compatible with your network. Change as appropriate.
Fo170 5:a46a2512e17e 52 const IPAddress MY_IP(192,168,0,170);
Fo170 5:a46a2512e17e 53 #define IPAdress_String "192.168.0.170"
Fo170 5:a46a2512e17e 54
hudakz 0:68a0003c4cb8 55 const uint16_t MY_PORT = 80; // for HTTP connection
hudakz 0:68a0003c4cb8 56 EthernetServer myServer = EthernetServer(MY_PORT);
hudakz 0:68a0003c4cb8 57 // In this example we are turning on/off LED1.
hudakz 4:d34811deedab 58 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!
hudakz 0:68a0003c4cb8 59
hudakz 0:68a0003c4cb8 60 const string PASSWORD = "secret"; // change as you like
hudakz 0:68a0003c4cb8 61 const string HTTP_OK = "HTTP/1.0 200 OK";
hudakz 0:68a0003c4cb8 62 const string MOVED_PERM = "HTTP/1.0 301 Moved Permanently\r\nLocation: ";
hudakz 0:68a0003c4cb8 63 const string UNAUTHORIZED = "HTTP/1.0 401 Unauthorized";
hudakz 0:68a0003c4cb8 64 string httpHeader; // HTTP header
hudakz 0:68a0003c4cb8 65 string httpContent; // HTTP content
hudakz 0:68a0003c4cb8 66
hudakz 0:68a0003c4cb8 67
hudakz 0:68a0003c4cb8 68 // analyse the url given
hudakz 0:68a0003c4cb8 69 // return values: -1 invalid password
hudakz 0:68a0003c4cb8 70 // -2 no command given but password valid
hudakz 0:68a0003c4cb8 71 // -3 just refresh page
hudakz 0:68a0003c4cb8 72 // 0 switch off
hudakz 0:68a0003c4cb8 73 // 1 switch on
hudakz 0:68a0003c4cb8 74 //
hudakz 0:68a0003c4cb8 75 // The string passed to this function will look like this:
hudakz 0:68a0003c4cb8 76 // GET /password HTTP/1.....
hudakz 0:68a0003c4cb8 77 // GET /password/ HTTP/1.....
hudakz 0:68a0003c4cb8 78 // GET /password/?sw=1 HTTP/1.....
hudakz 0:68a0003c4cb8 79 // GET /password/?sw=0 HTTP/1.....
hudakz 0:68a0003c4cb8 80
hudakz 0:68a0003c4cb8 81 int8_t analyse_get_url(string& str)
hudakz 0:68a0003c4cb8 82 {
hudakz 0:68a0003c4cb8 83 if(str.substr(5, PASSWORD.size()) != PASSWORD)
hudakz 0:68a0003c4cb8 84 return(-1);
hudakz 0:68a0003c4cb8 85
hudakz 0:68a0003c4cb8 86 uint8_t pos = 5 + PASSWORD.size();
hudakz 0:68a0003c4cb8 87
hudakz 0:68a0003c4cb8 88 if(str.substr(pos, 1) == " ")
hudakz 0:68a0003c4cb8 89 return(-2);
hudakz 0:68a0003c4cb8 90
hudakz 0:68a0003c4cb8 91 if(str.substr(pos, 1) != "/")
hudakz 0:68a0003c4cb8 92 return(-1);
hudakz 0:68a0003c4cb8 93
hudakz 0:68a0003c4cb8 94 pos++;
hudakz 0:68a0003c4cb8 95
hudakz 0:68a0003c4cb8 96 string cmd(str.substr(pos, 5));
hudakz 0:68a0003c4cb8 97
hudakz 0:68a0003c4cb8 98 if(cmd == "?sw=0")
hudakz 0:68a0003c4cb8 99 return(0);
hudakz 0:68a0003c4cb8 100
hudakz 0:68a0003c4cb8 101 if(cmd == "?sw=1")
hudakz 0:68a0003c4cb8 102 return(1);
hudakz 0:68a0003c4cb8 103
hudakz 0:68a0003c4cb8 104 return(-3);
hudakz 0:68a0003c4cb8 105 }
hudakz 0:68a0003c4cb8 106
hudakz 0:68a0003c4cb8 107 string& moved_perm(uint8_t flag)
hudakz 0:68a0003c4cb8 108 {
hudakz 0:68a0003c4cb8 109 if(flag == 1)
hudakz 0:68a0003c4cb8 110 httpContent = "/" + PASSWORD + "/";
hudakz 0:68a0003c4cb8 111 else
hudakz 0:68a0003c4cb8 112 httpContent = "";
hudakz 0:68a0003c4cb8 113
hudakz 0:68a0003c4cb8 114 httpContent += "<h1>301 Moved Permanently</h1>\r\n";
hudakz 0:68a0003c4cb8 115
hudakz 0:68a0003c4cb8 116 return (httpContent);
hudakz 0:68a0003c4cb8 117 }
hudakz 0:68a0003c4cb8 118
hudakz 0:68a0003c4cb8 119 string& page(uint8_t status)
hudakz 0:68a0003c4cb8 120 {
Fo170 5:a46a2512e17e 121 char buffer[32];
Fo170 5:a46a2512e17e 122 char time_stamp[32];//as our lcd is of 16 character it is so
Fo170 5:a46a2512e17e 123 //-------------
Fo170 5:a46a2512e17e 124 httpContent = "<h2>Web Switch Nucleo-F411RE & ENC28J60 (RTC et ADC) ";
Fo170 5:a46a2512e17e 125 httpContent += IPAdress_String "\r\n";
Fo170 5:a46a2512e17e 126
Fo170 5:a46a2512e17e 127 // Test d'image en base64 :
Fo170 5:a46a2512e17e 128 // http://webcodertools.com/imagetobase64converter/Create
Fo170 5:a46a2512e17e 129 httpContent += "<img alt='' src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAOwwAADsMBx2+oZAAAABp0RVh0U29mdHdhcmUAUGFpbnQuTkVUIHYzLjUuMTAw9HKhAAAAUklEQVQ4T2NggIEGMIBzCTDgSh0cHAjrIcFgiGFoGoCWELYQrgLIgGsg4CtkaTibWNfu378fq2tpHx6EbcAMQ8J6iPU3cpIhbCqaisFpCTwVAwB5lit+0ltbrgAAAABJRU5ErkJggg=='>";
Fo170 5:a46a2512e17e 130 httpContent += "</h2>\r\n";
hudakz 0:68a0003c4cb8 131
hudakz 0:68a0003c4cb8 132 if(status == 1) {
Fo170 5:a46a2512e17e 133 httpContent += "<pre>\r\n <font color=#00FF00>ON</font>";
hudakz 0:68a0003c4cb8 134 httpContent += " <a href=\"./?sw=0\">[switch off]</a>\r\n";
hudakz 0:68a0003c4cb8 135 } else {
Fo170 5:a46a2512e17e 136 httpContent += "<pre>\r\n <font color=#FF0000>OFF</font>";
hudakz 0:68a0003c4cb8 137 httpContent += " <a href=\"./?sw=1\">[switch on]</a>\r\n";
hudakz 0:68a0003c4cb8 138 }
hudakz 0:68a0003c4cb8 139
hudakz 0:68a0003c4cb8 140 httpContent += " <a href=\".\">[refresh status]</a>\r\n";
hudakz 0:68a0003c4cb8 141 httpContent += "</pre>\r\n";
hudakz 0:68a0003c4cb8 142 httpContent += "<hr>\r\n";
Fo170 5:a46a2512e17e 143 //-------------
Fo170 5:a46a2512e17e 144 time_t seconds = time(NULL)+ 19800; // time(null) gives the GMT time .
Fo170 5:a46a2512e17e 145 // printf("Time as seconds since January 1, 1970 = %d\n", seconds);
Fo170 5:a46a2512e17e 146 strftime(time_stamp, 32, "%y %m %d, %H:%M:%Ss", localtime(&seconds));
Fo170 5:a46a2512e17e 147 // this converts the value in seconds obtained above to human readable format and assigns it to the timestamp
Fo170 5:a46a2512e17e 148 sprintf(buffer, "%s", time_stamp);// diplays the human readable time
Fo170 5:a46a2512e17e 149 httpContent += buffer;
Fo170 5:a46a2512e17e 150 httpContent += "\r\n";
Fo170 5:a46a2512e17e 151 httpContent += "<hr>\r\n";
Fo170 5:a46a2512e17e 152 //----------------
Fo170 5:a46a2512e17e 153 httpContent += "Temperature: ";
Fo170 5:a46a2512e17e 154 sprintf(buffer, "%4.3f", a_in * 3.3);
Fo170 5:a46a2512e17e 155 httpContent += buffer;
Fo170 5:a46a2512e17e 156 httpContent += "&deg;C\r\n";
Fo170 5:a46a2512e17e 157 httpContent += "<hr>\r\n";
Fo170 5:a46a2512e17e 158 //-----------
Fo170 5:a46a2512e17e 159 wait(1);
hudakz 0:68a0003c4cb8 160 return httpContent;
hudakz 0:68a0003c4cb8 161 }
hudakz 0:68a0003c4cb8 162
hudakz 0:68a0003c4cb8 163 void http_send(EthernetClient& client, string& header, string& content)
hudakz 0:68a0003c4cb8 164 {
hudakz 0:68a0003c4cb8 165 char content_length[5] = {};
hudakz 0:68a0003c4cb8 166
hudakz 2:76f339a1ba9b 167 header += "\r\nContent-Type: text/html\r\n";
hudakz 0:68a0003c4cb8 168 header += "Content-Length: ";
hudakz 0:68a0003c4cb8 169 sprintf(content_length, "%d", content.length());
hudakz 0:68a0003c4cb8 170 header += string(content_length) + "\r\n";
hudakz 0:68a0003c4cb8 171 header += "Pragma: no-cache\r\n";
hudakz 0:68a0003c4cb8 172 header += "Connection: About to close\r\n";
hudakz 0:68a0003c4cb8 173 header += "\r\n";
hudakz 0:68a0003c4cb8 174 string webpage = header + content;
hudakz 0:68a0003c4cb8 175 client.write((uint8_t*)webpage.c_str(),webpage.length());
hudakz 0:68a0003c4cb8 176 }
hudakz 0:68a0003c4cb8 177
hudakz 0:68a0003c4cb8 178 int main()
Fo170 5:a46a2512e17e 179 {
Fo170 5:a46a2512e17e 180 // RTC
Fo170 5:a46a2512e17e 181 set_time(1387188323); // Set RTC time to 16 December 2013 10:05:23 UTC
Fo170 5:a46a2512e17e 182 // Date and time are set.
Fo170 5:a46a2512e17e 183
Fo170 5:a46a2512e17e 184
hudakz 3:0133517ba02d 185 UIPEthernet.begin(MY_MAC,MY_IP);
hudakz 0:68a0003c4cb8 186 myServer.begin();
hudakz 0:68a0003c4cb8 187 while(1) {
hudakz 0:68a0003c4cb8 188 EthernetClient client = myServer.available();
hudakz 0:68a0003c4cb8 189 if (client) {
hudakz 0:68a0003c4cb8 190 size_t size = client.available();
hudakz 0:68a0003c4cb8 191 if(size > 0) {
hudakz 0:68a0003c4cb8 192 uint8_t* buf = (uint8_t*)malloc(size);
hudakz 0:68a0003c4cb8 193 size = client.read(buf, size);
hudakz 0:68a0003c4cb8 194 string received((char*)buf);
hudakz 0:68a0003c4cb8 195 free(buf);
hudakz 0:68a0003c4cb8 196 if(received.substr(0, 3) != "GET") {
hudakz 0:68a0003c4cb8 197 // head, post or other method
hudakz 0:68a0003c4cb8 198 // for possible status codes see:
hudakz 0:68a0003c4cb8 199 // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
hudakz 0:68a0003c4cb8 200 httpHeader = HTTP_OK;
hudakz 0:68a0003c4cb8 201 httpContent = "<h1>200 OK</h1>";
hudakz 0:68a0003c4cb8 202 http_send(client, httpHeader, httpContent);
hudakz 0:68a0003c4cb8 203 continue;
hudakz 0:68a0003c4cb8 204 }
hudakz 0:68a0003c4cb8 205
hudakz 0:68a0003c4cb8 206 if(received.substr(0, 6) == "GET / ") {
hudakz 0:68a0003c4cb8 207 httpHeader = HTTP_OK;
hudakz 0:68a0003c4cb8 208 httpContent = "<p>Usage: http://host_or_ip/password</p>\r\n";
hudakz 0:68a0003c4cb8 209 http_send(client, httpHeader, httpContent);
hudakz 0:68a0003c4cb8 210 continue;
hudakz 0:68a0003c4cb8 211 }
hudakz 0:68a0003c4cb8 212
hudakz 0:68a0003c4cb8 213 int cmd = analyse_get_url(received);
hudakz 0:68a0003c4cb8 214
hudakz 0:68a0003c4cb8 215 if(cmd == -2) {
hudakz 0:68a0003c4cb8 216 // redirect to the right base url
hudakz 0:68a0003c4cb8 217 httpHeader = MOVED_PERM;
hudakz 0:68a0003c4cb8 218 http_send(client, httpHeader, moved_perm(1));
hudakz 0:68a0003c4cb8 219 continue;
hudakz 0:68a0003c4cb8 220 }
hudakz 0:68a0003c4cb8 221
hudakz 0:68a0003c4cb8 222 if(cmd == -1) {
hudakz 0:68a0003c4cb8 223 httpHeader = UNAUTHORIZED;
hudakz 0:68a0003c4cb8 224 httpContent = "<h1>401 Unauthorized</h1>";
hudakz 0:68a0003c4cb8 225 http_send(client, httpHeader, httpContent);
hudakz 0:68a0003c4cb8 226 continue;
hudakz 0:68a0003c4cb8 227 }
hudakz 0:68a0003c4cb8 228
hudakz 0:68a0003c4cb8 229 if(cmd == 1) {
hudakz 0:68a0003c4cb8 230 sw = 1; // switch on
hudakz 0:68a0003c4cb8 231 }
hudakz 0:68a0003c4cb8 232
hudakz 0:68a0003c4cb8 233 if(cmd == 0) {
hudakz 0:68a0003c4cb8 234 sw = 0; // switch off
hudakz 0:68a0003c4cb8 235 }
hudakz 0:68a0003c4cb8 236
hudakz 0:68a0003c4cb8 237 httpHeader = HTTP_OK;
hudakz 0:68a0003c4cb8 238 http_send(client, httpHeader, page(sw));
hudakz 0:68a0003c4cb8 239 }
hudakz 0:68a0003c4cb8 240 }
hudakz 0:68a0003c4cb8 241 }
hudakz 0:68a0003c4cb8 242 }
hudakz 0:68a0003c4cb8 243
hudakz 3:0133517ba02d 244