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/

Revision:
13:939e0fa0cd39
Parent:
12:61b10a733ede
Child:
15:4b3975520691
--- a/main.cpp	Wed Jul 29 18:02:03 2015 +0000
+++ b/main.cpp	Wed Aug 05 19:25:47 2015 +0000
@@ -60,31 +60,59 @@
 const IPAddress  MY_IP(192,168,0,170);
 const string __IP_LOCAL__                             =  "192.168.0.170";
 const string __hebergement__                          =  "http://olivier.fournet.free.fr/electronique/e/WebServerNucleo/js/";
-const string __Temp_between_measurements__            =  "1";
-#define      __Temp_between_measurements_in_Second__     1
+const string __Time_between_page_refresh__            =  "1";
+
+const string __image_Password_Folder__  = "<img alt='' src='http://olivier.fournet.free.fr/jpg/password_folder.jpg'>";
+
+//const string __image_301_Moved_Permanently__  = "<img alt='' src='http://olivier.fournet.free.fr/jpg/301_moved_permanently.jpg'>";
+#define __image_301_Moved_Permanently__           "<img alt='' src='http://olivier.fournet.free.fr/jpg/301_moved_permanently.jpg'>"
+const string str_moved_perm   = "<h1>301 Moved Permanently " __image_301_Moved_Permanently__ "</h1>\r\n";
 
-// Logo Test d'image en base64 :
-// http://webcodertools.com/imagetobase64converter/Create
-const string __Logo_image__ =  "<img alt='' src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAOwwAADsMBx2+oZAAAABp0RVh0U29mdHdhcmUAUGFpbnQuTkVUIHYzLjUuMTAw9HKhAAAAUklEQVQ4T2NggIEGMIBzCTDgSh0cHAjrIcFgiGFoGoCWELYQrgLIgGsg4CtkaTibWNfu378fq2tpHx6EbcAMQ8J6iPU3cpIhbCqaisFpCTwVAwB5lit+0ltbrgAAAABJRU5ErkJggg=='>";
+//const string __image_401_Unauthorized__       = "<img alt='' src='http://olivier.fournet.free.fr/png/401_Unauthorized.png'>";
+#define __image_401_Unauthorized__                "<img alt='' src='http://olivier.fournet.free.fr/png/401_Unauthorized.png'>"
+const string str_Unauthorized = "<h1>401 Unauthorized " __image_401_Unauthorized__ "</h1>\r\n";
+
+#define FREQUENCE_SECTEUR             50
+#define NB_SAMPLES_PAR_OSCILLATION    100
+#define NB_SAMPLES                    5000   // FREQUENCE_SECTEUR * NB_SAMPLES_PAR_OSCILLATION
+#define TIME_SAMPLES_us               200    // 1000000 / NB_SAMPLES
+unsigned long int Samples = 0;
+
+float Seconds = 0.0;
+float time_between_two_measurement_ADC = 0.0;
+float meas, meas_sum, meas_moy, meas_min, meas_max, v_min, v_max;
 
-const string __image_Password_Folder__        = "<img alt='' src='http://olivier.fournet.free.fr/jpg/password_folder.jpg'>";
-const string __image_301_Moved_Permanently__  = "<img alt='' src='http://olivier.fournet.free.fr/jpg/301_moved_permanently.jpg'>";
-const string __image_401_Unauthorized__       = "<img alt='' src='http://olivier.fournet.free.fr/png/401_Unauthorized.png'>";
+Ticker time_measurement_ADC;
+ 
+void measurement_ADC()
+{
 
-#define NB_SAMPLES    10
-unsigned long int Sample = 0;
-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 };
-float time_samples[NB_SAMPLES];// = { -0.1,2,3,4,5,6,7,8,9,10,11,12,13,14 };
-float x_min = 0.0, x_max = 0.0;
-float y_min = 0.0, y_max = 0.0;
-float Seconds = 0.0;
-float k = 0.0;
+    meas = a_in.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
+    //wait_us(10);
+    meas_sum += meas;
+    if(meas_min > meas) meas_min = meas;
+    if(meas_max < meas) meas_max = meas;
+    Samples++;
+    
+    if( Samples == NB_SAMPLES )
+    {
+     meas_moy = meas_sum * 3300.0; // Change the value to be in the 0 to 3300 range
+     meas_moy /= (float)NB_SAMPLES;
+     Samples = 0;
+     meas_sum = 0.0;
+     v_min = 3300.0 * meas_min, v_max = 3300.0 * meas_max;
+     meas_min = 1.0 , meas_max = 0.0;
+    }
+}
+    
 //------------
+#define      __Time_between_refresh_in_Second__      0.1
+
 Ticker second_ticker;
 
 void add_one_second()
 {       
- Seconds = Seconds + k;//0.1;
+ Seconds = Seconds + (float)__Time_between_refresh_in_Second__;
 }
 //------------
 const uint16_t   MY_PORT = 80;      // for HTTP connection
@@ -141,10 +169,13 @@
         httpContent = "/" +  PASSWORD + "/";
     else
         httpContent = "";
-
+ /*
     httpContent += "<h1>301 Moved Permanently ";
     httpContent += __image_301_Moved_Permanently__;
     httpContent += "</h1>\r\n";
+    */
+    httpContent += str_moved_perm;
+    
     return (httpContent);
 }
 
@@ -152,80 +183,26 @@
 {
     char buffer[128];
     //char time_stamp[32];
-    float meas;
-    meas = a_in.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
-    meas = meas * 3300.0; // Change the value to be in the 0 to 3300 range
-    //Seconds = Seconds + 1;
-    x_min = x_max = Seconds;
-    y_min = y_max = meas;
-    int i;
-                    
-    for(i = 1 ; i < NB_SAMPLES ; i++)
-    {
-     time_samples[i-1] = time_samples[i];
-     adc_samples[i-1] = adc_samples[i];
-     if( time_samples[i] < x_min ) x_min = time_samples[i];
-     if( time_samples[i] > x_max ) x_max = time_samples[i];
-     if( adc_samples[i] < y_min ) y_min = adc_samples[i];
-     if( adc_samples[i] > y_max ) y_max = adc_samples[i];
-    }
-
-    adc_samples[NB_SAMPLES-1] = meas;
-    time_samples[NB_SAMPLES-1] = Seconds;
+   
     //-------------
     httpContent = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\r\n";
     httpContent += "<meta http-equiv=\"refresh\" content=\"";
-    httpContent += __Temp_between_measurements__;
+    httpContent += __Time_between_page_refresh__;
     httpContent += ";url=http://";
     httpContent += __IP_LOCAL__;
     httpContent += "/\">\r\n";
     httpContent += "<HTML><HEAD>\r\n";
-    httpContent += "<title>WEB Server Nucleo F411RE - ENC28J60 - Flot Examples: Interactivity</title>\r\n";
-    httpContent += "<script language=\"javascript\" type=\"text/javascript\" src=\"";
-    httpContent += __hebergement__;
-    httpContent += "WebServerNucleo_Interactivity_init.js\"></script>\r\n";
-    httpContent += "<script language=\"javascript\" type=\"text/javascript\">init_WebServerNucleo_Interactivity();</script>\r\n";
- // Variables JavaScript
-    httpContent += "<script language=\"javascript\" type=\"text/javascript\">\r\n";
-    httpContent += "var color_Y = \"#FF0000\";\r\n";
-    httpContent += "var label_Y = \"Adc(x)\";\r\n";
-    //httpContent += "var x_min = -0.5, x_max =  14.5, y_min = -0.5, y_max =  1.5;\r\n";
-    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);
-    httpContent += buffer;
-    //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";
-    if(Sample > NB_SAMPLES)
-    {
-     httpContent += "var array_value = [";
-     for(i = 0 ; i < NB_SAMPLES ; i++)
-     {
-      sprintf(buffer, "[%f,%f],", time_samples[i], adc_samples[i]);
-      httpContent += buffer;
-     }
-     httpContent += "];\r\n";
-    }
-    Sample++;
-    httpContent += "</script>\r\n";
- // Fin Variable JavaScript
+    httpContent += "<title>WEB Server Nucleo F411RE & ENC28J60 - ADC</title>\r\n";
+
+    
+
     httpContent += "</HEAD><BODY>\r\n";
     httpContent += "<center><h2>WEB Server Nucleo F411RE</h2>\r\n";
-    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";
+    httpContent += "<p>Designed for STM32F411RE & ENC28J60 (ADC)\r\n";
     httpContent += "<p>Compilation avec mBED &agrave; " __TIME__ " le " __DATE__" \r\n";   
-           
-    // httpContent += __Logo_image__ ;
     
     httpContent += "<p></center>\r\n<hr><p>\r\n";
     
-    if(status == 1)
-    {
-        httpContent += "<font color=#00FF00>Switch ON</font>\r\n";
-    } 
-    else 
-    {
-        httpContent += "<font color=#FF0000>Switch OFF</font>\r\n";
-    }
-
-    httpContent += "<hr>\r\n";
-    //-------------
     /*
     httpContent += "Local Time: ";
     time_t seconds = time(NULL)+ 19800; // time(null) gives the GMT time .
@@ -238,48 +215,19 @@
     httpContent += buffer;
     httpContent += "Seconds\r\n<hr>\r\n";
     //----------------
-    httpContent += "AnalogIn(PC_5) : ";
-    sprintf(buffer, "%.0f mV\r\n", meas);
-    httpContent += buffer;
-    httpContent += "<hr>\r\n<p>Usage Password Page :<p>http://host_or_ip/password<p><hr>\r\n";
-    httpContent += "<script language=\"javascript\" type=\"text/javascript\">WebServerNucleo_Interactivity();</script>\r\n";
+        
+     httpContent += "AnalogIn(PC_5) : (DC de 0 a 3,3V)<p>\r\n";
+     sprintf(buffer, "%.0f mV Moy ( sum : %.0f - Samples : %u )\r\n", meas_moy, meas_sum, Samples);
+     httpContent += buffer;
+     httpContent += "<p>\r\n";
+     sprintf(buffer, "%.0f mV Min, %.0f mV Max , diff : %.0f mV\r\n", v_min, v_max, v_max - v_min);
+     httpContent += buffer;
     httpContent += "</BODY></HTML>";
     //-----------
     //wait(1);
     return httpContent;
 }
 
-string& page_toggle_switch(uint8_t status)
-{
-    //-------------
-    httpContent = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\r\n";
-    httpContent += "<HTML><HEAD>\r\n";
-    httpContent += "<title>WEB Server Nucleo F411RE - ENC28J60 - Password Page</title>\r\n";
-    httpContent += "</HEAD><BODY>\r\n";
-    httpContent += "<center><h2>WEB Server Nucleo F411RE - ENC28J60 - Password Page</h2></center>\r\n<p>";
-    httpContent += __image_Password_Folder__;
-    httpContent += "<p>\r\n";
-    
-    if(status == 1)
-    {
-        httpContent += "<hr><pre>\r\n  <font color=#00FF00>ON</font>";
-        httpContent += " <a href=\"./?sw=0\">[switch off]</a>\r\n";
-    }
-    else
-    {
-        httpContent += "<hr><pre>\r\n  <font color=#FF0000>OFF</font>";
-        httpContent += " <a href=\"./?sw=1\">[switch on]</a>\r\n";
-    }
-
-    httpContent += "  <a href=\".\">[refresh status]</a>\r\n";
-    httpContent += "</pre>\r\n<hr>\r\n";
-    
-    httpContent += "</BODY></HTML>";
-    //-----------
-    wait(1);
-    return httpContent;
-}
-
 void http_send(EthernetClient& client, string& header, string& content)
 {
     char content_length[5] = {};
@@ -297,14 +245,25 @@
 
 int main()
 {
+ meas_moy = 0.0;
+ Samples = 0;
+ meas_sum = 0.0;
+ meas_min = 1.0 , meas_max = 0.0;
  // RTC
  //set_time(1387188323); // Set RTC time to 16 December 2013 10:05:23 UTC
  
  // Date and time are set.
  
- // Init the ticker with the address of the function (add_one_second) to be attached and the interval (100 ms)
- second_ticker.attach(&add_one_second, 0.1);
- k = (float)__Temp_between_measurements_in_Second__ / (float)NB_SAMPLES;
+ // Init the ticker with the address of the function (add_one_second) to be attached and the interval (1000 ms)
+ second_ticker.attach(&add_one_second, __Time_between_refresh_in_Second__);
+ 
+ //-----------------
+ // 50 Hz --> 20ms
+ // 100 samples par oscillations = 20 / 100 = 0.2ms = 200µs
+ // 100 samples * 50 oscillations = 5000 samples au total par secondes
+ // interval: 200 micro seconds chaques samples
+ time_measurement_ADC.attach_us(&measurement_ADC, TIME_SAMPLES_us);
+ 
  //-----------------      
     UIPEthernet.begin(MY_MAC,MY_IP);
     myServer.begin();
@@ -355,25 +314,15 @@
                 if(cmd == -1)
                 {
                     httpHeader = UNAUTHORIZED;
+                    /*
                     httpContent = "<h1>401 Unauthorized ";
                     httpContent += __image_401_Unauthorized__;
                     httpContent += "</h1>\r\n";
+                    */
+                    httpContent = str_Unauthorized;
                     http_send(client, httpHeader, httpContent);
                     continue;
                 }
-
-                if(cmd == 1)
-                { 
-                 sw = 1;     // switch on
-                }
-
-                if(cmd == 0)
-                {
-                 sw = 0;    // switch off
-                }
-
-                httpHeader = HTTP_OK;
-                http_send(client, httpHeader, page_toggle_switch(sw));
             }
         }
     }