Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SDFileSystem STATIC_COLORS WIZnetInterface mbed
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 #include "SDFileSystem.h" 00004 #include <math.h> 00005 #include <stdio.h> 00006 #include <string.h> 00007 00008 #define NB_SAMPLES 10 00009 unsigned long int Sample = 0; 00010 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 }; 00011 float time_samples[NB_SAMPLES];// = { -0.1,2,3,4,5,6,7,8,9,10,11,12,13,14 }; 00012 float x_min = 0.0, x_max = 0.0; 00013 float y_min = 0.0, y_max = 0.0; 00014 float Seconds = 0.0; 00015 float meas = 0.0; 00016 00017 #define USE_DHCP 1 00018 /* 00019 MAC Address Details ( http://www.macvendorlookup.com/ ) 00020 Company Wiznet Address 00021 Seoul 135-830 00022 Nonyhun, Kangnam 00023 KOREA, REPUBLIC OF 00024 Range : 00:08:DC:00:00:00 - 00:08:DC:FF:FF:FF 00025 Type : MA-L: IEEE MAC Address Large (24-bit block size) 00026 00027 */ 00028 //#define MAC "\x31\x41\x59\x26\x53\x58" 00029 #define MAC "\x00\x08\xDC\x31\x41\x59" 00030 //#define MAC "\x00\x08\xDC\x11\x34\x78" 00031 #define IP "192.168.0.170" 00032 #define MASK "255.255.255.0" 00033 #define GATEWAY "192.168.0.254" 00034 00035 #define HTTPD_SERVER_PORT 80 00036 #define HTTPD_MAX_REQ_LENGTH 1023 00037 #define HTTPD_MAX_HDR_LENGTH 255 00038 #define HTTPD_MAX_FNAME_LENGTH 127 00039 #define HTTPD_MAX_DNAME_LENGTH 127 00040 00041 #if defined(TARGET_WIZwiki_W7500) 00042 Serial uart(USBTX, USBRX); 00043 SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "sd"); // WIZwiki-W7500 00044 #include "static_colors.h" 00045 // LED RED : server listning status 00046 // LED GREEN : socket connecting status Ok 00047 // LED BLUE : socket connecting status Busy 00048 #endif 00049 00050 EthernetInterface eth; 00051 TCPSocketServer server; 00052 TCPSocketConnection client; 00053 00054 //------ THING_SPEAK ------------------ 00055 00056 #define THRESHOLD_V 0.01 00057 float adc_v = 0.0; 00058 float adc_v_moy = 0.0; 00059 float adc_v_moy_old = 0.0; 00060 int ret; 00061 bool status; 00062 float calcule_update_time; 00063 #define UPDATE_TIME_THING_SPEAK 15.0 00064 00065 char paq_en[64]; 00066 #define THING_SPEAK_IP_STR "184.106.153.149" /* thingspeak.com IP Address */ 00067 #define THING_SPEAK_IP_PORT 80 /* port number */ 00068 #define THING_SPEAK_KEY_STR "PD6JY2N94E3N0UNO" /* API key */ 00069 #define THING_SPEAK_CHANNEL 132986 /* channel ID */ 00070 #define THING_SPEAK_LABEL_STR "field1" 00071 char * str0 = "POST /update HTTP/1.1\n"; 00072 char * str1 = "Host: api.thingspeak.com\n"; 00073 char * str2 = "Connection: close\n"; 00074 char * str3 = "X-THINGSPEAKAPIKEY: "; 00075 char * str4 = "Content-Type: application/x-www-form-urlencoded\n"; 00076 char * write_key = "PD6JY2N94E3N0UNO"; 00077 char * str5 = "Content-Length: "; 00078 TCPSocketConnection client_th; 00079 00080 void thingspeak_update(void) 00081 { 00082 int attempt = 0; 00083 int length; 00084 char buffer_th[64]; 00085 char data_entry[64]; 00086 00087 // active l'envoi sur THING SPEAK si l'évolution moyenne est supérieure au seuil désiré 00088 if((abs(adc_v_moy - adc_v_moy_old) >= THRESHOLD_V)) 00089 { 00090 while(attempt < 4) 00091 { 00092 uart.printf("\n\rWaiting for connection to ThingSpeak server...\n\r"); 00093 ret = client_th.connect(THING_SPEAK_IP_STR,THING_SPEAK_IP_PORT); 00094 00095 if(!ret) 00096 { 00097 uart.printf("Connected to ThingSpeak server\n\r"); 00098 } 00099 else 00100 { 00101 uart.printf("Connection attempt to ThingSpeak server failed\n\r"); 00102 attempt++; 00103 } 00104 00105 if(client_th.is_connected() ) // try to send data 00106 { 00107 // METHODE POST 00108 00109 // update function 00110 // POST /update HTTP/1.1\n 00111 client_th.send(str0,strlen(str0)); 00112 uart.printf("%s >%d\n\r",str0,strlen(str0)); 00113 // Host: api.thingspeak.com\n 00114 client_th.send(str1,strlen(str1)); 00115 uart.printf("%s >%d\n\r",str1,strlen(str1)); 00116 // Connection: close\n 00117 client_th.send(str2,strlen(str2)); 00118 uart.printf("%s >%d\n\r",str2,strlen(str2)); 00119 // X-THINGSPEAKAPIKEY: PD6JY2N94E3N0UNO 00120 sprintf(buffer_th,"%s %s\n",str3,write_key); 00121 client_th.send(buffer_th,strlen(buffer_th)); 00122 uart.printf("%s >%d",buffer_th,strlen(buffer_th)); 00123 // Content-Type: application/x-www-form-urlencoded\n 00124 client_th.send(str4,strlen(str4)); 00125 uart.printf("%s >%d",str4,strlen(str4)); 00126 00127 sprintf(data_entry,"field1=%f\n",adc_v_moy ); //adc moyenne en volts 00128 length=strlen(data_entry); 00129 // Content-Length: 00130 sprintf(buffer_th,"Content-Length: %d\n\n",length); 00131 uart.printf("%s >%d\n\r",buffer_th,strlen(buffer_th)); 00132 00133 uart.printf("%s >%d\n\r",data_entry,strlen(data_entry)); 00134 client_th.send(buffer_th,strlen(buffer_th)); 00135 client_th.send(data_entry,strlen(data_entry)); 00136 00137 // METHODE GET 00138 // http://api.thingspeak.com/update?api_key=PD6JY2N94E3N0UNO&field1=value 00139 00140 attempt = 4; 00141 } 00142 } 00143 00144 adc_v_moy_old = adc_v_moy; 00145 } 00146 } 00147 00148 //------ THING_SPEAK ------------------ 00149 00150 00151 char buffer[HTTPD_MAX_REQ_LENGTH+1]; 00152 char httpHeader[HTTPD_MAX_HDR_LENGTH+1]; 00153 char fileName[HTTPD_MAX_FNAME_LENGTH+1]; 00154 char dirName[HTTPD_MAX_DNAME_LENGTH+1]; 00155 char *uristr; 00156 char *eou; 00157 char *qrystr; 00158 00159 FILE *fp; 00160 int rdCnt; 00161 00162 // Initialize a pins to perform analog input and digital output fucntions 00163 AnalogIn ain0(A0); 00164 AnalogIn ain1(A1); 00165 AnalogIn ain2(A2); 00166 AnalogIn ain3(A3); 00167 00168 float a0_f, a1_f, a2_f, a3_f; 00169 00170 #define __IP_LOCAL__ IP 00171 #define __hebergement__ "http://olivier.fournet.free.fr/" 00172 #define __Time_between_page_refresh__ "1" 00173 00174 int refresh = 1; // 1 second refresh 00175 00176 //------------ 00177 #define __Time_tic_in_Second__ 0.1 00178 00179 Ticker time_tic; 00180 00181 void add_one_tic() 00182 { 00183 int i; 00184 00185 Seconds = Seconds + (float)__Time_tic_in_Second__; 00186 00187 // mesures ADC 00188 meas = ain0.read(); // Converts and read the analog input value (value from 0.0 to 1.0) 00189 // valeur instantanée pour Thing Speak 00190 adc_v = meas * 3.3; 00191 meas = meas * 3300.0; // Change the value to be in the 0 to 3300 range 00192 // moyenne sur 10 valeurs 00193 adc_v_moy = 0.1 * (9.0 * adc_v_moy + adc_v); 00194 00195 x_min = x_max = Seconds; 00196 y_min = y_max = meas; 00197 00198 for(i = 1 ; i < NB_SAMPLES ; i++) 00199 { 00200 time_samples[i-1] = time_samples[i]; 00201 adc_samples[i-1] = adc_samples[i]; 00202 if( time_samples[i] < x_min ) x_min = time_samples[i]; 00203 if( time_samples[i] > x_max ) x_max = time_samples[i]; 00204 if( adc_samples[i] < y_min ) y_min = adc_samples[i]; 00205 if( adc_samples[i] > y_max ) y_max = adc_samples[i]; 00206 } 00207 00208 adc_samples[NB_SAMPLES-1] = meas; 00209 time_samples[NB_SAMPLES-1] = Seconds; 00210 } 00211 //------------ 00212 00213 Ticker ledTick; 00214 00215 char *pch; 00216 char ext[5]; 00217 char ext_gif[] = "gif"; 00218 char ext_jpg[] = "jpg"; 00219 char ext_png[] = "png"; 00220 char ext_tiff[] = "tiff"; 00221 int pos_ext; 00222 int extLen; 00223 00224 void ledTickfunc() 00225 { 00226 led_r = !led_r; 00227 } 00228 00229 void printf_send_client(const char *str_c) 00230 { 00231 char http_send[HTTPD_MAX_REQ_LENGTH+1]; 00232 sprintf(http_send,str_c); 00233 client.send(http_send,strlen(http_send)); 00234 } 00235 00236 void variables(void) 00237 { 00238 printf_send_client("<SCRIPT script language=\"javascript\" type=\"text/javascript\">\r\n"); 00239 00240 a0_f = ain0.read()*3.3; 00241 sprintf(httpHeader,"A0 = %3.3f;\r\n", a0_f); 00242 client.send(httpHeader,strlen(httpHeader)); 00243 00244 a1_f = ain1.read()*3.3; 00245 sprintf(httpHeader,"A1 = %3.3f;\r\n", a1_f); 00246 client.send(httpHeader,strlen(httpHeader)); 00247 00248 a2_f = ain2.read()*3.3; 00249 sprintf(httpHeader,"A2 = %3.3f;\r\n", a2_f); 00250 client.send(httpHeader,strlen(httpHeader)); 00251 00252 a3_f = ain3.read()*3.3; 00253 sprintf(httpHeader,"A3 = %3.3f;\r\n", a3_f); 00254 client.send(httpHeader,strlen(httpHeader)); 00255 00256 printf_send_client("</SCRIPT>\r\n"); 00257 } 00258 00259 00260 void ETAT(void) 00261 { 00262 int i; 00263 00264 // httpHeader 00265 printf_send_client("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n"); 00266 00267 // Début page Web 00268 printf_send_client("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\r\n"); 00269 00270 // meta_refresh 00271 sprintf(httpHeader,"<meta http-equiv=\"refresh\" content=\"" __Time_between_page_refresh__ ";url=http://%s/\">\r\n", eth.getIPAddress() ); 00272 //sprintf(httpHeader,"<meta http-equiv=\"refresh\" content=\"1;url=http://%s/\">\r\n", eth.getIPAddress() ); 00273 //sprintf(httpHeader,"<meta http-equiv=\"refresh\" content=\"%u;url=http://%s/\">\r\n", refresh, eth.getIPAddress() ); 00274 client.send(httpHeader,strlen(httpHeader)); 00275 00276 printf_send_client("<html><head>\r\n"); 00277 // title 00278 printf_send_client("<title>WIZwiki-W7500 - Flot Examples: Interactivity</title>\r\n"); 00279 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"); 00280 // JavaScript Interactivity 00281 sprintf(httpHeader,"<script language=\"javascript\" type=\"text/javascript\" src=\"" __hebergement__ "electronique/e/WIZwiki-W7500/js/WIZwiki-W7500_Interactivity_init.js\"></script>\r\n"); 00282 client.send(httpHeader,strlen(httpHeader)); 00283 00284 printf_send_client("<script language=\"javascript\" type=\"text/javascript\">init_WIZwiki_W7500_Interactivity();</script>\r\n"); 00285 00286 // Variables JavaScript 00287 printf_send_client("<script language=\"javascript\" type=\"text/javascript\">\r\n"); 00288 printf_send_client("var color_Y = \"#FF0000\";\r\n"); 00289 printf_send_client("var label_Y = \"Adc(mV)\";\r\n"); 00290 // sprintf(httpHeader, "var x_min = -0.5, x_max = 14.5, y_min = -0.5, y_max = 1.5;\r\n"); // TEST 00291 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); 00292 client.send(httpHeader,strlen(httpHeader)); 00293 00294 // 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 00295 // client.send(httpHeader,strlen(httpHeader)); // TEST 00296 00297 if(Sample > NB_SAMPLES) 00298 { 00299 printf_send_client("var array_value = ["); 00300 00301 for(i = 0 ; i < NB_SAMPLES ; i++) 00302 { 00303 if(i < NB_SAMPLES) sprintf(httpHeader, "[%.1f,%.1f],", time_samples[i], adc_samples[i]); 00304 else sprintf(httpHeader, "[%.1f,%.1f]", time_samples[i], adc_samples[i]); 00305 client.send(httpHeader,strlen(httpHeader)); 00306 } 00307 printf_send_client("];\r\n"); 00308 } 00309 00310 Sample++; 00311 printf_send_client("</script>\r\n"); 00312 00313 // <SCRIPT> 00314 variables(); 00315 // <FIN SCRIPT> 00316 // Fin Variable JavaScript 00317 printf_send_client("</head><body><center>\r\n"); 00318 00319 sprintf(httpHeader,"<h2>WIZwiki-W7500 - mBED</h2> ( Compiled at : %s and %s )<p>\r\n", __DATE__ , __TIME__); 00320 client.send(httpHeader,strlen(httpHeader)); 00321 00322 printf_send_client("<p>(<a href=\"http://www.flotcharts.org/flot/examples/interacting/index.html\">Flot Examples: Interactivity</a>)<p>\r\n"); 00323 00324 printf_send_client("ETAT :<p>\r\n"); 00325 00326 sprintf(httpHeader,"IP: %s, MASK: %s, GW: %s<p>\r\n", 00327 eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); 00328 client.send(httpHeader,strlen(httpHeader)); 00329 00330 //sprintf(httpHeader,"˜A0 :<script>document.write(A0);</script>V<br>\r\n"); 00331 sprintf(httpHeader,"˜A0 : %3.3fV<br>\r\n", a0_f); 00332 client.send(httpHeader,strlen(httpHeader)); 00333 00334 //sprintf(httpHeader,"˜A1 :<script>document.write(A1);</script>V<br>\r\n"); 00335 sprintf(httpHeader,"˜A1 : %3.3fV<br>\r\n", a1_f); 00336 client.send(httpHeader,strlen(httpHeader)); 00337 00338 //sprintf(httpHeader,"˜A2 :<script>document.write(A2);</script>V<br>\r\n"); 00339 sprintf(httpHeader,"˜A2 : %3.3fV<br>\r\n", a2_f); 00340 client.send(httpHeader,strlen(httpHeader)); 00341 00342 //sprintf(httpHeader,"˜A3 :<script>document.write(A3);</script>V<p>\r\n"); 00343 sprintf(httpHeader,"˜A3 : %3.3fV<br>\r\n", a3_f); 00344 client.send(httpHeader,strlen(httpHeader)); 00345 00346 sprintf(httpHeader, "Time : %.1f Seconds - Sample : %u<p>\r\n", Seconds, Sample);// diplays the human readable Seconds 00347 client.send(httpHeader,strlen(httpHeader)); 00348 00349 printf_send_client("<p><script language=\"javascript\" type=\"text/javascript\">WIZwiki_W7500_Interactivity();</script><p>\r\n"); 00350 00351 printf_send_client("<p><a href=\"..\">Root</a>\r\n"); 00352 00353 printf_send_client("</center></body></html>\r\n"); 00354 } 00355 00356 //-------------------------------------------- 00357 void Serial_Interface_init(void) 00358 { 00359 // Serial Interface eth; 00360 // Serial port configuration (valeurs par defaut) : 9600 baud, 8-bit data, no parity, stop bit 00361 uart.baud(9600); 00362 uart.format(8, SerialBase::None, 1); 00363 uart.printf("Initializing\n"); 00364 wait(1.0); 00365 } 00366 00367 void Check_File_System(void) 00368 { 00369 // Check File System 00370 uart.printf("Checking File System\n"); 00371 DIR *d = opendir("/sd/"); 00372 if(d != NULL) 00373 { 00374 uart.printf("SD Card Present\n"); 00375 } 00376 else 00377 { 00378 uart.printf("SD Card Root Directory Not Found\n"); 00379 } 00380 wait(1.0); 00381 } 00382 00383 void ethernet_init(void) 00384 { 00385 // EthernetInterface eth; 00386 uart.printf("Initializing Ethernet\n"); 00387 #if USE_DHCP 00388 //eth.init Use DHCP 00389 int ret = eth.init((uint8_t*)MAC); // Use DHCP for WIZnetInterface 00390 uart.printf("Connecting DHCP\n"); 00391 #else 00392 int ret = eth.init((uint8_t*)MAC,IP,MASK,GATEWAY); //IP,mask,Gateway 00393 uart.printf("Connecting (IP,mask,Gateway)\n"); 00394 #endif 00395 wait(1.0); 00396 // Check Ethernet Link-Done 00397 uart.printf("Check Ethernet Link\r\n"); 00398 00399 if(eth.link() == true) 00400 { 00401 uart.printf("- Ethernet PHY Link - Done\r\n"); 00402 COLOR(_RED_); 00403 } 00404 else 00405 { 00406 uart.printf("- Ethernet PHY Link - Fail\r\n"); 00407 COLOR(_BLACK_); 00408 } 00409 wait(1.0); 00410 if(!ret) 00411 { 00412 uart.printf("Initialized, MAC: %s\r\n", eth.getMACAddress()); 00413 ret = eth.connect(); 00414 00415 if(!ret) 00416 { 00417 uart.printf("IP: %s, MASK: %s, GW: %s\r\n", 00418 eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); 00419 COLOR(_CYAN_); 00420 } 00421 else 00422 { 00423 uart.printf("Error ethernet.connect() - ret = %d\r\n", ret); 00424 COLOR(_BLUE_); 00425 exit(0); 00426 } 00427 } 00428 else 00429 { 00430 uart.printf("Error ethernet.init() - ret = %d\r\n", ret); 00431 COLOR(_BLACK_); 00432 exit(0); 00433 } 00434 00435 wait(1.0); 00436 // TCPSocketServer server; 00437 server.bind(HTTPD_SERVER_PORT); 00438 server.listen(); 00439 uart.printf("Server Listening\n"); 00440 } 00441 00442 int main(void) 00443 { 00444 // initialisation des variables 00445 int i; 00446 00447 for(i = 0 ; i < NB_SAMPLES ; i++) 00448 { 00449 time_samples[i] = 0; 00450 adc_samples[i] = 0.0; 00451 } 00452 00453 adc_v_moy = ain0.read()*3.3; 00454 adc_v_moy_old = adc_v_moy; 00455 //------------------ 00456 Serial_Interface_init(); 00457 00458 // Init the ticker with the address of the function (add_one_second) to be attached and the interval (1000 ms) 00459 time_tic.attach(&add_one_tic, __Time_tic_in_Second__); 00460 //-------------- 00461 ledTick.attach(&ledTickfunc,0.5); 00462 //--------------- 00463 Check_File_System(); 00464 ethernet_init(); 00465 00466 while(true) 00467 { 00468 uart.printf("\nWait for new connection...\r\n"); 00469 server.accept(client); 00470 client.set_blocking(false, 1500); // Timeout after (1.5)s 00471 00472 uart.printf("Connection from: %s\r\n", client.get_address()); 00473 while(true) 00474 { 00475 //led_g = LED_ON; 00476 COLOR(_GREEN_); 00477 int n = client.receive(buffer, sizeof(buffer)); 00478 if(n <= 0) break; 00479 uart.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer); 00480 if(n >= 1024) 00481 { 00482 sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); 00483 client.send(httpHeader,strlen(httpHeader)); 00484 client.send(buffer,n); 00485 break; 00486 } 00487 else 00488 { 00489 buffer[n]=0; 00490 } 00491 if(!strncmp(buffer, "GET ", 4)) 00492 { 00493 uristr = buffer + 4; 00494 eou = strstr(uristr, " "); 00495 if(eou == NULL) 00496 { 00497 sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); 00498 client.send(httpHeader,strlen(httpHeader)); 00499 client.send(buffer,n); 00500 } 00501 else 00502 { 00503 *eou = 0; 00504 //get_file(uristr); 00505 ETAT(); 00506 thingspeak_update(); 00507 } 00508 } 00509 } 00510 //led_g = LED_OFF; 00511 COLOR(_BLACK_); 00512 client.close(); 00513 client_th.close(); 00514 // 00515 } 00516 }
Generated on Wed Jul 13 2022 19:36:50 by
1.7.2