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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* In this example LED1 is switched on/off using a web browser connected to this HTTP server.
00002  * The example is based on the Tuxgraphics Web Switch <http://www.tuxgraphics.org/>.
00003  * This HTTP server is built around the the ENC28J60 chip 
00004  * driven by the UIPEthernet library <https://github.com/ntruchsess/arduino_uip>
00005  * ported to mbed.
00006  */
00007 
00008 #include <mbed.h>
00009 #include <UIPEthernet.h>
00010 #include <UIPServer.h>
00011 #include <UIPClient.h>
00012 #include <string>
00013 
00014  // Carte ENC28J60 : http://www.mikroe.com/add-on-boards/communication/serial-ethernet-proto/
00015  
00016 // PC_5 (connectique Morpho) : Entrée Analogique (0 à 3.3V)
00017 
00018 // Carte ENC28J60 <--> Nucleo F411RE
00019 // PB_5 (connectique Morpho) : MOSI
00020 // PB_4 (connectique Morpho) : MISO
00021 // PB_3 (connectique Morpho) : SCK
00022 // PB_6 (connectique Morpho) : CS
00023 
00024 DigitalOut  myled(LED1);
00025 AnalogIn    a_in(PC_5); // PC_5 au lieu de PC_0
00026 DigitalIn   button_usr(USER_BUTTON);
00027 
00028 using namespace std;
00029 
00030 // UIPEthernet is the name of a global instance of UIPEthernetClass.
00031 // Do not change the name! It is used within the UIPEthernet library.
00032 #if defined(TARGET_LPC1768)
00033 UIPEthernetClass UIPEthernet(p11, p12, p13, p8);        // mosi, miso, sck, cs
00034 #elif defined(TARGET_LPC1114)
00035 UIPEthernetClass UIPEthernet(dp2, dp1, dp6, dp25);      // mosi, miso, sck, cs
00036 #elif defined(TARGET_LPC11U68)
00037 UIPEthernetClass UIPEthernet(P0_9, P0_8, P1_29, P0_2);  // mosi, miso, sck, cs
00038 #elif defined (TARGET_NUCLEO_F103RB)
00039 UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6);   // mosi, miso, sck, cs
00040 #elif defined (TARGET_NUCLEO_F401RE)
00041 UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6);   // mosi, miso, sck, cs
00042 #elif defined (TARGET_NUCLEO_F411RE)
00043 UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6);   // mosi, miso, sck, cs
00044 
00045 // If your board/plaform is not present yet then uncomment the following two lines and replace TARGET_YOUR_BOARD as appropriate.
00046 
00047 //#elif defined (TARGET_YOUR_BOARD)
00048 //UIPEthernetClass UIPEthernet(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS);   // mosi, miso, sck, cs
00049 
00050 #endif
00051 
00052 // Note:
00053 // If it happends that any of the SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS pins collide with LED1 pin
00054 // then either use different SPI port (if available on the board) and change the pin names in the constructor UIPEthernet(...) accordingly
00055 // 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.
00056 // In the second case remember to replace LED1 in sw(LED1) constructor (see below).
00057 
00058 // MAC number must be unique within the connected network. Modify as appropriate.
00059 const uint8_t    MY_MAC[6] = {0x00,0x01,0x02,0x03,0x04,0x06};
00060 
00061 // IP address must be also unique and compatible with your network. Change as appropriate.
00062 const IPAddress  MY_IP(10,0,0,170);
00063 #define  __IP_LOCAL__                     "10.0.0.170"
00064 
00065 /* Maison
00066 const IPAddress  MY_IP(192,168,0,170);
00067 #define  __IP_LOCAL__                     "192.168.0.170"
00068 */
00069 const string PASSWORD     = "secret";   // change as you like
00070 
00071 // In this example we are turning on/off LED1.
00072 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!
00073 
00074 #include <hebergement.h>
00075 #include <Fct_Web.h>
00076 
00077 #define NB_SAMPLES    10
00078 unsigned long int Sample = 0;
00079 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 };
00080 float time_samples[NB_SAMPLES];// = { -0.1,2,3,4,5,6,7,8,9,10,11,12,13,14 };
00081 float x_min = 0.0, x_max = 0.0;
00082 float y_min = 0.0, y_max = 0.0;
00083 float Seconds = 0.0;
00084 float meas = 0.0;
00085 
00086 //------------
00087 #define      __Time_tic_in_Second__      0.1
00088 
00089 Ticker time_tic;
00090 
00091 void add_one_tic()
00092 {       
00093  int i;
00094  
00095  Seconds = Seconds + (float)__Time_tic_in_Second__;
00096  
00097  // mesures ADC
00098     meas = a_in.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
00099     meas = meas * 3300.0; // Change the value to be in the 0 to 3300 range
00100     
00101     x_min = x_max = Seconds;
00102     y_min = y_max = meas;
00103                     
00104     for(i = 1 ; i < NB_SAMPLES ; i++)
00105     {
00106      time_samples[i-1] = time_samples[i];
00107      adc_samples[i-1] = adc_samples[i];
00108      if( time_samples[i] < x_min ) x_min = time_samples[i];
00109      if( time_samples[i] > x_max ) x_max = time_samples[i];
00110      if( adc_samples[i] < y_min ) y_min = adc_samples[i];
00111      if( adc_samples[i] > y_max ) y_max = adc_samples[i];
00112     }
00113 
00114     adc_samples[NB_SAMPLES-1] = meas;
00115     time_samples[NB_SAMPLES-1] = Seconds;
00116 }
00117 //------------
00118 
00119 
00120 string& page(uint8_t status)
00121 {
00122     char buffer[128];
00123     int i;
00124     //char time_stamp[32];
00125 
00126     //-------------
00127     httpContent = str_DOCTYPE;
00128     httpContent += str_meta_refresh;
00129     httpContent += "\r\n<HTML><HEAD>\r\n";
00130     httpContent += "<title>WEB Server Nucleo F411RE - ENC28J60 - Flot Examples: Interactivity</title>\r\n";
00131     httpContent += str_favicon;
00132     httpContent += str_JavaScript;
00133     httpContent += "<script language=\"javascript\" type=\"text/javascript\">init_WebServerNucleo_Interactivity();</script>\r\n";
00134     // Variables JavaScript
00135     httpContent += "<script language=\"javascript\" type=\"text/javascript\">\r\n";
00136     httpContent += "var color_Y = \"#FF0000\";\r\n";
00137     httpContent += "var label_Y = \"Adc(x)\";\r\n";
00138     //httpContent += "var x_min = -0.5, x_max =  14.5, y_min = -0.5, y_max =  1.5;\r\n";
00139     sprintf(buffer, "var x_min = %.1f, x_max = %.1f, y_min = %.1f, y_max = %.1f;\r\n", x_min, x_max, y_min, y_max);
00140     httpContent += buffer;
00141     //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";
00142     if(Sample > NB_SAMPLES)
00143     {
00144      httpContent += "var array_value = [";
00145      for(i = 0 ; i < NB_SAMPLES ; i++)
00146      {
00147       sprintf(buffer, "[%.1f,%.1f],", time_samples[i], adc_samples[i]);
00148       httpContent += buffer;
00149      }
00150      httpContent += "];\r\n";
00151     }
00152     Sample++;
00153     httpContent += "</script>\r\n";
00154     // Fin Variable JavaScript
00155     httpContent += "</HEAD><BODY>\r\n";
00156     httpContent += "<center><h2>WEB Server Nucleo F411RE</h2>\r\n";
00157     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";
00158      
00159     httpContent += str_Compilation_DATE_AND_TIME;       
00160     // httpContent += str_Logo_image;
00161     
00162     httpContent += "<p>\r\n<hr><p>\r\n";
00163     
00164     if(status == 1)
00165     {
00166         httpContent += "<font color=#00FF00>Switch ON</font> ";
00167         httpContent += str_ampoule_ON;
00168         httpContent += "\r\n";
00169     } 
00170     else 
00171     {
00172         httpContent += "<font color=#FF0000>Switch OFF</font> ";
00173         httpContent += str_ampoule_OFF;
00174         httpContent += "\r\n";
00175     }
00176 
00177     httpContent += "<hr>\r\n";
00178     //-------------
00179     httpContent += "USER BUTTON (Contact repos) \r\n";
00180     if(button_usr)
00181     {
00182         httpContent += "<font color=#00FF00>ON</font> ";
00183         httpContent += str_ampoule_ON;
00184         httpContent += "\r\n";
00185     } 
00186     else 
00187     {
00188         httpContent += "<font color=#FF0000>OFF</font> ";
00189         httpContent += str_ampoule_OFF;
00190         httpContent += "\r\n";
00191     }
00192     
00193     httpContent += "<hr>\r\n";
00194     //-------------
00195     /*
00196     httpContent += "Local Time: ";
00197     time_t seconds = time(NULL)+ 19800; // time(null) gives the GMT time .
00198     // printf("Time as seconds since January 1, 1970 = %d\n", seconds);
00199     strftime(time_stamp, 32, "%y %m %d, %H:%M:%Ss", localtime(&seconds)); 
00200     // this converts the value in seconds obtained above to human readable format and assigns it to the timestamp   
00201     sprintf(buffer, "%s", time_stamp);// diplays the human readable time
00202     */
00203     sprintf(buffer, "%.1f Seconds ", Seconds);// diplays the human readable Seconds
00204     httpContent += buffer;
00205     httpContent += str_chrono;
00206     httpContent += "\r\n<hr>\r\n";
00207     //----------------
00208     httpContent += "AnalogIn(PC_5) : ";
00209     sprintf(buffer, "%.0f mV ", meas);
00210     httpContent += buffer;
00211     httpContent += str_thermometre;
00212     httpContent += "<hr>\r\n<p>Usage Password Page : ";
00213     httpContent += str_password_folder_x64;
00214     httpContent += "<p>http://host_or_ip/password<p><hr>\r\n";
00215     httpContent += "<script language=\"javascript\" type=\"text/javascript\">WebServerNucleo_Interactivity();</script>\r\n";
00216     httpContent += "</center></BODY></HTML>";
00217     //-----------
00218     //wait(1);
00219     return httpContent;
00220 }
00221 
00222 int main()
00223 {
00224  // RTC
00225  //set_time(1387188323); // Set RTC time to 16 December 2013 10:05:23 UTC
00226  // Date and time are set.
00227  
00228  // initialisation des variables
00229  int i;
00230  
00231  for(i = 0 ; i < NB_SAMPLES ; i++)
00232  {
00233       time_samples[i] = 0;
00234       adc_samples[i] = 0.0;
00235  }
00236  // Init the ticker with the address of the function (add_one_second) to be attached and the interval (1000 ms)
00237  time_tic.attach(&add_one_tic, __Time_tic_in_Second__);
00238  
00239  // interval: 200 micro seconds chaques samples
00240  //time_measurement_ADC.attach_us(&measurement_ADC, TIME_SAMPLES_us);
00241  
00242  HTTP_LOOP();
00243 }
00244 
00245