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@11:afb33350db83, 2015-07-28 (annotated)
- Committer:
- Fo170
- Date:
- Tue Jul 28 20:24:02 2015 +0000
- Revision:
- 11:afb33350db83
- Parent:
- 10:bbee7dd4bcda
- Child:
- 12:61b10a733ede
mise en place du code pour graphique dynamique ADC, les test de bon fonctionnement : au bout d'une apr?s-midi la page ne s'affiche plus, le red?marrage de l'ordinateur redonne la visibilit? de la page qui fonctionne toujours au-del?s des 25 400 sec...
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 | 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 | 11:afb33350db83 | 75 | unsigned long int Sample = 0; |
Fo170 | 6:2ce163810c2f | 76 | 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 | 77 | float time_samples[NB_SAMPLES];// = { -0.1,2,3,4,5,6,7,8,9,10,11,12,13,14 }; |
Fo170 | 6:2ce163810c2f | 78 | float x_min = 0.0, x_max = 0.0; |
Fo170 | 6:2ce163810c2f | 79 | float y_min = 0.0, y_max = 0.0; |
Fo170 | 7:acad90a34466 | 80 | float Seconds = 0.0; |
Fo170 | 11:afb33350db83 | 81 | float k = 0.0; |
Fo170 | 11:afb33350db83 | 82 | //------------ |
Fo170 | 7:acad90a34466 | 83 | Ticker second_ticker; |
Fo170 | 7:acad90a34466 | 84 | |
Fo170 | 7:acad90a34466 | 85 | void add_one_second() |
Fo170 | 11:afb33350db83 | 86 | { |
Fo170 | 11:afb33350db83 | 87 | Seconds = Seconds + k;//0.1; |
Fo170 | 7:acad90a34466 | 88 | } |
Fo170 | 11:afb33350db83 | 89 | //------------ |
hudakz | 0:68a0003c4cb8 | 90 | const uint16_t MY_PORT = 80; // for HTTP connection |
hudakz | 0:68a0003c4cb8 | 91 | EthernetServer myServer = EthernetServer(MY_PORT); |
hudakz | 0:68a0003c4cb8 | 92 | // In this example we are turning on/off LED1. |
hudakz | 4:d34811deedab | 93 | 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 | 94 | |
hudakz | 0:68a0003c4cb8 | 95 | const string PASSWORD = "secret"; // change as you like |
hudakz | 0:68a0003c4cb8 | 96 | const string HTTP_OK = "HTTP/1.0 200 OK"; |
hudakz | 0:68a0003c4cb8 | 97 | const string MOVED_PERM = "HTTP/1.0 301 Moved Permanently\r\nLocation: "; |
hudakz | 0:68a0003c4cb8 | 98 | const string UNAUTHORIZED = "HTTP/1.0 401 Unauthorized"; |
hudakz | 0:68a0003c4cb8 | 99 | string httpHeader; // HTTP header |
hudakz | 0:68a0003c4cb8 | 100 | string httpContent; // HTTP content |
hudakz | 0:68a0003c4cb8 | 101 | |
hudakz | 0:68a0003c4cb8 | 102 | |
hudakz | 0:68a0003c4cb8 | 103 | // analyse the url given |
hudakz | 0:68a0003c4cb8 | 104 | // return values: -1 invalid password |
hudakz | 0:68a0003c4cb8 | 105 | // -2 no command given but password valid |
hudakz | 0:68a0003c4cb8 | 106 | // -3 just refresh page |
hudakz | 0:68a0003c4cb8 | 107 | // 0 switch off |
hudakz | 0:68a0003c4cb8 | 108 | // 1 switch on |
hudakz | 0:68a0003c4cb8 | 109 | // |
hudakz | 0:68a0003c4cb8 | 110 | // The string passed to this function will look like this: |
hudakz | 0:68a0003c4cb8 | 111 | // GET /password HTTP/1..... |
hudakz | 0:68a0003c4cb8 | 112 | // GET /password/ HTTP/1..... |
hudakz | 0:68a0003c4cb8 | 113 | // GET /password/?sw=1 HTTP/1..... |
hudakz | 0:68a0003c4cb8 | 114 | // GET /password/?sw=0 HTTP/1..... |
hudakz | 0:68a0003c4cb8 | 115 | |
hudakz | 0:68a0003c4cb8 | 116 | int8_t analyse_get_url(string& str) |
hudakz | 0:68a0003c4cb8 | 117 | { |
hudakz | 0:68a0003c4cb8 | 118 | if(str.substr(5, PASSWORD.size()) != PASSWORD) |
hudakz | 0:68a0003c4cb8 | 119 | return(-1); |
hudakz | 0:68a0003c4cb8 | 120 | |
hudakz | 0:68a0003c4cb8 | 121 | uint8_t pos = 5 + PASSWORD.size(); |
hudakz | 0:68a0003c4cb8 | 122 | |
Fo170 | 7:acad90a34466 | 123 | if(str.substr(pos, 1) == " ") return(-2); |
hudakz | 0:68a0003c4cb8 | 124 | |
Fo170 | 7:acad90a34466 | 125 | if(str.substr(pos, 1) != "/") return(-1); |
hudakz | 0:68a0003c4cb8 | 126 | |
hudakz | 0:68a0003c4cb8 | 127 | pos++; |
hudakz | 0:68a0003c4cb8 | 128 | |
hudakz | 0:68a0003c4cb8 | 129 | string cmd(str.substr(pos, 5)); |
hudakz | 0:68a0003c4cb8 | 130 | |
Fo170 | 7:acad90a34466 | 131 | if(cmd == "?sw=0") return(0); |
hudakz | 0:68a0003c4cb8 | 132 | |
Fo170 | 7:acad90a34466 | 133 | if(cmd == "?sw=1") return(1); |
hudakz | 0:68a0003c4cb8 | 134 | |
hudakz | 0:68a0003c4cb8 | 135 | return(-3); |
hudakz | 0:68a0003c4cb8 | 136 | } |
hudakz | 0:68a0003c4cb8 | 137 | |
hudakz | 0:68a0003c4cb8 | 138 | string& moved_perm(uint8_t flag) |
hudakz | 0:68a0003c4cb8 | 139 | { |
hudakz | 0:68a0003c4cb8 | 140 | if(flag == 1) |
hudakz | 0:68a0003c4cb8 | 141 | httpContent = "/" + PASSWORD + "/"; |
hudakz | 0:68a0003c4cb8 | 142 | else |
hudakz | 0:68a0003c4cb8 | 143 | httpContent = ""; |
hudakz | 0:68a0003c4cb8 | 144 | |
Fo170 | 6:2ce163810c2f | 145 | httpContent += "<h1>301 Moved Permanently "; |
Fo170 | 9:2f7ad2b13ec8 | 146 | httpContent += __image_301_Moved_Permanently__; |
Fo170 | 9:2f7ad2b13ec8 | 147 | httpContent += "</h1>\r\n"; |
hudakz | 0:68a0003c4cb8 | 148 | return (httpContent); |
hudakz | 0:68a0003c4cb8 | 149 | } |
hudakz | 0:68a0003c4cb8 | 150 | |
hudakz | 0:68a0003c4cb8 | 151 | string& page(uint8_t status) |
hudakz | 0:68a0003c4cb8 | 152 | { |
Fo170 | 11:afb33350db83 | 153 | char buffer[128]; |
Fo170 | 8:305b64d7dd23 | 154 | //char time_stamp[32]; |
Fo170 | 7:acad90a34466 | 155 | float meas; |
Fo170 | 7:acad90a34466 | 156 | meas = a_in.read(); // Converts and read the analog input value (value from 0.0 to 1.0) |
Fo170 | 8:305b64d7dd23 | 157 | meas = meas * 3300.0; // Change the value to be in the 0 to 3300 range |
Fo170 | 8:305b64d7dd23 | 158 | //Seconds = Seconds + 1; |
Fo170 | 11:afb33350db83 | 159 | x_min = x_max = Seconds; |
Fo170 | 11:afb33350db83 | 160 | y_min = y_max = meas; |
Fo170 | 11:afb33350db83 | 161 | int i; |
Fo170 | 11:afb33350db83 | 162 | |
Fo170 | 11:afb33350db83 | 163 | for(i = 1 ; i < NB_SAMPLES ; i++) |
Fo170 | 11:afb33350db83 | 164 | { |
Fo170 | 11:afb33350db83 | 165 | time_samples[i-1] = time_samples[i]; |
Fo170 | 11:afb33350db83 | 166 | adc_samples[i-1] = adc_samples[i]; |
Fo170 | 11:afb33350db83 | 167 | if( time_samples[i] < x_min ) x_min = time_samples[i]; |
Fo170 | 11:afb33350db83 | 168 | if( time_samples[i] > x_max ) x_max = time_samples[i]; |
Fo170 | 11:afb33350db83 | 169 | if( adc_samples[i] < y_min ) y_min = adc_samples[i]; |
Fo170 | 11:afb33350db83 | 170 | if( adc_samples[i] > y_max ) y_max = adc_samples[i]; |
Fo170 | 11:afb33350db83 | 171 | } |
Fo170 | 11:afb33350db83 | 172 | |
Fo170 | 11:afb33350db83 | 173 | adc_samples[NB_SAMPLES-1] = meas; |
Fo170 | 11:afb33350db83 | 174 | time_samples[NB_SAMPLES-1] = Seconds; |
Fo170 | 5:a46a2512e17e | 175 | //------------- |
Fo170 | 6:2ce163810c2f | 176 | httpContent = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\r\n"; |
Fo170 | 9:2f7ad2b13ec8 | 177 | httpContent += "<meta http-equiv=\"refresh\" content=\""; |
Fo170 | 9:2f7ad2b13ec8 | 178 | httpContent += __Temp_between_measurements__; |
Fo170 | 9:2f7ad2b13ec8 | 179 | httpContent += ";url=http://"; |
Fo170 | 9:2f7ad2b13ec8 | 180 | httpContent += __IP_LOCAL__; |
Fo170 | 9:2f7ad2b13ec8 | 181 | httpContent += "/\">\r\n"; |
Fo170 | 6:2ce163810c2f | 182 | httpContent += "<HTML><HEAD>\r\n"; |
Fo170 | 6:2ce163810c2f | 183 | httpContent += "<title>WEB Server Nucleo F411RE - ENC28J60 - Flot Examples: Interactivity</title>\r\n"; |
Fo170 | 9:2f7ad2b13ec8 | 184 | httpContent += "<script language=\"javascript\" type=\"text/javascript\" src=\""; |
Fo170 | 9:2f7ad2b13ec8 | 185 | httpContent += __hebergement__; |
Fo170 | 9:2f7ad2b13ec8 | 186 | httpContent += "WebServerNucleo_Interactivity_init.js\"></script>\r\n"; |
Fo170 | 8:305b64d7dd23 | 187 | httpContent += "<script language=\"javascript\" type=\"text/javascript\">init_WebServerNucleo_Interactivity();</script>\r\n"; |
Fo170 | 6:2ce163810c2f | 188 | // Variables JavaScript |
Fo170 | 6:2ce163810c2f | 189 | httpContent += "<script language=\"javascript\" type=\"text/javascript\">\r\n"; |
Fo170 | 8:305b64d7dd23 | 190 | httpContent += "var color_Y = \"#FF0000\";\r\n"; |
Fo170 | 8:305b64d7dd23 | 191 | httpContent += "var label_Y = \"Adc(x)\";\r\n"; |
Fo170 | 11:afb33350db83 | 192 | //httpContent += "var x_min = -0.5, x_max = 14.5, y_min = -0.5, y_max = 1.5;\r\n"; |
Fo170 | 11:afb33350db83 | 193 | sprintf(buffer, "var x_min = %f, x_max = %f, y_min = %f, y_max = %f;\r\n", x_min, x_max, y_min, y_max); |
Fo170 | 11:afb33350db83 | 194 | httpContent += buffer; |
Fo170 | 11:afb33350db83 | 195 | //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 | 11:afb33350db83 | 196 | if(Sample > NB_SAMPLES) |
Fo170 | 11:afb33350db83 | 197 | { |
Fo170 | 11:afb33350db83 | 198 | httpContent += "var array_value = ["; |
Fo170 | 11:afb33350db83 | 199 | for(i = 0 ; i < NB_SAMPLES ; i++) |
Fo170 | 11:afb33350db83 | 200 | { |
Fo170 | 11:afb33350db83 | 201 | sprintf(buffer, "[%f,%f],", time_samples[i], adc_samples[i]); |
Fo170 | 11:afb33350db83 | 202 | httpContent += buffer; |
Fo170 | 11:afb33350db83 | 203 | } |
Fo170 | 11:afb33350db83 | 204 | httpContent += "];\r\n"; |
Fo170 | 11:afb33350db83 | 205 | } |
Fo170 | 11:afb33350db83 | 206 | Sample++; |
Fo170 | 6:2ce163810c2f | 207 | httpContent += "</script>\r\n"; |
Fo170 | 6:2ce163810c2f | 208 | // Fin Variable JavaScript |
Fo170 | 6:2ce163810c2f | 209 | httpContent += "</HEAD><BODY>\r\n"; |
Fo170 | 6:2ce163810c2f | 210 | httpContent += "<center><h2>WEB Server Nucleo F411RE</h2>\r\n"; |
Fo170 | 7:acad90a34466 | 211 | 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 | 212 | httpContent += "<p>Compilation avec mBED à " __TIME__ " le " __DATE__" \r\n"; |
Fo170 | 6:2ce163810c2f | 213 | |
Fo170 | 8:305b64d7dd23 | 214 | // httpContent += __Logo_image__ ; |
Fo170 | 8:305b64d7dd23 | 215 | |
Fo170 | 8:305b64d7dd23 | 216 | httpContent += "<p></center>\r\n<hr><p>\r\n"; |
Fo170 | 5:a46a2512e17e | 217 | |
Fo170 | 6:2ce163810c2f | 218 | if(status == 1) |
Fo170 | 6:2ce163810c2f | 219 | { |
Fo170 | 6:2ce163810c2f | 220 | httpContent += "<font color=#00FF00>Switch ON</font>\r\n"; |
Fo170 | 6:2ce163810c2f | 221 | } |
Fo170 | 6:2ce163810c2f | 222 | else |
Fo170 | 6:2ce163810c2f | 223 | { |
Fo170 | 6:2ce163810c2f | 224 | httpContent += "<font color=#FF0000>Switch OFF</font>\r\n"; |
Fo170 | 6:2ce163810c2f | 225 | } |
hudakz | 0:68a0003c4cb8 | 226 | |
Fo170 | 6:2ce163810c2f | 227 | httpContent += "<hr>\r\n"; |
Fo170 | 6:2ce163810c2f | 228 | //------------- |
Fo170 | 7:acad90a34466 | 229 | /* |
Fo170 | 7:acad90a34466 | 230 | httpContent += "Local Time: "; |
Fo170 | 6:2ce163810c2f | 231 | time_t seconds = time(NULL)+ 19800; // time(null) gives the GMT time . |
Fo170 | 6:2ce163810c2f | 232 | // printf("Time as seconds since January 1, 1970 = %d\n", seconds); |
Fo170 | 7:acad90a34466 | 233 | strftime(time_stamp, 32, "%y %m %d, %H:%M:%Ss", localtime(&seconds)); |
Fo170 | 6:2ce163810c2f | 234 | // this converts the value in seconds obtained above to human readable format and assigns it to the timestamp |
Fo170 | 6:2ce163810c2f | 235 | sprintf(buffer, "%s", time_stamp);// diplays the human readable time |
Fo170 | 7:acad90a34466 | 236 | */ |
Fo170 | 8:305b64d7dd23 | 237 | sprintf(buffer, "%.1f ", Seconds);// diplays the human readable Seconds |
Fo170 | 6:2ce163810c2f | 238 | httpContent += buffer; |
Fo170 | 7:acad90a34466 | 239 | httpContent += "Seconds\r\n<hr>\r\n"; |
Fo170 | 6:2ce163810c2f | 240 | //---------------- |
Fo170 | 6:2ce163810c2f | 241 | httpContent += "AnalogIn(PC_0) : "; |
Fo170 | 7:acad90a34466 | 242 | sprintf(buffer, "%.0f mV\r\n", meas); |
Fo170 | 6:2ce163810c2f | 243 | httpContent += buffer; |
Fo170 | 6:2ce163810c2f | 244 | httpContent += "<hr>\r\n<p>Usage Password Page :<p>http://host_or_ip/password<p><hr>\r\n"; |
Fo170 | 8:305b64d7dd23 | 245 | httpContent += "<script language=\"javascript\" type=\"text/javascript\">WebServerNucleo_Interactivity();</script>\r\n"; |
Fo170 | 6:2ce163810c2f | 246 | httpContent += "</BODY></HTML>"; |
Fo170 | 6:2ce163810c2f | 247 | //----------- |
Fo170 | 7:acad90a34466 | 248 | //wait(1); |
Fo170 | 6:2ce163810c2f | 249 | return httpContent; |
Fo170 | 6:2ce163810c2f | 250 | } |
Fo170 | 6:2ce163810c2f | 251 | |
Fo170 | 6:2ce163810c2f | 252 | string& page_toggle_switch(uint8_t status) |
Fo170 | 6:2ce163810c2f | 253 | { |
Fo170 | 6:2ce163810c2f | 254 | //------------- |
Fo170 | 6:2ce163810c2f | 255 | httpContent = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\r\n"; |
Fo170 | 6:2ce163810c2f | 256 | httpContent += "<HTML><HEAD>\r\n"; |
Fo170 | 6:2ce163810c2f | 257 | httpContent += "<title>WEB Server Nucleo F411RE - ENC28J60 - Password Page</title>\r\n"; |
Fo170 | 6:2ce163810c2f | 258 | httpContent += "</HEAD><BODY>\r\n"; |
Fo170 | 9:2f7ad2b13ec8 | 259 | httpContent += "<center><h2>WEB Server Nucleo F411RE - ENC28J60 - Password Page</h2></center>\r\n<p>"; |
Fo170 | 9:2f7ad2b13ec8 | 260 | httpContent += __image_Password_Folder__; |
Fo170 | 9:2f7ad2b13ec8 | 261 | httpContent += "<p>\r\n"; |
Fo170 | 8:305b64d7dd23 | 262 | |
Fo170 | 6:2ce163810c2f | 263 | if(status == 1) |
Fo170 | 6:2ce163810c2f | 264 | { |
Fo170 | 6:2ce163810c2f | 265 | httpContent += "<hr><pre>\r\n <font color=#00FF00>ON</font>"; |
hudakz | 0:68a0003c4cb8 | 266 | httpContent += " <a href=\"./?sw=0\">[switch off]</a>\r\n"; |
Fo170 | 6:2ce163810c2f | 267 | } |
Fo170 | 6:2ce163810c2f | 268 | else |
Fo170 | 6:2ce163810c2f | 269 | { |
Fo170 | 6:2ce163810c2f | 270 | httpContent += "<hr><pre>\r\n <font color=#FF0000>OFF</font>"; |
hudakz | 0:68a0003c4cb8 | 271 | httpContent += " <a href=\"./?sw=1\">[switch on]</a>\r\n"; |
hudakz | 0:68a0003c4cb8 | 272 | } |
hudakz | 0:68a0003c4cb8 | 273 | |
hudakz | 0:68a0003c4cb8 | 274 | httpContent += " <a href=\".\">[refresh status]</a>\r\n"; |
Fo170 | 11:afb33350db83 | 275 | httpContent += "</pre>\r\n<hr>\r\n"; |
Fo170 | 6:2ce163810c2f | 276 | |
Fo170 | 6:2ce163810c2f | 277 | httpContent += "</BODY></HTML>"; |
Fo170 | 5:a46a2512e17e | 278 | //----------- |
Fo170 | 5:a46a2512e17e | 279 | wait(1); |
hudakz | 0:68a0003c4cb8 | 280 | return httpContent; |
hudakz | 0:68a0003c4cb8 | 281 | } |
hudakz | 0:68a0003c4cb8 | 282 | |
hudakz | 0:68a0003c4cb8 | 283 | void http_send(EthernetClient& client, string& header, string& content) |
hudakz | 0:68a0003c4cb8 | 284 | { |
hudakz | 0:68a0003c4cb8 | 285 | char content_length[5] = {}; |
hudakz | 0:68a0003c4cb8 | 286 | |
hudakz | 2:76f339a1ba9b | 287 | header += "\r\nContent-Type: text/html\r\n"; |
hudakz | 0:68a0003c4cb8 | 288 | header += "Content-Length: "; |
hudakz | 0:68a0003c4cb8 | 289 | sprintf(content_length, "%d", content.length()); |
hudakz | 0:68a0003c4cb8 | 290 | header += string(content_length) + "\r\n"; |
hudakz | 0:68a0003c4cb8 | 291 | header += "Pragma: no-cache\r\n"; |
hudakz | 0:68a0003c4cb8 | 292 | header += "Connection: About to close\r\n"; |
hudakz | 0:68a0003c4cb8 | 293 | header += "\r\n"; |
hudakz | 0:68a0003c4cb8 | 294 | string webpage = header + content; |
hudakz | 0:68a0003c4cb8 | 295 | client.write((uint8_t*)webpage.c_str(),webpage.length()); |
hudakz | 0:68a0003c4cb8 | 296 | } |
hudakz | 0:68a0003c4cb8 | 297 | |
hudakz | 0:68a0003c4cb8 | 298 | int main() |
Fo170 | 7:acad90a34466 | 299 | { |
Fo170 | 5:a46a2512e17e | 300 | // RTC |
Fo170 | 8:305b64d7dd23 | 301 | //set_time(1387188323); // Set RTC time to 16 December 2013 10:05:23 UTC |
Fo170 | 7:acad90a34466 | 302 | |
Fo170 | 5:a46a2512e17e | 303 | // Date and time are set. |
Fo170 | 7:acad90a34466 | 304 | |
Fo170 | 7:acad90a34466 | 305 | // Init the ticker with the address of the function (add_one_second) to be attached and the interval (100 ms) |
Fo170 | 8:305b64d7dd23 | 306 | second_ticker.attach(&add_one_second, 0.1); |
Fo170 | 11:afb33350db83 | 307 | k = (float)__Temp_between_measurements_in_Second__ / (float)NB_SAMPLES; |
Fo170 | 7:acad90a34466 | 308 | //----------------- |
hudakz | 3:0133517ba02d | 309 | UIPEthernet.begin(MY_MAC,MY_IP); |
hudakz | 0:68a0003c4cb8 | 310 | myServer.begin(); |
Fo170 | 7:acad90a34466 | 311 | while(1) |
Fo170 | 7:acad90a34466 | 312 | { |
hudakz | 0:68a0003c4cb8 | 313 | EthernetClient client = myServer.available(); |
Fo170 | 7:acad90a34466 | 314 | if(client) |
Fo170 | 7:acad90a34466 | 315 | { |
hudakz | 0:68a0003c4cb8 | 316 | size_t size = client.available(); |
Fo170 | 7:acad90a34466 | 317 | if(size > 0) |
Fo170 | 7:acad90a34466 | 318 | { |
hudakz | 0:68a0003c4cb8 | 319 | uint8_t* buf = (uint8_t*)malloc(size); |
hudakz | 0:68a0003c4cb8 | 320 | size = client.read(buf, size); |
hudakz | 0:68a0003c4cb8 | 321 | string received((char*)buf); |
hudakz | 0:68a0003c4cb8 | 322 | free(buf); |
Fo170 | 7:acad90a34466 | 323 | if(received.substr(0, 3) != "GET") |
Fo170 | 7:acad90a34466 | 324 | { |
hudakz | 0:68a0003c4cb8 | 325 | // head, post or other method |
hudakz | 0:68a0003c4cb8 | 326 | // for possible status codes see: |
hudakz | 0:68a0003c4cb8 | 327 | // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html |
hudakz | 0:68a0003c4cb8 | 328 | httpHeader = HTTP_OK; |
hudakz | 0:68a0003c4cb8 | 329 | httpContent = "<h1>200 OK</h1>"; |
hudakz | 0:68a0003c4cb8 | 330 | http_send(client, httpHeader, httpContent); |
hudakz | 0:68a0003c4cb8 | 331 | continue; |
hudakz | 0:68a0003c4cb8 | 332 | } |
hudakz | 0:68a0003c4cb8 | 333 | |
Fo170 | 7:acad90a34466 | 334 | if(received.substr(0, 6) == "GET / ") |
Fo170 | 7:acad90a34466 | 335 | { |
hudakz | 0:68a0003c4cb8 | 336 | httpHeader = HTTP_OK; |
Fo170 | 6:2ce163810c2f | 337 | /* |
hudakz | 0:68a0003c4cb8 | 338 | httpContent = "<p>Usage: http://host_or_ip/password</p>\r\n"; |
hudakz | 0:68a0003c4cb8 | 339 | http_send(client, httpHeader, httpContent); |
Fo170 | 6:2ce163810c2f | 340 | */ |
Fo170 | 6:2ce163810c2f | 341 | http_send(client, httpHeader, page(sw)); |
hudakz | 0:68a0003c4cb8 | 342 | continue; |
hudakz | 0:68a0003c4cb8 | 343 | } |
hudakz | 0:68a0003c4cb8 | 344 | |
hudakz | 0:68a0003c4cb8 | 345 | int cmd = analyse_get_url(received); |
hudakz | 0:68a0003c4cb8 | 346 | |
Fo170 | 7:acad90a34466 | 347 | if(cmd == -2) |
Fo170 | 7:acad90a34466 | 348 | { |
hudakz | 0:68a0003c4cb8 | 349 | // redirect to the right base url |
hudakz | 0:68a0003c4cb8 | 350 | httpHeader = MOVED_PERM; |
hudakz | 0:68a0003c4cb8 | 351 | http_send(client, httpHeader, moved_perm(1)); |
hudakz | 0:68a0003c4cb8 | 352 | continue; |
hudakz | 0:68a0003c4cb8 | 353 | } |
hudakz | 0:68a0003c4cb8 | 354 | |
Fo170 | 7:acad90a34466 | 355 | if(cmd == -1) |
Fo170 | 7:acad90a34466 | 356 | { |
hudakz | 0:68a0003c4cb8 | 357 | httpHeader = UNAUTHORIZED; |
Fo170 | 9:2f7ad2b13ec8 | 358 | httpContent = "<h1>401 Unauthorized "; |
Fo170 | 9:2f7ad2b13ec8 | 359 | httpContent += __image_401_Unauthorized__; |
Fo170 | 9:2f7ad2b13ec8 | 360 | httpContent += "</h1>\r\n"; |
hudakz | 0:68a0003c4cb8 | 361 | http_send(client, httpHeader, httpContent); |
hudakz | 0:68a0003c4cb8 | 362 | continue; |
hudakz | 0:68a0003c4cb8 | 363 | } |
hudakz | 0:68a0003c4cb8 | 364 | |
Fo170 | 6:2ce163810c2f | 365 | if(cmd == 1) |
Fo170 | 6:2ce163810c2f | 366 | { |
Fo170 | 6:2ce163810c2f | 367 | sw = 1; // switch on |
hudakz | 0:68a0003c4cb8 | 368 | } |
hudakz | 0:68a0003c4cb8 | 369 | |
Fo170 | 6:2ce163810c2f | 370 | if(cmd == 0) |
Fo170 | 6:2ce163810c2f | 371 | { |
Fo170 | 6:2ce163810c2f | 372 | sw = 0; // switch off |
hudakz | 0:68a0003c4cb8 | 373 | } |
hudakz | 0:68a0003c4cb8 | 374 | |
hudakz | 0:68a0003c4cb8 | 375 | httpHeader = HTTP_OK; |
Fo170 | 6:2ce163810c2f | 376 | http_send(client, httpHeader, page_toggle_switch(sw)); |
hudakz | 0:68a0003c4cb8 | 377 | } |
hudakz | 0:68a0003c4cb8 | 378 | } |
hudakz | 0:68a0003c4cb8 | 379 | } |
hudakz | 0:68a0003c4cb8 | 380 | } |
hudakz | 0:68a0003c4cb8 | 381 | |
hudakz | 3:0133517ba02d | 382 |