2 instances of Metro causing problems with ethernet shield web server

Hi,
I'm slowly putting together an automated heater project, so far mostly by chaining together examples I've found online.

The foundation is this Arduino Ethernet Shield Web Server Tutorial with an UNO R2 and an Ethernet Shield. So far I've also added in temperature sensing (TMP35, output displayed on the webpage), and interfacing with remote control sockets to control mains appliances (fairy lights as a useful debugging tool initially, and now the heater too).

I've hit a problem when I've started adding timers for checking the temperature (every few seconds) and for switching the heater on/off according to the current temperature (at longer intervals).

If I have two instances of Metro, the web page no longer displays - I just get a blank white screen.
Activity in the serial monitor shows that the temperature logging and heater switching is still taking place thought.

I'm new to AJAX and working with the ethernet shield, can anyone explain to me what's causing this problem, please? Could there be some sort of conflict between the Metro library and the ethernet shield?
Workarounds also welcomed :slight_smile:

I've attached the sketch I'm using, rather than posting it in-thread, as it's over the character limit...

If you have to check that the timer has expired, anyway, what is the value of using the Metro class?

Record when you last did a temperature check or a heater check, and test if it's been long enough to require another check.

I don't have much experience with coding and I tend to get in a bit of a tangle with approaches that use millis() to keep track of time intervals (I assume this is the alternative method you're suggesting?).

A friend mentioned the Metro library to me last week and I find it much easier to understand and implement. I understand that this may not be the most elegant or efficient way of doing it, but as my project gains complexity, keeping as much as I can on an intuitive level will be very valuable to me. ...so that's why I'm trying Metro!

I assume this is the alternative method you're suggesting?

Yes. I don't understand why it's a problem, though. Suppose a friend called, and said "Meet be at the bar in an hour; I'll buy drinks for two hours". You could set a timer for one hour, and sit next to it until it went off. At which time, you'd be late, unless you had been standing outside the bar when the phone rang.

Or, you could see what time it is now, and then periodically check to see if it was time to meet your friend.

I can't see that the Metro class is doing any more than telling you "yes, it's time" or "no, it isn't", and then keeping track of when to next meet you friend for free drinks.

The if statement simply asks the instance if time is up. Changing it to if(millis() - lastTempCheckTime > tempCheckInterval) isn't rocket surgery.

I've since tried the Repeating Timer using elapsedMillis approach from http://www.forward.com.au/pfod/ArduinoProgramming/TimingDelaysInArduino.html and had the same problem with the webpage not showing if I had two instances of elapsedMillis.

Moving away from libraries, the top of my sketch now looks like

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>

float minTemp = 13.5;

unsigned long ReadTempInterval = 3000;
unsigned long previousReadTempMillis =0;
unsigned long HeaterSwitchInterval = 30000;
unsigned long previousHeaterSwitchMillis =0;

And the relevant part of void loop():

  unsigned long currentReadTempMillis = millis();
  
  if(currentReadTempMillis - previousReadTempMillis> ReadTempInterval) {
    // save the last time 
    previousReadTempMillis = currentReadTempMillis;
    // do stuff here each interval (interval -- an unsigned long)

 getTemp();
    Serial.print(temperatureC);     
    Serial.println(""); 

    // subtract the last reading:
    total= total - readings[index];         
    // read from the sensor:  
    readings[index] = temperatureC; 
    // add the reading to the total:
    total= total + readings[index];       
    // advance to the next position in the array:  
    index = index + 1;                    

    // if we're at the end of the array...
    if (index >= numReadings) {             
      // ...wrap around to the beginning: 
      index = 0;   
    }                        

    // calculate the average:
    average = total / numReadings;         
    // send it to the computer as ASCII digits

      Serial.print("average temp: ");   

    Serial.println(average);   

 
  }


  unsigned long currentHeaterSwitchMillis = millis();
  
  if(currentHeaterSwitchMillis - previousHeaterSwitchMillis > HeaterSwitchInterval) {
    // save the last time 
    previousHeaterSwitchMillis = currentHeaterSwitchMillis;
    // do stuff here each interval (interval -- an unsigned long)
 

    if (average < minTemp){
      simulate_button(1, 3, 1);
      delay(1000);
      Serial.print("ON"); 
      Serial.print(","); 
    }

    else {
      simulate_button(1, 3, 0);
     delay(1000);

      Serial.print("OFF"); 
      Serial.print(","); 

    }
 
  }

Same problem: with both timer sections in there the web page is blank, but the thermostat code seems to run. If I comment either one out then the web page displays and updates as expected.

What could be causing the ethernet part of the sketch to fail? I'm not changing any of the code related to the html or xml generation...

If I comment either one out then the web page displays and updates as expected.

This suggests that you are running out of memory. Arduino Playground - HomePage

nods
I was wondering about that.

I've just soldered some headers onto an old Seeduino Mega I've got and will experiment with transferring the shield/code onto that later today.

Thanks for the link to the page for getting values for available memory - I wasn't aware of those.

Okay, back online after moving to Mega!

Thanks for your help, PaulS.