test ADC avec page Web reduite
Dependencies: UIPEthernet mbed FCT_WEB hebergement
Fork of Nucleo_Web_ENC28J60 by
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/
main.cpp@6:2ce163810c2f, 2015-07-25 (annotated)
- Committer:
- Fo170
- Date:
- Sat Jul 25 21:49:36 2015 +0000
- Revision:
- 6:2ce163810c2f
- Parent:
- 5:a46a2512e17e
- Child:
- 7:acad90a34466
Web Server Nucleo F411RE & ENC28J60, ADC en Time mesure + Flot Exemples Interactivity (In construct...)
Who changed what in which revision?
User | Revision | Line number | New 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 | 6:2ce163810c2f | 53 | #define __IP_LOCAL__ "192.168.0.170" |
Fo170 | 6:2ce163810c2f | 54 | #define __hebergement__ "http://olivier.fournet.free.fr/electronique/Display_temperature_on_mini_web_server_v2/js/" |
Fo170 | 6:2ce163810c2f | 55 | #define __Temp_between_measurements__ "1" |
Fo170 | 6:2ce163810c2f | 56 | #define __Temp_between_measurements_in_Second__ 1 |
Fo170 | 6:2ce163810c2f | 57 | // Logo Test d'image en base64 : |
Fo170 | 6:2ce163810c2f | 58 | // http://webcodertools.com/imagetobase64converter/Create |
Fo170 | 6:2ce163810c2f | 59 | #define __Logo_image__ "<img alt='' src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAOwwAADsMBx2+oZAAAABp0RVh0U29mdHdhcmUAUGFpbnQuTkVUIHYzLjUuMTAw9HKhAAAAUklEQVQ4T2NggIEGMIBzCTDgSh0cHAjrIcFgiGFoGoCWELYQrgLIgGsg4CtkaTibWNfu378fq2tpHx6EbcAMQ8J6iPU3cpIhbCqaisFpCTwVAwB5lit+0ltbrgAAAABJRU5ErkJggg=='>"; |
Fo170 | 6:2ce163810c2f | 60 | #define __image_Password_Folder__ "<img alt='' src='http://olivier.fournet.free.fr/jpg/password_folder.jpg'>" |
Fo170 | 6:2ce163810c2f | 61 | #define __image_301_Moved_Permanently__ "<img alt='' src='http://olivier.fournet.free.fr/jpg/301_moved_permanently.jpg'>" |
Fo170 | 6:2ce163810c2f | 62 | #define __image_401_Unauthorized__ "<img alt='' src='http://olivier.fournet.free.fr/png/401_Unauthorized.png'>" |
Fo170 | 6:2ce163810c2f | 63 | |
Fo170 | 6:2ce163810c2f | 64 | #define NB_SAMPLES 10 |
Fo170 | 6:2ce163810c2f | 65 | 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 | 66 | float time_samples[NB_SAMPLES];// = { -0.1,2,3,4,5,6,7,8,9,10,11,12,13,14 }; |
Fo170 | 6:2ce163810c2f | 67 | float x_min = 0.0, x_max = 0.0; |
Fo170 | 6:2ce163810c2f | 68 | float y_min = 0.0, y_max = 0.0; |
Fo170 | 6:2ce163810c2f | 69 | float Seconds; |
Fo170 | 5:a46a2512e17e | 70 | |
hudakz | 0:68a0003c4cb8 | 71 | const uint16_t MY_PORT = 80; // for HTTP connection |
hudakz | 0:68a0003c4cb8 | 72 | EthernetServer myServer = EthernetServer(MY_PORT); |
hudakz | 0:68a0003c4cb8 | 73 | // In this example we are turning on/off LED1. |
hudakz | 4:d34811deedab | 74 | 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 | 75 | |
hudakz | 0:68a0003c4cb8 | 76 | const string PASSWORD = "secret"; // change as you like |
hudakz | 0:68a0003c4cb8 | 77 | const string HTTP_OK = "HTTP/1.0 200 OK"; |
hudakz | 0:68a0003c4cb8 | 78 | const string MOVED_PERM = "HTTP/1.0 301 Moved Permanently\r\nLocation: "; |
hudakz | 0:68a0003c4cb8 | 79 | const string UNAUTHORIZED = "HTTP/1.0 401 Unauthorized"; |
hudakz | 0:68a0003c4cb8 | 80 | string httpHeader; // HTTP header |
hudakz | 0:68a0003c4cb8 | 81 | string httpContent; // HTTP content |
hudakz | 0:68a0003c4cb8 | 82 | |
hudakz | 0:68a0003c4cb8 | 83 | |
hudakz | 0:68a0003c4cb8 | 84 | // analyse the url given |
hudakz | 0:68a0003c4cb8 | 85 | // return values: -1 invalid password |
hudakz | 0:68a0003c4cb8 | 86 | // -2 no command given but password valid |
hudakz | 0:68a0003c4cb8 | 87 | // -3 just refresh page |
hudakz | 0:68a0003c4cb8 | 88 | // 0 switch off |
hudakz | 0:68a0003c4cb8 | 89 | // 1 switch on |
hudakz | 0:68a0003c4cb8 | 90 | // |
hudakz | 0:68a0003c4cb8 | 91 | // The string passed to this function will look like this: |
hudakz | 0:68a0003c4cb8 | 92 | // GET /password HTTP/1..... |
hudakz | 0:68a0003c4cb8 | 93 | // GET /password/ HTTP/1..... |
hudakz | 0:68a0003c4cb8 | 94 | // GET /password/?sw=1 HTTP/1..... |
hudakz | 0:68a0003c4cb8 | 95 | // GET /password/?sw=0 HTTP/1..... |
hudakz | 0:68a0003c4cb8 | 96 | |
hudakz | 0:68a0003c4cb8 | 97 | int8_t analyse_get_url(string& str) |
hudakz | 0:68a0003c4cb8 | 98 | { |
hudakz | 0:68a0003c4cb8 | 99 | if(str.substr(5, PASSWORD.size()) != PASSWORD) |
hudakz | 0:68a0003c4cb8 | 100 | return(-1); |
hudakz | 0:68a0003c4cb8 | 101 | |
hudakz | 0:68a0003c4cb8 | 102 | uint8_t pos = 5 + PASSWORD.size(); |
hudakz | 0:68a0003c4cb8 | 103 | |
hudakz | 0:68a0003c4cb8 | 104 | if(str.substr(pos, 1) == " ") |
hudakz | 0:68a0003c4cb8 | 105 | return(-2); |
hudakz | 0:68a0003c4cb8 | 106 | |
hudakz | 0:68a0003c4cb8 | 107 | if(str.substr(pos, 1) != "/") |
hudakz | 0:68a0003c4cb8 | 108 | return(-1); |
hudakz | 0:68a0003c4cb8 | 109 | |
hudakz | 0:68a0003c4cb8 | 110 | pos++; |
hudakz | 0:68a0003c4cb8 | 111 | |
hudakz | 0:68a0003c4cb8 | 112 | string cmd(str.substr(pos, 5)); |
hudakz | 0:68a0003c4cb8 | 113 | |
hudakz | 0:68a0003c4cb8 | 114 | if(cmd == "?sw=0") |
hudakz | 0:68a0003c4cb8 | 115 | return(0); |
hudakz | 0:68a0003c4cb8 | 116 | |
hudakz | 0:68a0003c4cb8 | 117 | if(cmd == "?sw=1") |
hudakz | 0:68a0003c4cb8 | 118 | return(1); |
hudakz | 0:68a0003c4cb8 | 119 | |
hudakz | 0:68a0003c4cb8 | 120 | return(-3); |
hudakz | 0:68a0003c4cb8 | 121 | } |
hudakz | 0:68a0003c4cb8 | 122 | |
hudakz | 0:68a0003c4cb8 | 123 | string& moved_perm(uint8_t flag) |
hudakz | 0:68a0003c4cb8 | 124 | { |
hudakz | 0:68a0003c4cb8 | 125 | if(flag == 1) |
hudakz | 0:68a0003c4cb8 | 126 | httpContent = "/" + PASSWORD + "/"; |
hudakz | 0:68a0003c4cb8 | 127 | else |
hudakz | 0:68a0003c4cb8 | 128 | httpContent = ""; |
hudakz | 0:68a0003c4cb8 | 129 | |
Fo170 | 6:2ce163810c2f | 130 | httpContent += "<h1>301 Moved Permanently "; |
Fo170 | 6:2ce163810c2f | 131 | httpContent += __image_301_Moved_Permanently__ "</h1>\r\n"; |
hudakz | 0:68a0003c4cb8 | 132 | return (httpContent); |
hudakz | 0:68a0003c4cb8 | 133 | } |
hudakz | 0:68a0003c4cb8 | 134 | |
hudakz | 0:68a0003c4cb8 | 135 | string& page(uint8_t status) |
hudakz | 0:68a0003c4cb8 | 136 | { |
Fo170 | 5:a46a2512e17e | 137 | char buffer[32]; |
Fo170 | 6:2ce163810c2f | 138 | char time_stamp[32]; |
Fo170 | 5:a46a2512e17e | 139 | //------------- |
Fo170 | 6:2ce163810c2f | 140 | httpContent = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\r\n"; |
Fo170 | 6:2ce163810c2f | 141 | httpContent += "<meta http-equiv=\"refresh\" content=\"" __Temp_between_measurements__ ";url=http://" __IP_LOCAL__ "/\">\r\n"; |
Fo170 | 6:2ce163810c2f | 142 | httpContent += "<HTML><HEAD>\r\n"; |
Fo170 | 6:2ce163810c2f | 143 | httpContent += "<title>WEB Server Nucleo F411RE - ENC28J60 - Flot Examples: Interactivity</title>\r\n"; |
Fo170 | 6:2ce163810c2f | 144 | httpContent += "<script language=\"javascript\" type=\"text/javascript\" src=\"" __hebergement__ "Interactivity_init.js\"></script>\r\n"; |
Fo170 | 6:2ce163810c2f | 145 | httpContent += "<script language=\"javascript\" type=\"text/javascript\">init_Interactivity();</script>\r\n"; |
Fo170 | 6:2ce163810c2f | 146 | // Variables JavaScript |
Fo170 | 6:2ce163810c2f | 147 | httpContent += "<script language=\"javascript\" type=\"text/javascript\">\r\n"; |
Fo170 | 6:2ce163810c2f | 148 | httpContent += "var color_T = \"#FF0000\";\r\n"; |
Fo170 | 6:2ce163810c2f | 149 | httpContent += "var label_t = \"Adc(x)\";\r\n"; |
Fo170 | 6:2ce163810c2f | 150 | httpContent += "var x_min = -0.5, x_max = 14.5, y_min = -0.5, y_max = 1.5;\r\n"; |
Fo170 | 6:2ce163810c2f | 151 | httpContent += "var temperature = [[-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 | 152 | httpContent += "</script>\r\n"; |
Fo170 | 6:2ce163810c2f | 153 | // Fin Variable JavaScript |
Fo170 | 6:2ce163810c2f | 154 | httpContent += "</HEAD><BODY>\r\n"; |
Fo170 | 6:2ce163810c2f | 155 | httpContent += "<center><h2>WEB Server Nucleo F411RE</h2>\r\n"; |
Fo170 | 6:2ce163810c2f | 156 | httpContent += "<p>Designed for STM32F411RE & ENC28J60 (RTC, ADC - Flot Examples: Interactivity),\r\n"; |
Fo170 | 6:2ce163810c2f | 157 | httpContent += "<p>Compilation avec mBED à " __TIME__ " le " __DATE__" \r\n"; |
Fo170 | 6:2ce163810c2f | 158 | |
Fo170 | 6:2ce163810c2f | 159 | httpContent += __Logo_image__ "<p></center>\r\n"; |
Fo170 | 6:2ce163810c2f | 160 | httpContent += "<hr><p>\r\n"; |
Fo170 | 5:a46a2512e17e | 161 | |
Fo170 | 6:2ce163810c2f | 162 | if(status == 1) |
Fo170 | 6:2ce163810c2f | 163 | { |
Fo170 | 6:2ce163810c2f | 164 | httpContent += "<font color=#00FF00>Switch ON</font>\r\n"; |
Fo170 | 6:2ce163810c2f | 165 | } |
Fo170 | 6:2ce163810c2f | 166 | else |
Fo170 | 6:2ce163810c2f | 167 | { |
Fo170 | 6:2ce163810c2f | 168 | httpContent += "<font color=#FF0000>Switch OFF</font>\r\n"; |
Fo170 | 6:2ce163810c2f | 169 | } |
hudakz | 0:68a0003c4cb8 | 170 | |
Fo170 | 6:2ce163810c2f | 171 | httpContent += "<hr>\r\n"; |
Fo170 | 6:2ce163810c2f | 172 | //------------- |
Fo170 | 6:2ce163810c2f | 173 | time_t seconds = time(NULL)+ 19800; // time(null) gives the GMT time . |
Fo170 | 6:2ce163810c2f | 174 | // printf("Time as seconds since January 1, 1970 = %d\n", seconds); |
Fo170 | 6:2ce163810c2f | 175 | strftime(time_stamp, 32, "Local Time: %y %m %d, %H:%M:%Ss", localtime(&seconds)); |
Fo170 | 6:2ce163810c2f | 176 | // this converts the value in seconds obtained above to human readable format and assigns it to the timestamp |
Fo170 | 6:2ce163810c2f | 177 | sprintf(buffer, "%s", time_stamp);// diplays the human readable time |
Fo170 | 6:2ce163810c2f | 178 | httpContent += buffer; |
Fo170 | 6:2ce163810c2f | 179 | httpContent += "\r\n"; |
Fo170 | 6:2ce163810c2f | 180 | httpContent += "<hr>\r\n"; |
Fo170 | 6:2ce163810c2f | 181 | //---------------- |
Fo170 | 6:2ce163810c2f | 182 | httpContent += "AnalogIn(PC_0) : "; |
Fo170 | 6:2ce163810c2f | 183 | sprintf(buffer, "%4.3f", a_in * 3.3); |
Fo170 | 6:2ce163810c2f | 184 | httpContent += buffer; |
Fo170 | 6:2ce163810c2f | 185 | httpContent += "V\r\n"; |
Fo170 | 6:2ce163810c2f | 186 | httpContent += "<hr>\r\n<p>Usage Password Page :<p>http://host_or_ip/password<p><hr>\r\n"; |
Fo170 | 6:2ce163810c2f | 187 | httpContent += "<script language=\"javascript\" type=\"text/javascript\">Interactivity();</script>\r\n"; |
Fo170 | 6:2ce163810c2f | 188 | httpContent += "</BODY></HTML>"; |
Fo170 | 6:2ce163810c2f | 189 | //----------- |
Fo170 | 6:2ce163810c2f | 190 | wait(1); |
Fo170 | 6:2ce163810c2f | 191 | return httpContent; |
Fo170 | 6:2ce163810c2f | 192 | } |
Fo170 | 6:2ce163810c2f | 193 | |
Fo170 | 6:2ce163810c2f | 194 | string& page_toggle_switch(uint8_t status) |
Fo170 | 6:2ce163810c2f | 195 | { |
Fo170 | 6:2ce163810c2f | 196 | //------------- |
Fo170 | 6:2ce163810c2f | 197 | httpContent = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\r\n"; |
Fo170 | 6:2ce163810c2f | 198 | httpContent += "<HTML><HEAD>\r\n"; |
Fo170 | 6:2ce163810c2f | 199 | httpContent += "<title>WEB Server Nucleo F411RE - ENC28J60 - Password Page</title>\r\n"; |
Fo170 | 6:2ce163810c2f | 200 | httpContent += "</HEAD><BODY>\r\n"; |
Fo170 | 6:2ce163810c2f | 201 | httpContent += "<center><h2>WEB Server Nucleo F411RE - ENC28J60 - Password Page</h2></center>\r\n"; |
Fo170 | 6:2ce163810c2f | 202 | httpContent += "<p>" __image_Password_Folder__ "<p>\r\n"; |
Fo170 | 6:2ce163810c2f | 203 | if(status == 1) |
Fo170 | 6:2ce163810c2f | 204 | { |
Fo170 | 6:2ce163810c2f | 205 | httpContent += "<hr><pre>\r\n <font color=#00FF00>ON</font>"; |
hudakz | 0:68a0003c4cb8 | 206 | httpContent += " <a href=\"./?sw=0\">[switch off]</a>\r\n"; |
Fo170 | 6:2ce163810c2f | 207 | } |
Fo170 | 6:2ce163810c2f | 208 | else |
Fo170 | 6:2ce163810c2f | 209 | { |
Fo170 | 6:2ce163810c2f | 210 | httpContent += "<hr><pre>\r\n <font color=#FF0000>OFF</font>"; |
hudakz | 0:68a0003c4cb8 | 211 | httpContent += " <a href=\"./?sw=1\">[switch on]</a>\r\n"; |
hudakz | 0:68a0003c4cb8 | 212 | } |
hudakz | 0:68a0003c4cb8 | 213 | |
hudakz | 0:68a0003c4cb8 | 214 | httpContent += " <a href=\".\">[refresh status]</a>\r\n"; |
hudakz | 0:68a0003c4cb8 | 215 | httpContent += "</pre>\r\n"; |
hudakz | 0:68a0003c4cb8 | 216 | httpContent += "<hr>\r\n"; |
Fo170 | 6:2ce163810c2f | 217 | |
Fo170 | 6:2ce163810c2f | 218 | httpContent += "</BODY></HTML>"; |
Fo170 | 5:a46a2512e17e | 219 | //----------- |
Fo170 | 5:a46a2512e17e | 220 | wait(1); |
hudakz | 0:68a0003c4cb8 | 221 | return httpContent; |
hudakz | 0:68a0003c4cb8 | 222 | } |
hudakz | 0:68a0003c4cb8 | 223 | |
hudakz | 0:68a0003c4cb8 | 224 | void http_send(EthernetClient& client, string& header, string& content) |
hudakz | 0:68a0003c4cb8 | 225 | { |
hudakz | 0:68a0003c4cb8 | 226 | char content_length[5] = {}; |
hudakz | 0:68a0003c4cb8 | 227 | |
hudakz | 2:76f339a1ba9b | 228 | header += "\r\nContent-Type: text/html\r\n"; |
hudakz | 0:68a0003c4cb8 | 229 | header += "Content-Length: "; |
hudakz | 0:68a0003c4cb8 | 230 | sprintf(content_length, "%d", content.length()); |
hudakz | 0:68a0003c4cb8 | 231 | header += string(content_length) + "\r\n"; |
hudakz | 0:68a0003c4cb8 | 232 | header += "Pragma: no-cache\r\n"; |
hudakz | 0:68a0003c4cb8 | 233 | header += "Connection: About to close\r\n"; |
hudakz | 0:68a0003c4cb8 | 234 | header += "\r\n"; |
hudakz | 0:68a0003c4cb8 | 235 | string webpage = header + content; |
hudakz | 0:68a0003c4cb8 | 236 | client.write((uint8_t*)webpage.c_str(),webpage.length()); |
hudakz | 0:68a0003c4cb8 | 237 | } |
hudakz | 0:68a0003c4cb8 | 238 | |
hudakz | 0:68a0003c4cb8 | 239 | int main() |
Fo170 | 5:a46a2512e17e | 240 | { |
Fo170 | 5:a46a2512e17e | 241 | // RTC |
Fo170 | 5:a46a2512e17e | 242 | set_time(1387188323); // Set RTC time to 16 December 2013 10:05:23 UTC |
Fo170 | 5:a46a2512e17e | 243 | // Date and time are set. |
Fo170 | 6:2ce163810c2f | 244 | |
hudakz | 3:0133517ba02d | 245 | UIPEthernet.begin(MY_MAC,MY_IP); |
hudakz | 0:68a0003c4cb8 | 246 | myServer.begin(); |
hudakz | 0:68a0003c4cb8 | 247 | while(1) { |
hudakz | 0:68a0003c4cb8 | 248 | EthernetClient client = myServer.available(); |
hudakz | 0:68a0003c4cb8 | 249 | if (client) { |
hudakz | 0:68a0003c4cb8 | 250 | size_t size = client.available(); |
hudakz | 0:68a0003c4cb8 | 251 | if(size > 0) { |
hudakz | 0:68a0003c4cb8 | 252 | uint8_t* buf = (uint8_t*)malloc(size); |
hudakz | 0:68a0003c4cb8 | 253 | size = client.read(buf, size); |
hudakz | 0:68a0003c4cb8 | 254 | string received((char*)buf); |
hudakz | 0:68a0003c4cb8 | 255 | free(buf); |
hudakz | 0:68a0003c4cb8 | 256 | if(received.substr(0, 3) != "GET") { |
hudakz | 0:68a0003c4cb8 | 257 | // head, post or other method |
hudakz | 0:68a0003c4cb8 | 258 | // for possible status codes see: |
hudakz | 0:68a0003c4cb8 | 259 | // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html |
hudakz | 0:68a0003c4cb8 | 260 | httpHeader = HTTP_OK; |
hudakz | 0:68a0003c4cb8 | 261 | httpContent = "<h1>200 OK</h1>"; |
hudakz | 0:68a0003c4cb8 | 262 | http_send(client, httpHeader, httpContent); |
hudakz | 0:68a0003c4cb8 | 263 | continue; |
hudakz | 0:68a0003c4cb8 | 264 | } |
hudakz | 0:68a0003c4cb8 | 265 | |
hudakz | 0:68a0003c4cb8 | 266 | if(received.substr(0, 6) == "GET / ") { |
hudakz | 0:68a0003c4cb8 | 267 | httpHeader = HTTP_OK; |
Fo170 | 6:2ce163810c2f | 268 | /* |
hudakz | 0:68a0003c4cb8 | 269 | httpContent = "<p>Usage: http://host_or_ip/password</p>\r\n"; |
hudakz | 0:68a0003c4cb8 | 270 | http_send(client, httpHeader, httpContent); |
Fo170 | 6:2ce163810c2f | 271 | */ |
Fo170 | 6:2ce163810c2f | 272 | http_send(client, httpHeader, page(sw)); |
hudakz | 0:68a0003c4cb8 | 273 | continue; |
hudakz | 0:68a0003c4cb8 | 274 | } |
hudakz | 0:68a0003c4cb8 | 275 | |
hudakz | 0:68a0003c4cb8 | 276 | int cmd = analyse_get_url(received); |
hudakz | 0:68a0003c4cb8 | 277 | |
hudakz | 0:68a0003c4cb8 | 278 | if(cmd == -2) { |
hudakz | 0:68a0003c4cb8 | 279 | // redirect to the right base url |
hudakz | 0:68a0003c4cb8 | 280 | httpHeader = MOVED_PERM; |
hudakz | 0:68a0003c4cb8 | 281 | http_send(client, httpHeader, moved_perm(1)); |
hudakz | 0:68a0003c4cb8 | 282 | continue; |
hudakz | 0:68a0003c4cb8 | 283 | } |
hudakz | 0:68a0003c4cb8 | 284 | |
hudakz | 0:68a0003c4cb8 | 285 | if(cmd == -1) { |
hudakz | 0:68a0003c4cb8 | 286 | httpHeader = UNAUTHORIZED; |
Fo170 | 6:2ce163810c2f | 287 | httpContent = "<h1>401 Unauthorized"; |
Fo170 | 6:2ce163810c2f | 288 | httpContent += __image_401_Unauthorized__ "</h1>\r\n"; |
hudakz | 0:68a0003c4cb8 | 289 | http_send(client, httpHeader, httpContent); |
hudakz | 0:68a0003c4cb8 | 290 | continue; |
hudakz | 0:68a0003c4cb8 | 291 | } |
hudakz | 0:68a0003c4cb8 | 292 | |
Fo170 | 6:2ce163810c2f | 293 | if(cmd == 1) |
Fo170 | 6:2ce163810c2f | 294 | { |
Fo170 | 6:2ce163810c2f | 295 | sw = 1; // switch on |
hudakz | 0:68a0003c4cb8 | 296 | } |
hudakz | 0:68a0003c4cb8 | 297 | |
Fo170 | 6:2ce163810c2f | 298 | if(cmd == 0) |
Fo170 | 6:2ce163810c2f | 299 | { |
Fo170 | 6:2ce163810c2f | 300 | sw = 0; // switch off |
hudakz | 0:68a0003c4cb8 | 301 | } |
hudakz | 0:68a0003c4cb8 | 302 | |
hudakz | 0:68a0003c4cb8 | 303 | httpHeader = HTTP_OK; |
Fo170 | 6:2ce163810c2f | 304 | http_send(client, httpHeader, page_toggle_switch(sw)); |
hudakz | 0:68a0003c4cb8 | 305 | } |
hudakz | 0:68a0003c4cb8 | 306 | } |
hudakz | 0:68a0003c4cb8 | 307 | } |
hudakz | 0:68a0003c4cb8 | 308 | } |
hudakz | 0:68a0003c4cb8 | 309 | |
hudakz | 3:0133517ba02d | 310 |