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.
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.
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):
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).
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.
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:
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.
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