Timer interrupts ethernet - MQTT library

Hello,

i tried to use the ETHERNET and MQTT library in connection with timer interrupts.
The programm works and stops after a while, no timer interrupt is generated anymore. What is the reason for disabling the interrupts?

Attached the programm:

#include <Ethernet.h>
#include <MQTT.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip[] = {192, 168, 1, 177};  // <- change to match your network

EthernetClient net;
MQTTClient client;

unsigned long timecount = 0;

void connect() {
  Serial.print("connecting...");
  while (!client.connect("arduino", "public", "public")) {
    Serial.print(".");
    delay(1000);
  }

  Serial.println("\nconnected!");

  client.subscribe("/hello");
  // client.unsubscribe("/hello");
}

void messageReceived(String &topic, String &payload) {
  Serial.println("incoming: " + topic + " - " + payload);

  // Note: Do not use the client in the callback to publish, subscribe or
  // unsubscribe as it may cause deadlocks when other things arrive while
  // sending and receiving acknowledgments. Instead, change a global variable,
  // or push to a queue and handle it in the loop after calling `client.loop()`.
}

void setup() {
  cli(); 
 /* // Pin change IRQ control register
  // X X X X X PCIE2 PCIE1 PCIE0
  PCICR |= 0b00000100;  
  // Pin Change Mask Register 0
  // PCINT7 PCINT6 PCINT5 PCINT4 PCINT3 PCINT2 PCINT1 PCINT0
  PCMSK0 |= 0b00000000; 
  // Pin Change Mask Register 0
  // PCINT15 PCINT14 PCINT13 PCINT12 PCINT11 PCINT10 PCINT9 PCINT8
  PCMSK1 |= 0b00000000; 
  // Pin Change Mask Register 0
  // PCINT23 PCINT22 PCINT21 PCINT20 PCINT19 PCINT18 PCINT17 PCINT16 
  PCMSK2 |= 0b00001111; 

  DDRK = 0x00;   // Port K as INPUT (1 = OUTPUT)
  PORTK = 0xFF;  // pullup enable 
*/
  //These are for Timer1
  #define PRESCALE0_1 1u
  #define PRESCALE0_8 2u
  #define PRESCALE0_64 3u
  #define PRESCALE0_256 4u
  #define PRESCALE0_1024 5u

  //set 16bit timer5 interrupt at 2kHz ISR(TIMER5_COMPA_vect)
  TCCR4A = 0; // set entire TCCR2A register to 0
  TCCR4B = 0; // same for TCCR2B
  TCNT4  = 0; //initialize counter value to 0
  // set compare match register for 1ms
  OCR4A = 0x0800;
  // turn on CTC mode
  TCCR4B |= (1 << WGM42);
  TCCR4B |= PRESCALE0_8;
  // enable timer compare interrupt
  TIMSK4 |= (1 << OCIE4A);

  //set 16bit Timer1 PhaseCorrect-PWM
  TCCR1A = 0u;
  TCCR1B = 0u;
  TCCR1A |= (1 << WGM11)|(1 << COM1A1);
  TCCR1B |= (1 << WGM13)|(1 << WGM12);
  OCR1A = 120u;
  ICR1 = 256u;
  TCCR1B |= PRESCALE0_64;
  
  sei();//allow interrupts
  Serial.begin(9600);
    
  Serial.println("Ethernet connect");

  Ethernet.begin(mac, ip);
  client.begin("192.168.1.120", net);
  client.onMessage(messageReceived);
  connect();

  //wdt_enable(WDTO_1S);
}

//timer4 interrupt 2kHz
ISR(TIMER4_COMPA_vect)
{
  // publish a message roughly every second.
  timecount++;
  if ( timecount > 2000) 
  {
    Serial.println("Timer ready ...");
    client.publish("/hello", "world");
    timecount = 0u;
  }
}

void loop() 
{
  client.loop();
  if (!client.connected()) {
    connect();
  }
}

Kind regards
Maxl

You must not use any code depending on interrupt to be working inside an interrupt handler. Both the serial and the Ethernet library depends on interrupts.

You use timer interrupts to do a task once a second? Don't know how to use millis()? Take a look at the BlinkWithoutDelay example!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.