FOURNET Olivier / Mbed 2 deprecated WIZwiki-W7500_ADC_Sampling

Dependencies:   SDFileSystem STATIC_COLORS WIZnetInterface mbed

Fork of WIZwiki-W7500_ADC by FOURNET Olivier

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "SDFileSystem.h"
00004 #include <stdio.h>
00005 #include <string.h>
00006 
00007 #define USE_DHCP    1
00008 /*
00009 MAC Address Details ( http://www.macvendorlookup.com/ )
00010 Company Wiznet Address
00011 Seoul 135-830
00012 Nonyhun, Kangnam
00013 KOREA, REPUBLIC OF
00014 Range : 00:08:DC:00:00:00 - 00:08:DC:FF:FF:FF
00015 Type : MA-L: IEEE MAC Address Large (24-bit block size)
00016 */
00017 //#define MAC     "\x31\x41\x59\x26\x53\x58"
00018 #define MAC     "\x00\x08\xDC\x31\x41\x59"
00019 //#define MAC     "\x00\x08\xDC\x11\x34\x78"
00020 #define IP      "192.168.0.170"
00021 #define MASK    "255.255.255.0"
00022 #define GATEWAY "192.168.0.254"
00023 
00024 #define HTTPD_SERVER_PORT   80
00025 #define HTTPD_MAX_REQ_LENGTH   1023
00026 #define HTTPD_MAX_HDR_LENGTH   255
00027 #define HTTPD_MAX_FNAME_LENGTH   127
00028 #define HTTPD_MAX_DNAME_LENGTH   127
00029 
00030 #if defined(TARGET_WIZwiki_W7500)
00031 Serial uart(USBTX, USBRX);
00032 SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "sd"); // WIZwiki-W7500
00033 #include "static_colors.h"
00034 // LED RED   : server listning status
00035 // LED GREEN : socket connecting status Ok
00036 // LED BLUE  : socket connecting status Busy
00037 #endif
00038 
00039 EthernetInterface eth;
00040 TCPSocketServer server;
00041 TCPSocketConnection client;
00042 
00043 char buffer[HTTPD_MAX_REQ_LENGTH+1];
00044 char httpHeader[HTTPD_MAX_HDR_LENGTH+1];
00045 char fileName[HTTPD_MAX_FNAME_LENGTH+1];
00046 char dirName[HTTPD_MAX_DNAME_LENGTH+1];
00047 char *uristr;
00048 char *eou;
00049 char *qrystr;
00050 
00051 FILE *fp;
00052 int rdCnt;
00053 
00054 // Initialize a pins to perform analog input and digital output fucntions
00055 AnalogIn   ain0(A0);
00056 AnalogIn   ain1(A1);
00057 AnalogIn   ain2(A2);
00058 AnalogIn   ain3(A3);
00059 
00060 float a0_f, a1_f, a2_f, a3_f;
00061 
00062 #define __IP_LOCAL__           IP
00063 #define  __hebergement__                  "http://olivier.fournet.free.fr/"
00064 #define  __Time_between_page_refresh__    "1"
00065 //#include <hebergement.h>
00066 
00067 int refresh = 1; // 1 second refresh
00068 
00069 #define NB_SAMPLES_GPH    10
00070 unsigned long int Sample_gph = 0;
00071 float adc_samples[NB_SAMPLES_GPH];// = { 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 };
00072 float time_samples[NB_SAMPLES_GPH];// = { -0.1,2,3,4,5,6,7,8,9,10,11,12,13,14 };
00073 float x_min = 0.0, x_max = 0.0;
00074 float y_min = 0.0, y_max = 0.0;
00075 float Seconds = 0.0;
00076 //float meas = 0.0;
00077 
00078 //------------
00079 #define      __Time_tic_in_Second__      0.1
00080 
00081 Ticker time_tic;
00082 
00083 //------------
00084 
00085 #define FREQUENCE_SECTEUR             50
00086 #define NB_SAMPLES_PAR_OSCILLATION    100
00087 #define NB_SAMPLES                    FREQUENCE_SECTEUR * NB_SAMPLES_PAR_OSCILLATION // 5000   // FREQUENCE_SECTEUR * NB_SAMPLES_PAR_OSCILLATION
00088 #define TIME_SAMPLES_us               1000000 / NB_SAMPLES                           // 200    // 1000000 / NB_SAMPLES
00089 unsigned long int Samples = 0;
00090 
00091 float time_between_two_measurement_ADC = 0.0;
00092 float meas, meas_sum, meas_moy, meas_min, meas_max;
00093 float vdc, vdc_min, vdc_max;
00094 float vac, pow2_vac, sum_pow2_vac, veff;
00095 
00096 Ticker time_measurement_ADC;
00097 
00098 void measurement_ADC(void)
00099 {
00100  float meas_moy_t, veff_t; // valeurs temporaires
00101  
00102     meas = 3.3 * ain0.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
00103     //wait_us(10);
00104     meas_sum += meas;
00105     if(meas_min > meas) meas_min = meas;
00106     if(meas_max < meas) meas_max = meas;
00107 
00108     vac = meas - 1.650; // 0V AC = 3,3V/2
00109     pow2_vac = vac * vac; // valeur VAC au carré
00110     sum_pow2_vac += pow2_vac; // somme des valeurs AC au carré
00111 
00112     Samples++;
00113 
00114     if( Samples == NB_SAMPLES ) 
00115     {
00116         // VDC
00117         meas_moy_t = meas_sum;
00118         meas_moy = meas_moy_t / (float)NB_SAMPLES;
00119         // VAC
00120         veff_t = sum_pow2_vac;
00121         veff_t = veff_t / (float)NB_SAMPLES;
00122         veff = sqrt(veff_t);
00123 
00124         Samples = 0;
00125         meas_sum = 0.0;
00126         vdc_min = meas_min, vdc_max = meas_max;
00127         meas_min = 3.3 , meas_max = 0.0;
00128         sum_pow2_vac = 0.0;
00129     }
00130 }
00131 
00132 void init_ADC_sampling(void)
00133 {
00134  //-------------
00135  meas_moy = 0.0;
00136  Samples = 0;
00137  meas_sum = 0.0;
00138  meas_min = 3.3 , meas_max = 0.0;
00139  sum_pow2_vac = 0.0;
00140  //-----------------
00141  // 50 Hz --> 20ms
00142  // 100 samples par oscillations = 20 / 100 = 0.2ms = 200µs
00143  // 100 samples * 50 oscillations = 5000 samples au total par secondes
00144  // interval: 200 micro seconds chaques samples
00145  time_measurement_ADC.attach_us(&measurement_ADC, TIME_SAMPLES_us);   
00146 }
00147 //------------
00148 
00149 void add_one_tic()
00150 {       
00151  int i;
00152  
00153  Seconds = Seconds + (float)__Time_tic_in_Second__;
00154  
00155  // mesures ADC  --> measurement_ADC();
00156  /*
00157     meas = ain0.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
00158     meas = meas * 3.3; // Change the value to be in the 0 to 3300 range
00159   */  
00160     x_min = x_max = Seconds;
00161     y_min = y_max = meas_moy; //meas;
00162                     
00163     for(i = 1 ; i < NB_SAMPLES_GPH ; i++)
00164     {
00165      time_samples[i-1] = time_samples[i];
00166      adc_samples[i-1] = adc_samples[i];
00167      if( time_samples[i] < x_min ) x_min = time_samples[i];
00168      if( time_samples[i] > x_max ) x_max = time_samples[i];
00169      if( adc_samples[i] < y_min ) y_min = adc_samples[i];
00170      if( adc_samples[i] > y_max ) y_max = adc_samples[i];
00171     }
00172 
00173     adc_samples[NB_SAMPLES_GPH-1] = meas_moy; //meas;
00174     time_samples[NB_SAMPLES_GPH-1] = Seconds;
00175 }
00176 //------------
00177 
00178 Ticker ledTick;
00179 
00180 char *pch;
00181 char ext[5];
00182 char ext_gif[] = "gif";
00183 char ext_jpg[] = "jpg";
00184 char ext_png[] = "png";
00185 char ext_tiff[] = "tiff";
00186 int pos_ext;
00187 int extLen;
00188 
00189 void ledTickfunc()
00190 {
00191     led_r = !led_r;
00192 }
00193 
00194 void printf_send_client(const char *str_c)
00195 {
00196  char http_send[HTTPD_MAX_REQ_LENGTH+1];   
00197  sprintf(http_send,str_c);
00198  client.send(http_send,strlen(http_send));
00199 }
00200 
00201 #include "WIZwiki_W7500_Interactivity_css.h"
00202 #include "WIZwiki_W7500_Interactivity_js.h"
00203 
00204 void variables(void)
00205 {
00206  printf_send_client("<SCRIPT script language=\"javascript\" type=\"text/javascript\">\r\n");
00207  
00208  a0_f = ain0.read()*3.3;
00209  sprintf(httpHeader,"A0 = %3.3f;\r\n", a0_f);
00210  client.send(httpHeader,strlen(httpHeader));
00211  
00212  a1_f = ain1.read()*3.3;
00213  sprintf(httpHeader,"A1 = %3.3f;\r\n", a1_f);
00214  client.send(httpHeader,strlen(httpHeader));
00215  
00216  a2_f = ain2.read()*3.3;
00217  sprintf(httpHeader,"A2 = %3.3f;\r\n", a2_f);
00218  client.send(httpHeader,strlen(httpHeader));
00219  
00220  a3_f = ain3.read()*3.3;
00221  sprintf(httpHeader,"A3 = %3.3f;\r\n", a3_f);
00222  client.send(httpHeader,strlen(httpHeader));
00223  
00224  printf_send_client("</SCRIPT>\r\n");
00225 }
00226 
00227 void ETAT(void)
00228 {
00229   int i;
00230   
00231   // httpHeader
00232   printf_send_client("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
00233             
00234   // Début page Web
00235   printf_send_client("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\r\n");
00236   // meta_refresh
00237   sprintf(httpHeader,"<meta http-equiv=\"refresh\" content=\"" __Time_between_page_refresh__ ";url=http://%s/\">\r\n", eth.getIPAddress() );
00238   //sprintf(httpHeader,"<meta http-equiv=\"refresh\" content=\"1;url=http://%s/\">\r\n", eth.getIPAddress() );
00239   //sprintf(httpHeader,"<meta http-equiv=\"refresh\" content=\"%u;url=http://%s/\">\r\n", refresh, eth.getIPAddress() );
00240   client.send(httpHeader,strlen(httpHeader));
00241   
00242   printf_send_client("<html><head>\r\n");
00243   // title
00244   printf_send_client("<title>WIZwiki-W7500 - Flot Examples: Interactivity</title>\r\n");
00245   printf_send_client("<LINK REL=\"SHORTCUT ICON\" type=\"image/x-icon\" href=\"" __hebergement__ "favicon.ico\">\r\n<link rel=\"icon\" href=\"" __hebergement__ "favicon.ico\" type=\"image/x-icon\">\r\n");
00246   // CSS
00247   CSS();
00248   // JavaScript Interactivity
00249   init_WIZwiki_W7500_Interactivity_JS();
00250   WIZwiki_W7500_Interactivity_JS(); 
00251   /*sprintf(httpHeader,"<script language=\"javascript\" type=\"text/javascript\" src=\"" __hebergement__ "electronique/e/WIZwiki-W7500/js/WIZwiki-W7500_Interactivity_init.js\"></script>\r\n");
00252   client.send(httpHeader,strlen(httpHeader));
00253   */
00254   //printf_send_client("<script language=\"javascript\" type=\"text/javascript\">init_WIZwiki_W7500_Interactivity();</script>\r\n");
00255   
00256   // Variables JavaScript
00257   printf_send_client("<script language=\"javascript\" type=\"text/javascript\">\r\n");
00258   printf_send_client("var color_Y = \"#FF0000\";\r\n");
00259   printf_send_client("var label_Y = \"Meas Moy (V)\";\r\n");
00260   // sprintf(httpHeader, "var x_min = -0.5, x_max =  14.5, y_min = -0.5, y_max =  1.5;\r\n"); // TEST
00261   sprintf(httpHeader, "var x_min = %.1f, x_max = %.1f, y_min = %.1f, y_max = %.1f;\r\n", x_min, x_max, y_min, y_max);
00262   client.send(httpHeader,strlen(httpHeader));
00263   
00264   // sprintf(httpHeader, "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"); // TEST
00265   // client.send(httpHeader,strlen(httpHeader));  // TEST
00266   
00267   if(Sample_gph > NB_SAMPLES_GPH)
00268     {
00269      printf_send_client("var array_value = [");
00270 
00271      for(i = 0 ; i < NB_SAMPLES_GPH ; i++)
00272      {
00273       if(i < NB_SAMPLES_GPH-1) sprintf(httpHeader, "[%.1f,%.1f],", time_samples[i], adc_samples[i]);
00274       else sprintf(httpHeader, "[%.1f,%.1f]", time_samples[i], adc_samples[i]);
00275       client.send(httpHeader,strlen(httpHeader));
00276      }
00277      printf_send_client("];\r\n");
00278     }
00279     
00280     Sample_gph++;
00281     printf_send_client("</script>\r\n");
00282     
00283     // <SCRIPT>
00284     variables();
00285     // <FIN SCRIPT>
00286     // Fin Variable JavaScript
00287     printf_send_client("</head><body><center>\r\n");
00288             
00289   sprintf(httpHeader,"<h2>WIZwiki-W7500 - mBED</h2> ( Compiled at : %s and %s )<p>\r\n",  __DATE__ , __TIME__);
00290   client.send(httpHeader,strlen(httpHeader));
00291    
00292   printf_send_client("<p>(<a href=\"http://www.flotcharts.org/flot/examples/interacting/index.html\">Flot Examples: Interactivity</a>)<p>\r\n");
00293   
00294   printf_send_client("ETAT :<p>\r\n");
00295   
00296   sprintf(httpHeader,"IP: %s, MASK: %s, GW: %s<p>\r\n",
00297                       eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
00298   client.send(httpHeader,strlen(httpHeader));
00299   
00300   sprintf(httpHeader,"&tilde;A0 : %3.3fV , \r\n", a0_f);
00301   client.send(httpHeader,strlen(httpHeader));
00302             
00303   sprintf(httpHeader,"&tilde;A1 : %3.3fV , \r\n", a1_f);
00304   client.send(httpHeader,strlen(httpHeader));
00305             
00306   sprintf(httpHeader,"&tilde;A2 : %3.3fV , \r\n", a2_f);
00307   client.send(httpHeader,strlen(httpHeader));
00308             
00309   sprintf(httpHeader,"&tilde;A3 : %3.3fV<p>\r\n", a3_f);
00310   client.send(httpHeader,strlen(httpHeader));
00311   
00312   sprintf(httpHeader, "ADC (A0) :<p>DC : %.3fVmoy ( sum : %.3f - Samples : %u )<br>\r\n", meas_moy, meas_sum, Samples);
00313   client.send(httpHeader,strlen(httpHeader));
00314     
00315   sprintf(httpHeader, "DC (0 &agrave; 3,3V) : %.3fVmin,   %.3fVmax , Vdiff : %.0fmV<br>\r\n", vdc_min, vdc_max, 1000.0*(vdc_max - vdc_min));
00316   client.send(httpHeader,strlen(httpHeader));
00317     
00318   sprintf(httpHeader, "AC (-1,65V &agrave; 1,65V) : %.3fVeff<p>\r\n", veff);
00319   client.send(httpHeader,strlen(httpHeader));
00320   
00321   sprintf(httpHeader, "Time : %.1f Seconds - Sample Graphique : %u<p>\r\n", Seconds, Sample_gph);// diplays the human readable Seconds
00322   client.send(httpHeader,strlen(httpHeader));
00323   
00324   WIZwiki_W7500_Interactivity_div();
00325   //printf_send_client("<p><script language=\"javascript\" type=\"text/javascript\">WIZwiki_W7500_Interactivity();</script><p>\r\n");
00326   
00327   printf_send_client("<p><p><a href=\"..\">Root</a>\r\n");
00328   
00329   printf_send_client("</center></body></html>\r\n");               
00330 }
00331 
00332 //--------------------------------------------
00333 
00334 int main(void)
00335 {
00336     // initialisation des variables
00337  int i;
00338  
00339  init_ADC_sampling();
00340  //-----------------
00341  
00342  for(i = 0 ; i < NB_SAMPLES_GPH ; i++)
00343  {
00344       time_samples[i] = 0;
00345       adc_samples[i] = 0.0;
00346  }
00347  // Init the ticker with the address of the function (add_one_second) to be attached and the interval (1000 ms)
00348  time_tic.attach(&add_one_tic, __Time_tic_in_Second__);
00349  //--------------
00350     ledTick.attach(&ledTickfunc,0.5);
00351     // Serial Interface eth;
00352     // Serial port configuration (valeurs par defaut) : 9600 baud, 8-bit data, no parity, stop bit
00353     uart.baud(9600);
00354     uart.format(8, SerialBase::None, 1);
00355     uart.printf("Initializing\n");
00356   wait(1.0);
00357 //    Check File System
00358     uart.printf("Checking File System\n");
00359     DIR *d = opendir("/sd/");
00360     if(d != NULL) 
00361     {
00362         uart.printf("SD Card Present\n");
00363     } 
00364     else 
00365     {
00366         uart.printf("SD Card Root Directory Not Found\n");
00367     }
00368    wait(1.0);
00369 //    EthernetInterface eth;
00370     uart.printf("Initializing Ethernet\n");
00371     #if USE_DHCP
00372     //eth.init Use DHCP
00373     int ret = eth.init((uint8_t*)MAC);    // Use DHCP for WIZnetInterface
00374     uart.printf("Connecting DHCP\n");
00375     #else
00376     int ret = eth.init((uint8_t*)MAC,IP,MASK,GATEWAY);  //IP,mask,Gateway
00377     uart.printf("Connecting (IP,mask,Gateway)\n");
00378     #endif
00379     wait(1.0);
00380     // Check Ethernet Link-Done
00381     uart.printf("Check Ethernet Link\r\n");
00382     
00383     if(eth.link() == true) 
00384     { 
00385      uart.printf("- Ethernet PHY Link - Done\r\n");
00386      //led_r = LED_ON;
00387      COLOR(_RED_);
00388     }
00389     else 
00390     {
00391      uart.printf("- Ethernet PHY Link - Fail\r\n");
00392      //led_r = LED_OFF;
00393      COLOR(_BLACK_);
00394     }
00395     wait(1.0);
00396     if(!ret) 
00397     {
00398      uart.printf("Initialized, MAC: %s\r\n", eth.getMACAddress());
00399      ret = eth.connect();
00400      
00401      if(!ret) 
00402      {
00403             uart.printf("IP: %s, MASK: %s, GW: %s\r\n",
00404                       eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
00405       // led_b = LED_ON, led_g = LED_ON;
00406       COLOR(_CYAN_);
00407      } 
00408      else 
00409      {
00410             uart.printf("Error ethernet.connect() - ret = %d\r\n", ret);
00411       //led_b = LED_OFF;
00412       COLOR(_BLUE_);
00413       exit(0);
00414      }
00415     } 
00416     else 
00417     {
00418         uart.printf("Error ethernet.init() - ret = %d\r\n", ret);
00419      //led_b = LED_OFF;
00420      COLOR(_BLACK_);
00421      exit(0);
00422     }    
00423     wait(1.0);
00424 //    TCPSocketServer server;
00425     server.bind(HTTPD_SERVER_PORT);
00426     server.listen();
00427     uart.printf("Server Listening\n");
00428 
00429     while(true) 
00430     {
00431         uart.printf("\nWait for new connection...\r\n");
00432         server.accept(client);
00433         client.set_blocking(false, 1500); // Timeout after (1.5)s
00434 
00435         uart.printf("Connection from: %s\r\n", client.get_address());
00436         while(true) 
00437         {
00438             //led_g = LED_ON;
00439             COLOR(_GREEN_);
00440             int n = client.receive(buffer, sizeof(buffer));
00441             if(n <= 0) break;
00442             uart.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer);
00443             if(n >= 1024) 
00444             {
00445                 sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
00446                 client.send(httpHeader,strlen(httpHeader));
00447                 client.send(buffer,n);
00448                 break;
00449             } 
00450             else 
00451             {
00452                 buffer[n]=0;
00453             }
00454             if(!strncmp(buffer, "GET ", 4)) 
00455             {
00456                 uristr = buffer + 4;
00457                 eou = strstr(uristr, " ");
00458                 if(eou == NULL) 
00459                 {
00460                     sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
00461                     client.send(httpHeader,strlen(httpHeader));
00462                     client.send(buffer,n);
00463                 } 
00464                 else 
00465                 {
00466                     *eou = 0;
00467                     //get_file(uristr);
00468                     ETAT();
00469                 }
00470             }
00471         }
00472         //led_g = LED_OFF;
00473         COLOR(_BLACK_);
00474         client.close();
00475     }
00476 }