I’m getting an SOS error on my current application, referencing a Memory Manager issue. SOS, three flashes, SOS.
My application runs the HTTPClient Library to download trains times as a small string of numbers (twice) and then display those numbers via some WS2812’s. It runs properly and does everything it needs to do. However, when it goes to run again (after a 20 second delay), I get the Memory Manager issue and I can see the device restart itself.
See code below:
// This #include statement was automatically added by the Particle IDE.
#include "neopixel/neopixel.h"
#include "application.h"
#include "HttpClient/HttpClient.h"
/**
* Declaring the variables.
*/
unsigned int nextTime = 0; // Next time to contact the server
HttpClient http;
// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
// { "Content-Type", "application/json" },
// { "Accept" , "application/json" },
{ "Accept" , "*/*"},
{ NULL, NULL } // NOTE: Always terminate headers with NULL
};
http_request_t request;
http_response_t response;
SYSTEM_MODE(AUTOMATIC);
#define PIXEL_PIN D3
#define PIXEL_PIN2 D2
#define PIXEL_COUNT 8
#define PIXEL_TYPE WS2812B
Adafruit_NeoPixel north = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
Adafruit_NeoPixel south = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN2, PIXEL_TYPE);
void setup()
{
north.begin();
north.show(); // Initialize all pixels to 'off'
north.setPixelColor(0, 0, 0, 0);
north.setBrightness(180);
north.show();
south.begin();
south.show(); // Initialize all pixels to 'off'
south.setPixelColor(0, 0, 0, 0);
south.setBrightness(180);
south.show();
}
void loop() {
if (nextTime > millis()) {
return;
}
//30273 - Addison Red North
//30274 - Addison Red South
//30071 - Southport Brown South
//30070 - Southport Brown North
// Set up or url path
downloadSet(north, "30273");
downloadSet(south, "30274");
//repeat again in 20 seconds
nextTime = millis() + 20000;
}
void downloadSet(Adafruit_NeoPixel strip, String t) {
//s == strip
//t = trip
request.hostname = "www.johnventions.com";
request.port = 80;
request.path = "/trains/trains.php?trip=" + t;
// Get request for train times
http.get(request, response, headers);
//set up a character array to copy the response to
const int value_count = 8; // the maximum number of values
int trains[value_count]; // store the values here
char string[50]; //string to copy the response to
strcpy(string, response.body); // the string to split
//strcpy(string, "2, 8, 11");
int idx = 0;
for (char* pt=strtok(string, " "); pt && idx<value_count; pt=strtok(NULL, " ")) {
trains[idx++] = atoi(pt); //pt = the substring, atoi converts it to an int
}
//loop through and set all pixels to blue (meaning no train)
for (int i=0; i<PIXEL_COUNT; i++) {
strip.setPixelColor(i, 255, 255, 255);
}
strip.show();
//set train pixels to red (meaning there is a train in that time slot)
for (int j=0; j<idx; j++) {
if (trains[j] && trains[j] > 9) {
int minute = trains[j]-11;
if (minute < 8 && minute >= 0) {
strip.setPixelColor(minute, 255, 0, 0);
}
}
}
//make char array to display
strip.show();
}
Is there a flush function or any other memory saving technique I should be running on the request or the response to save memory? I didn’t see anything in the library documentation about it.