Versão sem FFT e aquisição por DMA. 256 amostras.

Dependencies:   EthernetInterface NTPClient mbed-rtos mbed

Committer:
rebonatto
Date:
Tue Jan 05 11:45:44 2016 +0000
Revision:
0:e57bc370d339
Vers?o est?vel sem calculo de FFT. Aquisi??o por DMA. Usa 256 amostras.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rebonatto 0:e57bc370d339 1 /*
rebonatto 0:e57bc370d339 2 * Http_post.cpp
rebonatto 0:e57bc370d339 3 *
rebonatto 0:e57bc370d339 4 * Created on:
rebonatto 0:e57bc370d339 5 * Author:
rebonatto 0:e57bc370d339 6 */
rebonatto 0:e57bc370d339 7 #include "Http_post.h"
rebonatto 0:e57bc370d339 8 #include "Pmed_reset.h"
rebonatto 0:e57bc370d339 9
rebonatto 0:e57bc370d339 10 void HttpPost::HttpPost_Thread(void const *arg)
rebonatto 0:e57bc370d339 11 {
rebonatto 0:e57bc370d339 12 printf("HTTP POST Thread starting...\r\n");
rebonatto 0:e57bc370d339 13
rebonatto 0:e57bc370d339 14 //inicializar socket
rebonatto 0:e57bc370d339 15 // TCPSocketConnection sock;
rebonatto 0:e57bc370d339 16
rebonatto 0:e57bc370d339 17 CaptureMailbox& mbox = EventDetector::GetMailbox();
rebonatto 0:e57bc370d339 18
rebonatto 0:e57bc370d339 19 osEvent evt;
rebonatto 0:e57bc370d339 20 //printf("aqui\n");
rebonatto 0:e57bc370d339 21 while(1)
rebonatto 0:e57bc370d339 22 {
rebonatto 0:e57bc370d339 23 TCPSocketConnection sock;
rebonatto 0:e57bc370d339 24 //printf("Esperando Evento\n");
rebonatto 0:e57bc370d339 25 evt = mbox.get();
rebonatto 0:e57bc370d339 26
rebonatto 0:e57bc370d339 27 if(evt.status == osEventMail)
rebonatto 0:e57bc370d339 28 {
rebonatto 0:e57bc370d339 29 //printf("Recebido osEventMail...\n ");
rebonatto 0:e57bc370d339 30 CaptureEvent* cap = (CaptureEvent*)evt.value.p;
rebonatto 0:e57bc370d339 31 //printf("Pegou evento\n");
rebonatto 0:e57bc370d339 32 DoPost(sock,Settings::get_ServerUrl(),cap);
rebonatto 0:e57bc370d339 33 mbox.free(cap);
rebonatto 0:e57bc370d339 34 //printf("Enviado e Liberado Mbox ...\n ");
rebonatto 0:e57bc370d339 35 //sock.reset_address();
rebonatto 0:e57bc370d339 36 }
rebonatto 0:e57bc370d339 37 //Thread::yield();
rebonatto 0:e57bc370d339 38 }
rebonatto 0:e57bc370d339 39 }
rebonatto 0:e57bc370d339 40
rebonatto 0:e57bc370d339 41
rebonatto 0:e57bc370d339 42
rebonatto 0:e57bc370d339 43 void HttpPost::DoPost(TCPSocketConnection sock, char *host, CaptureEvent* dados){
rebonatto 0:e57bc370d339 44 char *http_cmd;
rebonatto 0:e57bc370d339 45 int escritos, r=-1, i;
rebonatto 0:e57bc370d339 46
rebonatto 0:e57bc370d339 47 http_cmd = (char *) malloc(1800);
rebonatto 0:e57bc370d339 48
rebonatto 0:e57bc370d339 49 if (http_cmd == NULL)
rebonatto 0:e57bc370d339 50 printf("Sem memoria\n");
rebonatto 0:e57bc370d339 51
rebonatto 0:e57bc370d339 52 memset(http_cmd, 0, 1800);
rebonatto 0:e57bc370d339 53
rebonatto 0:e57bc370d339 54 PreparePost( dados,http_cmd );
rebonatto 0:e57bc370d339 55
rebonatto 0:e57bc370d339 56 //printf("Tamanho do comando %d\n", strlen(http_cmd));
rebonatto 0:e57bc370d339 57 //printf("Comando: /* %s */\n", http_cmd);
rebonatto 0:e57bc370d339 58
rebonatto 0:e57bc370d339 59
rebonatto 0:e57bc370d339 60
rebonatto 0:e57bc370d339 61 for(i=0; i < MAXTRIES; i++){
rebonatto 0:e57bc370d339 62 r = sock.connect(host, 80);
rebonatto 0:e57bc370d339 63 if (r < 0) {
rebonatto 0:e57bc370d339 64 printf("Error: Unable to connect to (%s) on port (%d) Try %d\n", host, 80, i);
rebonatto 0:e57bc370d339 65 Thread::wait(Settings::get_DelayTry());
rebonatto 0:e57bc370d339 66 }
rebonatto 0:e57bc370d339 67 else
rebonatto 0:e57bc370d339 68 break;
rebonatto 0:e57bc370d339 69 }
rebonatto 0:e57bc370d339 70 if (r == 0){
rebonatto 0:e57bc370d339 71 for(i=0; i < MAXTRIES; i++){
rebonatto 0:e57bc370d339 72 escritos = sock.send_all(http_cmd, strlen(http_cmd));
rebonatto 0:e57bc370d339 73 if(escritos != strlen(http_cmd)){
rebonatto 0:e57bc370d339 74 printf("Erro ao gravar no socket HTTP!! Escritos %d\t Tam %d Try %d\n", escritos, strlen(http_cmd), i);
rebonatto 0:e57bc370d339 75 Thread::wait(Settings::get_DelayTry());
rebonatto 0:e57bc370d339 76 }
rebonatto 0:e57bc370d339 77 else
rebonatto 0:e57bc370d339 78 break;
rebonatto 0:e57bc370d339 79 }
rebonatto 0:e57bc370d339 80 if ( i != MAXTRIES )
rebonatto 0:e57bc370d339 81 Thread::wait(Settings::get_DelaySend());
rebonatto 0:e57bc370d339 82 else{
rebonatto 0:e57bc370d339 83 //printf("Reset\n");
rebonatto 0:e57bc370d339 84
rebonatto 0:e57bc370d339 85 Pmed_reset(PMEDLOG_HTTP_CONNECT);
rebonatto 0:e57bc370d339 86 }
rebonatto 0:e57bc370d339 87
rebonatto 0:e57bc370d339 88 //Codigo para buscar o retorno do servidor HTTP
rebonatto 0:e57bc370d339 89 /*
rebonatto 0:e57bc370d339 90 printf("Vai pegar retorno\n");
rebonatto 0:e57bc370d339 91
rebonatto 0:e57bc370d339 92 char buffer[300];
rebonatto 0:e57bc370d339 93 int ret;
rebonatto 0:e57bc370d339 94 while (true) {
rebonatto 0:e57bc370d339 95 ret = sock.receive(buffer, sizeof(buffer)-1);
rebonatto 0:e57bc370d339 96 if (ret <= 0)
rebonatto 0:e57bc370d339 97 break;
rebonatto 0:e57bc370d339 98 buffer[ret] = '\0';
rebonatto 0:e57bc370d339 99 printf("Received %d chars from server:\n%s\n", ret, buffer);
rebonatto 0:e57bc370d339 100 }
rebonatto 0:e57bc370d339 101 */
rebonatto 0:e57bc370d339 102 }
rebonatto 0:e57bc370d339 103 else{
rebonatto 0:e57bc370d339 104 printf("Reset\n");
rebonatto 0:e57bc370d339 105
rebonatto 0:e57bc370d339 106 Pmed_reset(PMEDLOG_HTTP_SEND);
rebonatto 0:e57bc370d339 107 }
rebonatto 0:e57bc370d339 108
rebonatto 0:e57bc370d339 109 sock.close();
rebonatto 0:e57bc370d339 110 free(http_cmd);
rebonatto 0:e57bc370d339 111
rebonatto 0:e57bc370d339 112 }
rebonatto 0:e57bc370d339 113
rebonatto 0:e57bc370d339 114 void HttpPost::PreparePost(CaptureEvent* dados,char *strfinal){
rebonatto 0:e57bc370d339 115 char aux[20];
rebonatto 0:e57bc370d339 116 int i, ContentLen = 1662;
rebonatto 0:e57bc370d339 117
rebonatto 0:e57bc370d339 118 const char *header1 = "POST /capturesfft.php HTTP/1.1\r\n";
rebonatto 0:e57bc370d339 119 //"Host: 192.168.1.26\r\n"
rebonatto 0:e57bc370d339 120 //"Content-Length: "
rebonatto 0:e57bc370d339 121
rebonatto 0:e57bc370d339 122 const char *header2 = "\r\n"
rebonatto 0:e57bc370d339 123 "Content-Type: application/x-www-form-urlencoded\r\n"
rebonatto 0:e57bc370d339 124 "\r\n";
rebonatto 0:e57bc370d339 125
rebonatto 0:e57bc370d339 126 //str = (char *) malloc(450);
rebonatto 0:e57bc370d339 127 //strfinal = (char *) malloc(450);
rebonatto 0:e57bc370d339 128 //memset(str,0,400);
rebonatto 0:e57bc370d339 129 //memset(strfinal,0,500);
rebonatto 0:e57bc370d339 130
rebonatto 0:e57bc370d339 131 strcat(strfinal, header1);
rebonatto 0:e57bc370d339 132 strcat(strfinal, "Host: ");
rebonatto 0:e57bc370d339 133 strcat(strfinal, Settings::get_ServerUrl() );
rebonatto 0:e57bc370d339 134 strcat(strfinal, "\r\n");
rebonatto 0:e57bc370d339 135
rebonatto 0:e57bc370d339 136 sprintf(aux,"%d",ContentLen);
rebonatto 0:e57bc370d339 137
rebonatto 0:e57bc370d339 138 strcat(strfinal, "Content-Length: ");
rebonatto 0:e57bc370d339 139 strcat(strfinal, aux);
rebonatto 0:e57bc370d339 140 strcat(strfinal, header2);
rebonatto 0:e57bc370d339 141
rebonatto 0:e57bc370d339 142 sprintf(aux,"TYPE=%02d",dados->get_Type());
rebonatto 0:e57bc370d339 143 strcat(strfinal, aux);
rebonatto 0:e57bc370d339 144 //ContentLen += 7;
rebonatto 0:e57bc370d339 145
rebonatto 0:e57bc370d339 146 sprintf(aux,"&OUTLET=%03d",dados->get_OutletNumber());
rebonatto 0:e57bc370d339 147 strcat(strfinal, aux);
rebonatto 0:e57bc370d339 148 //ContentLen += 11;
rebonatto 0:e57bc370d339 149
rebonatto 0:e57bc370d339 150 sprintf(aux,"&RFID=%s", dados->get_RFID());
rebonatto 0:e57bc370d339 151 strcat(strfinal,aux);
rebonatto 0:e57bc370d339 152 //ContentLen += 14;
rebonatto 0:e57bc370d339 153
rebonatto 0:e57bc370d339 154 sprintf(aux,"&OFFSET=%04d",dados->get_Offset());
rebonatto 0:e57bc370d339 155 strcat(strfinal,aux);
rebonatto 0:e57bc370d339 156 //ContentLen += 12;
rebonatto 0:e57bc370d339 157
rebonatto 0:e57bc370d339 158 float f = dados->get_Gain();
rebonatto 0:e57bc370d339 159 sprintf(aux,"&GAIN=%08X", *(unsigned int*)&f);
rebonatto 0:e57bc370d339 160 strcat(strfinal,aux);
rebonatto 0:e57bc370d339 161 //ContentLen += 14;
rebonatto 0:e57bc370d339 162
rebonatto 0:e57bc370d339 163 f = dados->get_RMSValue();
rebonatto 0:e57bc370d339 164 sprintf(aux,"&RMS=%08X",*(unsigned int*)&f);
rebonatto 0:e57bc370d339 165 strcat(strfinal,aux);
rebonatto 0:e57bc370d339 166 //ContentLen += 13;
rebonatto 0:e57bc370d339 167
rebonatto 0:e57bc370d339 168 f = dados->get_MeanValue();
rebonatto 0:e57bc370d339 169 sprintf(aux,"&MV=%08X",*(unsigned int*)&f);
rebonatto 0:e57bc370d339 170 strcat(strfinal,aux);
rebonatto 0:e57bc370d339 171 //ContentLen += 12;
rebonatto 0:e57bc370d339 172
rebonatto 0:e57bc370d339 173 sprintf(aux,"&UNDER=%04d",dados->get_Under());
rebonatto 0:e57bc370d339 174 strcat(strfinal,aux);
rebonatto 0:e57bc370d339 175 //ContentLen += 11;
rebonatto 0:e57bc370d339 176
rebonatto 0:e57bc370d339 177 sprintf(aux,"&OVER=%04d",dados->get_Over());
rebonatto 0:e57bc370d339 178 strcat(strfinal,aux);
rebonatto 0:e57bc370d339 179 //ContentLen += 10;
rebonatto 0:e57bc370d339 180
rebonatto 0:e57bc370d339 181 sprintf(aux,"&DURATION=%04d",dados->get_Duration());
rebonatto 0:e57bc370d339 182 strcat(strfinal,aux);
rebonatto 0:e57bc370d339 183 //ContentLen += 14;
rebonatto 0:e57bc370d339 184
rebonatto 0:e57bc370d339 185 char s[7];
rebonatto 0:e57bc370d339 186 strcat(strfinal,"&SAMPLES=");
rebonatto 0:e57bc370d339 187
rebonatto 0:e57bc370d339 188 for(i=0;i<NUMBER_OF_SAMPLES;i++)
rebonatto 0:e57bc370d339 189 {
rebonatto 0:e57bc370d339 190 sprintf(s,"%05d",dados->get_SampleValue(i));
rebonatto 0:e57bc370d339 191
rebonatto 0:e57bc370d339 192 strcat(strfinal,s);
rebonatto 0:e57bc370d339 193 if (i < (NUMBER_OF_SAMPLES - 1))
rebonatto 0:e57bc370d339 194 strcat(strfinal, ";");
rebonatto 0:e57bc370d339 195 }
rebonatto 0:e57bc370d339 196
rebonatto 0:e57bc370d339 197 strcat(strfinal,"\r\n");
rebonatto 0:e57bc370d339 198
rebonatto 0:e57bc370d339 199 //printf("Request=[%s]\n",strfinal);
rebonatto 0:e57bc370d339 200 //printf("Tamanho STRFINAL %d\n", strlen(strfinal));
rebonatto 0:e57bc370d339 201 }