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:
Sun Jul 26 22:14:02 2015 +0000
Revision:
7:acad90a34466
Parent:
6:2ce163810c2f
Child:
8:305b64d7dd23
Modification du code d'affichage des pages Web, ajout d'image base64, lien d'images externe pour test, mesures analogique (avec Ticker), affichage d'un graphique www.flotcharts.org (pour le moment statique...)

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