Open Energy Monitor library problem (emonlib)

Hi there!
(sorry for my english :wink: )

I’m trying to make a project with Arduino for reading the energy consumption of my house and the temperature of each room. I’d like to show the data in a LCD and upload them to devicehub.net with an ENC28J60 Ethernet adapter.

The problem is: when I initialize the emonlib sensors, Arduino starts doing weird things (rare characters on the screen, resets, etc).

Here is the code:

#include "DHT.h"
#include "EmonLib.h"                   // Librería para medir potencia
#include <EtherCard.h>
#include <IRremote.h>                  // Librería para mando IR
#include <LiquidCrystal.h>             // Librería para LCD


#define DHTTYPE DHT11 // Sensor de temperatura DHT 11 
#define DHTPIN 3      // Sensor de temp conectado al pin TRES
int RECV_PIN = 2;     // Mando IR conectado al pin 2
int maxh=0, minh=100, maxt=0, mint=100;  //Variables para ir comprobando maximos y minimos sensor

// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 
  0x74,0x69,0x69,0x2D,0x30,0x31 };
byte Ethernet::buffer[700];
static uint32_t timer;
char website[] PROGMEM = "www.devicehub.net";

EnergyMonitor emon1, emon2, emon3; // Creamos instancia del sensor de corriente
IRrecv irrecv(RECV_PIN);  // Creamos instancia del IR
decode_results results;   
LiquidCrystal lcd(9, 8, 7, 6, 5, 4); // Creamos instancia del LCD
DHT dht(DHTPIN, DHTTYPE);  //Creamos instancia del sensor de temperatura

// called when the client request is complete
static void my_callback (byte status, word off, word len) {

  Ethernet::buffer[off+300] = 0;
}

void setup()
{

  Serial.begin(9600);
  emon1.current(0,60.606); 
  emon2.current(1,60.606); 
  emon3.current(2,60.606); 
  
  if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0)
    Serial.println( "Failed to access Ethernet controller");
  if (!ether.dhcpSetup())
    Serial.println("DHCP failed");
  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);
  ether.printIp("DNS: ", ether.dnsip);
  if (!ether.dnsLookup(website))
    Serial.println("DNS failed");
  ether.printIp("SRV: ", ether.hisip);
   irrecv.enableIRIn(); // Inicializamos el IR
   lcd.print("Preparado");
}

void loop()
{
   int h = dht.readHumidity();  //Variable para humedad instantanea
    int t = dht.readTemperature(); // Variable para temperatura instantanea 
   maxmin(t, h);  //Calculamos máximo y minimo
  
  ether.packetLoop(ether.packetReceive());
  if (millis() > timer) {
    char queryString[256] = {
      0                                                                                                                                };
    sprintf(queryString, "?apiKey=014d8689-6cd7-4f9d-814b-152aab1c981e&hum=%d&temp=%d", (int)h,(int)t);
    timer = millis() + 5000;
    ether.browseUrl(PSTR("/io/213/"), queryString, website, my_callback);
  }
  
   else if (irrecv.decode(&results))  // Si recibimos petición del IR
     {
  
     if (results.value==3225413655)
        temperaturaLCD(t, h);  
     
     else
      {
        lcd.clear();
        lcd.setCursor(6,1);
        lcd.print("--ERROR--");
        lcd.setCursor(6,2);
        lcd.print("*********");
        lcd.setCursor(5,3);
        lcd.print(results.value, DEC);
      }  
      irrecv.resume(); // Receive the next value
      }
}





//FUNCION PARA MOSTRAR TEMPERATURA POR LCD
void temperaturaLCD (int t, int h)
{
       lcd.setCursor(0,2);
       lcd.print("   Humedad: ");
       lcd.print(h);
       lcd.print(" %");
       lcd.setCursor(0,0);
       lcd.print("     Temp: ");
       lcd.print(t);
       lcd.print(" C");
       lcd.setCursor(0,3);  
       lcd.print("Max: ");
       lcd.print(maxh);
       lcd.print(" % ");
       lcd.print("Min: ");
       lcd.print(minh);
       lcd.print(" %");
       lcd.setCursor(0,1);
       lcd.print("Max: ");   
       lcd.print(maxt);
       lcd.print(" C ");
       lcd.print("Min: ");
       lcd.print(mint);
       lcd.print(" C");
       
}  // FIN TEMPERATURALCD


  

// FUNCION PARA CALCULAR MAX Y MIN DE TEMPERATURA
void maxmin( float t, float h)
{
  if (maxh<h)
      maxh=h;
    if (h<minh)
      minh=h;
    if (maxt<t)
      maxt=t;
    if (t<mint)
      mint=t; 
}  // FIN MAXMIN

And, if I comment the following lines:

emon1.current(0,60.606);
  emon2.current(1,60.606);
  emon3.current(2,60.606);

or only initialize one of them, it all works properly.

I have no idea, can somebody give me a solution?

Thank you so much.

I have no idea, can somebody give me a solution?

Perhaps you need to swap out that 2K memory and replace it with a couple of terrabytes of memory. Oh, wait, that's not possible.

So, it's incumbent on you to not be wasting memory. http://playground.arduino.cc/Code/AvailableMemory

The F() macro bears investigation, too. http://playground.arduino.cc/Learning/Memory