连续GET请求后ESP8266崩溃 - 响应代码' -2'

时间:2017-12-25 03:49:45

标签: esp8266 arduino-esp8266

我正在为Arduino IDE中的ESP8266写一个草图。草图以及其他功能之一具有一种模式,该模式将Web服务器每秒调查尽可能接近10次。此轮询是使用ESP8266HTTPClient通过HTTP GET请求完成的。

请求代码:

void updateLEDS() {
  HTTPClient pihome;
  //Attempt connection to PiHome server.
  if (!pihome.begin(endpoint)) {
    Serial.println("Connection to PiHome host failed.");
    return;
  }
  pihome.setAuthorization(username, password);
  Serial.println("Set authorization");
  //If a connection is formed, send get request to device endpoint.
  int code = pihome.GET();
  Serial.print("Pushed get request:");
  Serial.println(code);
  if (code > 0) {
    Serial.println("Returned http status code > 0");
    String response = pihome.getString();
    pihome.getStream().write("HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 0\r\n\r\n");
    //int CRLF = 0;
    int data[NUMLEDS*4];
    for (int i = 0;i < response.length() && i < NUMLEDS*4;i++) {
      int b = (int) response.charAt(i);
      data[i] = b;
    }

    for (int i = 0;i < NUMLEDS;i++) {
      if (sizeof(data) > i*4 + 3) {
        front.setPixelColor(response.charAt(i*4)-1, front.Color(pgm_read_byte(&gamma8[response.charAt(i*4+1)-1]), int(pgm_read_byte(&gamma8[response.charAt(i*4+2)-1])*0.8), pgm_read_byte(&gamma8[response.charAt(i*4+3)-1])));
        back.setPixelColor(response.charAt(i*4)-1, front.Color(pgm_read_byte(&gamma8[response.charAt(i*4+1)-1]), int(pgm_read_byte(&gamma8[response.charAt(i*4+2)-1])*0.8), pgm_read_byte(&gamma8[response.charAt(i*4+3)-1])));
      }
    }
    front.show();
    back.show();
  }
  pihome.end();
}

在我的程序中的每个其他模式下,ESP运行稳定。但是,当我切换到发出HTTP请求的模式时,大约15-20秒后,ESP崩溃了异常代码29.经过一些调试后,我发现崩溃发生在&#34; int code = pihome.GET ();&#34;线。在大约10个显示响应代码为-2的GET请求之后,也会发生崩溃。正如您在代码中看到的,无论响应代码是什么,我都会调用pihome.end()。在添加其他模式之前,这不是问题,但这些模式没有以任何方式被引用。这与异常代码29一起让我相信这是一个内存问题(也许是容量?)。但是,我正在关闭HTTP连接,所以我不确定还能做什么。

使用EspExceptionDecoder,这是串行监视器中的堆栈跟踪:

    Decoding 16 results
0x401004f4: calloc at C:\Users\Qwertyness\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266\umm_malloc/umm_malloc.c line 1684
0x401074dc: pvPortZalloc at C:\Users\Qwertyness\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/heap.c line 33
0x40102cf0: wDev_ProcessFiq at ?? line ?
0x40223234: dns_recv at /Users/igrokhotkov/espressif/arduino/tools/sdk/lwip/src/core/dns.c line 755
0x40223bd9: pbuf_free at /Users/igrokhotkov/espressif/arduino/tools/sdk/lwip/src/core/pbuf.c line 758
0x40226100: udp_input at /Users/igrokhotkov/espressif/arduino/tools/sdk/lwip/src/core/udp.c line 343
0x40107498: pvPortMalloc at C:\Users\Qwertyness\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/heap.c line 13
0x40100000: _stext at ?? line ?
0x4022783c: ip_input at /Users/igrokhotkov/espressif/arduino/tools/sdk/lwip/src/core/ipv4/ip.c line 553
0x40226c45: ethernet_input at /Users/igrokhotkov/espressif/arduino/tools/sdk/lwip/src/netif/etharp.c line 1379
0x4020765f: pp_tx_idle_timeout at ?? line ?
0x402075d2: pp_tx_idle_timeout at ?? line ?
0x4021b6c7: ets_snprintf at ?? line ?

供参考,这是整个草图(省略敏感信息):

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <Adafruit_NeoPixel.h>
#include <math.h>

const uint8_t PROGMEM gamma8[] = {
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,
    1,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2,  2,  2,
    2,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  5,  5,  5,
    5,  6,  6,  6,  6,  7,  7,  7,  7,  8,  8,  8,  9,  9,  9, 10,
   10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
   17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
   25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
   37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
   51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
   69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
   90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
  115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
  144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
  177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
  215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 };

#define FRONT       2
#define BACK        4
#define BUTTON      5
#define NUMLEDS     51

const char* ssid     = "SSID";
const char* password = "PASSWORD";                                                                                                                                                                                                                                                                                                                                                                  
const char* username = "USER";

const char* endpoint = "http://host/endpoint";

Adafruit_NeoPixel front = Adafruit_NeoPixel(NUMLEDS, FRONT, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel back = Adafruit_NeoPixel(NUMLEDS, BACK, NEO_GRB + NEO_KHZ800);
int requestCounter = 0;
bool lastButton = false;
int debounceCount = 0;
int ledMode = 2;
double beatBrightness = 0;
int beatDirection = 1;
int beatNumber = 0;
int beatPause = 0;

void setup() {
  //Set up serial communication
  Serial.begin(115200);
  //Start WiFi connection
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("Attempting to Connect");
    delay(500);
  }
  Serial.println("Connected to WiFi");
  //Initialize LED strip
  front.begin();
  back.begin();

  pinMode(5, INPUT_PULLUP);
}

void getColor(int rgb[]) {
  rgb[0] = pgm_read_byte(&gamma8[rgb[0]]);
  rgb[1] = pgm_read_byte(&gamma8[rgb[1]]);
  rgb[2] = pgm_read_byte(&gamma8[rgb[2]]);
}

void updateLEDS() {
  HTTPClient pihome;
  //Attempt connection to PiHome server.
  if (!pihome.begin(endpoint)) {
    Serial.println("Connection to PiHome host failed.");
    return;
  }
  pihome.setAuthorization(username, password);
  Serial.println("Set authorization");
  //If a connection is formed, send get request to device endpoint.
  int code = pihome.GET();
  Serial.print("Pushed get request:");
  Serial.println(code);
  if (code > 0) {
    Serial.println("Returned http status code > 0");
    String response = pihome.getString();
    pihome.getStream().write("HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 0\r\n\r\n");
    //int CRLF = 0;
    int data[NUMLEDS*4];
    for (int i = 0;i < response.length() && i < NUMLEDS*4;i++) {
      int b = (int) response.charAt(i);
      data[i] = b;
    }

    for (int i = 0;i < NUMLEDS;i++) {
      if (sizeof(data) > i*4 + 3) {
        front.setPixelColor(response.charAt(i*4)-1, front.Color(pgm_read_byte(&gamma8[response.charAt(i*4+1)-1]), int(pgm_read_byte(&gamma8[response.charAt(i*4+2)-1])*0.8), pgm_read_byte(&gamma8[response.charAt(i*4+3)-1])));
        back.setPixelColor(response.charAt(i*4)-1, front.Color(pgm_read_byte(&gamma8[response.charAt(i*4+1)-1]), int(pgm_read_byte(&gamma8[response.charAt(i*4+2)-1])*0.8), pgm_read_byte(&gamma8[response.charAt(i*4+3)-1])));
      }
    }
    front.show();
    back.show();
  }
  pihome.end();
}

void loop() {
  //Detect rising edge
  if (lastButton == false && !digitalRead(5)) {
    debounceCount++;
    if (debounceCount >= 2) {
      debounceCount = 0;
      lastButton = true;
      ledMode++;
      if (ledMode > 3) {
        ledMode = 0;
      }
    }
  }
  //Detect falling edge
  else if (lastButton == true && digitalRead(5)) {
    debounceCount++;
    if (debounceCount >= 2) {
      debounceCount = 0;
      lastButton = false;
    }
  }

  //Only make HTTP request every tenth of a second.
  if (requestCounter >= 5) {
    if (ledMode == 3) {
      updateLEDS();
    }
    requestCounter = 0;
  }
  requestCounter++;

  //Off mode
  if (ledMode == 0) {
    for (int i = 0;i < NUMLEDS;i++) {
      front.setPixelColor(i, front.Color(0, 0, 0));
      back.setPixelColor(i, back.Color(0, 0, 0));
    }
    front.show();
    back.show();
  }
  //Solid heart mode
  else if (ledMode == 1) {
    int color[] = {153, 75, 75};
    getColor(color);
    for (int i = 0;i < NUMLEDS;i++) {
      front.setPixelColor(i, front.Color(color[0], color[1], color[2]));
      back.setPixelColor(i, back.Color(color[0], color[1], color[2]));
    }
    front.show();
    back.show();
  }
  //Beating heart mode
  else if (ledMode == 2) {
    if (beatNumber == 2) {
      for (int i = 0;i < NUMLEDS;i++) {
        front.setPixelColor(i, 0, 0, 0);
        back.setPixelColor(i, 0, 0, 0);
      }
      if (beatPause >= 50) {
        beatPause = 0;
        beatNumber = 0;
        beatBrightness = 0;
        beatDirection = 1;
      }
      beatPause++;
    } else {
      beatBrightness += beatDirection;
      if (beatBrightness >= 7) {
        beatDirection = -1;
      } else if (beatBrightness <= 0) {
        beatDirection = 1;
        beatNumber++;
      }
      if (beatNumber == 0) {
          int color[] = {(int)(153*beatBrightness/7)+50, (int)(75*beatBrightness/7)+25, (int)(75*beatBrightness/7)+25};
          getColor(color);
        for (int i = 0;i < NUMLEDS;i++) {
          front.setPixelColor(i, color[0], color[1], color[2]);
          back.setPixelColor(i, color[0], color[1], color[2]);
        }
      } else if (beatNumber == 1) {
        int color[] = {(int)(153*beatBrightness/14)+50, (int)(75*beatBrightness/14)+25, (int)(75*beatBrightness/14)+25};
        getColor(color);
        for (int i = 0;i < NUMLEDS;i++) {
          front.setPixelColor(i, color[0], color[1], color[2]);
          back.setPixelColor(i, color[0], color[1], color[2]);
        }
      }
    }
    front.show();
    back.show();
  }
  delay(20);
}

0 个答案:

没有答案