Go Down

Topic: Arduino Uno with Ethernet shield stops working after a few days (Read 155 times) previous topic - next topic

2stimpy

For a project to monitor analog sensors at a gate I bought an Arduino Uno with an Ethernet shield.
I found it a cheap solution to my needs.

With the help of this forum I got 4 sensors working to send an MQTT message on state change.
It worked perfect on the bench.

After installing it in a pilar at the gate, it worked fine for a week or so. But then suddenly stopped working.
After cutting power to the power adapter and reconnecting ot to power, the Uno starts again and works for a while again.
It can be a few day, or a week. But then it stops again.

This is not reliable so I need to find out why this is happening.

The thing that i did not know before is that I have cheap replica boards.

I am happy to buy Original Boards if i can be sure that these will run reliable for a long time without having to fiddle with it.



Juraj


2stimpy

Yes, this is the code.

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <Bounce2.h>

// Set led variables to Arduino digital pins
const byte led1 = 2;

// Set button variables to Arduino digital pins
const byte button1 = 7;

// Set variable values initially to LOW (and not HIGH)
byte led1Value = LOW;           

//---------------------------------------------------------------------------

// Arduino MAC address is on a sticker on your Ethernet shield
// must be unique for every node in same network
// To make a new unique address change last letter

byte mac[]    = { *, *, *, *, *, * }; 

// Unique static IP address of this Arduino - change to adapt to your network
IPAddress ip(*, *, *, *);

// IP Address of your MQTT broker - change to adapt to your network
IPAddress server (*, *, *, *);

// Handle and convert incoming MQTT messages ----------------------------------------
void callback(char* topic, byte* payload, unsigned int length) {
  // handle message arrived
  String content="";
  char character;
  for (int num=0;num<length;num++) {
      character = payload[num];
      content.concat(character);
  } 
  Serial.println(topic);
  Serial.println(content); // message sent out by button actions is returned from broker and serial printed

// Set specific virtual switches on basis of specific incoming messages ----------------------------
    if (content == "1on") {
    led1Value = HIGH;
  }
 
  if (content == "1off") {
    led1Value = LOW;
  }
   
  // Set digital pin states according to virtual switch settings 
  digitalWrite(led1,led1Value);
}

EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);

// Initiate a bouncer instance for each button
Bounce bouncer1 = Bounce();


//-------------------------------------------------------
void setup()
{ // setup led, button, bouncer 1 -----------------------
  pinMode(led1, OUTPUT);
  pinMode(button1,INPUT_PULLUP);
  digitalWrite(button1,HIGH);
  bouncer1.attach(button1);
  bouncer1.interval(25);

  // Setup serial connection
  Serial.begin(9600);

  // Setup ethernet connection to MQTT broker
  Ethernet.begin(mac);
  if (client.connect("arduino-ip-238", "*****", "*****")) {   // change as desired - clientname must be unique for MQTT broker
    client.publish("led","hello world - here arduino ip 239");
    Serial.println("connected");
    client.subscribe("led"); // subscribe to topic "led"
  }
}
//----------------------------------------------
void loop()
{
// Listen for button interactions and take actions ---------------------------------------- 
// Note: Button actions do send MQTT message AND do set led(x)Value to HIGH or LOW
if (bouncer1.update())
{
    if (bouncer1.fell()) { //button pressed, state change to LOW
      {
        led1Value = HIGH;
        Serial.println("LED1 ON");
        digitalWrite(led1, led1Value);
        client.publish("led","1on");               
      }
    }   else if (bouncer1.rose()) { //button released
      {
        led1Value = LOW;
        Serial.println("LED1 OFF");
        digitalWrite(led1, led1Value);
        client.publish("led","1off");
      }
    }
  }
//------------------------------------------------ 
client.loop();
}
// End of sketch ---------------------------------

Juraj

then don't use it.
https://majenko.co.uk/blog/evils-arduino-strings

2stimpy

Ah okay, thanks.

I will try and figure out how to get rid of the string.
My coding is at beginners level.

I don't know if there are also strings used in these components.
 <SPI.h>
 <Ethernet.h>
 <PubSubClient.h>
 <Bounce2.h>

Probably not.

Go Up