Go Down

Topic: Low memory available, stability problems may occur. (Read 415 times) previous topic - next topic

kelvinswee94

Hi Everyone,
I was trying to use Cytron Uno to connect with the cytron LoRa RFM shield and Espresso Lite V2.0. I want to received the incoming data and send the data to the webserver.

Here is my code:
Code: [Select]
#include <CytronWiFiShield.h>
#include <CytronWiFiClient.h>
#include <SoftwareSerial.h>
#include <ThingSpeak.h>
#include <WiFi.h>
#include <SPI.h>
#include <RH_RF95.h>
const char *ssid = "xxxxxx";
const char *pass = "xxxxx";
char thingSpeakAddress[] = "api.thingspeak.com";
String APIKey = "xxxxxxxxxxxxx";
const int updateThingSpeakInterval;     
ESP8266Client client;
WiFiServer server(80);
long lastConnectionTime = 0;
boolean lastConnected = false;
#define RFM95_CS 10
#define RFM95_RST 7
#define RFM95_INT 2
#define RF95_FREQ 915.0
RH_RF95 rf95(RFM95_CS, RFM95_INT);                     
void setup() {
// put your setup code here, to run once:
  pinMode(RFM95_RST, OUTPUT);
  digitalWrite(RFM95_RST, HIGH);
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  if(!wifi.begin(8, 9))
  {
    Serial.println(F("Error talking to shield"));
    while(1);
  }
  Serial.println(F("Start wifi connection"));
  if(!wifi.connectAP(ssid, pass))
  {
    Serial.println(F("Error connecting to WiFi"));
    while(1);
  }
  Serial.print(F("Connected to "));Serial.println(wifi.SSID());
  Serial.println(F("IP address: "));
  Serial.println(wifi.localIP());
  ThingSpeak.begin(client);
  delay(1000);
  Serial.println(F("LoRa Receiver"));
  digitalWrite(RFM95_RST, LOW);
  delay(10);
  digitalWrite(RFM95_RST, HIGH);
  delay(10);
  while (!rf95.init()) {
    Serial.println(F("LoRa radio init failed"));
    while (1);
  }
  Serial.println(F("LoRa radio init OK!"));
  if (!rf95.setFrequency(RF95_FREQ)) {
    Serial.println(F("setFreq failed"));
    while (1);
  }
  Serial.print(F("Set Freq to: ")); Serial.println(RF95_FREQ);


}
void loop() {

  String distance = (F("11")); // read light value
  String help = (F("14"));

  // Print Update Response to Serial Monitor
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
  // Disconnect from ThingSpeak
  if (!client.connected() && lastConnected) {
    Serial.println(F("disconnected"));
    Serial.println();
    client.stop();
  }
  // Update ThingSpeak
  if (!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval)) {
    updateThingSpeak("field1=" + distance + "&field2=" + help);
    Serial.println(distance);
    Serial.println(help);

  }
  lastConnected = client.connected();
 
}

void updateThingSpeak(String tsData) {
  if (client.connect(thingSpeakAddress, 80)) {
    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: " + APIKey + "\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(tsData.length());
    client.print("\n\n");
    client.print(tsData);
    lastConnectionTime = millis();

    if (client.connected()) {
      Serial.println(F("Connecting to ThingSpeak..."));
      Serial.println();
    }
  }
}


However, I facing this problem when i upload the sketch.
Low memory available, stability problems may occur.

Any one know what is the caused of the low memory and how to reduce the use of memory?
I already use F()marco for the string to let them store in the flash memory.

Thanks!

TolpuddleSartre

Code: [Select]
client.print(F("POST /update HTTP/1.1\n"));
    client.print(F("Host: api.thingspeak.com\n"));

Etc etc.

UKHeliBob

Quote
I already use F()marco for the string to let them store in the flash memory.
Except when you don't
Code: [Select]
   client.print("POST /update HTTP/1.1\n");
etc



Why are you using Strings in your program ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Danois90

You could start by removing all usage of "String" types. In the void "updateThingSpeak" there's lots of strings which are not stored in PROGMEM (flash).
Instead of mocking what's wrong, teach what's right! ;)
When you get help, remember to thank the helper and give some karma!
Please, do NOT send me any private messages!!

Go Up