The Arduino OPTA jam occurred inexplicably, without any apparent cause

I have created a sketch in Arduino IDE for publishing MQTT messages. I have tested ArduinoMqtt with WifiClient and it worked perfectly, but when I try an identical approach with an ethernet client (Just modify the part that refers to connecting to Wifi ) OPTA behaves unexpectedly. LED above reset starts blinking red, and I have to double tap reset in order to make OPTA capable of receiving upload again. Here is the code (I have put some println's to locate the point of failure), the last printed string is "Entered loop".

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


EthernetClient ethClient;
MqttClient mqttClient(ethClient);

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { Some mac address };

IPAddress ip(. . . . );
IPAddress myDns(192, 168, 0, 1);

const char broker[] = "....";
int        port     = .....;
const char topic[]  = "TOPIC";
const char username[] = ".....";
const char password[] = ".....";

char config[8];
static const uint8_t analogArr[8]={A0,A1,A2,A3,A4,A5,A6,A7};
static const uint8_t digitalArr[8]={D0,D1,D2,D3,D4,D5,D6,D7};

//set interval for sending messages (milliseconds)
const long interval = 8000;
unsigned long previousMillis = 0;


void setup()
{
/*
    // Configure static IP address
	IPAddress ip(192, 168, 0, 188);
	IPAddress dns(8, 8, 8, 8);
	IPAddress gateway(192, 168, 0, 1);
	IPAddress subnet(255, 255, 255, 0);
	// If cable is not connected this will block the start of PLC with about 60s of timeout!
	eth.begin(ip, dns, gateway, subnet);
*/
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  //Output LED
  pinMode(LED_USER, OUTPUT);

  Serial.println("Initialize Ethernet with DHCP:");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
      while (true) {
        delay(1); // do nothing, no point running without Ethernet hardware
      }
    }
    if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }
    // try to configure using IP address instead of DHCP:
    Ethernet.begin(mac, ip, myDns);
  } else {
    Serial.print("  DHCP assigned IP ");
    Serial.println(Ethernet.localIP());
  }
    
  Serial.println("You're connected to the network");
  Serial.println();

  delay(1000);

  Serial.print("Attempting to connect to the MQTT broker: ");
  Serial.println(broker);
  mqttClient.setUsernamePassword(username, password);
  if (!mqttClient.connect(broker,port)) {
    Serial.print("MQTT connection failed! Error code = ");
    Serial.println(mqttClient.connectError());

    while (1);
  }

  Serial.println("You're connected to the MQTT broker!");
  Serial.println();
  delay(1500);
  sprintf(config,"DDADDDDA");

}

void loop() {
  // call poll() regularly to allow the library to send MQTT keep alive which
  // avoids being disconnected by the broker
  Serial.println("Entered loop");
  if (!mqttClient.connected()) {
        digitalWrite(LED_USER,HIGH);
        reconnect();
        digitalWrite(LED_USER,LOW);
  }
  Serial.println("Still connected");
  mqttClient.poll();
  Serial.println("After mqtt.poll");
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time a message was sent
    previousMillis = currentMillis;
    sendData();
    Serial.println("Message sent");
  }
}

void reconnect()
{
    while(!mqttClient.connected())
    {
      mqttClient.connect(broker,port);
      Serial.println("Attempting to recconect...");
      delay(2000);
    }
    Serial.println("Reconnected successfully");
}

void sendData()
{
  mqttClient.beginMessage(topic);
  Serial.println("Starting to iterate");
  for (int i=0; i<8; i++)
    {
      char buffer[13];
      if (config[i]=='A')
      {
        sprintf(buffer,"AnalogIn%d: %d",i+1,analogRead(analogArr[i]));
        mqttClient.write((unsigned char*)buffer,sizeof(buffer));
     
      }
      else if(config[i]=='D')
      {
        sprintf(buffer,"DigitalIn%d: %d",i+1,digitalRead(digitalArr[i]));
        mqttClient.write((unsigned char*)buffer,sizeof(buffer));
      }
    }
  Serial.println("Finished iterating");
  mqttClient.endMessage();
}
2 Likes