If I understand correctly, the external devices each have two connections/wires. One is connected to GND, the other is either open or connected to 24V - right?
If that's the case, your only chance is to check the connection to GND of the external devices. So that we don't get confused here, we rename signal GND to the first external device at the connector to GNDext1.
You disconnect GNDext1 from the GND of the STM32 board at the connector.
Then you connect GNDext1:
- via a pull-up to VDD of the STM32,
- to the drain of a small n-channel logic-level MOSFET and
- to one of the ADC inputs of the STM32.
The source of the MOSFET then needs to be connected to GND of the STM32 board and the gate is controlled by a GPIO the STM32.
By disabling the MOSFET (gate = low) and measuring the ADC voltage, there are then two possibilities:
- ADC input ~ 0: device is connected
- ADC input ~ VDD: device disconnected
For the normal state, simply switch the MOSFET back on.
Of course you have to do the same with the 2nd channel, i.e. GNDext2.
Does it answer your question?
Good luck!
Regards
/Peter