Not enough memory. Alternatives? Using multiple arduino connected eachother?how?

Hello!

I'm using an arduino as a webserver and webclient at the same time using ethernet shield. I need it to send data to mysql and also receive commands.

For now i only control two relays, one led and receive data from dht22 sensor, but i need to put few more sensors and the memory it's almost full.

Sketch uses 19,340 bytes (59%) of program storage space. Maximum is 32,256 bytes.
Global variables use 1,527 bytes (74%) of dynamic memory, leaving 521 bytes for local variables. Maximum is 2,048 bytes.

How can i improve this? I already bought esp8266 to replace ethernet shield.
What is the best solution? i know that i can write code on that board. what is the memory? (dinamic memory and storage space) Can i make esp8266 to run the webserver and webclient while i can read data from sensors and control relays,led using arduino uno/micro/ etc?

Can i split webserver and webclient on two arduino and make them to comunicate eachother?
It is possible to receive commands from browser without having to create a webclient?

How can i show data directly from arduino on a different web server?

My code:

#include <SPI.h>
#include <Ethernet.h>
#include "DHT.h"

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Adresa MAC
IPAddress ip(192, 168, 0, 123); // ip in LAN
IPAddress myDns(8, 8, 8, 8);  // server DNS
//IPAddress myserver(192,168,0,114); // ip webhost apache
char myserver[] = "deseuridolj.ro";   // domeniu webhost apache
EthernetServer server(84); // portului webserver-ului arduino
EthernetClient client;
String readString; 

#define rel1 6     // definirea numelor pentru pinii folositi in a controla releele
#define rel2 7

//////////////////////  inceput Termistor/umiditate
#define DHTPIN 2                // Digital Pin 2
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE);

//////////// sfarsit Termistor

// Variabile
unsigned long previousMillis = 0;        // will store last time LED was updated

// constante
const long interval = 60000;           // intervalul pentru webclient (milliseconds)

void setup() {                         // vor fi rulate o singura data cand porneste microcontrollerul
  Ethernet.begin(mac, ip, myDns); 
  server.begin();
  Serial.begin(9600); 
  Serial.println("ok"); // Mesaj fisat in consola pentru debug
  pinMode(rel1, OUTPUT);             // pin ales pentru a controla releul 1 este setat pentru semnal de iesire
  pinMode(rel2, OUTPUT);             // pin ales pentru a controla releul 1  
  pinMode(4, OUTPUT);                  // pin ales pentru a controla led ul de pe pin-ul 4
  dht.begin();
}

void loop(){
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Fail read temp");
    return;
  }

  // here is where you'd put code that needs to be running all the time.
  
	EthernetClient client = server.available();
	if (client) 
	{
		while (client.connected()) 
		{	
		  if (client.available()) 
			{
				char c = client.read();

				//read char by char HTTP request
				if (readString.length() < 100) {
				  //store characters to string 
				  readString += c; 
				  //Serial.print(c);
				} 
				//if HTTP request has ended
				if (c == '\n') 
				{
				  ///////////////
				  Serial.println(readString); //print to serial monitor for debuging 

					//now output HTML data header
					if(readString.indexOf('?') >=0) { //don't send new page
						client.println("HTTP/1.1 204 OK");
						client.println();
						client.println();  
					}
					else {
						client.println("HTTP/1.1 200 OK"); //send new page
						client.println("Content-Type: text/html");
						client.println();

						client.println("<HTML>");
						client.println("<HEAD>");
						client.println("<TITLE>Hello</TITLE>");
						client.println("</HEAD>");
						client.println("<BODY>");
            client.println("<b>Led Control:</b>
");
            if( digitalRead(4)){
                          client.println("<a href=\"/?ledoff\" target=\"inlineframe\">Turn the Led OFF</a>

"); 
            }
            else {
              
            client.println("<a href=\"/?ledon\" target=\"inlineframe\">Turn the Led ON</a>

"); 
            }

 
           // conditii relee afiseaza comanda disponibila
            client.println("<b>Relay Control:</b>
");
            if( !digitalRead(rel1)){
              client.println("<a href=\"/?rel1off\" target=\"inlineframe\">Relay1 is opened. Close it!</a> -"); 
           }
           else {
              client.println("<a href=\"/?rel1on\" target=\"inlineframe\">Relay1 is closed. Open it!</a>  -"); 
           }

            if( !digitalRead(rel2)){
              client.println("<a href=\"/?rel2off\" target=\"inlineframe\">Relay2 is opened. Close it!</a>
");  
           }
           else {
              client.println("<a href=\"/?rel2on\" target=\"inlineframe\">Relay1 is closed. Open it!</a>
"); 
           }

				
						client.println("
Temperatura=");
						if(t >=20) {
							client.println("<font color='red'>");
							client.println(t);
							client.println("*C</font>");
						}
						else {
							client.println("<font color='blue'>");
							client.println(t);
							client.println("</font>");
						}

            client.println("
Umiditatea=");
            if(h == 70) {
              client.println("<font color='green'>");
              client.println(h);
              client.println("%</font>");
            }
            else {
              client.println("<font color='red'>");
              client.println(h);
              client.println("</font>");
            }
            
						client.println("</BODY>");
            client.println("</HTML>");           
					}

					delay(1);
					//stopping client
					client.stop();

					  ///////////////////// control arduino pin
					if(readString.indexOf("ledon") >0)//checks for on
					{
						digitalWrite(4, HIGH);    // set pin 4 high
					}
					if(readString.indexOf("ledoff") >0)//checks for off
					{
						digitalWrite(4, LOW);    // set pin 4 low
					}
					  //clearing string for next read

					  //control relay 1
					if(readString.indexOf("rel1on") >0)//checks for on
					{
						digitalWrite(rel1,0);    // set pin 4 high
					}
					if(readString.indexOf("rel1off") >0)//checks for off
					{
						digitalWrite(rel1,1);    // set pin 4 low
					}

            //control relay 2
          if(readString.indexOf("rel2on") >0)//checks for on
          {
            digitalWrite(rel2,0);    // set pin 4 high
          }
          if(readString.indexOf("rel2off") >0)//checks for off
          {
            digitalWrite(rel2,1);    // set pin 4 low
          }
					readString="";
				}
			}
		}
	}

	// check to see if it's time to blink the LED; that is, if the
	// difference between the current time and last time you blinked
	// the LED is bigger than the interval at which you want to
	// blink the LED.
	unsigned long currentMillis = millis();
	if (currentMillis - previousMillis >= interval) {
		// save the last time you blinked the LED - ultima oara cand a accesat link-ul
		previousMillis = currentMillis;
		// daca a trecut timpul acceseaza din nou link-ul
		sendGET(); // acceseaza link-ul (webclient)
	}
}

void sendGET() // functia webclient -  send/receie GET request data.
{
    // temperatura
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
    client.stop();
    if (client.connect(myserver, 80)) 
	{
		Serial.println("connected");
		client.print("GET /i.php?t=");
		client.print(t);
    client.print("&h=");
    client.print(h);
		client.println(" HTTP/1.1");    
		client.println("Host: deseuridolj.ro");  
		client.println("Connection: close");
		client.println();  
	} 
	else {
		Serial.println("confail");
		Serial.println();
	}

	while(client.connected() && !client.available()) delay(1); //waits for data
	while (client.connected() || client.available()) { //connected or data available
		char c = client.read();
		Serial.print(c);
	}

	Serial.println();
    Serial.println("Decon");
	Serial.println();
	client.stop();

}
client.println("HTTP/1.1 200 OK");
client.println("<a href=\"/?ledoff\" target=\"inlineframe\">Turn the Led OFF</a>

");

etc, etc
You could save more RAM by not wasting it.

Check the use of the F() macro to place strings in PROGMEM

client.println(F("HTTP/1.1 200 OK"));

Why not go all the way with the ESP8266? the largest modules (ESP12 / ESP12-E) got 32Mbit of flash memory and plenty of processing power! All you need is to get the ESP8266 Arduino addon from Github and buy your self a NodeMCU board. The ESP supports the DHT11 library too :wink:

i deleted all serial commands and also removed html codding. now it looks like this:

Sketch uses 17,202 bytes (53%) of program storage space. Maximum is 32,256 bytes.
Global variables use 538 bytes (26%) of dynamic memory, leaving 1,510 bytes for local variables. Maximum is 2,048 bytes.

now i hope that i can use pin 1 and 2.
:smiley: thanks

can i use analog pins as digital? i'm planning to ad a display, a button, a led emitter, led receiver, and a microtransformer for measuring amperage.

considering the posted code i only have analogs, pin 1,2,8,9? are they useful for my needs? should i swith some pins?

#include <SPI.h>
#include <Ethernet.h>
#include "DHT.h"

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Adresa MAC
IPAddress ip(192, 168, 0, 123); // ip in LAN
IPAddress myDns(8, 8, 8, 8);  // server DNS
//IPAddress myserver(192,168,0,114); // ip webhost apache
char myserver[] = "deseuridolj.ro";   // domeniu webhost apache
EthernetServer server(84); // portului webserver-ului arduino
EthernetClient client;
String readString; 

#define rel1 6     // definirea numelor pentru pinii folositi in a controla releele
#define rel2 7

//////////////////////  inceput Termistor/umiditate
#define DHTPIN 2        // Digital Pin 2
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE);
//////////// sfarsit Termistor

int w = 0;
int m = 0;

// set pin numbers:
const int motionPin = 5;     // the number of the pushbutton pin
const int monitorPin =  3;      // the number of the LED pin

// variables will change:
int motionState = 0;         // variable for reading the pushbutton status






// Variabile
unsigned long previousMillis = 0;        // will store last time LED was updated

// constante
const long interval = 60000;           // intervalul pentru webclient (milliseconds)

void setup() {                         // vor fi rulate o singura data cand porneste microcontrollerul
  Ethernet.begin(mac, ip, myDns); 
  server.begin();
  //// Serial.begin(9600); 
//  // Serial.println("ok"); // Mesaj fisat in consola pentru debug
  pinMode(rel1, OUTPUT);             // pin ales pentru a controla releul 1 este setat pentru semnal de iesire
  pinMode(rel2, OUTPUT);             // pin ales pentru a controla releul 1  
  pinMode(4, OUTPUT);                  // pin ales pentru a controla led ul de pe pin-ul 4
  // initialize the LED pin as an output:
  pinMode(monitorPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(motionPin, INPUT);
  dht.begin();
}

void loop(){
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();

   w = 200;

  // read the state of the motion detect value:
  motionState = digitalRead(motionPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (motionState == HIGH) {
    // turn LED on:
    digitalWrite(monitorPin, HIGH);
    m=1;
  } else {
    // turn LED off:
    digitalWrite(monitorPin , LOW);
  }
  
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    // Serial.println("Fail read temp");
    return;
  }

  // here is where you'd put code that needs to be running all the time.
  
  EthernetClient client = server.available();
  if (client) 
  {
    while (client.connected()) 
    { 
      if (client.available()) 
      {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string 
          readString += c; 
          //// Serial.print(c);
        } 
        //if HTTP request has ended
        if (c == '\n') 
        {
          ///////////////
          // // Serial.println(readString); //print to // Serial monitor for debuging 

          //now output HTML data header
          if(readString.indexOf('?') >=0) { //don't send new page
            client.println("HTTP/1.1 204 OK");
            client.println();
            client.println();  
          }
      else {

            if( digitalRead(4)){
                client.println("LED: ON"); 
                client.println(); 
            }
            else {
              client.println("Led: OFF"); 
              client.println(); 
            }

      // conditii relee afiseaza comanda disponibila
      if( !digitalRead(rel1)){
          client.println("Relay1: OPEN"); 
      }
      else {
        client.println("Relay1: CLOSE"); 
      }

      if( !digitalRead(rel2)){
        client.println("Relay2: OPEN");  
        }
      else {
        client.println("Relay2: CLOSE"); 
      }
        client.println();  
        client.print("Temp=");
        client.print(t);
        client.println();          
        client.print("Umid=");
        client.print(h);
        client.println();  
        client.println();  
        client.print("Misc=");
        client.print(m);
        client.println();  
        client.println();  
        client.print("Pow=");
        client.print(w);
      }

        delay(1);
        //stopping client
        client.stop();

      // control arduino pin
      if(readString.indexOf("ledon") >0)//checks for on
      {
        digitalWrite(4, HIGH);    // set pin 4 high
      }
      if(readString.indexOf("ledoff") >0)//checks for off
        {
        digitalWrite(4, LOW);    // set pin 4 low
        }
        //clearing string for next read

        //control relay 1
        if(readString.indexOf("rel1on") >0)//checks for on
          {
            digitalWrite(rel1,0);    // set pin 4 high
          }
          if(readString.indexOf("rel1off") >0)//checks for off
          {
            digitalWrite(rel1,1);    // set pin 4 low
          }

          //control relay 2
          if(readString.indexOf("rel2on") >0)//checks for on
          {
            digitalWrite(rel2,0);    // set pin 4 high
          }
          if(readString.indexOf("rel2off") >0)//checks for off
          {
            digitalWrite(rel2,1);    // set pin 4 low
          }
          readString="";
        }
      }
    }
  }

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED - ultima oara cand a accesat link-ul
    previousMillis = currentMillis;
    // daca a trecut timpul acceseaza din nou link-ul
    sendGET(); // acceseaza link-ul (webclient)
  }
}

void sendGET() // functia webclient -  send/receie GET request data.
{
    // temperatura
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
    if (client.connect(myserver, 80)) 
  {
    // Serial.println("connected");
    client.print("GET /i.php?t=");
    client.print(t);
    client.print("&h=");
    client.print(h);
    client.print("&w=");
    client.print(w);
    client.print("&m=");
    client.print(m);    
    client.println(" HTTP/1.1");    
    client.println("Host: deseuridolj.ro");  
    client.println("Connection: close");
    client.println();  
    m = 0;
    w=0;
  } 
  else {
    // Serial.println("confail");
    // Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read();
    // Serial.print(c);
  }

  // Serial.println("Decon");
  // Serial.println();
  client.stop();

}