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:
Tue Jul 28 11:26:32 2015 +0000
Revision:
10:bbee7dd4bcda
Parent:
9:2f7ad2b13ec8
Child:
11:afb33350db83
D?but du 2i?me test de d?bogage...

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 7:acad90a34466 14 // PC_0 (connectique Morpho) : Entrée Analogique (0 à 3.3V)
Fo170 7:acad90a34466 15
Fo170 7:acad90a34466 16 // Carte ENC28J60 <--> Nucleo F411RE
Fo170 7:acad90a34466 17 // PB_5 (connectique Morpho) : MOSI
Fo170 7:acad90a34466 18 // PB_4 (connectique Morpho) : MISO
Fo170 7:acad90a34466 19 // PB_3 (connectique Morpho) : SCK
Fo170 7:acad90a34466 20 // PB_6 (connectique Morpho) : CS
Fo170 7:acad90a34466 21
Fo170 5:a46a2512e17e 22 DigitalOut myled(LED1);
Fo170 5:a46a2512e17e 23 AnalogIn a_in(PC_0);
Fo170 5:a46a2512e17e 24
hudakz 0:68a0003c4cb8 25 using namespace std;
hudakz 0:68a0003c4cb8 26
hudakz 0:68a0003c4cb8 27 // UIPEthernet is the name of a global instance of UIPEthernetClass.
hudakz 0:68a0003c4cb8 28 // Do not change the name! It is used within the UIPEthernet library.
hudakz 0:68a0003c4cb8 29 #if defined(TARGET_LPC1768)
hudakz 4:d34811deedab 30 UIPEthernetClass UIPEthernet(p11, p12, p13, p8); // mosi, miso, sck, cs
hudakz 0:68a0003c4cb8 31 #elif defined(TARGET_LPC1114)
hudakz 0:68a0003c4cb8 32 UIPEthernetClass UIPEthernet(dp2, dp1, dp6, dp25); // mosi, miso, sck, cs
hudakz 0:68a0003c4cb8 33 #elif defined(TARGET_LPC11U68)
hudakz 0:68a0003c4cb8 34 UIPEthernetClass UIPEthernet(P0_9, P0_8, P1_29, P0_2); // mosi, miso, sck, cs
hudakz 0:68a0003c4cb8 35 #elif defined (TARGET_NUCLEO_F103RB)
hudakz 0:68a0003c4cb8 36 UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6); // mosi, miso, sck, cs
hudakz 4:d34811deedab 37 #elif defined (TARGET_NUCLEO_F401RE)
hudakz 4:d34811deedab 38 UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6); // mosi, miso, sck, cs
hudakz 4:d34811deedab 39 #elif defined (TARGET_NUCLEO_F411RE)
hudakz 4:d34811deedab 40 UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6); // mosi, miso, sck, cs
hudakz 4:d34811deedab 41
hudakz 4:d34811deedab 42 // If your board/plaform is not present yet then uncomment the following two lines and replace TARGET_YOUR_BOARD as appropriate.
hudakz 4:d34811deedab 43
hudakz 4:d34811deedab 44 //#elif defined (TARGET_YOUR_BOARD)
hudakz 4:d34811deedab 45 //UIPEthernetClass UIPEthernet(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS); // mosi, miso, sck, cs
hudakz 4:d34811deedab 46
hudakz 0:68a0003c4cb8 47 #endif
hudakz 0:68a0003c4cb8 48
hudakz 4:d34811deedab 49 // Note:
hudakz 4:d34811deedab 50 // If it happends that any of the SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS pins collide with LED1 pin
hudakz 4:d34811deedab 51 // then either use different SPI port (if available on the board) and change the pin names in the constructor UIPEthernet(...) accordingly
hudakz 4:d34811deedab 52 // 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 53 // In the second case remember to replace LED1 in sw(LED1) constructor (see below).
hudakz 4:d34811deedab 54
hudakz 0:68a0003c4cb8 55
hudakz 0:68a0003c4cb8 56 // MAC number must be unique within the connected network. Modify as appropriate.
hudakz 3:0133517ba02d 57 const uint8_t MY_MAC[6] = {0x00,0x01,0x02,0x03,0x04,0x06};
Fo170 5:a46a2512e17e 58
hudakz 0:68a0003c4cb8 59 // IP address must be also unique and compatible with your network. Change as appropriate.
Fo170 5:a46a2512e17e 60 const IPAddress MY_IP(192,168,0,170);
Fo170 8:305b64d7dd23 61 const string __IP_LOCAL__ = "192.168.0.170";
Fo170 8:305b64d7dd23 62 const string __hebergement__ = "http://olivier.fournet.free.fr/electronique/e/WebServerNucleo/js/";
Fo170 8:305b64d7dd23 63 const string __Temp_between_measurements__ = "1";
Fo170 8:305b64d7dd23 64 #define __Temp_between_measurements_in_Second__ 1
Fo170 8:305b64d7dd23 65
Fo170 6:2ce163810c2f 66 // Logo Test d'image en base64 :
Fo170 6:2ce163810c2f 67 // http://webcodertools.com/imagetobase64converter/Create
Fo170 8:305b64d7dd23 68 const string __Logo_image__ = "<img alt='' src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAOwwAADsMBx2+oZAAAABp0RVh0U29mdHdhcmUAUGFpbnQuTkVUIHYzLjUuMTAw9HKhAAAAUklEQVQ4T2NggIEGMIBzCTDgSh0cHAjrIcFgiGFoGoCWELYQrgLIgGsg4CtkaTibWNfu378fq2tpHx6EbcAMQ8J6iPU3cpIhbCqaisFpCTwVAwB5lit+0ltbrgAAAABJRU5ErkJggg=='>";
Fo170 8:305b64d7dd23 69
Fo170 8:305b64d7dd23 70 const string __image_Password_Folder__ = "<img alt='' src='http://olivier.fournet.free.fr/jpg/password_folder.jpg'>";
Fo170 8:305b64d7dd23 71 const string __image_301_Moved_Permanently__ = "<img alt='' src='http://olivier.fournet.free.fr/jpg/301_moved_permanently.jpg'>";
Fo170 8:305b64d7dd23 72 const string __image_401_Unauthorized__ = "<img alt='' src='http://olivier.fournet.free.fr/png/401_Unauthorized.png'>";
Fo170 6:2ce163810c2f 73
Fo170 6:2ce163810c2f 74 #define NB_SAMPLES 10
Fo170 6:2ce163810c2f 75 float adc_samples[NB_SAMPLES];// = { 0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.01,1.02,1.03,1.04 };
Fo170 6:2ce163810c2f 76 float time_samples[NB_SAMPLES];// = { -0.1,2,3,4,5,6,7,8,9,10,11,12,13,14 };
Fo170 6:2ce163810c2f 77 float x_min = 0.0, x_max = 0.0;
Fo170 6:2ce163810c2f 78 float y_min = 0.0, y_max = 0.0;
Fo170 7:acad90a34466 79 float Seconds = 0.0;
Fo170 7:acad90a34466 80
Fo170 7:acad90a34466 81 Ticker second_ticker;
Fo170 7:acad90a34466 82
Fo170 7:acad90a34466 83 void add_one_second()
Fo170 7:acad90a34466 84 {
Fo170 7:acad90a34466 85 Seconds = Seconds + 0.1;
Fo170 7:acad90a34466 86 }
Fo170 5:a46a2512e17e 87
hudakz 0:68a0003c4cb8 88 const uint16_t MY_PORT = 80; // for HTTP connection
hudakz 0:68a0003c4cb8 89 EthernetServer myServer = EthernetServer(MY_PORT);
hudakz 0:68a0003c4cb8 90 // In this example we are turning on/off LED1.
hudakz 4:d34811deedab 91 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 92
hudakz 0:68a0003c4cb8 93 const string PASSWORD = "secret"; // change as you like
hudakz 0:68a0003c4cb8 94 const string HTTP_OK = "HTTP/1.0 200 OK";
hudakz 0:68a0003c4cb8 95 const string MOVED_PERM = "HTTP/1.0 301 Moved Permanently\r\nLocation: ";
hudakz 0:68a0003c4cb8 96 const string UNAUTHORIZED = "HTTP/1.0 401 Unauthorized";
hudakz 0:68a0003c4cb8 97 string httpHeader; // HTTP header
hudakz 0:68a0003c4cb8 98 string httpContent; // HTTP content
hudakz 0:68a0003c4cb8 99
hudakz 0:68a0003c4cb8 100
hudakz 0:68a0003c4cb8 101 // analyse the url given
hudakz 0:68a0003c4cb8 102 // return values: -1 invalid password
hudakz 0:68a0003c4cb8 103 // -2 no command given but password valid
hudakz 0:68a0003c4cb8 104 // -3 just refresh page
hudakz 0:68a0003c4cb8 105 // 0 switch off
hudakz 0:68a0003c4cb8 106 // 1 switch on
hudakz 0:68a0003c4cb8 107 //
hudakz 0:68a0003c4cb8 108 // The string passed to this function will look like this:
hudakz 0:68a0003c4cb8 109 // GET /password HTTP/1.....
hudakz 0:68a0003c4cb8 110 // GET /password/ HTTP/1.....
hudakz 0:68a0003c4cb8 111 // GET /password/?sw=1 HTTP/1.....
hudakz 0:68a0003c4cb8 112 // GET /password/?sw=0 HTTP/1.....
hudakz 0:68a0003c4cb8 113
hudakz 0:68a0003c4cb8 114 int8_t analyse_get_url(string& str)
hudakz 0:68a0003c4cb8 115 {
hudakz 0:68a0003c4cb8 116 if(str.substr(5, PASSWORD.size()) != PASSWORD)
hudakz 0:68a0003c4cb8 117 return(-1);
hudakz 0:68a0003c4cb8 118
hudakz 0:68a0003c4cb8 119 uint8_t pos = 5 + PASSWORD.size();
hudakz 0:68a0003c4cb8 120
Fo170 7:acad90a34466 121 if(str.substr(pos, 1) == " ") return(-2);
hudakz 0:68a0003c4cb8 122
Fo170 7:acad90a34466 123 if(str.substr(pos, 1) != "/") return(-1);
hudakz 0:68a0003c4cb8 124
hudakz 0:68a0003c4cb8 125 pos++;
hudakz 0:68a0003c4cb8 126
hudakz 0:68a0003c4cb8 127 string cmd(str.substr(pos, 5));
hudakz 0:68a0003c4cb8 128
Fo170 7:acad90a34466 129 if(cmd == "?sw=0") return(0);
hudakz 0:68a0003c4cb8 130
Fo170 7:acad90a34466 131 if(cmd == "?sw=1") return(1);
hudakz 0:68a0003c4cb8 132
hudakz 0:68a0003c4cb8 133 return(-3);
hudakz 0:68a0003c4cb8 134 }
hudakz 0:68a0003c4cb8 135
hudakz 0:68a0003c4cb8 136 string& moved_perm(uint8_t flag)
hudakz 0:68a0003c4cb8 137 {
hudakz 0:68a0003c4cb8 138 if(flag == 1)
hudakz 0:68a0003c4cb8 139 httpContent = "/" + PASSWORD + "/";
hudakz 0:68a0003c4cb8 140 else
hudakz 0:68a0003c4cb8 141 httpContent = "";
hudakz 0:68a0003c4cb8 142
Fo170 6:2ce163810c2f 143 httpContent += "<h1>301 Moved Permanently ";
Fo170 9:2f7ad2b13ec8 144 httpContent += __image_301_Moved_Permanently__;
Fo170 9:2f7ad2b13ec8 145 httpContent += "</h1>\r\n";
hudakz 0:68a0003c4cb8 146 return (httpContent);
hudakz 0:68a0003c4cb8 147 }
hudakz 0:68a0003c4cb8 148
hudakz 0:68a0003c4cb8 149 string& page(uint8_t status)
hudakz 0:68a0003c4cb8 150 {
Fo170 5:a46a2512e17e 151 char buffer[32];
Fo170 8:305b64d7dd23 152 //char time_stamp[32];
Fo170 7:acad90a34466 153 float meas;
Fo170 7:acad90a34466 154 meas = a_in.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
Fo170 8:305b64d7dd23 155 meas = meas * 3300.0; // Change the value to be in the 0 to 3300 range
Fo170 8:305b64d7dd23 156 //Seconds = Seconds + 1;
Fo170 5:a46a2512e17e 157 //-------------
Fo170 6:2ce163810c2f 158 httpContent = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\r\n";
Fo170 9:2f7ad2b13ec8 159 httpContent += "<meta http-equiv=\"refresh\" content=\"";
Fo170 9:2f7ad2b13ec8 160 httpContent += __Temp_between_measurements__;
Fo170 9:2f7ad2b13ec8 161 httpContent += ";url=http://";
Fo170 9:2f7ad2b13ec8 162 httpContent += __IP_LOCAL__;
Fo170 9:2f7ad2b13ec8 163 httpContent += "/\">\r\n";
Fo170 6:2ce163810c2f 164 httpContent += "<HTML><HEAD>\r\n";
Fo170 6:2ce163810c2f 165 httpContent += "<title>WEB Server Nucleo F411RE - ENC28J60 - Flot Examples: Interactivity</title>\r\n";
Fo170 9:2f7ad2b13ec8 166 httpContent += "<script language=\"javascript\" type=\"text/javascript\" src=\"";
Fo170 9:2f7ad2b13ec8 167 httpContent += __hebergement__;
Fo170 9:2f7ad2b13ec8 168 httpContent += "WebServerNucleo_Interactivity_init.js\"></script>\r\n";
Fo170 8:305b64d7dd23 169 httpContent += "<script language=\"javascript\" type=\"text/javascript\">init_WebServerNucleo_Interactivity();</script>\r\n";
Fo170 6:2ce163810c2f 170 // Variables JavaScript
Fo170 6:2ce163810c2f 171 httpContent += "<script language=\"javascript\" type=\"text/javascript\">\r\n";
Fo170 8:305b64d7dd23 172 httpContent += "var color_Y = \"#FF0000\";\r\n";
Fo170 8:305b64d7dd23 173 httpContent += "var label_Y = \"Adc(x)\";\r\n";
Fo170 6:2ce163810c2f 174 httpContent += "var x_min = -0.5, x_max = 14.5, y_min = -0.5, y_max = 1.5;\r\n";
Fo170 8:305b64d7dd23 175 httpContent += "var array_value = [[-0.1,0.1],[2,0.2],[3,0.3],[4,0.4],[5,0.5],[6,0.6],[7,0.7],[8,0.8],[9,0.9],[10,1],[11,1.01],[12,1.02],[13,1.03],[14,1.04]];\r\n";
Fo170 6:2ce163810c2f 176 httpContent += "</script>\r\n";
Fo170 6:2ce163810c2f 177 // Fin Variable JavaScript
Fo170 6:2ce163810c2f 178 httpContent += "</HEAD><BODY>\r\n";
Fo170 6:2ce163810c2f 179 httpContent += "<center><h2>WEB Server Nucleo F411RE</h2>\r\n";
Fo170 7:acad90a34466 180 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";
Fo170 10:bbee7dd4bcda 181 httpContent += "<p>Compilation avec mBED &agrave; " __TIME__ " le " __DATE__" \r\n";
Fo170 6:2ce163810c2f 182
Fo170 8:305b64d7dd23 183 // httpContent += __Logo_image__ ;
Fo170 8:305b64d7dd23 184
Fo170 8:305b64d7dd23 185 httpContent += "<p></center>\r\n<hr><p>\r\n";
Fo170 5:a46a2512e17e 186
Fo170 6:2ce163810c2f 187 if(status == 1)
Fo170 6:2ce163810c2f 188 {
Fo170 6:2ce163810c2f 189 httpContent += "<font color=#00FF00>Switch ON</font>\r\n";
Fo170 6:2ce163810c2f 190 }
Fo170 6:2ce163810c2f 191 else
Fo170 6:2ce163810c2f 192 {
Fo170 6:2ce163810c2f 193 httpContent += "<font color=#FF0000>Switch OFF</font>\r\n";
Fo170 6:2ce163810c2f 194 }
hudakz 0:68a0003c4cb8 195
Fo170 6:2ce163810c2f 196 httpContent += "<hr>\r\n";
Fo170 6:2ce163810c2f 197 //-------------
Fo170 7:acad90a34466 198 /*
Fo170 7:acad90a34466 199 httpContent += "Local Time: ";
Fo170 6:2ce163810c2f 200 time_t seconds = time(NULL)+ 19800; // time(null) gives the GMT time .
Fo170 6:2ce163810c2f 201 // printf("Time as seconds since January 1, 1970 = %d\n", seconds);
Fo170 7:acad90a34466 202 strftime(time_stamp, 32, "%y %m %d, %H:%M:%Ss", localtime(&seconds));
Fo170 6:2ce163810c2f 203 // this converts the value in seconds obtained above to human readable format and assigns it to the timestamp
Fo170 6:2ce163810c2f 204 sprintf(buffer, "%s", time_stamp);// diplays the human readable time
Fo170 7:acad90a34466 205 */
Fo170 8:305b64d7dd23 206 sprintf(buffer, "%.1f ", Seconds);// diplays the human readable Seconds
Fo170 6:2ce163810c2f 207 httpContent += buffer;
Fo170 7:acad90a34466 208 httpContent += "Seconds\r\n<hr>\r\n";
Fo170 6:2ce163810c2f 209 //----------------
Fo170 6:2ce163810c2f 210 httpContent += "AnalogIn(PC_0) : ";
Fo170 7:acad90a34466 211 sprintf(buffer, "%.0f mV\r\n", meas);
Fo170 6:2ce163810c2f 212 httpContent += buffer;
Fo170 6:2ce163810c2f 213 httpContent += "<hr>\r\n<p>Usage Password Page :<p>http://host_or_ip/password<p><hr>\r\n";
Fo170 8:305b64d7dd23 214 httpContent += "<script language=\"javascript\" type=\"text/javascript\">WebServerNucleo_Interactivity();</script>\r\n";
Fo170 6:2ce163810c2f 215 httpContent += "</BODY></HTML>";
Fo170 6:2ce163810c2f 216 //-----------
Fo170 7:acad90a34466 217 //wait(1);
Fo170 6:2ce163810c2f 218 return httpContent;
Fo170 6:2ce163810c2f 219 }
Fo170 6:2ce163810c2f 220
Fo170 6:2ce163810c2f 221 string& page_toggle_switch(uint8_t status)
Fo170 6:2ce163810c2f 222 {
Fo170 6:2ce163810c2f 223 //-------------
Fo170 6:2ce163810c2f 224 httpContent = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\r\n";
Fo170 6:2ce163810c2f 225 httpContent += "<HTML><HEAD>\r\n";
Fo170 6:2ce163810c2f 226 httpContent += "<title>WEB Server Nucleo F411RE - ENC28J60 - Password Page</title>\r\n";
Fo170 6:2ce163810c2f 227 httpContent += "</HEAD><BODY>\r\n";
Fo170 9:2f7ad2b13ec8 228 httpContent += "<center><h2>WEB Server Nucleo F411RE - ENC28J60 - Password Page</h2></center>\r\n<p>";
Fo170 9:2f7ad2b13ec8 229 httpContent += __image_Password_Folder__;
Fo170 9:2f7ad2b13ec8 230 httpContent += "<p>\r\n";
Fo170 8:305b64d7dd23 231
Fo170 6:2ce163810c2f 232 if(status == 1)
Fo170 6:2ce163810c2f 233 {
Fo170 6:2ce163810c2f 234 httpContent += "<hr><pre>\r\n <font color=#00FF00>ON</font>";
hudakz 0:68a0003c4cb8 235 httpContent += " <a href=\"./?sw=0\">[switch off]</a>\r\n";
Fo170 6:2ce163810c2f 236 }
Fo170 6:2ce163810c2f 237 else
Fo170 6:2ce163810c2f 238 {
Fo170 6:2ce163810c2f 239 httpContent += "<hr><pre>\r\n <font color=#FF0000>OFF</font>";
hudakz 0:68a0003c4cb8 240 httpContent += " <a href=\"./?sw=1\">[switch on]</a>\r\n";
hudakz 0:68a0003c4cb8 241 }
hudakz 0:68a0003c4cb8 242
hudakz 0:68a0003c4cb8 243 httpContent += " <a href=\".\">[refresh status]</a>\r\n";
hudakz 0:68a0003c4cb8 244 httpContent += "</pre>\r\n";
hudakz 0:68a0003c4cb8 245 httpContent += "<hr>\r\n";
Fo170 6:2ce163810c2f 246
Fo170 6:2ce163810c2f 247 httpContent += "</BODY></HTML>";
Fo170 5:a46a2512e17e 248 //-----------
Fo170 5:a46a2512e17e 249 wait(1);
hudakz 0:68a0003c4cb8 250 return httpContent;
hudakz 0:68a0003c4cb8 251 }
hudakz 0:68a0003c4cb8 252
hudakz 0:68a0003c4cb8 253 void http_send(EthernetClient& client, string& header, string& content)
hudakz 0:68a0003c4cb8 254 {
hudakz 0:68a0003c4cb8 255 char content_length[5] = {};
hudakz 0:68a0003c4cb8 256
hudakz 2:76f339a1ba9b 257 header += "\r\nContent-Type: text/html\r\n";
hudakz 0:68a0003c4cb8 258 header += "Content-Length: ";
hudakz 0:68a0003c4cb8 259 sprintf(content_length, "%d", content.length());
hudakz 0:68a0003c4cb8 260 header += string(content_length) + "\r\n";
hudakz 0:68a0003c4cb8 261 header += "Pragma: no-cache\r\n";
hudakz 0:68a0003c4cb8 262 header += "Connection: About to close\r\n";
hudakz 0:68a0003c4cb8 263 header += "\r\n";
hudakz 0:68a0003c4cb8 264 string webpage = header + content;
hudakz 0:68a0003c4cb8 265 client.write((uint8_t*)webpage.c_str(),webpage.length());
hudakz 0:68a0003c4cb8 266 }
hudakz 0:68a0003c4cb8 267
hudakz 0:68a0003c4cb8 268 int main()
Fo170 7:acad90a34466 269 {
Fo170 5:a46a2512e17e 270 // RTC
Fo170 8:305b64d7dd23 271 //set_time(1387188323); // Set RTC time to 16 December 2013 10:05:23 UTC
Fo170 7:acad90a34466 272
Fo170 5:a46a2512e17e 273 // Date and time are set.
Fo170 7:acad90a34466 274
Fo170 7:acad90a34466 275 // Init the ticker with the address of the function (add_one_second) to be attached and the interval (100 ms)
Fo170 8:305b64d7dd23 276 second_ticker.attach(&add_one_second, 0.1);
Fo170 7:acad90a34466 277 //-----------------
hudakz 3:0133517ba02d 278 UIPEthernet.begin(MY_MAC,MY_IP);
hudakz 0:68a0003c4cb8 279 myServer.begin();
Fo170 7:acad90a34466 280 while(1)
Fo170 7:acad90a34466 281 {
hudakz 0:68a0003c4cb8 282 EthernetClient client = myServer.available();
Fo170 7:acad90a34466 283 if(client)
Fo170 7:acad90a34466 284 {
hudakz 0:68a0003c4cb8 285 size_t size = client.available();
Fo170 7:acad90a34466 286 if(size > 0)
Fo170 7:acad90a34466 287 {
hudakz 0:68a0003c4cb8 288 uint8_t* buf = (uint8_t*)malloc(size);
hudakz 0:68a0003c4cb8 289 size = client.read(buf, size);
hudakz 0:68a0003c4cb8 290 string received((char*)buf);
hudakz 0:68a0003c4cb8 291 free(buf);
Fo170 7:acad90a34466 292 if(received.substr(0, 3) != "GET")
Fo170 7:acad90a34466 293 {
hudakz 0:68a0003c4cb8 294 // head, post or other method
hudakz 0:68a0003c4cb8 295 // for possible status codes see:
hudakz 0:68a0003c4cb8 296 // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
hudakz 0:68a0003c4cb8 297 httpHeader = HTTP_OK;
hudakz 0:68a0003c4cb8 298 httpContent = "<h1>200 OK</h1>";
hudakz 0:68a0003c4cb8 299 http_send(client, httpHeader, httpContent);
hudakz 0:68a0003c4cb8 300 continue;
hudakz 0:68a0003c4cb8 301 }
hudakz 0:68a0003c4cb8 302
Fo170 7:acad90a34466 303 if(received.substr(0, 6) == "GET / ")
Fo170 7:acad90a34466 304 {
hudakz 0:68a0003c4cb8 305 httpHeader = HTTP_OK;
Fo170 6:2ce163810c2f 306 /*
hudakz 0:68a0003c4cb8 307 httpContent = "<p>Usage: http://host_or_ip/password</p>\r\n";
hudakz 0:68a0003c4cb8 308 http_send(client, httpHeader, httpContent);
Fo170 6:2ce163810c2f 309 */
Fo170 6:2ce163810c2f 310 http_send(client, httpHeader, page(sw));
hudakz 0:68a0003c4cb8 311 continue;
hudakz 0:68a0003c4cb8 312 }
hudakz 0:68a0003c4cb8 313
hudakz 0:68a0003c4cb8 314 int cmd = analyse_get_url(received);
hudakz 0:68a0003c4cb8 315
Fo170 7:acad90a34466 316 if(cmd == -2)
Fo170 7:acad90a34466 317 {
hudakz 0:68a0003c4cb8 318 // redirect to the right base url
hudakz 0:68a0003c4cb8 319 httpHeader = MOVED_PERM;
hudakz 0:68a0003c4cb8 320 http_send(client, httpHeader, moved_perm(1));
hudakz 0:68a0003c4cb8 321 continue;
hudakz 0:68a0003c4cb8 322 }
hudakz 0:68a0003c4cb8 323
Fo170 7:acad90a34466 324 if(cmd == -1)
Fo170 7:acad90a34466 325 {
hudakz 0:68a0003c4cb8 326 httpHeader = UNAUTHORIZED;
Fo170 9:2f7ad2b13ec8 327 httpContent = "<h1>401 Unauthorized ";
Fo170 9:2f7ad2b13ec8 328 httpContent += __image_401_Unauthorized__;
Fo170 9:2f7ad2b13ec8 329 httpContent += "</h1>\r\n";
hudakz 0:68a0003c4cb8 330 http_send(client, httpHeader, httpContent);
hudakz 0:68a0003c4cb8 331 continue;
hudakz 0:68a0003c4cb8 332 }
hudakz 0:68a0003c4cb8 333
Fo170 6:2ce163810c2f 334 if(cmd == 1)
Fo170 6:2ce163810c2f 335 {
Fo170 6:2ce163810c2f 336 sw = 1; // switch on
hudakz 0:68a0003c4cb8 337 }
hudakz 0:68a0003c4cb8 338
Fo170 6:2ce163810c2f 339 if(cmd == 0)
Fo170 6:2ce163810c2f 340 {
Fo170 6:2ce163810c2f 341 sw = 0; // switch off
hudakz 0:68a0003c4cb8 342 }
hudakz 0:68a0003c4cb8 343
hudakz 0:68a0003c4cb8 344 httpHeader = HTTP_OK;
Fo170 6:2ce163810c2f 345 http_send(client, httpHeader, page_toggle_switch(sw));
hudakz 0:68a0003c4cb8 346 }
hudakz 0:68a0003c4cb8 347 }
hudakz 0:68a0003c4cb8 348 }
hudakz 0:68a0003c4cb8 349 }
hudakz 0:68a0003c4cb8 350
hudakz 3:0133517ba02d 351