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