test ADC avec page Web reduite

Dependencies:   UIPEthernet mbed FCT_WEB hebergement

Fork of Nucleo_Web_ENC28J60 by FOURNET Olivier

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/

Committer:
Fo170
Date:
Mon Jul 27 21:20:49 2015 +0000
Revision:
8:305b64d7dd23
Parent:
7:acad90a34466
Child:
9:2f7ad2b13ec8
remplacement des #define par des const string, mise en commentaires des affichage de mesure de temps (sauf la fonction Ticker), changement des chemin web pour les sources js. mise en test de longue dur?e pour v?rifier si les plantages ont disparu...

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 8:305b64d7dd23 144 httpContent += __image_301_Moved_Permanently__ + "</h1>\r\n";
hudakz 0:68a0003c4cb8 145 return (httpContent);
hudakz 0:68a0003c4cb8 146 }
hudakz 0:68a0003c4cb8 147
hudakz 0:68a0003c4cb8 148 string& page(uint8_t status)
hudakz 0:68a0003c4cb8 149 {
Fo170 5:a46a2512e17e 150 char buffer[32];
Fo170 8:305b64d7dd23 151 //char time_stamp[32];
Fo170 7:acad90a34466 152 float meas;
Fo170 7:acad90a34466 153 meas = a_in.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
Fo170 8:305b64d7dd23 154 meas = meas * 3300.0; // Change the value to be in the 0 to 3300 range
Fo170 8:305b64d7dd23 155 //Seconds = Seconds + 1;
Fo170 5:a46a2512e17e 156 //-------------
Fo170 6:2ce163810c2f 157 httpContent = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\r\n";
Fo170 8:305b64d7dd23 158 httpContent += "<meta http-equiv=\"refresh\" content=\"" + __Temp_between_measurements__ + ";url=http://" + __IP_LOCAL__ + "/\">\r\n";
Fo170 6:2ce163810c2f 159 httpContent += "<HTML><HEAD>\r\n";
Fo170 6:2ce163810c2f 160 httpContent += "<title>WEB Server Nucleo F411RE - ENC28J60 - Flot Examples: Interactivity</title>\r\n";
Fo170 8:305b64d7dd23 161 httpContent += "<script language=\"javascript\" type=\"text/javascript\" src=\"" + __hebergement__ + "WebServerNucleo_Interactivity_init.js\"></script>\r\n";
Fo170 8:305b64d7dd23 162 httpContent += "<script language=\"javascript\" type=\"text/javascript\">init_WebServerNucleo_Interactivity();</script>\r\n";
Fo170 6:2ce163810c2f 163 // Variables JavaScript
Fo170 6:2ce163810c2f 164 httpContent += "<script language=\"javascript\" type=\"text/javascript\">\r\n";
Fo170 8:305b64d7dd23 165 httpContent += "var color_Y = \"#FF0000\";\r\n";
Fo170 8:305b64d7dd23 166 httpContent += "var label_Y = \"Adc(x)\";\r\n";
Fo170 6:2ce163810c2f 167 httpContent += "var x_min = -0.5, x_max = 14.5, y_min = -0.5, y_max = 1.5;\r\n";
Fo170 8:305b64d7dd23 168 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 169 httpContent += "</script>\r\n";
Fo170 6:2ce163810c2f 170 // Fin Variable JavaScript
Fo170 6:2ce163810c2f 171 httpContent += "</HEAD><BODY>\r\n";
Fo170 6:2ce163810c2f 172 httpContent += "<center><h2>WEB Server Nucleo F411RE</h2>\r\n";
Fo170 7:acad90a34466 173 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 8:305b64d7dd23 174 //httpContent += "<p>Compilation avec mBED &agrave; " __TIME__ " le " __DATE__" \r\n";
Fo170 6:2ce163810c2f 175
Fo170 8:305b64d7dd23 176 // httpContent += __Logo_image__ ;
Fo170 8:305b64d7dd23 177
Fo170 8:305b64d7dd23 178 httpContent += "<p></center>\r\n<hr><p>\r\n";
Fo170 5:a46a2512e17e 179
Fo170 6:2ce163810c2f 180 if(status == 1)
Fo170 6:2ce163810c2f 181 {
Fo170 6:2ce163810c2f 182 httpContent += "<font color=#00FF00>Switch ON</font>\r\n";
Fo170 6:2ce163810c2f 183 }
Fo170 6:2ce163810c2f 184 else
Fo170 6:2ce163810c2f 185 {
Fo170 6:2ce163810c2f 186 httpContent += "<font color=#FF0000>Switch OFF</font>\r\n";
Fo170 6:2ce163810c2f 187 }
hudakz 0:68a0003c4cb8 188
Fo170 6:2ce163810c2f 189 httpContent += "<hr>\r\n";
Fo170 6:2ce163810c2f 190 //-------------
Fo170 7:acad90a34466 191 /*
Fo170 7:acad90a34466 192 httpContent += "Local Time: ";
Fo170 6:2ce163810c2f 193 time_t seconds = time(NULL)+ 19800; // time(null) gives the GMT time .
Fo170 6:2ce163810c2f 194 // printf("Time as seconds since January 1, 1970 = %d\n", seconds);
Fo170 7:acad90a34466 195 strftime(time_stamp, 32, "%y %m %d, %H:%M:%Ss", localtime(&seconds));
Fo170 6:2ce163810c2f 196 // this converts the value in seconds obtained above to human readable format and assigns it to the timestamp
Fo170 6:2ce163810c2f 197 sprintf(buffer, "%s", time_stamp);// diplays the human readable time
Fo170 7:acad90a34466 198 */
Fo170 8:305b64d7dd23 199 sprintf(buffer, "%.1f ", Seconds);// diplays the human readable Seconds
Fo170 6:2ce163810c2f 200 httpContent += buffer;
Fo170 7:acad90a34466 201 httpContent += "Seconds\r\n<hr>\r\n";
Fo170 6:2ce163810c2f 202 //----------------
Fo170 6:2ce163810c2f 203 httpContent += "AnalogIn(PC_0) : ";
Fo170 7:acad90a34466 204 sprintf(buffer, "%.0f mV\r\n", meas);
Fo170 6:2ce163810c2f 205 httpContent += buffer;
Fo170 6:2ce163810c2f 206 httpContent += "<hr>\r\n<p>Usage Password Page :<p>http://host_or_ip/password<p><hr>\r\n";
Fo170 8:305b64d7dd23 207 httpContent += "<script language=\"javascript\" type=\"text/javascript\">WebServerNucleo_Interactivity();</script>\r\n";
Fo170 6:2ce163810c2f 208 httpContent += "</BODY></HTML>";
Fo170 6:2ce163810c2f 209 //-----------
Fo170 7:acad90a34466 210 //wait(1);
Fo170 6:2ce163810c2f 211 return httpContent;
Fo170 6:2ce163810c2f 212 }
Fo170 6:2ce163810c2f 213
Fo170 6:2ce163810c2f 214 string& page_toggle_switch(uint8_t status)
Fo170 6:2ce163810c2f 215 {
Fo170 6:2ce163810c2f 216 //-------------
Fo170 6:2ce163810c2f 217 httpContent = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\r\n";
Fo170 6:2ce163810c2f 218 httpContent += "<HTML><HEAD>\r\n";
Fo170 6:2ce163810c2f 219 httpContent += "<title>WEB Server Nucleo F411RE - ENC28J60 - Password Page</title>\r\n";
Fo170 6:2ce163810c2f 220 httpContent += "</HEAD><BODY>\r\n";
Fo170 6:2ce163810c2f 221 httpContent += "<center><h2>WEB Server Nucleo F411RE - ENC28J60 - Password Page</h2></center>\r\n";
Fo170 8:305b64d7dd23 222 httpContent += "<p>" + __image_Password_Folder__ + "<p>\r\n";
Fo170 8:305b64d7dd23 223
Fo170 6:2ce163810c2f 224 if(status == 1)
Fo170 6:2ce163810c2f 225 {
Fo170 6:2ce163810c2f 226 httpContent += "<hr><pre>\r\n <font color=#00FF00>ON</font>";
hudakz 0:68a0003c4cb8 227 httpContent += " <a href=\"./?sw=0\">[switch off]</a>\r\n";
Fo170 6:2ce163810c2f 228 }
Fo170 6:2ce163810c2f 229 else
Fo170 6:2ce163810c2f 230 {
Fo170 6:2ce163810c2f 231 httpContent += "<hr><pre>\r\n <font color=#FF0000>OFF</font>";
hudakz 0:68a0003c4cb8 232 httpContent += " <a href=\"./?sw=1\">[switch on]</a>\r\n";
hudakz 0:68a0003c4cb8 233 }
hudakz 0:68a0003c4cb8 234
hudakz 0:68a0003c4cb8 235 httpContent += " <a href=\".\">[refresh status]</a>\r\n";
hudakz 0:68a0003c4cb8 236 httpContent += "</pre>\r\n";
hudakz 0:68a0003c4cb8 237 httpContent += "<hr>\r\n";
Fo170 6:2ce163810c2f 238
Fo170 6:2ce163810c2f 239 httpContent += "</BODY></HTML>";
Fo170 5:a46a2512e17e 240 //-----------
Fo170 5:a46a2512e17e 241 wait(1);
hudakz 0:68a0003c4cb8 242 return httpContent;
hudakz 0:68a0003c4cb8 243 }
hudakz 0:68a0003c4cb8 244
hudakz 0:68a0003c4cb8 245 void http_send(EthernetClient& client, string& header, string& content)
hudakz 0:68a0003c4cb8 246 {
hudakz 0:68a0003c4cb8 247 char content_length[5] = {};
hudakz 0:68a0003c4cb8 248
hudakz 2:76f339a1ba9b 249 header += "\r\nContent-Type: text/html\r\n";
hudakz 0:68a0003c4cb8 250 header += "Content-Length: ";
hudakz 0:68a0003c4cb8 251 sprintf(content_length, "%d", content.length());
hudakz 0:68a0003c4cb8 252 header += string(content_length) + "\r\n";
hudakz 0:68a0003c4cb8 253 header += "Pragma: no-cache\r\n";
hudakz 0:68a0003c4cb8 254 header += "Connection: About to close\r\n";
hudakz 0:68a0003c4cb8 255 header += "\r\n";
hudakz 0:68a0003c4cb8 256 string webpage = header + content;
hudakz 0:68a0003c4cb8 257 client.write((uint8_t*)webpage.c_str(),webpage.length());
hudakz 0:68a0003c4cb8 258 }
hudakz 0:68a0003c4cb8 259
hudakz 0:68a0003c4cb8 260 int main()
Fo170 7:acad90a34466 261 {
Fo170 5:a46a2512e17e 262 // RTC
Fo170 8:305b64d7dd23 263 //set_time(1387188323); // Set RTC time to 16 December 2013 10:05:23 UTC
Fo170 7:acad90a34466 264
Fo170 5:a46a2512e17e 265 // Date and time are set.
Fo170 7:acad90a34466 266
Fo170 7:acad90a34466 267 // Init the ticker with the address of the function (add_one_second) to be attached and the interval (100 ms)
Fo170 8:305b64d7dd23 268 second_ticker.attach(&add_one_second, 0.1);
Fo170 7:acad90a34466 269 //-----------------
hudakz 3:0133517ba02d 270 UIPEthernet.begin(MY_MAC,MY_IP);
hudakz 0:68a0003c4cb8 271 myServer.begin();
Fo170 7:acad90a34466 272 while(1)
Fo170 7:acad90a34466 273 {
hudakz 0:68a0003c4cb8 274 EthernetClient client = myServer.available();
Fo170 7:acad90a34466 275 if(client)
Fo170 7:acad90a34466 276 {
hudakz 0:68a0003c4cb8 277 size_t size = client.available();
Fo170 7:acad90a34466 278 if(size > 0)
Fo170 7:acad90a34466 279 {
hudakz 0:68a0003c4cb8 280 uint8_t* buf = (uint8_t*)malloc(size);
hudakz 0:68a0003c4cb8 281 size = client.read(buf, size);
hudakz 0:68a0003c4cb8 282 string received((char*)buf);
hudakz 0:68a0003c4cb8 283 free(buf);
Fo170 7:acad90a34466 284 if(received.substr(0, 3) != "GET")
Fo170 7:acad90a34466 285 {
hudakz 0:68a0003c4cb8 286 // head, post or other method
hudakz 0:68a0003c4cb8 287 // for possible status codes see:
hudakz 0:68a0003c4cb8 288 // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
hudakz 0:68a0003c4cb8 289 httpHeader = HTTP_OK;
hudakz 0:68a0003c4cb8 290 httpContent = "<h1>200 OK</h1>";
hudakz 0:68a0003c4cb8 291 http_send(client, httpHeader, httpContent);
hudakz 0:68a0003c4cb8 292 continue;
hudakz 0:68a0003c4cb8 293 }
hudakz 0:68a0003c4cb8 294
Fo170 7:acad90a34466 295 if(received.substr(0, 6) == "GET / ")
Fo170 7:acad90a34466 296 {
hudakz 0:68a0003c4cb8 297 httpHeader = HTTP_OK;
Fo170 6:2ce163810c2f 298 /*
hudakz 0:68a0003c4cb8 299 httpContent = "<p>Usage: http://host_or_ip/password</p>\r\n";
hudakz 0:68a0003c4cb8 300 http_send(client, httpHeader, httpContent);
Fo170 6:2ce163810c2f 301 */
Fo170 6:2ce163810c2f 302 http_send(client, httpHeader, page(sw));
hudakz 0:68a0003c4cb8 303 continue;
hudakz 0:68a0003c4cb8 304 }
hudakz 0:68a0003c4cb8 305
hudakz 0:68a0003c4cb8 306 int cmd = analyse_get_url(received);
hudakz 0:68a0003c4cb8 307
Fo170 7:acad90a34466 308 if(cmd == -2)
Fo170 7:acad90a34466 309 {
hudakz 0:68a0003c4cb8 310 // redirect to the right base url
hudakz 0:68a0003c4cb8 311 httpHeader = MOVED_PERM;
hudakz 0:68a0003c4cb8 312 http_send(client, httpHeader, moved_perm(1));
hudakz 0:68a0003c4cb8 313 continue;
hudakz 0:68a0003c4cb8 314 }
hudakz 0:68a0003c4cb8 315
Fo170 7:acad90a34466 316 if(cmd == -1)
Fo170 7:acad90a34466 317 {
hudakz 0:68a0003c4cb8 318 httpHeader = UNAUTHORIZED;
Fo170 8:305b64d7dd23 319 httpContent = "<h1>401 Unauthorized " + __image_401_Unauthorized__ + "</h1>\r\n";
hudakz 0:68a0003c4cb8 320 http_send(client, httpHeader, httpContent);
hudakz 0:68a0003c4cb8 321 continue;
hudakz 0:68a0003c4cb8 322 }
hudakz 0:68a0003c4cb8 323
Fo170 6:2ce163810c2f 324 if(cmd == 1)
Fo170 6:2ce163810c2f 325 {
Fo170 6:2ce163810c2f 326 sw = 1; // switch on
hudakz 0:68a0003c4cb8 327 }
hudakz 0:68a0003c4cb8 328
Fo170 6:2ce163810c2f 329 if(cmd == 0)
Fo170 6:2ce163810c2f 330 {
Fo170 6:2ce163810c2f 331 sw = 0; // switch off
hudakz 0:68a0003c4cb8 332 }
hudakz 0:68a0003c4cb8 333
hudakz 0:68a0003c4cb8 334 httpHeader = HTTP_OK;
Fo170 6:2ce163810c2f 335 http_send(client, httpHeader, page_toggle_switch(sw));
hudakz 0:68a0003c4cb8 336 }
hudakz 0:68a0003c4cb8 337 }
hudakz 0:68a0003c4cb8 338 }
hudakz 0:68a0003c4cb8 339 }
hudakz 0:68a0003c4cb8 340
hudakz 3:0133517ba02d 341