I have to store a list of constants (font for NeoPixel display). The array is not extremely long. I wonder if there is a better way than just defining an array? Especially a way saving memory.
Sure, I can try… Have to check what is necessary for using mDash instead of AWS.
The whole app isn’t really fancy. NeoPixel display is working and I can update the status via AWS shawow. As soon as I additionally include the letters.js the ESP8266 crashes.
Is there a way to check how much memory is left? I already tried Sys.free_ram() - it was something about 14000 (at least more than the required 400 bytes).
Happy doing some further tests
load('api_config.js');
load('api_events.js');
load('api_net.js');
load('api_timer.js');
load('api_shadow.js');
load('api_gpio.js');
load('api_neopixel.js');
load('digits.js');
// load('letters.js');
let pin = 15, numPixels = 64, colorOrder = NeoPixel.GRB;
let strip = NeoPixel.create(pin, numPixels, colorOrder);
strip.clear();
strip.show();
let char = ' ';
let red = 0x0f;
let green = 0x0f;
let blue = 0x0f;
let deviceId = Cfg.get("aws.thing_name");
let connected = false;
let showState = function() {
strip.clear();
let high = 0x00000000;
let low = 0x00000000;
let code = char.charCodeAt(0);
if (code >= 48 && code <= 57) {
high = DIGITS[code - 48][0];
low = DIGITS[code - 48][1];
/* } else if (code >= 65 && code <= 90) {
high = LETTERS[code - 65][0];
low = LETTERS[code - 65][1]; */
}
for (let x = 0; x < 4; x++) {
for (let y = 0; y < 8; y++) {
let i = (high >> (x * 8)) & 0xff;
let j = (i >> y) & 0x01;
if (j) {
strip.setPixel((x + 4) * 8 + y, red, green, blue);
}
}
}
for (let x = 0; x < 4; x++) {
for (let y = 0; y < 8; y++) {
let i = (low >> (x * 8)) & 0xff;
let j = (i >> y) & 0x01;
if (j) {
strip.setPixel(x * 8 + y, red, green, blue);
}
}
}
strip.show();
};
let data = {};
let publishState = function() {
print('Sending current state...');
data = {DeviceId: deviceId, Char: char, Red: red, Green: green, Blue: blue};
print(JSON.stringify(data));
Shadow.update(0, data);
showState();
};
let cloudconnect = Timer.set(60000, Timer.REPEAT, publishState, null);
// Monitor network connectivity.
Event.addGroupHandler(Net.EVENT_GRP, function(ev, evdata, arg) {
let status = true && connected;
let evs = '???';
if (ev === Net.STATUS_DISCONNECTED) {
evs = 'DISCONNECTED';
connected = false;
} else if (ev === Net.STATUS_CONNECTING) {
evs = 'CONNECTING';
connected = false;
} else if (ev === Net.STATUS_CONNECTED) {
evs = 'CONNECTED';
connected = false;
} else if (ev === Net.STATUS_GOT_IP) {
evs = 'GOT_IP';
connected = true;
}
}, null);
Shadow.addHandler(function(event, obj) {
if (event === 'CONNECTED') {
// Connected to shadow - report our current state.
publishState();
} else if (event === 'UPDATE') {
print(JSON.stringify(obj));
} else if (event === 'UPDATE_ACCEPTED') {
print(JSON.stringify(obj));
} else if (event === 'UPDATE_DELTA') {
// Got delta. Iterate over the delta keys, handle those we know about.
for (let key in obj) {
if (key === 'Char') {
// Shadow wants us to change local state - do it.
char = obj.Char;
} else if (key === 'Red') {
// Shadow wants us to change local state - do it.
red = obj.Red;
} else if (key === 'Green') {
// Shadow wants us to change local state - do it.
green = obj.Green;
} else if (key === 'Blue') {
// Shadow wants us to change local state - do it.
blue = obj.Blue;
} else {
// ignore
}
}
// Once we've done synchronising with the shadow, report our state.
publishState();
} else {
print('Unhandled event', event);
}
});
When using mDash I do not get the OOM error. ESP8266 is sending data - however I do not see the right shadow on https://mdash.net.
Switching back to AWS and the error returns… Before the dump Sys.free_ram() shows something about 19000 bytes left. Not sure who is using all the memory.
As our project should run together with AWS Lambda moving to mDash is not really an option… Ok, just for testing.
I remember the Arduino IDE provides a feature storing constants out of RAM. Does Mongoose OS provide a similar approach? There should be a way storing some constants or structured data…
Btw. would be cool if this lib could be provided as well
If you’re using demo-js, I suggest to clone it, modify mos.yml by including an exact list of libraries you need - instead of demo bundle. The bundle has a lot of cruft you don’t need. You might solve your RAM issue.
Thanks, I reduced the libs giving some more free memory…
libs:
# common mgos libs
- origin: https://github.com/mongoose-os-libs/boards
- origin: https://github.com/mongoose-os-libs/wifi
# libs necessary for the current app
- origin: https://github.com/mongoose-os-libs/neopixel
- origin: https://github.com/mongoose-os-libs/mjs
- origin: https://github.com/mongoose-os-libs/aws
Unluckily the problem remains. After WiFi connection is establish the app crashes. Without loading the letters.js no crash. Following the “Free RAM” log I cannot really imagine an OOM error.
Try to move your DIGITS and LETTERS in C, add 2 C functions eg. int char_get_high(int c) and int char_get_low(int c), ffi them in the mJS code and the pressure on the RAM will/might be lower.