WIZwiki-W7500_ADC + Flot Exemple interactivity + THING SPEAK

Dependencies:   SDFileSystem STATIC_COLORS WIZnetInterface mbed

/media/uploads/Fo170/wizwiki-w7500_adc.png

/media/uploads/Fo170/wizwiki_thing_speak.png

test durée de fonctionnement :

/media/uploads/Fo170/test_duree.png

172 458 secondes ===> environs 48h

Committer:
Fo170
Date:
Wed Jul 13 17:39:47 2016 +0000
Revision:
7:c2acbe486cf2
Parent:
6:d68dd410b141
modification du code pour prise en compte plus rapide des changement dans thing speak

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Fo170 0:70a1e939250e 1 #include "mbed.h"
Fo170 0:70a1e939250e 2 #include "EthernetInterface.h"
Fo170 0:70a1e939250e 3 #include "SDFileSystem.h"
Fo170 6:d68dd410b141 4 #include <math.h>
Fo170 0:70a1e939250e 5 #include <stdio.h>
Fo170 0:70a1e939250e 6 #include <string.h>
Fo170 0:70a1e939250e 7
Fo170 6:d68dd410b141 8 #define NB_SAMPLES 10
Fo170 6:d68dd410b141 9 unsigned long int Sample = 0;
Fo170 6:d68dd410b141 10 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:d68dd410b141 11 float time_samples[NB_SAMPLES];// = { -0.1,2,3,4,5,6,7,8,9,10,11,12,13,14 };
Fo170 6:d68dd410b141 12 float x_min = 0.0, x_max = 0.0;
Fo170 6:d68dd410b141 13 float y_min = 0.0, y_max = 0.0;
Fo170 6:d68dd410b141 14 float Seconds = 0.0;
Fo170 6:d68dd410b141 15 float meas = 0.0;
Fo170 6:d68dd410b141 16
Fo170 0:70a1e939250e 17 #define USE_DHCP 1
Fo170 0:70a1e939250e 18 /*
Fo170 0:70a1e939250e 19 MAC Address Details ( http://www.macvendorlookup.com/ )
Fo170 0:70a1e939250e 20 Company Wiznet Address
Fo170 0:70a1e939250e 21 Seoul 135-830
Fo170 0:70a1e939250e 22 Nonyhun, Kangnam
Fo170 0:70a1e939250e 23 KOREA, REPUBLIC OF
Fo170 0:70a1e939250e 24 Range : 00:08:DC:00:00:00 - 00:08:DC:FF:FF:FF
Fo170 0:70a1e939250e 25 Type : MA-L: IEEE MAC Address Large (24-bit block size)
Fo170 4:0e09a4c5bd22 26
Fo170 0:70a1e939250e 27 */
Fo170 0:70a1e939250e 28 //#define MAC "\x31\x41\x59\x26\x53\x58"
Fo170 0:70a1e939250e 29 #define MAC "\x00\x08\xDC\x31\x41\x59"
Fo170 0:70a1e939250e 30 //#define MAC "\x00\x08\xDC\x11\x34\x78"
Fo170 0:70a1e939250e 31 #define IP "192.168.0.170"
Fo170 0:70a1e939250e 32 #define MASK "255.255.255.0"
Fo170 0:70a1e939250e 33 #define GATEWAY "192.168.0.254"
Fo170 0:70a1e939250e 34
Fo170 0:70a1e939250e 35 #define HTTPD_SERVER_PORT 80
Fo170 0:70a1e939250e 36 #define HTTPD_MAX_REQ_LENGTH 1023
Fo170 0:70a1e939250e 37 #define HTTPD_MAX_HDR_LENGTH 255
Fo170 0:70a1e939250e 38 #define HTTPD_MAX_FNAME_LENGTH 127
Fo170 0:70a1e939250e 39 #define HTTPD_MAX_DNAME_LENGTH 127
Fo170 0:70a1e939250e 40
Fo170 0:70a1e939250e 41 #if defined(TARGET_WIZwiki_W7500)
Fo170 0:70a1e939250e 42 Serial uart(USBTX, USBRX);
Fo170 0:70a1e939250e 43 SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "sd"); // WIZwiki-W7500
Fo170 0:70a1e939250e 44 #include "static_colors.h"
Fo170 0:70a1e939250e 45 // LED RED : server listning status
Fo170 0:70a1e939250e 46 // LED GREEN : socket connecting status Ok
Fo170 0:70a1e939250e 47 // LED BLUE : socket connecting status Busy
Fo170 0:70a1e939250e 48 #endif
Fo170 0:70a1e939250e 49
Fo170 0:70a1e939250e 50 EthernetInterface eth;
Fo170 0:70a1e939250e 51 TCPSocketServer server;
Fo170 0:70a1e939250e 52 TCPSocketConnection client;
Fo170 0:70a1e939250e 53
Fo170 6:d68dd410b141 54 //------ THING_SPEAK ------------------
Fo170 6:d68dd410b141 55
Fo170 6:d68dd410b141 56 #define THRESHOLD_V 0.01
Fo170 7:c2acbe486cf2 57 float adc_v = 0.0;
Fo170 6:d68dd410b141 58 float adc_v_moy = 0.0;
Fo170 6:d68dd410b141 59 float adc_v_moy_old = 0.0;
Fo170 6:d68dd410b141 60 int ret;
Fo170 6:d68dd410b141 61 bool status;
Fo170 6:d68dd410b141 62 float calcule_update_time;
Fo170 6:d68dd410b141 63 #define UPDATE_TIME_THING_SPEAK 15.0
Fo170 6:d68dd410b141 64
Fo170 6:d68dd410b141 65 char paq_en[64];
Fo170 6:d68dd410b141 66 #define THING_SPEAK_IP_STR "184.106.153.149" /* thingspeak.com IP Address */
Fo170 6:d68dd410b141 67 #define THING_SPEAK_IP_PORT 80 /* port number */
Fo170 6:d68dd410b141 68 #define THING_SPEAK_KEY_STR "PD6JY2N94E3N0UNO" /* API key */
Fo170 6:d68dd410b141 69 #define THING_SPEAK_CHANNEL 132986 /* channel ID */
Fo170 6:d68dd410b141 70 #define THING_SPEAK_LABEL_STR "field1"
Fo170 6:d68dd410b141 71 char * str0 = "POST /update HTTP/1.1\n";
Fo170 6:d68dd410b141 72 char * str1 = "Host: api.thingspeak.com\n";
Fo170 6:d68dd410b141 73 char * str2 = "Connection: close\n";
Fo170 6:d68dd410b141 74 char * str3 = "X-THINGSPEAKAPIKEY: ";
Fo170 6:d68dd410b141 75 char * str4 = "Content-Type: application/x-www-form-urlencoded\n";
Fo170 6:d68dd410b141 76 char * write_key = "PD6JY2N94E3N0UNO";
Fo170 6:d68dd410b141 77 char * str5 = "Content-Length: ";
Fo170 6:d68dd410b141 78 TCPSocketConnection client_th;
Fo170 6:d68dd410b141 79
Fo170 6:d68dd410b141 80 void thingspeak_update(void)
Fo170 6:d68dd410b141 81 {
Fo170 6:d68dd410b141 82 int attempt = 0;
Fo170 6:d68dd410b141 83 int length;
Fo170 6:d68dd410b141 84 char buffer_th[64];
Fo170 6:d68dd410b141 85 char data_entry[64];
Fo170 7:c2acbe486cf2 86
Fo170 6:d68dd410b141 87 // active l'envoi sur THING SPEAK si l'évolution moyenne est supérieure au seuil désiré
Fo170 6:d68dd410b141 88 if((abs(adc_v_moy - adc_v_moy_old) >= THRESHOLD_V))
Fo170 6:d68dd410b141 89 {
Fo170 6:d68dd410b141 90 while(attempt < 4)
Fo170 6:d68dd410b141 91 {
Fo170 6:d68dd410b141 92 uart.printf("\n\rWaiting for connection to ThingSpeak server...\n\r");
Fo170 6:d68dd410b141 93 ret = client_th.connect(THING_SPEAK_IP_STR,THING_SPEAK_IP_PORT);
Fo170 6:d68dd410b141 94
Fo170 6:d68dd410b141 95 if(!ret)
Fo170 6:d68dd410b141 96 {
Fo170 6:d68dd410b141 97 uart.printf("Connected to ThingSpeak server\n\r");
Fo170 6:d68dd410b141 98 }
Fo170 6:d68dd410b141 99 else
Fo170 6:d68dd410b141 100 {
Fo170 6:d68dd410b141 101 uart.printf("Connection attempt to ThingSpeak server failed\n\r");
Fo170 6:d68dd410b141 102 attempt++;
Fo170 6:d68dd410b141 103 }
Fo170 6:d68dd410b141 104
Fo170 6:d68dd410b141 105 if(client_th.is_connected() ) // try to send data
Fo170 6:d68dd410b141 106 {
Fo170 6:d68dd410b141 107 // METHODE POST
Fo170 6:d68dd410b141 108
Fo170 6:d68dd410b141 109 // update function
Fo170 6:d68dd410b141 110 // POST /update HTTP/1.1\n
Fo170 6:d68dd410b141 111 client_th.send(str0,strlen(str0));
Fo170 6:d68dd410b141 112 uart.printf("%s >%d\n\r",str0,strlen(str0));
Fo170 6:d68dd410b141 113 // Host: api.thingspeak.com\n
Fo170 6:d68dd410b141 114 client_th.send(str1,strlen(str1));
Fo170 6:d68dd410b141 115 uart.printf("%s >%d\n\r",str1,strlen(str1));
Fo170 6:d68dd410b141 116 // Connection: close\n
Fo170 6:d68dd410b141 117 client_th.send(str2,strlen(str2));
Fo170 6:d68dd410b141 118 uart.printf("%s >%d\n\r",str2,strlen(str2));
Fo170 6:d68dd410b141 119 // X-THINGSPEAKAPIKEY: PD6JY2N94E3N0UNO
Fo170 6:d68dd410b141 120 sprintf(buffer_th,"%s %s\n",str3,write_key);
Fo170 6:d68dd410b141 121 client_th.send(buffer_th,strlen(buffer_th));
Fo170 6:d68dd410b141 122 uart.printf("%s >%d",buffer_th,strlen(buffer_th));
Fo170 6:d68dd410b141 123 // Content-Type: application/x-www-form-urlencoded\n
Fo170 6:d68dd410b141 124 client_th.send(str4,strlen(str4));
Fo170 6:d68dd410b141 125 uart.printf("%s >%d",str4,strlen(str4));
Fo170 6:d68dd410b141 126
Fo170 6:d68dd410b141 127 sprintf(data_entry,"field1=%f\n",adc_v_moy ); //adc moyenne en volts
Fo170 6:d68dd410b141 128 length=strlen(data_entry);
Fo170 6:d68dd410b141 129 // Content-Length:
Fo170 6:d68dd410b141 130 sprintf(buffer_th,"Content-Length: %d\n\n",length);
Fo170 6:d68dd410b141 131 uart.printf("%s >%d\n\r",buffer_th,strlen(buffer_th));
Fo170 6:d68dd410b141 132
Fo170 6:d68dd410b141 133 uart.printf("%s >%d\n\r",data_entry,strlen(data_entry));
Fo170 6:d68dd410b141 134 client_th.send(buffer_th,strlen(buffer_th));
Fo170 6:d68dd410b141 135 client_th.send(data_entry,strlen(data_entry));
Fo170 6:d68dd410b141 136
Fo170 6:d68dd410b141 137 // METHODE GET
Fo170 6:d68dd410b141 138 // http://api.thingspeak.com/update?api_key=PD6JY2N94E3N0UNO&field1=value
Fo170 6:d68dd410b141 139
Fo170 6:d68dd410b141 140 attempt = 4;
Fo170 6:d68dd410b141 141 }
Fo170 6:d68dd410b141 142 }
Fo170 6:d68dd410b141 143
Fo170 6:d68dd410b141 144 adc_v_moy_old = adc_v_moy;
Fo170 6:d68dd410b141 145 }
Fo170 6:d68dd410b141 146 }
Fo170 6:d68dd410b141 147
Fo170 6:d68dd410b141 148 //------ THING_SPEAK ------------------
Fo170 6:d68dd410b141 149
Fo170 6:d68dd410b141 150
Fo170 0:70a1e939250e 151 char buffer[HTTPD_MAX_REQ_LENGTH+1];
Fo170 0:70a1e939250e 152 char httpHeader[HTTPD_MAX_HDR_LENGTH+1];
Fo170 0:70a1e939250e 153 char fileName[HTTPD_MAX_FNAME_LENGTH+1];
Fo170 0:70a1e939250e 154 char dirName[HTTPD_MAX_DNAME_LENGTH+1];
Fo170 0:70a1e939250e 155 char *uristr;
Fo170 0:70a1e939250e 156 char *eou;
Fo170 0:70a1e939250e 157 char *qrystr;
Fo170 0:70a1e939250e 158
Fo170 0:70a1e939250e 159 FILE *fp;
Fo170 0:70a1e939250e 160 int rdCnt;
Fo170 0:70a1e939250e 161
Fo170 0:70a1e939250e 162 // Initialize a pins to perform analog input and digital output fucntions
Fo170 0:70a1e939250e 163 AnalogIn ain0(A0);
Fo170 0:70a1e939250e 164 AnalogIn ain1(A1);
Fo170 0:70a1e939250e 165 AnalogIn ain2(A2);
Fo170 0:70a1e939250e 166 AnalogIn ain3(A3);
Fo170 0:70a1e939250e 167
Fo170 0:70a1e939250e 168 float a0_f, a1_f, a2_f, a3_f;
Fo170 0:70a1e939250e 169
Fo170 0:70a1e939250e 170 #define __IP_LOCAL__ IP
Fo170 0:70a1e939250e 171 #define __hebergement__ "http://olivier.fournet.free.fr/"
Fo170 0:70a1e939250e 172 #define __Time_between_page_refresh__ "1"
Fo170 0:70a1e939250e 173
Fo170 0:70a1e939250e 174 int refresh = 1; // 1 second refresh
Fo170 0:70a1e939250e 175
Fo170 0:70a1e939250e 176 //------------
Fo170 0:70a1e939250e 177 #define __Time_tic_in_Second__ 0.1
Fo170 0:70a1e939250e 178
Fo170 0:70a1e939250e 179 Ticker time_tic;
Fo170 0:70a1e939250e 180
Fo170 0:70a1e939250e 181 void add_one_tic()
Fo170 0:70a1e939250e 182 {
Fo170 0:70a1e939250e 183 int i;
Fo170 0:70a1e939250e 184
Fo170 0:70a1e939250e 185 Seconds = Seconds + (float)__Time_tic_in_Second__;
Fo170 0:70a1e939250e 186
Fo170 4:0e09a4c5bd22 187 // mesures ADC
Fo170 0:70a1e939250e 188 meas = ain0.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
Fo170 7:c2acbe486cf2 189 // valeur instantanée pour Thing Speak
Fo170 7:c2acbe486cf2 190 adc_v = meas * 3.3;
Fo170 4:0e09a4c5bd22 191 meas = meas * 3300.0; // Change the value to be in the 0 to 3300 range
Fo170 7:c2acbe486cf2 192 // moyenne sur 10 valeurs
Fo170 7:c2acbe486cf2 193 adc_v_moy = 0.1 * (9.0 * adc_v_moy + adc_v);
Fo170 7:c2acbe486cf2 194
Fo170 0:70a1e939250e 195 x_min = x_max = Seconds;
Fo170 4:0e09a4c5bd22 196 y_min = y_max = meas;
Fo170 0:70a1e939250e 197
Fo170 4:0e09a4c5bd22 198 for(i = 1 ; i < NB_SAMPLES ; i++)
Fo170 0:70a1e939250e 199 {
Fo170 0:70a1e939250e 200 time_samples[i-1] = time_samples[i];
Fo170 0:70a1e939250e 201 adc_samples[i-1] = adc_samples[i];
Fo170 0:70a1e939250e 202 if( time_samples[i] < x_min ) x_min = time_samples[i];
Fo170 0:70a1e939250e 203 if( time_samples[i] > x_max ) x_max = time_samples[i];
Fo170 0:70a1e939250e 204 if( adc_samples[i] < y_min ) y_min = adc_samples[i];
Fo170 0:70a1e939250e 205 if( adc_samples[i] > y_max ) y_max = adc_samples[i];
Fo170 0:70a1e939250e 206 }
Fo170 0:70a1e939250e 207
Fo170 4:0e09a4c5bd22 208 adc_samples[NB_SAMPLES-1] = meas;
Fo170 4:0e09a4c5bd22 209 time_samples[NB_SAMPLES-1] = Seconds;
Fo170 0:70a1e939250e 210 }
Fo170 0:70a1e939250e 211 //------------
Fo170 0:70a1e939250e 212
Fo170 0:70a1e939250e 213 Ticker ledTick;
Fo170 0:70a1e939250e 214
Fo170 0:70a1e939250e 215 char *pch;
Fo170 0:70a1e939250e 216 char ext[5];
Fo170 0:70a1e939250e 217 char ext_gif[] = "gif";
Fo170 0:70a1e939250e 218 char ext_jpg[] = "jpg";
Fo170 0:70a1e939250e 219 char ext_png[] = "png";
Fo170 0:70a1e939250e 220 char ext_tiff[] = "tiff";
Fo170 0:70a1e939250e 221 int pos_ext;
Fo170 0:70a1e939250e 222 int extLen;
Fo170 0:70a1e939250e 223
Fo170 0:70a1e939250e 224 void ledTickfunc()
Fo170 0:70a1e939250e 225 {
Fo170 0:70a1e939250e 226 led_r = !led_r;
Fo170 0:70a1e939250e 227 }
Fo170 6:d68dd410b141 228
Fo170 0:70a1e939250e 229 void printf_send_client(const char *str_c)
Fo170 0:70a1e939250e 230 {
Fo170 0:70a1e939250e 231 char http_send[HTTPD_MAX_REQ_LENGTH+1];
Fo170 0:70a1e939250e 232 sprintf(http_send,str_c);
Fo170 0:70a1e939250e 233 client.send(http_send,strlen(http_send));
Fo170 0:70a1e939250e 234 }
Fo170 0:70a1e939250e 235
Fo170 0:70a1e939250e 236 void variables(void)
Fo170 0:70a1e939250e 237 {
Fo170 0:70a1e939250e 238 printf_send_client("<SCRIPT script language=\"javascript\" type=\"text/javascript\">\r\n");
Fo170 0:70a1e939250e 239
Fo170 0:70a1e939250e 240 a0_f = ain0.read()*3.3;
Fo170 0:70a1e939250e 241 sprintf(httpHeader,"A0 = %3.3f;\r\n", a0_f);
Fo170 0:70a1e939250e 242 client.send(httpHeader,strlen(httpHeader));
Fo170 0:70a1e939250e 243
Fo170 0:70a1e939250e 244 a1_f = ain1.read()*3.3;
Fo170 0:70a1e939250e 245 sprintf(httpHeader,"A1 = %3.3f;\r\n", a1_f);
Fo170 0:70a1e939250e 246 client.send(httpHeader,strlen(httpHeader));
Fo170 0:70a1e939250e 247
Fo170 0:70a1e939250e 248 a2_f = ain2.read()*3.3;
Fo170 0:70a1e939250e 249 sprintf(httpHeader,"A2 = %3.3f;\r\n", a2_f);
Fo170 0:70a1e939250e 250 client.send(httpHeader,strlen(httpHeader));
Fo170 0:70a1e939250e 251
Fo170 0:70a1e939250e 252 a3_f = ain3.read()*3.3;
Fo170 0:70a1e939250e 253 sprintf(httpHeader,"A3 = %3.3f;\r\n", a3_f);
Fo170 0:70a1e939250e 254 client.send(httpHeader,strlen(httpHeader));
Fo170 0:70a1e939250e 255
Fo170 4:0e09a4c5bd22 256 printf_send_client("</SCRIPT>\r\n");
Fo170 0:70a1e939250e 257 }
Fo170 0:70a1e939250e 258
Fo170 6:d68dd410b141 259
Fo170 0:70a1e939250e 260 void ETAT(void)
Fo170 0:70a1e939250e 261 {
Fo170 0:70a1e939250e 262 int i;
Fo170 0:70a1e939250e 263
Fo170 0:70a1e939250e 264 // httpHeader
Fo170 0:70a1e939250e 265 printf_send_client("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
Fo170 0:70a1e939250e 266
Fo170 0:70a1e939250e 267 // Début page Web
Fo170 0:70a1e939250e 268 printf_send_client("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\r\n");
Fo170 4:0e09a4c5bd22 269
Fo170 0:70a1e939250e 270 // meta_refresh
Fo170 0:70a1e939250e 271 sprintf(httpHeader,"<meta http-equiv=\"refresh\" content=\"" __Time_between_page_refresh__ ";url=http://%s/\">\r\n", eth.getIPAddress() );
Fo170 0:70a1e939250e 272 //sprintf(httpHeader,"<meta http-equiv=\"refresh\" content=\"1;url=http://%s/\">\r\n", eth.getIPAddress() );
Fo170 0:70a1e939250e 273 //sprintf(httpHeader,"<meta http-equiv=\"refresh\" content=\"%u;url=http://%s/\">\r\n", refresh, eth.getIPAddress() );
Fo170 0:70a1e939250e 274 client.send(httpHeader,strlen(httpHeader));
Fo170 0:70a1e939250e 275
Fo170 0:70a1e939250e 276 printf_send_client("<html><head>\r\n");
Fo170 0:70a1e939250e 277 // title
Fo170 0:70a1e939250e 278 printf_send_client("<title>WIZwiki-W7500 - Flot Examples: Interactivity</title>\r\n");
Fo170 5:d5a3be58b76a 279 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");
Fo170 4:0e09a4c5bd22 280 // JavaScript Interactivity
Fo170 4:0e09a4c5bd22 281 sprintf(httpHeader,"<script language=\"javascript\" type=\"text/javascript\" src=\"" __hebergement__ "electronique/e/WIZwiki-W7500/js/WIZwiki-W7500_Interactivity_init.js\"></script>\r\n");
Fo170 0:70a1e939250e 282 client.send(httpHeader,strlen(httpHeader));
Fo170 4:0e09a4c5bd22 283
Fo170 4:0e09a4c5bd22 284 printf_send_client("<script language=\"javascript\" type=\"text/javascript\">init_WIZwiki_W7500_Interactivity();</script>\r\n");
Fo170 0:70a1e939250e 285
Fo170 0:70a1e939250e 286 // Variables JavaScript
Fo170 0:70a1e939250e 287 printf_send_client("<script language=\"javascript\" type=\"text/javascript\">\r\n");
Fo170 0:70a1e939250e 288 printf_send_client("var color_Y = \"#FF0000\";\r\n");
Fo170 4:0e09a4c5bd22 289 printf_send_client("var label_Y = \"Adc(mV)\";\r\n");
Fo170 0:70a1e939250e 290 // sprintf(httpHeader, "var x_min = -0.5, x_max = 14.5, y_min = -0.5, y_max = 1.5;\r\n"); // TEST
Fo170 0:70a1e939250e 291 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);
Fo170 0:70a1e939250e 292 client.send(httpHeader,strlen(httpHeader));
Fo170 0:70a1e939250e 293
Fo170 0:70a1e939250e 294 // 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
Fo170 0:70a1e939250e 295 // client.send(httpHeader,strlen(httpHeader)); // TEST
Fo170 0:70a1e939250e 296
Fo170 4:0e09a4c5bd22 297 if(Sample > NB_SAMPLES)
Fo170 0:70a1e939250e 298 {
Fo170 0:70a1e939250e 299 printf_send_client("var array_value = [");
Fo170 2:86f52ca432b8 300
Fo170 4:0e09a4c5bd22 301 for(i = 0 ; i < NB_SAMPLES ; i++)
Fo170 0:70a1e939250e 302 {
Fo170 4:0e09a4c5bd22 303 if(i < NB_SAMPLES) sprintf(httpHeader, "[%.1f,%.1f],", time_samples[i], adc_samples[i]);
Fo170 0:70a1e939250e 304 else sprintf(httpHeader, "[%.1f,%.1f]", time_samples[i], adc_samples[i]);
Fo170 0:70a1e939250e 305 client.send(httpHeader,strlen(httpHeader));
Fo170 0:70a1e939250e 306 }
Fo170 0:70a1e939250e 307 printf_send_client("];\r\n");
Fo170 0:70a1e939250e 308 }
Fo170 0:70a1e939250e 309
Fo170 4:0e09a4c5bd22 310 Sample++;
Fo170 0:70a1e939250e 311 printf_send_client("</script>\r\n");
Fo170 0:70a1e939250e 312
Fo170 0:70a1e939250e 313 // <SCRIPT>
Fo170 0:70a1e939250e 314 variables();
Fo170 0:70a1e939250e 315 // <FIN SCRIPT>
Fo170 0:70a1e939250e 316 // Fin Variable JavaScript
Fo170 0:70a1e939250e 317 printf_send_client("</head><body><center>\r\n");
Fo170 0:70a1e939250e 318
Fo170 0:70a1e939250e 319 sprintf(httpHeader,"<h2>WIZwiki-W7500 - mBED</h2> ( Compiled at : %s and %s )<p>\r\n", __DATE__ , __TIME__);
Fo170 0:70a1e939250e 320 client.send(httpHeader,strlen(httpHeader));
Fo170 4:0e09a4c5bd22 321
Fo170 0:70a1e939250e 322 printf_send_client("<p>(<a href=\"http://www.flotcharts.org/flot/examples/interacting/index.html\">Flot Examples: Interactivity</a>)<p>\r\n");
Fo170 0:70a1e939250e 323
Fo170 0:70a1e939250e 324 printf_send_client("ETAT :<p>\r\n");
Fo170 0:70a1e939250e 325
Fo170 0:70a1e939250e 326 sprintf(httpHeader,"IP: %s, MASK: %s, GW: %s<p>\r\n",
Fo170 0:70a1e939250e 327 eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
Fo170 0:70a1e939250e 328 client.send(httpHeader,strlen(httpHeader));
Fo170 0:70a1e939250e 329
Fo170 4:0e09a4c5bd22 330 //sprintf(httpHeader,"&tilde;A0 :<script>document.write(A0);</script>V<br>\r\n");
Fo170 4:0e09a4c5bd22 331 sprintf(httpHeader,"&tilde;A0 : %3.3fV<br>\r\n", a0_f);
Fo170 0:70a1e939250e 332 client.send(httpHeader,strlen(httpHeader));
Fo170 0:70a1e939250e 333
Fo170 4:0e09a4c5bd22 334 //sprintf(httpHeader,"&tilde;A1 :<script>document.write(A1);</script>V<br>\r\n");
Fo170 4:0e09a4c5bd22 335 sprintf(httpHeader,"&tilde;A1 : %3.3fV<br>\r\n", a1_f);
Fo170 0:70a1e939250e 336 client.send(httpHeader,strlen(httpHeader));
Fo170 0:70a1e939250e 337
Fo170 4:0e09a4c5bd22 338 //sprintf(httpHeader,"&tilde;A2 :<script>document.write(A2);</script>V<br>\r\n");
Fo170 4:0e09a4c5bd22 339 sprintf(httpHeader,"&tilde;A2 : %3.3fV<br>\r\n", a2_f);
Fo170 0:70a1e939250e 340 client.send(httpHeader,strlen(httpHeader));
Fo170 0:70a1e939250e 341
Fo170 4:0e09a4c5bd22 342 //sprintf(httpHeader,"&tilde;A3 :<script>document.write(A3);</script>V<p>\r\n");
Fo170 4:0e09a4c5bd22 343 sprintf(httpHeader,"&tilde;A3 : %3.3fV<br>\r\n", a3_f);
Fo170 0:70a1e939250e 344 client.send(httpHeader,strlen(httpHeader));
Fo170 0:70a1e939250e 345
Fo170 4:0e09a4c5bd22 346 sprintf(httpHeader, "Time : %.1f Seconds - Sample : %u<p>\r\n", Seconds, Sample);// diplays the human readable Seconds
Fo170 2:86f52ca432b8 347 client.send(httpHeader,strlen(httpHeader));
Fo170 0:70a1e939250e 348
Fo170 4:0e09a4c5bd22 349 printf_send_client("<p><script language=\"javascript\" type=\"text/javascript\">WIZwiki_W7500_Interactivity();</script><p>\r\n");
Fo170 0:70a1e939250e 350
Fo170 4:0e09a4c5bd22 351 printf_send_client("<p><a href=\"..\">Root</a>\r\n");
Fo170 2:86f52ca432b8 352
Fo170 4:0e09a4c5bd22 353 printf_send_client("</center></body></html>\r\n");
Fo170 0:70a1e939250e 354 }
Fo170 0:70a1e939250e 355
Fo170 0:70a1e939250e 356 //--------------------------------------------
Fo170 6:d68dd410b141 357 void Serial_Interface_init(void)
Fo170 0:70a1e939250e 358 {
Fo170 6:d68dd410b141 359 // Serial Interface eth;
Fo170 0:70a1e939250e 360 // Serial port configuration (valeurs par defaut) : 9600 baud, 8-bit data, no parity, stop bit
Fo170 0:70a1e939250e 361 uart.baud(9600);
Fo170 0:70a1e939250e 362 uart.format(8, SerialBase::None, 1);
Fo170 0:70a1e939250e 363 uart.printf("Initializing\n");
Fo170 6:d68dd410b141 364 wait(1.0);
Fo170 6:d68dd410b141 365 }
Fo170 6:d68dd410b141 366
Fo170 6:d68dd410b141 367 void Check_File_System(void)
Fo170 6:d68dd410b141 368 {
Fo170 6:d68dd410b141 369 // Check File System
Fo170 6:d68dd410b141 370 uart.printf("Checking File System\n");
Fo170 6:d68dd410b141 371 DIR *d = opendir("/sd/");
Fo170 6:d68dd410b141 372 if(d != NULL)
Fo170 6:d68dd410b141 373 {
Fo170 6:d68dd410b141 374 uart.printf("SD Card Present\n");
Fo170 6:d68dd410b141 375 }
Fo170 6:d68dd410b141 376 else
Fo170 6:d68dd410b141 377 {
Fo170 6:d68dd410b141 378 uart.printf("SD Card Root Directory Not Found\n");
Fo170 6:d68dd410b141 379 }
Fo170 6:d68dd410b141 380 wait(1.0);
Fo170 6:d68dd410b141 381 }
Fo170 6:d68dd410b141 382
Fo170 6:d68dd410b141 383 void ethernet_init(void)
Fo170 6:d68dd410b141 384 {
Fo170 6:d68dd410b141 385 // EthernetInterface eth;
Fo170 0:70a1e939250e 386 uart.printf("Initializing Ethernet\n");
Fo170 0:70a1e939250e 387 #if USE_DHCP
Fo170 0:70a1e939250e 388 //eth.init Use DHCP
Fo170 0:70a1e939250e 389 int ret = eth.init((uint8_t*)MAC); // Use DHCP for WIZnetInterface
Fo170 0:70a1e939250e 390 uart.printf("Connecting DHCP\n");
Fo170 0:70a1e939250e 391 #else
Fo170 0:70a1e939250e 392 int ret = eth.init((uint8_t*)MAC,IP,MASK,GATEWAY); //IP,mask,Gateway
Fo170 0:70a1e939250e 393 uart.printf("Connecting (IP,mask,Gateway)\n");
Fo170 0:70a1e939250e 394 #endif
Fo170 0:70a1e939250e 395 wait(1.0);
Fo170 0:70a1e939250e 396 // Check Ethernet Link-Done
Fo170 0:70a1e939250e 397 uart.printf("Check Ethernet Link\r\n");
Fo170 0:70a1e939250e 398
Fo170 0:70a1e939250e 399 if(eth.link() == true)
Fo170 0:70a1e939250e 400 {
Fo170 0:70a1e939250e 401 uart.printf("- Ethernet PHY Link - Done\r\n");
Fo170 0:70a1e939250e 402 COLOR(_RED_);
Fo170 0:70a1e939250e 403 }
Fo170 0:70a1e939250e 404 else
Fo170 0:70a1e939250e 405 {
Fo170 0:70a1e939250e 406 uart.printf("- Ethernet PHY Link - Fail\r\n");
Fo170 0:70a1e939250e 407 COLOR(_BLACK_);
Fo170 0:70a1e939250e 408 }
Fo170 0:70a1e939250e 409 wait(1.0);
Fo170 0:70a1e939250e 410 if(!ret)
Fo170 0:70a1e939250e 411 {
Fo170 0:70a1e939250e 412 uart.printf("Initialized, MAC: %s\r\n", eth.getMACAddress());
Fo170 0:70a1e939250e 413 ret = eth.connect();
Fo170 0:70a1e939250e 414
Fo170 0:70a1e939250e 415 if(!ret)
Fo170 0:70a1e939250e 416 {
Fo170 0:70a1e939250e 417 uart.printf("IP: %s, MASK: %s, GW: %s\r\n",
Fo170 0:70a1e939250e 418 eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
Fo170 0:70a1e939250e 419 COLOR(_CYAN_);
Fo170 0:70a1e939250e 420 }
Fo170 0:70a1e939250e 421 else
Fo170 0:70a1e939250e 422 {
Fo170 0:70a1e939250e 423 uart.printf("Error ethernet.connect() - ret = %d\r\n", ret);
Fo170 0:70a1e939250e 424 COLOR(_BLUE_);
Fo170 0:70a1e939250e 425 exit(0);
Fo170 0:70a1e939250e 426 }
Fo170 0:70a1e939250e 427 }
Fo170 0:70a1e939250e 428 else
Fo170 0:70a1e939250e 429 {
Fo170 0:70a1e939250e 430 uart.printf("Error ethernet.init() - ret = %d\r\n", ret);
Fo170 0:70a1e939250e 431 COLOR(_BLACK_);
Fo170 0:70a1e939250e 432 exit(0);
Fo170 4:0e09a4c5bd22 433 }
Fo170 4:0e09a4c5bd22 434
Fo170 0:70a1e939250e 435 wait(1.0);
Fo170 6:d68dd410b141 436 // TCPSocketServer server;
Fo170 0:70a1e939250e 437 server.bind(HTTPD_SERVER_PORT);
Fo170 0:70a1e939250e 438 server.listen();
Fo170 6:d68dd410b141 439 uart.printf("Server Listening\n");
Fo170 6:d68dd410b141 440 }
Fo170 0:70a1e939250e 441
Fo170 6:d68dd410b141 442 int main(void)
Fo170 6:d68dd410b141 443 {
Fo170 6:d68dd410b141 444 // initialisation des variables
Fo170 6:d68dd410b141 445 int i;
Fo170 6:d68dd410b141 446
Fo170 6:d68dd410b141 447 for(i = 0 ; i < NB_SAMPLES ; i++)
Fo170 6:d68dd410b141 448 {
Fo170 6:d68dd410b141 449 time_samples[i] = 0;
Fo170 6:d68dd410b141 450 adc_samples[i] = 0.0;
Fo170 6:d68dd410b141 451 }
Fo170 6:d68dd410b141 452
Fo170 6:d68dd410b141 453 adc_v_moy = ain0.read()*3.3;
Fo170 6:d68dd410b141 454 adc_v_moy_old = adc_v_moy;
Fo170 6:d68dd410b141 455 //------------------
Fo170 6:d68dd410b141 456 Serial_Interface_init();
Fo170 6:d68dd410b141 457
Fo170 6:d68dd410b141 458 // Init the ticker with the address of the function (add_one_second) to be attached and the interval (1000 ms)
Fo170 6:d68dd410b141 459 time_tic.attach(&add_one_tic, __Time_tic_in_Second__);
Fo170 6:d68dd410b141 460 //--------------
Fo170 6:d68dd410b141 461 ledTick.attach(&ledTickfunc,0.5);
Fo170 6:d68dd410b141 462 //---------------
Fo170 6:d68dd410b141 463 Check_File_System();
Fo170 6:d68dd410b141 464 ethernet_init();
Fo170 6:d68dd410b141 465
Fo170 0:70a1e939250e 466 while(true)
Fo170 0:70a1e939250e 467 {
Fo170 0:70a1e939250e 468 uart.printf("\nWait for new connection...\r\n");
Fo170 0:70a1e939250e 469 server.accept(client);
Fo170 0:70a1e939250e 470 client.set_blocking(false, 1500); // Timeout after (1.5)s
Fo170 0:70a1e939250e 471
Fo170 0:70a1e939250e 472 uart.printf("Connection from: %s\r\n", client.get_address());
Fo170 0:70a1e939250e 473 while(true)
Fo170 0:70a1e939250e 474 {
Fo170 0:70a1e939250e 475 //led_g = LED_ON;
Fo170 0:70a1e939250e 476 COLOR(_GREEN_);
Fo170 0:70a1e939250e 477 int n = client.receive(buffer, sizeof(buffer));
Fo170 0:70a1e939250e 478 if(n <= 0) break;
Fo170 0:70a1e939250e 479 uart.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer);
Fo170 0:70a1e939250e 480 if(n >= 1024)
Fo170 0:70a1e939250e 481 {
Fo170 0:70a1e939250e 482 sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
Fo170 0:70a1e939250e 483 client.send(httpHeader,strlen(httpHeader));
Fo170 0:70a1e939250e 484 client.send(buffer,n);
Fo170 0:70a1e939250e 485 break;
Fo170 0:70a1e939250e 486 }
Fo170 0:70a1e939250e 487 else
Fo170 0:70a1e939250e 488 {
Fo170 0:70a1e939250e 489 buffer[n]=0;
Fo170 0:70a1e939250e 490 }
Fo170 0:70a1e939250e 491 if(!strncmp(buffer, "GET ", 4))
Fo170 0:70a1e939250e 492 {
Fo170 0:70a1e939250e 493 uristr = buffer + 4;
Fo170 0:70a1e939250e 494 eou = strstr(uristr, " ");
Fo170 0:70a1e939250e 495 if(eou == NULL)
Fo170 0:70a1e939250e 496 {
Fo170 0:70a1e939250e 497 sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
Fo170 0:70a1e939250e 498 client.send(httpHeader,strlen(httpHeader));
Fo170 0:70a1e939250e 499 client.send(buffer,n);
Fo170 0:70a1e939250e 500 }
Fo170 0:70a1e939250e 501 else
Fo170 0:70a1e939250e 502 {
Fo170 0:70a1e939250e 503 *eou = 0;
Fo170 0:70a1e939250e 504 //get_file(uristr);
Fo170 0:70a1e939250e 505 ETAT();
Fo170 6:d68dd410b141 506 thingspeak_update();
Fo170 0:70a1e939250e 507 }
Fo170 0:70a1e939250e 508 }
Fo170 0:70a1e939250e 509 }
Fo170 0:70a1e939250e 510 //led_g = LED_OFF;
Fo170 0:70a1e939250e 511 COLOR(_BLACK_);
Fo170 0:70a1e939250e 512 client.close();
Fo170 6:d68dd410b141 513 client_th.close();
Fo170 6:d68dd410b141 514 //
Fo170 0:70a1e939250e 515 }
Fo170 0:70a1e939250e 516 }