BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Low memory available - New problem


#1

I’ve been working on a project to monitor the air quality in my home and control my air purifier depending upon the pollutant level. It has become quite complicated with PM1.0, PM2.5, CO2, HC2O, temperature and humidity sensors plus a wireless link to my air purifier.

I have linked the whole system to Blynk and it’s great, in fact I would say fantastic, I get all the data and I can control all the settings on my air purifier. Well done Blynk team.

I’m using a Arduino Mega (3 serial ports + 1 software serial) + ESP8266 wifi module.

Everything was going well up until this last weekend when I noticed a typo on my GLCD so I corrected the mistake and re-loaded the program. No other changes were made.

Now, I get a new error message after loading it “Low memory available, stability problems may occur” and now it will not permanently connect to Blynk.

Okay, you might think this is not a Blynk problem but the only things I can think of that may have changed/updated are the 2 Blynk libraries:-

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

I have been very methodical with program changes and versions, every time I make a change I make a note and change the program version number. I have gone as far back as 4 weeks to versions that I know worked well and still I get this new error. I have 2 monitor systems running, 1 is running with fine with the last program version (with typo on the GLCD) and the other will not run with the same program.
I have a third Mega + ESP and that will not run either.

Any help or thoughts would be appreciated.


#2

Unless you manually downloaded and installed the latest library, v0.5.3 released two weeks ago, then they are unlikely to be the change that caused this problem.

image

What is the IDE error exactly?

And without posting your code (properly formatted), no one else can attempt to see if it will load on their system to confirm your issue.


#3

@Gunner Thanks for your quick reply, the Blynk library I’m using is v0.5.3

My first what I call “Blynk IE version V3” was created on the 29th May 2018 and worked okay, what I don’t understand is that ALL versions V3 (29/05/2018) to V7 (15/06/2018) will now not run.

This is the IDE error:-

Global variables use 6662 bytes (81%) of dynamic memory, leaving 1530 bytes for local variables. Maximum is 8192 bytes.
Low memory available, stability problems may occur.

I have just tried to post the code but it runs over the 32000 character limit and won’t let me do it!


#4

Sounds like your code needs some refining :smiley:

You will have to split it in two posts then… don’t miss any.

But I suspect that what we will see will be nothing much we can assist with… we try to help people learn Blynk, not teach programming… but let’s see first…


#5

Yes you’re correct @Gunner this is more than likely a programming issue.

I’m not a seasoned programmer like you and others on this forum and I’ve spent many, many hours scanning this and other forums.

I only started this hobby about 2 months ago as something to do in retirement and age, memory and the ability to learn new things is getting the better of me.

I have my own “low memory available, stability problems may occur” problems sometime.:rofl:

I find the best way to learn is by trial and error so I will persevere, I have plenty of time.

Thanks.


#6

I am in the same boat… a little younger, but over the last year and while foggy headed to boot due health issues, I am totally teaching myself this programming and Blynk stuff.

Post your code in a couple of posts and I at least can try loading it on my Mega and see what, if any ideas I have.


#7

Do you have a lot of Serial.print statements in your code?
If so, any text contained within quoyayion marks is actually using up dynamic memory.

Tyt using the F() macro like this.

Change this:

Serial.println("Hello World");

to this:

Serial.println(F("Hello World"));

You can’t use this technique when serial printing variables, so in that case you can do this:

Serial.print(F("The current temperature reading is: "));
Serial.println(Temp_value);

Commenting-out your Serial.print statements will also mean that they don’t take-up any dynamic memory, as the comments aren’t compiled.

There are other ways to reduce memory usage as well, and lots of tutorials online. Try googling something like “Reducing Arduino memory usage”

Pete.


#8

Hi rha10, as @Gunner said, post your code in two or more posts… but try to not split one function in two… to avoid to lose some lines by mistake…

The problem you have seems to ve not Blynk related… but i like to program and i’ll ever try to help others with their programation problems…

How many pins you need for your project? You know about adventages of use an ESP32 instead an arduino mega?

As @PeteKnight said you can use the F() macro, but only works as expected in a AVR environment is a little different with an ESP…

Other way to avoid the warning you have is to select the correct var type for each var you use… sometimes a int or a long var is used for values between 0 and 255 when using a byte var would be better…

If you like you can pm me and i’ll assist you to try to slim your code, but keep in mind that i’m spanish and i speak english aswell but no other languages… sometimes i receive linecodes with words in german, french, etc and trust me it’s fkn hard to try to understand what is supposed to the code will do… so if you do that, please change the code into english (or spanish if you know it) before.


#9

@PeteKnight

Hi Pete, thanks I did have some Serial.print commands written in so that I could see what was happening with some variables etc. but they were the first items to be taken out.

The problem is that I’m sensing a lot of things, averaging values over time (DS1307 included), displaying values in different colors depending upon value, sending pwm values via wireless to a fan, storing the data on SD card and now sending/receiving them to Blynk, so my Global Variable count is BIG.

I need to take a closer look at the global variables and see what or how I can do to reduce the count.

I live in China (I’m British) and to “Google” something is not that easy, but believe me I have spent many hours reading about everything to do with programing. Some things I can understand and a lot I don’t yet understand and maybe never will. :persevere:

@trystan4861 Thanks, I have changed a lot of the variables from int to byte today and will try a new version later.

At the moment my global variables stand @ 81%, I read somewhere that a value around 60% - 70% should be okay. I have a lot of work to do.

I will post the code later.

Richard.


#10

Call my name when you post your code so i’ll be noticed about that, if you reply to another i’ll need to check it manually and i’ll reply you much time after…


#11

Well, I’ve had an interesting time analyzing my program bit by bit or should that be byte by byte!

I have declared a total of 58 global variables, most of then unsigned int’s and I really cannot reduce the count without compromising what I want to achieve. But correct me if I’m wrong, 58 int’s only take up 116 bytes!

So I decided to analyze the program and find out just where all my dynamic memory was going. I started with a blank Blynk sketch for the Arduino Mega + ESP8266 shield, compiled it and noted the memory usage. Then one by one I added the various sections, compiled it and noted the memory usage.

Basic Blynk sketch dynamic memory usage 33%
Pre-setup dynamic memory usage 41%
Setup dynamic memory usage 68%
Main program dynamic memory usage 81% (main program has 8 void routines + loop)

So apart from the main program the biggest memory hungry animal is the setup, so I looked into what I had in there, interesting…

I’m using 6 Blynk timers, the dataSave timer is commented out because I’ve taken out the SD card save routine.

timer.setInterval(500L, airData); //Run airData every 0.5 seconds
timer.setInterval(45000L, battery); //Update battery voltage every 45 seconds
timer.setInterval(1000L, time_clk); //Check time clock every 1 second
timer.setInterval(30000L, clk); //Update clock every 30 seconds
timer.setInterval(5000L, airData_print); //Update screen every 5 seconds
timer.setInterval(60000L, sendData); //Send data to Blynk every 60 seconds
//timer.setInterval(900000L, dataSave); //Save data to SD card every 900 seconds (15 minutes).

These 6 timers take up 826 bytes or 10.1% of dynamic memory and there doesn’t seem to be any correlation between time and bytes i.e.the 60000L takes up 330 bytes and the 45000L only 14 bytes.

Are my findings correct and is there any way to reduce this memory usage?

Richard


#12

Without seeing your code, it turns into a “20 Questions” type of exercise, but here’s my Question 2:
Do you have any unused/unnecessary libraries that you’re including in your sketch - i.e any libraries you could live without?

Pete.


#13

@PeteKnight

Pete, believe me I have been through and through all my code line by line and taken out or changed anything that was redundant or not required. I have a GLCD display with showing many data values and a graph, I perform many algorithms with the data to arrive at the AQI (Air Quality Index) using different world standards. This is where the bulk of the program is.

Before I used Blynk I saved the data every 15 minutes to an SD card and everything worked very well, Blynk is a great platform and now I can view all the data on my phone anywhere, plus I can export the data. However, I would still like to include the SD card routine because if the equipment goes “off line” then no data can be sent, in China the Internet can be a bit intermittent. I think I have reached the limit of the Arduino Mega + ESP8266 combination and need to look for something a bit bigger/better…

It just seemed strange to me that newcomers and beginners are always "beaten up’ on this forum about using and how to use the timer function yet they take up sooooo… much memory.

BigFish

Richard


#14

I’d agree that the Arduino + ESP or Ethernet shield is far from ideal. The only advantage that the Arduino gives is tgat it has lots of GPIOs, Analogue pins and UARTs if you go for the Mega.
Without knowing exactly what sensors you’re using and how they are connected, and hoe your air purifier is controlled, it’s impossible to reccomended a suitable alternative, but I guess you’d be looking at a NodeMCU or even ESP32 device.

Personally, I take a different approach, which is to use multiple ESP devices (the Wemos D1 Mini is my board of choice) and attach my sensors/controllers to them, and have them “talking” to a Raspberry Pi running Node-Red and MQTT. This means that it’s easy to build small temperature and humidity sensors, Infrared and 433MHz gateways, weather monitors etc etc and position each one where you need it. I use a Nextion touch screen as my primary interface/ controller, which is also connected to a Wemos, but run a Blynk plugin in Node-Red that gives me Blynk connectivity as well. You can also run a database on the Pi if you wish, to save data locally (although anything that uses SD cards for frequent read/writes isn’t a great long term solution as they fail quite quickly, so a HDD/SDD is a far better solution).

Pete.


#15

But you understand, that you need to use timers in almost ANY code. Personally I’m mostly using simple

if (millis () - starttime > delay) {
starttime = millis();
...
}

This is just a preview, but I’m sure you got the idea

The AtMega 1284 has 16K of SRAM, but I think you can safely switch to some advanced dev board too (ESP, Raspberry, etc…)


#16

@PeteKnight

The system is up and running again and at the moment seems quite stable, the program is running at 81% dynamic memory usage.

I changed another thing, how many times have I read on this forum and others “do not power the ESP8266 using the 3.3V output from the arduino” and I’ve thought “I’m sure it will be okay”. Well the ESP module now has it’s own psu and it looks very happy.

I’m a retired instrumentation engineer and still work as a “consultant” to the air filtration industry in China, so I’m not exactly new to different types of sensors and programming although most of my experience is with PLC’s and HMI’s. I have a similar PLC based system running now but it’s big, expensive and industrial looking. The basic programming functions are the same but C type language is new to me, the first programming language I learn’t as a young man was Fortran and not many people remember that!

You’re correct, the reason I chose the Mega was that it has 3 + 1 serial ports but even now I’m having to use Software Serial to give me an additional port. I’m using 2 serial air quality sensors, a serial 433MHz wireless link to the air purifier and the ESP8266 wifi unit. I would dearly like to add another 433MHz link to an outside sensor unit so I can compare indoor and outdoor air quality but for this setup it will be a step too far!

I have another project running getting the weather info from openweathermap, if you’ve seen one of my earlier posts I couldn’t get the Mega + ESP to upload the json data file without “buffer overflow” and after spending many hours trawling through the Internet and different forums I still haven’t found the answer. But, this project is running very happily using a espduino and it was so easy to do, I’m trying to get an API key from AirVisual but they want silly money per month for the info I want so to have a second outdoor sensor system linked to what I have would be great.

I don’t want to rely too much on the Internet connection hence the SD card data storage, Blynk is very good but if the Internet connection is lost then so is the data collection. The reason I chose the SD card route is that they are easy and cheap here in China, in fact all electronic items are cheap you just need to be careful where you buy. I’ve lived here nearly 10 years so I know the market!

Anyway, thanks for your thoughts and I will certainly look at what you have proposed and be back.

Richard.


#17

Yup :stuck_out_tongue_winking_eye:


#18

@Gunner

Just for you Gunner, a nice and happy ESP-01S!


#19

I dabbled with Fortran, COBOL and Pascal as well as BASIC in the early 80’s, but I’ve only been messing around with the “Arduino C+” type language for a couple of years. It’s so much easier now with the resources available on the internet (unless you live in China of course!).

A practical alternative to the architecture that I use could be to set up a local server (to eliminate your internet connectivity issues) and have separate devices which update various virtual pins with sensor data, then read this data into a master device which does all of the processing and drives your display.
This isn’t actually very far removed from what my architecture does, using MQTT messages to relay data to the Pi server then pick this data up and process it.

Pete.


#20

@marvin7

You are correct, timers are essential in any program. I was just suprised at how much memory the Blynk timers appeared to take up.

I’m still using the 6 Blynk timers and at the moment the system is running well, now that I’ve sorted the ESP power supply. :roll_eyes:

I need to find out if I can “divert” some of the variables to the program memory because I’m only using about 25% of the program storage space.

Time to do some more reading if think.