2
\$\begingroup\$

I'm writing some code which is basically supposed to http get some information from my own website and parse it, which (finally) works. However, when I compile the code, it shows this:

Sketch uses 14,466 bytes (44%) of program storage space. Maximum is 32,256 bytes. Global variables use 1,281 bytes (62%) of dynamic memory, leaving 767 bytes for local variables. Maximum is 2,048 bytes.

Considering I'm going to add additional functionality which I'm afraid will use a ton of memory, is there any way that I can get the memory footprint of this code down?

#include <SoftwareSerial.h> // Include software serial library, ESP8266 library dependency
#include <SparkFunESP8266WiFi.h> // Include the ESP8266 AT library
#include <WString.h>

const char WIFI_NAME[] = "********";
const char WIFI_PASS[] = "********";

const String FORECAST_API_KEY = "********";
const String DOMAIN_NAME = "********";

const String LATITUDE = "********";
const String LONGITUDE = "********";

const String httpRequest = "GET /weather.php?apiKey=" + FORECAST_API_KEY + "&lat=" + LATITUDE + "&lon=" + LONGITUDE + "&units=us HTTP/1.1\n"
                           "Host: " + DOMAIN_NAME + "\n"
                           "Connection: close\n\n";

String weather; // CLEAR_DAY, CLEAR_NIGHT, RAIN, SNOW, SLEET, WIND, FOG, CLOUDY, PARTLY_CLOUDY_DAY, PARTLY_CLOUDY_NIGHT
int temp;

void setup() {
  Serial.begin(9600);
  Serial.print(F("Initializing esp8266: "));
  if (esp8266.begin()) // Initialize the ESP8266 and check it's return status
    Serial.println(F("Success")); // Communication and setup successful
  else
    Serial.println(F("Failure"));

  Serial.print(F("Connecting to network: "));
  int retVal = esp8266.connect(WIFI_NAME, WIFI_PASS);
  if (retVal >= 0){
    Serial.println(F("Success"));
  } else {
    Serial.print(F("Failure("));
    Serial.print(retVal);
    Serial.println(')');
  }
  updateWeather();
  Serial.print(F("Temperature: "));
  Serial.println(temp);
  Serial.print(F("Weather: "));
  Serial.println(weather);
}

void loop() {
  // put your main code here, to run repeatedly:

}


void updateWeather(){
  Serial.print("Attempting to connect to weather service: ");
  ESP8266Client client;
  if (client.connect(DOMAIN_NAME, 80) <= 0)
  {
    Serial.println(F("Failure"));
    return;
  }else {
    Serial.println(F("Success"));
  }
  // print and write can be used to send data to a connected
  // client connection.
  client.print(httpRequest);

  // wait for response from server
  while(!client.available()) {
    delay(100); 
  }

  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\n');
    //Serial.println(line);
    String key = getKey(line);
    if (key.length() > 0) {
      String value = getValue(line);
        if (key=="CURRENT_TEMP") {
         temp = value.toInt();
        } else if (key =="CURRENT_ICON") {
          weather = value;
        }
     }
  }

  // connected() is a boolean return value - 1 if the 
  // connection is active, 0 if it's closed.
  if (client.connected())
    client.stop(); // stop() closes a TCP connection.
}
String getKey(String line) {
  int separatorPosition = line.indexOf("=");
  if (separatorPosition == -1) {
    return "";
  }  
  return line.substring(0, separatorPosition);
}
String getValue(String line) {
  int separatorPosition = line.indexOf("=");
  if (separatorPosition == -1) {
    return "";
  }  
  return line.substring(separatorPosition + 1);
}

I've heard strings are a big memory hog in Arduino code — is this true and is there a way I can mitigate this?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.