SparkFun Forums 

Where electronics enthusiasts find answers.

Have questions about a SparkFun product or board? This is the place to be.
By maximelanoelle
#199299
Hello all.
First time posting on the forum (so apologies if I am not writing in the correct location).

I have recently purchased a sparkfun esp8266 shield to connect to an arduino uno and send some data online (on thingspeak).
I managed to connect the shield & arduino uno to my local wifi, and display analogRead values on the serial monitor. But after loading the sketch on my arduino uno, the memory is ~89% full.

When trying to send data online, only blank data seem to be uploaded to Thingspeak page. I can see the number of entries on the page going up, but no data are actually plotted. I have tried to manually upload a value successfully. It seems more to be an issue with my code and / or shield.

Do any of you have experienced similar memory issues, do you have any recommendation, advice, etc...? I am only starting with Arduino, and am hoping we can find a workaround.
Thanks in advance
Max
PS: My arduino code below.

#include "ThingSpeak.h"
#include <SparkFunESP8266WiFi.h>
#include <SoftwareSerial.h>
char ssid[] = "<VALUE_REMOVED>"; // network SSID (name)
char pass[] = "<VALUE_REMOVED>"; // network password
ESP8266Client client;

unsigned long myChannelNumber = "<VALUE_REMOVED>"; // ThingSpeak Channel No
const char * myWriteAPIKey = "<VALUE_REMOVED>"; // ThingSpeak API Key

void setup() {
Serial.begin(9600);
}

void loop() {
Serial.println("start loop");

// Verifies that the ESP8266 is operational and connects to WIFI if not currently connected.
initializeESP8266();

ThingSpeak.begin(client);
delay(100);

send_data();
delay(20000); // ThingSpeak will only accept updates every 15 seconds.
Serial.println("wait 20s");
}




void initializeESP8266()
{
int ESP8266_test = esp8266.begin();
for (int i = 0; i < 20; i++)
{
if (ESP8266_test != true) {
delay (500);
Serial.print("ESP8266 not ready... iteration "); Serial.println(i);
} else {
Serial.println("ESP8266 ready!");
connectESP8266();
i=20;
}
}
}

void connectESP8266()
{
// Connect ESP8266 to WIFI if not currently connected.
int retVal = esp8266.status();
if (retVal == 1) {
Serial.println("Already connected to WIFI");
} else {
int retVal2=esp8266.connect(ssid, pass);
for (int j = 0; j < 20; j++)
{
if (retVal2 < 0) {
delay (10000);
Serial.print("Attempting connection to WIFI... iteration "); Serial.print(j); Serial.print(" Status "); Serial.println(retVal2);
} else {
j=20;
Serial.println("Connected to WIFI!");
}
}
}

}

void send_data()
{
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
ThingSpeak.writeField(myChannelNumber, 1, sensorValue, myWriteAPIKey);

if (client.connected()) {client.stop();}
Serial.println("WIFI connection stopped");

}
By maximelanoelle
#199303
Hi again.
I did a few more tests, with the following findings.
When removing most lines from my code but the single line "#include <SparkFunESP8266WiFi.h>", it returns:
"Global variables use 580 bytes (28%) of dynamic memory, leaving 1468 bytes for local variables. Maximum is 2048 bytes."

With the following two lines...
#include <SparkFunESP8266WiFi.h>
ESP8266Client client;
return:
"Global variables use 1184 bytes (57%) of dynamic memory, leaving 864 bytes for local variables. Maximum is 2048 bytes."

Finally with the following three lines...
#include "ThingSpeak.h"
#include <SparkFunESP8266WiFi.h>
ESP8266Client client;
return:
"Global variables use 1298 bytes (63%) of dynamic memory, leaving 750 bytes for local variables. Maximum is 2048 bytes."

These numbers seem to give very limited room for additional code. Have people from the forum used successfully the board with an arduino uno? Are there any tricks to reduce the size associated with these 3 lines?

Thanks. Max
By paulvha
#199309
I have downloaded THingSpeak and SparkFunESP8266Wifi master library on Windows. After compling your code :
Global variables use 1532 bytes (74%) of dynamic memory, leaving 516 bytes for local variables. Maximum is 2048 bytes.

By changing the remarks to be passed from flash-memory (with the F()) option, you can save 178 bytes in RAM:
Global variables use 1354 bytes (66%) of dynamic memory, leaving 694 bytes for local variables. Maximum is 2048 bytes.

Are you sure memory is your problem ? I am confsued why WIFI is connected an disconnected with every send_data() in the loop. I would expect you only have to connect once..
By maximelanoelle
#199311
Hi Paul. Thanks for your response. My code may not be the best. But the three lines in my second post are sufficient to fill most of the memory (60+%). This is not related to my code. This is what concerns me. Thanks. Max
By paulvha
#199312
well nothing to concern ..... if you comment out/remove : #include <SparkFunESP8266WiFi.h> you basically do NOT include the complete driver/library with all it's internal calls and buffers. None of the calls for ESP8266 will work. The .h (header) file is a reference to include the driver. A driver has code and buffers/variables which takes memory. There might/will be driver code and buffers/variables that are not relevant for your solution ( e.g. running the ESP8266 as a server), but It is an all of nothing game. You can see the code in c:" user"\documents\arduino\libraries\Sparkfun....\src. Same is true for ThingSpeak.h or any other .h file. If you want to reduce the memory used, you could decide to change/reduce the driver/library code to the only needed subroutines and remove / reduce the buffers. I would strongly vote against that if not necessary. Some drivers get regulalr updates and then you have to rework that again & again, and next time you want to build a server with the ESP8266 you will have to make a new instance...