Versão estável sem DMA e FFT. 128 amostras.

Dependencies:   EthernetInterface NTPClient mbed-rtos mbed

Committer:
rebonatto
Date:
Tue Jan 05 11:47:35 2016 +0000
Revision:
0:fac116e94d44
Vers?o est?vel sem DMA e FFT. 128 amostras.

Who changed what in which revision?

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