Web サーバーに POST リクエストを送信し、サーバーの応答に基づいていくつかのタスクを実行する小さなプロジェクトを作成しています。サーバーは常に JSON または配列で応答します
サーバーからの従来の応答は次のようになります。
POST /app_dev.php/api/?XDEBUG_SESSION_START=vagrant HTTP/1.1
Host: XXXXXX.vagrant:8080
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
Postman-Token: 37fddefd-e120
id=594cdf2c962d740c2b1dac22&identifier=594cdf2c962d740c2b1dac22
問題は、完璧に 4 時間実行した後、arduino メガの実行速度が非常に遅くなることです。最初は、コマンドを取得してタスクを実行するのに 10 ~ 20 ミリ秒しかかかりません (LED の電源を入れます)。現在、4時間後、同じLEDに電力を供給するのに約5秒かかります. イーサネットの LED がゆっくりと点滅しているのを見ました。サーバーの応答を保存するために const char* を使用したためだと思います。私の質問は、ループ関数の最後で、const char* のような変数が削除されるということですか? 私のarduinoが数時間後にとてもゆっくりと動くのは何ですか? ありがとう
ループ関数で実行されるコード:
void loop () {
dataReceived = 0;
apiResponseReceived = 1;
apiCommandsResponseReceived = 1;
sendConfirmationCommand = 0;
checkForConfirmationResponse = 0;;
payloadData dataPacket,dataBuffer;
tmElements_t tm;
RTC.read(tm);
Serial.print("some things ");
Serial.println(queueEncrypted.count());
Serial.print("some things");
Serial.println(queueDecrypted.count());
printf_P(PSTR("Free ram: %d"),freeRam());
if(apiCommandsResponseReceived == 1) {
Serial.print("flag message");
byte check = stash.create();
stash.print("identifier=");
stash.print("8234nsda987123");
stash.save();
int stash_size = stash.size();
Stash::prepare(PSTR("POST http://$F/$F HTTP/1.0" "\r\n"
"Host: $F" "\r\n"
"Connection: close" "\r\n"
"Content-Type: application/x-www-form-urlencoded" "\r\n"
"Content-Length: $D" "\r\n"
"\r\n"
"$H"),
website,readCommands,website, stash_size, check);
// send the packet - this also releases all stash buffers once done
sessionDoi = ether.tcpSend();
int freeCount = stash.freeCount();
if (freeCount <= 3) {
Stash::initMap(56);
}
apiCommandsResponseReceived = 0;
}
long timeCommand = millis();
while(millis()- timeCommand < 5000) {
word len = ether.packetReceive(); // go receive new packets
word pos = ether.packetLoop(len); // respond to incoming pings
const char* reply2 = ether.tcpReply(sessionDoi);
StaticJsonBuffer<200> jsonBuffer;
Serial.print("flag message");
if (reply2 != 0) {
Serial.println(F(" >>>REPLY recieved...."));
Serial.println(reply2);
reply2 = removeHTTPHeader(reply2);
Serial.println("------HEADER REMOVED-----");
Serial.println(reply2);
JsonObject& root = jsonBuffer.parseObject(reply2);
if (root.success()) {
const char* id = root["id"];
strcpy(global_ID_command,id);
const char* command = root["command"];
const char* value = root["value"];
Serial.print("id: ");
Serial.println(id);
Serial.print("id glabal: ");
Serial.println(global_ID_command);
Serial.print("comanda: ");
Serial.println(command);
Serial.print("flag message");
Serial.print(value);
int comanda = atoi(value);
if(comanda == 1){
Serial.print("e 1");
digitalWrite(error_ETH,HIGH);
}
if(comanda == 0 ){
Serial.print("e 0");
digitalWrite(error_ETH,LOW);
}
sendConfirmationCommand = 1;
}
else {
Serial.println("flag message");
}
if (strstr(reply2,success)) {
Serial.println("flag message");
}
apiCommandsResponseReceived = 1;
jsonBuffer.clear();
break;
}
}
pingSite(ether.hisip, 1, 500);
if(sendConfirmationCommand == 1) {
Serial.print("flag message");
byte response = stash.create();
stash.print("response%5Bcommand%5D=");
stash.print(global_ID_command);
stash.print("&response%5Bcurrent_state%5D=");
stash.print("ON");
stash.print("&response%5Bsuccess%5D=");
stash.print("true");
stash.save();
int stash_size = stash.size();
Stash::prepare(PSTR("POST http://$F/$F HTTP/1.0" "\r\n"
"Host: $F" "\r\n"
"Connection: close" "\r\n"
"Content-Type: application/x-www-form-urlencoded" "\r\n"
"Content-Length: $D" "\r\n"
"\r\n"
"$H"),
website,executeCommands,website, stash_size, response);
// send the packet - this also releases all stash buffers once done
sessionTrei = ether.tcpSend();
int freeCount = stash.freeCount();
if (freeCount <= 3) {
Stash::initMap(56);
}
sendConfirmationCommand = 0;
checkForConfirmationResponse = 1;
}
if(checkForConfirmationResponse == 1){
long timeExecute = millis();
while(millis()- timeExecute < 5000) {
word len = ether.packetReceive(); // go receive new packets
word pos = ether.packetLoop(len); // respond to incoming pings
const char* reply3 = ether.tcpReply(sessionTrei);
Serial.print("flag message");
if (reply3 != 0) {
Serial.println(F(" >>>REPLY recieved...."));
Serial.println(reply3);
reply3 = removeHTTPHeader(reply3);
Serial.println("------HEADER REMOVED-----");
Serial.println(reply3);
if (strstr(reply3,success)) {
Serial.println("flag message");
}
checkForConfirmationResponse = 0;
break;
}
}
}
tcp レスポンスからヘッダーを削除する機能
char *removeHTTPHeader(char *buffer) {
char *t = strstr(buffer, "\r\n\r\n");
t = t + 4;
return t;
}