Turnout Sensor Commands

{From the laptop (4) to the Raspberry Pi (3A)}


To add 24 additional sensors to the system, a second Raspberry Pi is added to provide the GPIO inputs needed.


This second Raspberry Pi will accept the DCC++ sensor commands <S...> with an ID greater than 50 and return results identical to the Base Station i.e. <O>, <X> or <Q ID PIN PULLUP>. Process <Q> commands to report the current status of each defined sensor. In addition, the <H> and <h> commands will be accepted to store or clear EEPROM in this Raspberry PI, similar to the <E>, <e> commands for the Base Station or <N>, <n> commands for the other Raspberry Pi.


Command summary

    • < -- Start of added command
    • H|h|Q|S -- command character
    • Zero, 1 or 3 data bytes
      • ID -- The numeric ID (51 - 32767) of the sensor
      • PIN -- The Raspberry Pi GPIO pin number the sensor is connected to on the Raspberry Pi
      • PULLUP -- 1 = Use the internal pull-up resistor for the PIN, 0 = don't use the internal pull-up resistor for the PIN
    • > -- End of added command-

 Command response summary

    • < -- Start of response
    • h|O|Q|q|X -- Response character
    • Zero or 3 data bytes
      • (see above)
    • > -- End of response


  • Create a new sensor with ID > 50

IF ID > 50 Associate the ID with a GPIO PIN and PULLUP. If sensor ID already exists, it is updated with the specified PIN and PULLUP.

IF ID <= 50 pass command on to Arduino (2) through first Raspberry Pi (3)

<S ID PIN PULLUP> 3 bytes

Returns: <O> if successful or <X> if unsuccessful (e.g. out of memory)

IF OK:        Set RAM only

(See <H> below)


  • Delete definition of sensor with ID > 50

IF ID > 50 Delete definition of sensor ID

IF ID <= 50 pass command on to Arduino (2) through first Raspberry Pi (3)

<S ID> 1 byte

Returns: <O> if successful or <X> if unsuccessful (e.g. ID does not exist)

IF OK:        Set RAM only

(See <H> below)


  • List all defined sensors

List all defined sensors stored on this Raspberry Pi (ID > 50) and pass the command, unchanged on to the Arduino for additional processing

<S> 0 bytes

Returns: <Q ID PIN PULLUP> for each defined senor stored on this Raspberry Pi or <X>


  • List the status of all sensors

List the status of all sensors defined on this Raspberry Pi (ID > 50) and pass the command, unchanged on to the Arduino for additional processing.

<Q> 0 bytes

Return: <Q ID> when sensor ID is active or <q ID> when sensor ID is inactive for each ID defined


  • Store Raspberry Pi (3A) RAM to EPPROM

       Store sensors with ID > 50 in this Raspberry Pi RAM into Pi EPPROM

<H> 0 bytes

       Returns: <h nsensors>


  • Erase all Raspberry Pi (3A) EPPROM

       Erase all sensors data from this Raspberry Pi EPPROM

<h> 0 bytes

Returns: <U> Done

               Erase all EPPROM data. Read EPROM into RAM


Sensor Status Change


When the status of a sensor connected to Raspberry Pi (3A) and associated with an ID changes, Raspberry Pi (3A) will return one of the following to the laptop:


  • When a sensor goes active, GPIO pin transitions from a HIGH state to a LOW state

       <Q ID>


  • When a sensor goes inactive, GPIO pin transitions from a LOW state to a HIGH state

       <q ID>


All sensors defined as described above are repeatedly and sequentially checked within the main loop of the Raspberry Pi (3A) program. For each sensor two values are stored: Last reported state and Last read state. The following logic is used to debounce and detect a change in a sensor state.


Read the NEW_State of the sensor

IF NEW_State != LAST_State

       Set LAST_State = NEW_State

ELSE

IF NEW_State != LAST_Reported

IF NEW_State == LOW

output <Q ID>

ELSE

output <q ID>

Set LAST_Reported = NEW_State

ELSE

do nothing