![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Versão do protegemed que calcula o tempo em ms da fuga, calcula o numero de onverflow (valores muito baixo) e underflow (valores muito altos). Além disso, calcula um valor médio a partir dos valores capturados e não apenas pela fft.
Dependencies: EthernetInterface mbed-rtos mbed
Codes/Http_post.cpp@2:86c3cb25577b, 2014-07-21 (annotated)
- Committer:
- rebonatto
- Date:
- Mon Jul 21 00:58:34 2014 +0000
- Revision:
- 2:86c3cb25577b
- Parent:
- 0:c64e1194230b
Problemas com objeto para aquisi??o da rfid (serial)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rebonatto | 0:c64e1194230b | 1 | /* |
rebonatto | 0:c64e1194230b | 2 | * Http_post.cpp |
rebonatto | 0:c64e1194230b | 3 | * |
rebonatto | 0:c64e1194230b | 4 | * Created on: |
rebonatto | 0:c64e1194230b | 5 | * Author: |
rebonatto | 0:c64e1194230b | 6 | */ |
rebonatto | 0:c64e1194230b | 7 | #include "Http_post.h" |
rebonatto | 0:c64e1194230b | 8 | |
rebonatto | 0:c64e1194230b | 9 | extern "C" void mbed_reset(); |
rebonatto | 0:c64e1194230b | 10 | |
rebonatto | 0:c64e1194230b | 11 | void HttpPost::HttpPost_Thread(void const *arg) |
rebonatto | 0:c64e1194230b | 12 | { |
rebonatto | 0:c64e1194230b | 13 | printf("HTTP POST Thread starting...\r\n"); |
rebonatto | 0:c64e1194230b | 14 | |
rebonatto | 0:c64e1194230b | 15 | //inicializar socket |
rebonatto | 0:c64e1194230b | 16 | // TCPSocketConnection sock; |
rebonatto | 0:c64e1194230b | 17 | |
rebonatto | 0:c64e1194230b | 18 | CaptureMailbox& mbox = EventDetector::GetMailbox(); |
rebonatto | 0:c64e1194230b | 19 | |
rebonatto | 0:c64e1194230b | 20 | osEvent evt; |
rebonatto | 0:c64e1194230b | 21 | //printf("aqui\n"); |
rebonatto | 0:c64e1194230b | 22 | while(1) |
rebonatto | 0:c64e1194230b | 23 | { |
rebonatto | 0:c64e1194230b | 24 | TCPSocketConnection sock; |
rebonatto | 0:c64e1194230b | 25 | //printf("Esperando Evento\n"); |
rebonatto | 0:c64e1194230b | 26 | evt = mbox.get(); |
rebonatto | 0:c64e1194230b | 27 | |
rebonatto | 0:c64e1194230b | 28 | if(evt.status == osEventMail) |
rebonatto | 0:c64e1194230b | 29 | { |
rebonatto | 0:c64e1194230b | 30 | //printf("Recebido osEventMail...\n "); |
rebonatto | 0:c64e1194230b | 31 | CaptureEvent* cap = (CaptureEvent*)evt.value.p; |
rebonatto | 0:c64e1194230b | 32 | DoPost(sock,Settings::get_ServerUrl(),cap); |
rebonatto | 0:c64e1194230b | 33 | mbox.free(cap); |
rebonatto | 0:c64e1194230b | 34 | //printf("Enviado e Liberado Mbox ...\n "); |
rebonatto | 0:c64e1194230b | 35 | //sock.reset_address(); |
rebonatto | 0:c64e1194230b | 36 | } |
rebonatto | 0:c64e1194230b | 37 | //Thread::yield(); |
rebonatto | 0:c64e1194230b | 38 | } |
rebonatto | 0:c64e1194230b | 39 | } |
rebonatto | 0:c64e1194230b | 40 | |
rebonatto | 0:c64e1194230b | 41 | |
rebonatto | 0:c64e1194230b | 42 | |
rebonatto | 0:c64e1194230b | 43 | void HttpPost::DoPost(TCPSocketConnection sock, char *host, CaptureEvent* dados){ |
rebonatto | 0:c64e1194230b | 44 | char http_cmd[600]; |
rebonatto | 0:c64e1194230b | 45 | int escritos, r=-1, i; |
rebonatto | 0:c64e1194230b | 46 | FILE *f; |
rebonatto | 0:c64e1194230b | 47 | |
rebonatto | 0:c64e1194230b | 48 | //Timer t; |
rebonatto | 0:c64e1194230b | 49 | //t.start(); |
rebonatto | 0:c64e1194230b | 50 | |
rebonatto | 0:c64e1194230b | 51 | //printf("HTTP Socket %s:%d\n", host, sock.get_port()); |
rebonatto | 0:c64e1194230b | 52 | //printf("Antes Connect\n"); |
rebonatto | 0:c64e1194230b | 53 | //rs= sock.connect(host, 80); |
rebonatto | 0:c64e1194230b | 54 | //printf("%d\n", rs); |
rebonatto | 0:c64e1194230b | 55 | //printf("Depois Connect "); |
rebonatto | 0:c64e1194230b | 56 | |
rebonatto | 0:c64e1194230b | 57 | //t.stop(); |
rebonatto | 0:c64e1194230b | 58 | //printf("HHTP: The time taken in connection was %d useconds\n", t.read_us()); |
rebonatto | 0:c64e1194230b | 59 | |
rebonatto | 0:c64e1194230b | 60 | //http_cmd = (char *) malloc(500); |
rebonatto | 0:c64e1194230b | 61 | memset(http_cmd, 0, 600); |
rebonatto | 0:c64e1194230b | 62 | PreparePost( dados,http_cmd ); |
rebonatto | 0:c64e1194230b | 63 | //printf("Fuga\n"); |
rebonatto | 0:c64e1194230b | 64 | //printf("Tamanho do comando %d\n", strlen(http_cmd)); |
rebonatto | 0:c64e1194230b | 65 | //printf("Comando: /* %s */\n", http_cmd); |
rebonatto | 0:c64e1194230b | 66 | |
rebonatto | 0:c64e1194230b | 67 | //http_cmd = prepare_POST( dados ); |
rebonatto | 0:c64e1194230b | 68 | |
rebonatto | 0:c64e1194230b | 69 | //printf("Tamanho comando %d\n", strlen(http_cmd)); |
rebonatto | 0:c64e1194230b | 70 | //printf("Request %d\n [%s]\n", Settings::get_MaxTries(), http_cmd); |
rebonatto | 0:c64e1194230b | 71 | |
rebonatto | 0:c64e1194230b | 72 | |
rebonatto | 0:c64e1194230b | 73 | for(i=0; i < Settings::get_MaxTries(); i++){ |
rebonatto | 0:c64e1194230b | 74 | r = sock.connect(host, 80); |
rebonatto | 0:c64e1194230b | 75 | if (r < 0) { |
rebonatto | 0:c64e1194230b | 76 | printf("Error: Unable to connect to (%s) on port (%d) Try %d\n", host, 80, i); |
rebonatto | 0:c64e1194230b | 77 | Thread::wait(Settings::get_DelayTry()); |
rebonatto | 0:c64e1194230b | 78 | } |
rebonatto | 0:c64e1194230b | 79 | else |
rebonatto | 0:c64e1194230b | 80 | break; |
rebonatto | 0:c64e1194230b | 81 | } |
rebonatto | 0:c64e1194230b | 82 | if (r == 0){ |
rebonatto | 0:c64e1194230b | 83 | for(i=0; i < Settings::get_MaxTries(); i++){ |
rebonatto | 0:c64e1194230b | 84 | escritos = sock.send_all(http_cmd, strlen(http_cmd)); |
rebonatto | 0:c64e1194230b | 85 | if(escritos != strlen(http_cmd)){ |
rebonatto | 0:c64e1194230b | 86 | printf("Erro ao gravar no socket HTTP!! Escritos %d\t Tam %d Try %d\n", escritos, strlen(http_cmd), i); |
rebonatto | 0:c64e1194230b | 87 | Thread::wait(Settings::get_DelayTry()); |
rebonatto | 0:c64e1194230b | 88 | } |
rebonatto | 0:c64e1194230b | 89 | else |
rebonatto | 0:c64e1194230b | 90 | break; |
rebonatto | 0:c64e1194230b | 91 | } |
rebonatto | 0:c64e1194230b | 92 | if (i != Settings::get_MaxTries() ) |
rebonatto | 0:c64e1194230b | 93 | Thread::wait(Settings::get_DelaySend()); |
rebonatto | 0:c64e1194230b | 94 | else{ |
rebonatto | 0:c64e1194230b | 95 | //printf("Reset\n"); |
rebonatto | 0:c64e1194230b | 96 | f = fopen(FILENAMERESET, "a"); |
rebonatto | 0:c64e1194230b | 97 | if (f == NULL) |
rebonatto | 0:c64e1194230b | 98 | f = fopen(FILENAMERESET, "w"); |
rebonatto | 0:c64e1194230b | 99 | fprintf(f, "Reset - Connect\n"); |
rebonatto | 0:c64e1194230b | 100 | fclose(f); |
rebonatto | 0:c64e1194230b | 101 | |
rebonatto | 0:c64e1194230b | 102 | mbed_reset(); |
rebonatto | 0:c64e1194230b | 103 | } |
rebonatto | 0:c64e1194230b | 104 | |
rebonatto | 0:c64e1194230b | 105 | //Codigo para buscar o retorno do servidor HTTP |
rebonatto | 0:c64e1194230b | 106 | /* |
rebonatto | 0:c64e1194230b | 107 | printf("Vai pegar retorno\n"); |
rebonatto | 0:c64e1194230b | 108 | |
rebonatto | 0:c64e1194230b | 109 | char buffer[300]; |
rebonatto | 0:c64e1194230b | 110 | int ret; |
rebonatto | 0:c64e1194230b | 111 | while (true) { |
rebonatto | 0:c64e1194230b | 112 | ret = sock.receive(buffer, sizeof(buffer)-1); |
rebonatto | 0:c64e1194230b | 113 | if (ret <= 0) |
rebonatto | 0:c64e1194230b | 114 | break; |
rebonatto | 0:c64e1194230b | 115 | buffer[ret] = '\0'; |
rebonatto | 0:c64e1194230b | 116 | printf("Received %d chars from server:\n%s\n", ret, buffer); |
rebonatto | 0:c64e1194230b | 117 | } |
rebonatto | 0:c64e1194230b | 118 | */ |
rebonatto | 0:c64e1194230b | 119 | } |
rebonatto | 0:c64e1194230b | 120 | else{ |
rebonatto | 0:c64e1194230b | 121 | printf("Reset\n"); |
rebonatto | 0:c64e1194230b | 122 | f = fopen(FILENAMERESET, "a"); |
rebonatto | 0:c64e1194230b | 123 | if (f == NULL) |
rebonatto | 0:c64e1194230b | 124 | f = fopen(FILENAMERESET, "w"); |
rebonatto | 0:c64e1194230b | 125 | fprintf(f, "Reset - Send\n"); |
rebonatto | 0:c64e1194230b | 126 | fclose(f); |
rebonatto | 0:c64e1194230b | 127 | |
rebonatto | 0:c64e1194230b | 128 | mbed_reset(); |
rebonatto | 0:c64e1194230b | 129 | } |
rebonatto | 0:c64e1194230b | 130 | |
rebonatto | 0:c64e1194230b | 131 | sock.close(); |
rebonatto | 0:c64e1194230b | 132 | } |
rebonatto | 0:c64e1194230b | 133 | |
rebonatto | 0:c64e1194230b | 134 | void HttpPost::PreparePost(CaptureEvent* dados,char *strfinal){ |
rebonatto | 0:c64e1194230b | 135 | char str[400]; |
rebonatto | 0:c64e1194230b | 136 | char aux[NUMBER_OF_HARMONICS]; |
rebonatto | 0:c64e1194230b | 137 | int i; |
rebonatto | 0:c64e1194230b | 138 | |
rebonatto | 0:c64e1194230b | 139 | const char *header1 = "POST /capture.php HTTP/1.1\r\n"; |
rebonatto | 0:c64e1194230b | 140 | //"Host: 192.168.1.26\r\n" |
rebonatto | 0:c64e1194230b | 141 | //"Content-Length: " |
rebonatto | 0:c64e1194230b | 142 | |
rebonatto | 0:c64e1194230b | 143 | const char *header2 = "\r\n" |
rebonatto | 0:c64e1194230b | 144 | "Content-Type: application/x-www-form-urlencoded\r\n" |
rebonatto | 0:c64e1194230b | 145 | "\r\n"; |
rebonatto | 0:c64e1194230b | 146 | |
rebonatto | 0:c64e1194230b | 147 | //str = (char *) malloc(450); |
rebonatto | 0:c64e1194230b | 148 | //strfinal = (char *) malloc(450); |
rebonatto | 0:c64e1194230b | 149 | memset(str,0,400); |
rebonatto | 0:c64e1194230b | 150 | memset(strfinal,0,500); |
rebonatto | 0:c64e1194230b | 151 | |
rebonatto | 0:c64e1194230b | 152 | strcat(strfinal, header1); |
rebonatto | 0:c64e1194230b | 153 | strcat(strfinal, "Host: "); |
rebonatto | 0:c64e1194230b | 154 | strcat(strfinal, Settings::get_ServerUrl() ); |
rebonatto | 0:c64e1194230b | 155 | strcat(strfinal, "\r\n"); |
rebonatto | 0:c64e1194230b | 156 | |
rebonatto | 0:c64e1194230b | 157 | sprintf(aux,"TYPE=0%d",dados->get_Type()); |
rebonatto | 0:c64e1194230b | 158 | strcat(str, aux); |
rebonatto | 0:c64e1194230b | 159 | |
rebonatto | 0:c64e1194230b | 160 | sprintf(aux,"&OUTLET=%02d",dados->get_OutletNumber()); |
rebonatto | 0:c64e1194230b | 161 | strcat(str, aux); |
rebonatto | 0:c64e1194230b | 162 | |
rebonatto | 0:c64e1194230b | 163 | sprintf(aux,"&RFID=%s", dados->get_RFID()); |
rebonatto | 0:c64e1194230b | 164 | strcat(str,aux); |
rebonatto | 0:c64e1194230b | 165 | |
rebonatto | 0:c64e1194230b | 166 | sprintf(aux,"&OFFSET=%04d",dados->get_Offset()); |
rebonatto | 0:c64e1194230b | 167 | strcat(str,aux); |
rebonatto | 0:c64e1194230b | 168 | |
rebonatto | 0:c64e1194230b | 169 | float f = dados->get_Gain(); |
rebonatto | 0:c64e1194230b | 170 | sprintf(aux,"&GAIN=%08X", *(unsigned int*)&f); |
rebonatto | 0:c64e1194230b | 171 | strcat(str,aux); |
rebonatto | 0:c64e1194230b | 172 | |
rebonatto | 0:c64e1194230b | 173 | f = dados->get_RMSValue(); |
rebonatto | 0:c64e1194230b | 174 | sprintf(aux,"&RMS=%08X",*(unsigned int*)&f); |
rebonatto | 0:c64e1194230b | 175 | strcat(str,aux); |
rebonatto | 0:c64e1194230b | 176 | |
rebonatto | 0:c64e1194230b | 177 | f = dados->get_MeanValue(); |
rebonatto | 0:c64e1194230b | 178 | sprintf(aux,"&MV=%08X",*(unsigned int*)&f); |
rebonatto | 0:c64e1194230b | 179 | strcat(str,aux); |
rebonatto | 0:c64e1194230b | 180 | |
rebonatto | 0:c64e1194230b | 181 | /* Adicionados para alteracao */ |
rebonatto | 0:c64e1194230b | 182 | //printf("MV %f MV2 %f\n", dados->get_MeanValue(), dados->get_MV2()); |
rebonatto | 0:c64e1194230b | 183 | f = dados->get_MV2(); |
rebonatto | 0:c64e1194230b | 184 | sprintf(aux,"&MV2=%08X",*(unsigned int*)&f); |
rebonatto | 0:c64e1194230b | 185 | strcat(str,aux); |
rebonatto | 0:c64e1194230b | 186 | |
rebonatto | 0:c64e1194230b | 187 | sprintf(aux,"&UNDER=%04d",dados->get_Under()); |
rebonatto | 0:c64e1194230b | 188 | strcat(str,aux); |
rebonatto | 0:c64e1194230b | 189 | |
rebonatto | 0:c64e1194230b | 190 | sprintf(aux,"&OVER=%04d",dados->get_Over()); |
rebonatto | 0:c64e1194230b | 191 | strcat(str,aux); |
rebonatto | 0:c64e1194230b | 192 | |
rebonatto | 0:c64e1194230b | 193 | sprintf(aux,"&DURATION=%04d",dados->get_Duration()); |
rebonatto | 0:c64e1194230b | 194 | strcat(str,aux); |
rebonatto | 0:c64e1194230b | 195 | /* Ate Aqui */ |
rebonatto | 0:c64e1194230b | 196 | |
rebonatto | 0:c64e1194230b | 197 | strcat(str,"&SIN="); |
rebonatto | 0:c64e1194230b | 198 | for(i=0;i<Settings::get_MaxHarmonics();i++) |
rebonatto | 0:c64e1194230b | 199 | { |
rebonatto | 0:c64e1194230b | 200 | char s[10]; |
rebonatto | 0:c64e1194230b | 201 | //According to RFC1738,RFC3986 the semicolon is a reserved character and must be encoded |
rebonatto | 0:c64e1194230b | 202 | f = dados->get_SineValue(i); |
rebonatto | 0:c64e1194230b | 203 | sprintf(s,"%08X",*(unsigned int*)&f); |
rebonatto | 0:c64e1194230b | 204 | strcat(str,s); |
rebonatto | 0:c64e1194230b | 205 | if (i < (Settings::get_MaxHarmonics() - 1)) |
rebonatto | 0:c64e1194230b | 206 | strcat(str, "%3B"); |
rebonatto | 0:c64e1194230b | 207 | } |
rebonatto | 0:c64e1194230b | 208 | |
rebonatto | 0:c64e1194230b | 209 | strcat(str,"&COS="); |
rebonatto | 0:c64e1194230b | 210 | for(i=0;i<Settings::get_MaxHarmonics();i++) |
rebonatto | 0:c64e1194230b | 211 | { |
rebonatto | 0:c64e1194230b | 212 | char c[10]; |
rebonatto | 0:c64e1194230b | 213 | //According to RFC1738,RFC3986 the semicolon is a reserved character and must be encoded |
rebonatto | 0:c64e1194230b | 214 | f = dados->get_CossineValue(i); |
rebonatto | 0:c64e1194230b | 215 | sprintf(c,"%08X",*(unsigned int*)&f); |
rebonatto | 0:c64e1194230b | 216 | strcat(str,c); |
rebonatto | 0:c64e1194230b | 217 | if (i < (Settings::get_MaxHarmonics()-1)) |
rebonatto | 0:c64e1194230b | 218 | strcat(str, "%3B"); |
rebonatto | 0:c64e1194230b | 219 | } |
rebonatto | 0:c64e1194230b | 220 | strcat(str,"\r\n"); |
rebonatto | 0:c64e1194230b | 221 | |
rebonatto | 0:c64e1194230b | 222 | char len[5]; |
rebonatto | 0:c64e1194230b | 223 | sprintf(len,"%d",strlen(str)); |
rebonatto | 0:c64e1194230b | 224 | |
rebonatto | 0:c64e1194230b | 225 | strcat(strfinal, "Content-Length: "); |
rebonatto | 0:c64e1194230b | 226 | strcat(strfinal, len); |
rebonatto | 0:c64e1194230b | 227 | strcat(strfinal, header2); |
rebonatto | 0:c64e1194230b | 228 | strcat(strfinal, str); |
rebonatto | 0:c64e1194230b | 229 | strcat(strfinal, "\r\n"); |
rebonatto | 0:c64e1194230b | 230 | |
rebonatto | 0:c64e1194230b | 231 | //printf("Request=[%s]\n",strfinal); |
rebonatto | 0:c64e1194230b | 232 | //printf("Tamanho STR %d\n", strlen(str)); |
rebonatto | 0:c64e1194230b | 233 | //printf("Tamanho STRFINAL %d\n", strlen(strfinal)); |
rebonatto | 0:c64e1194230b | 234 | } |