Arduino Mega MQTT issues - Lost connection to MQTT Broker

Have you thought about logging things in the serial on the arduino too? So you can see any errors on there? Like it hanging and not looping correctly?

From your sketch, it connects, broadcasts hello world, and then does nothing. It probably disconnects because you aren’t telling it to do anything.

In your loop, put a delay (1000) should be fine, and do a client.publish(“I’m HERE!”) to your broker.

Could you give me an example of the delay in the void loop or client loop?

void loop(){
  client.loop();
  delay(1000);
  client.publish("topic/on/broker", "I'm still here!");
}

Thanks for the example, but i can see my broker receive that message on client.publish, but after 10-15 messages its stopped, i can still ping my ethernet shield / Arduino.

Any other things i can try?

DK,

Try mqtt_reconnect_nonblocking sketch in the PubSubClient library. I have 3 megas with W5100 ehternet shield based on that one so I can confirm this works.

One is reading an array of photoresistors looking at LED lights on 3 old domotics controllers each 12 outputs and reporting lights on/off and is switching those outputs on/off over infrared. So those are 36 MQTT lights with status topic and command topic.

Other one is reporting air and water temperature and controlling valves for a pool solar heating installation. This is an MQTT sensor and switches.

Last one is a lawn sprinkler controller - getting status of 4 zones and switching zones on and off, so again status and command topic.

Combined they have sent thousands of messages and this is super stable, only reason to restart them is because I’m extending functionality.

Jhh

This doesn’t indicate anything really except that the device is still connected to the network.

Yearh, so its should not be the board.

Right now i get message more than 3 minutes now, so i will keep testing it.

I will like to see your setup @jhhbe

Meaby something with the content on SD card on the W5100 Ethernet shield?

Hmm - there is some material on memory card and ethernet which I don’t recall as I don’t use a memory card on the ethernet shield.

If you have multiple shields you do need to set a different MAC address for each one that you have.

I’m travelling but let me know what you are interested in and I can take some pics.

Jhh

I only have one Arduino Mega, but it will be nice to see your sketch and your wire diagram of the system, sound like what i want to be :slight_smile:

I need to use the SD card before its even working for me.

Sorry, i took out the SD Card now, and its still connected.

setting a delay of 100 ms its seems to done the job man.
Can someone explain me why this little amount of delay can keep it for spinning?

It’s trying to run too fast. Your loop was running at 100% CPU usage as fast as it can. Always put some delay in it.

The sketch its so small and minimal, but thanks :slight_smile:

@jhhbe Could you post screenshot of the system you are running, how did you wire you sensor and what sketch are you using.

I want some multisensors around my house that is running on Arduino Mega :slight_smile:

1 Like

Dk,

This is what the modules look like:

IMG_1532

and this is the photoresistor sensor array that is looking at the LEDs on the modules (if I had a 3D printer it would look nicer but it works anyway):

IMG_1533

and that is the box where it happens, 2 ethernet cables per sensor - 1 GND and 6 resistors. In the top left you see a Niko IR receiver and that is what I use to switch things on/off. Each sensor bar is connected to a 16 port analog multiplex chip so there is a lot of looping to read everything but you need 1 analog input per 12 inputs. (I waste 4 inputs per chip but wiring and coding is a lot cleaner).

IMG_1534

the sketch keeps the arduino so busy hat the IR library stopped working, timing was getting off so IR couldn’t be recognized. Solution was to connect an arduino nano over serial to emit the IR.

It has a webserver to switch things on/off but then I saw Home Assistant and was glad someone else offered a very nice frontend (and much more). It still has a webpage for timers so I can switch some lights on/off to simulate presence.

My problem was easy: the LEDs indicate on/off, if I notice a second late that something has changed that is not the end of the world. In your setup all switches would need to have interrupts so you don’t miss anything. Also todays systems use a bus and switches put a message on the bus which reduces cabling quite a bit.

There are some threads with modified inwall MQTT switches here on the board which may be an easier way to achieve what you want.

Jhh

1 Like

Hi Jhh, looks good, how are your Arduino Mega/Nano connected?
And is this system only for lights, not mains.

Dk,

There is no high voltage involved at all. I only look at lights, a magnetic sensor to see if the garage door is open and an ultrasonic sensor to see how thick the roll of my rolling shutters is - thick roll means they are up (though translating the thickness of the roll to a % open/closed was too ambitious).

The arduino is connected to an analog multiplex chip as in below diagram:

wiring

This is the wiring for sensorbar 3 which is wired to patch panel port 5 and 6 on the last picture higher up. (corresponds to 1st and 2nd half of nikobus module 3). (the label on the enable pin top right is wrong that one goes to 5V, not to 5-5)

A0-A3 are the address lines connected to 4 digital outputs on the arduino. Pin 28 OUT top left goes to an analog input on the arduino. So by stepping through all the addresses I can read 13 sensors pin by pin on the 506A chip to a single analog input. That one then senses if the LED on the module is on or off and informs Home Assistant through MQTT.

Switching a light on/off relies on the nikobus IR receiver - as my sketch messed up the timing of the arduino IR library I decided to link the MEGA to a NANO. The NANO scans the serial port and if it gets a command in over serial it emits that through infrared. (I connected common GND for the 2 boards and serial TX from the MEGA to serial RX on the NANO).

I don’t swich/sense high voltages and it’s a non-invasive solution: I look at LED lights and I send IR so no hacking of my installation was required.

Now that you can send MQTT reliably I think your remaining challenge is most likely on the arduino side - there is a quite busy arduino forum too.

Good luck!

Thanks for the time you spent on giving info regarding your setup.
How does your part of the arduino sketch on your arduino mega code look like then you use the magnetic sensor and ultrasonic sensor.

What i have on my sketch is only button pressed and then sent out mqtt info to my mqtt broker, but i will like to get magnetic/ultrasonic sensors mqtt aswell.

Could you sent that to me aswell, and that will be awesome :slight_smile: