Search code examples
c++performancememory-managementarduinoethernet

Overcome Arduino memory limitations with multiple sensors


I have an Arduino Nano piggybacked on a ENC28j60 ethernet module. I have eight (8) DHT22 sensors (named A, B, C ... H ) and i want to write their temperature and humidity data to Pushingbox.

The program is working great with ONE sensor. So that's good. However when i un-remark (i.e. take out the //'s) for anything more than one sensor, yes even for ONE other sensor, it won't write anything at all.

The arduino IDE complier says:

Sketch uses 23824 bytes (77%) of program storage space. Maximum is 30720 bytes.
Global variables use 1870 bytes (91%) of dynamic memory, leaving 178 bytes for local variables. Maximum is 2048 bytes.
Low memory available, stability problems may occur.

FYI, If i un-remark just one other sensor it goes up +2% to 93% of dynamic memory and stops working. So i'm assuming its a memory problem. i have already removed all floats (and made integers *10 to keep one decimal place of accuracy), i need 8 instances of DHT so considered reducing the library size but the .h seems tiny and pretty lean already (within the .h file it even boasts: Very low memory footprint - Very small code. so i havent modified this or the .cpp.

The overall code i have written is not large, maybe there are some in-efficiencies there, but i can't see that it is going to make anything near the amount of memory saving needed for the next 7 sensors.

The full arduino code (written for all 8 sensors, with 'ghosted' bits for 4 sensors and in use only working for one sensor) is below:

#include "DHTesp.h"
//#include <SPI.h>
#include <UIPEthernet.h>

DHTesp dhtA;
//DHTesp dhtB; 
//DHTesp dhtC;
//DHTesp dhtD;
//DHTesp dhtE;
//DHTesp dhtF;
//DHTesp dhtG;
//DHTesp dhtH;


const int ledPin =  LED_BUILTIN; 
int ledState = LOW;

int interval = 10; // this is the number of seconds between reads (120=2mins)
int numReads = 5;   // Number of reads between posting to google docs.
int multFact = 10;   // multiplication factor 10 = 1 dec. place 100 = 2 dec places

byte mac[] = {0xBE, 0xEF, 0xDE, 0xAD, 0xDE, 0xAD };  //Ethernet shield MAC. Andy's working {0xBE, 0xEF, 0xDE, 0xAD, 0xDE, 0xAD}
byte ip[] = { 192,168,1,12 };     //  Arduino device IP address     
char devid [] = "vCE3D036xxxxxxxx";  // Gsheets device ID from Pushingbox ('x's for privacy:)

char server[] = "api.pushingbox.com";
EthernetClient client;


void setup()
{
  Serial.begin(9600);
  Serial.println ("RESTART");

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("Trying to connect...");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }
  else{
    Serial.print("OK, connected. Ethernet ready. ");
    // print the Ethernet board/shield's IP address:
    Serial.print("IP address: ");
    Serial.println(Ethernet.localIP());
  }

  // give the Ethernet shield a second to initialize:
  delay(1000);   
  Serial.println();
  //Serial.println("A-Stat\t\tA-Temp (C)\tA-humdid%\tA-HeatI (C)\tB-Stat\t\tB-Temp (C)\tA-humdid%\tB-HeatI (C)");
  Serial.println("\t\t\t\tt-A\th-A\tt-B\th-B\tt-C\th-C\tt-D\th-D\tt-E\th-E\tt-F\th-F\tt-G\th-G\tt-H\th-H");

  //hey dB for some reason the Ethernet sheild uses pin D2 :(      and pins 10,11,12,13
  // https://arduinodiy.wordpress.com/2013/04/07/connect-an-enc28j60-ethernet-module-to-an-arduino/

  // assign data pins
  dhtA.setup(3); 
  //dhtB.setup(4);
  //dhtC.setup(5); 
  //dhtD.setup(6);
  //dhtE.setup(7); 
  //dhtF.setup(8);
  //dhtG.setup(9); 
  //dhtH.setup(10); //watchout! i think Ethernet uses this pin too?
  pinMode(ledPin, OUTPUT);

 //end of void setup 
}


void loop()
{
  int Ahumid = 0;  int Atemp = 0;
  int Bhumid = 0;  int Btemp = 0;
  int Chumid = 0;  int Ctemp = 0;
  int Dhumid = 0;  int Dtemp = 0;
  //int Ehumid = 0;  int Etemp = 0;
  //int Fhumid = 0;  int Ftemp = 0;
  //int Ghumid = 0;  int Gtemp = 0;
  //int Hhumid = 0;  int Htemp = 0;

  int j=0;
  for (j = 1; j <= numReads ; j++ ) {

    int p = 0;
    for (p=1; p <= interval ; p++) {
      delay (1000);
      // swap the led state
      if (ledState == LOW) { ledState = HIGH; } else { ledState = LOW; }
      Serial.print (p); //show the seconds passing
      Serial.print (",");
      digitalWrite(ledPin, ledState);
    }

    Serial.print (" Reading");
    Atemp += dhtA.getTemperature()*multFact;  Ahumid += dhtA.getHumidity()*multFact;
    //Btemp += dhtB.getTemperature()*multFact;  Bhumid += dhtB.getHumidity()*multFact;
    //Ctemp += dhtC.getTemperature()*multFact;   Chumid += dhtC.getHumidity()*multFact;  
    //Dtemp += dhtD.getTemperature()*multFact;   Dhumid += dhtD.getHumidity()*multFact;

    // print the readings
    //Serial.print(dhtA.getStatusString());
    Serial.print("\t"); Serial.print(Atemp); 
    Serial.print("\t"); Serial.print(Ahumid);

    Serial.print("\t"); Serial.print(Btemp); 
    Serial.print("\t"); Serial.print(Bhumid);

    Serial.print("\t"); Serial.print(Ctemp);
    Serial.print("\t"); Serial.print(Chumid);

    Serial.print("\t"); Serial.print(Dtemp);
    Serial.print("\t"); Serial.print(Dhumid);
    Serial.println();
    // and so here endeth 'j', the number of reads
  }

  Serial.print ("Avg...");
  Atemp = Atemp/numReads;    Ahumid = Ahumid/numReads;
  Btemp = Btemp/numReads;    Bhumid = Bhumid/numReads;
  Ctemp = Ctemp/numReads;    Chumid = Chumid/numReads;
  Dtemp = Dtemp/numReads;    Dhumid = Dhumid/numReads;

  // print the averages so we can see what it is going to send
  Serial.print("\t\t\t");
  Serial.print("\t");  Serial.print(Atemp); Serial.print("\t"); Serial.print(Ahumid);
  Serial.print("\t");  Serial.print(Btemp); Serial.print("\t"); Serial.print(Bhumid);
  Serial.print("\t");  Serial.print(Ctemp); Serial.print("\t"); Serial.print(Chumid);
  Serial.print("\t");  Serial.print(Dtemp); Serial.print("\t"); Serial.print(Dhumid);
  Serial.println();

  Serial.print ("Prep for upload... ");
  if (client.connect(server, 80)) 
  {
    Serial.print("Connected OK ... writing...");

    client.print("GET /pushingbox?devid=");
    client.print(devid);
    client.print("&tempA=");     client.print(Atemp);
    client.print("&tempB=");     client.print(Btemp);
    client.print("&tempC=");     client.print(Ctemp);
    client.print("&tempD=");     client.print(Dtemp);
    client.print("&tempE=29&tempF=39&tempG=49&tempH=59");
    //now humidity too
    client.print("&humidA=");     client.print(Ahumid);
    client.print("&humidB=");     client.print(Bhumid);
    client.print("&humidC=");     client.print(Chumid);
    client.print("&humidD=");     client.print(Dhumid);
    client.print("&humidE=26&humidF=27&humidG=28&humidH=29");
    client.print("&submit=Submit");
    client.println(" HTTP/1.1"); 
    client.println("Host: api.pushingbox.com");
    client.println("Connection: close");
    client.println();

    Serial.println("written OK. & connection closed.");
    Serial.println(); //Serial.println();

    delay(1000); // maybe take this out to keep time stable?
    client.stop(); 
  }
  else {
    Serial.println("** NO CONNEX **"); Serial.println(); 
  }

//here endeth void loop     
}

Extra info (that i dont think is relevant, but maybe): IDE compiler also reports:

WARNING: library DHT_sensor_library_for_ESP32 claims to run on [esp32] architecture(s) and may be incompatible with your current board which runs on [avr] architecture(s).

Solution

  • Use the F macro for double-quoted string literal prints. Instead of this:

    Serial.println ("RESTART");
           or
    client.print("GET /pushingbox?devid=");
    

    ... do this:

    Serial.println ( F("RESTART") );
           or
    client.print( F("GET /pushingbox?devid=") );
    

    This will easily save a bunch o' RAM.

    I would also suggest using the single quoted literal for single characters, not the double quote:

    Serial.print( '\t' );