IR Sensors and CMRI

Wadloper87 Feb 19, 2021

  1. Wadloper87

    Wadloper87 TrainBoard Member

    26
    2
    2
    Hi All.

    I don't know if the way I'm trying to do thing is too outdated, but I'm having an issue using CMRI with JMRI.

    I've set up an arduino nano with three leds (Red, yellow, green). These are connected to pins D2, D3, D4. I have set these up in JMRI and these seem to work properly! I can change aspects and such.

    Now to the sensors. I have these hooked up to D7 and D8. I have confirmed the sensors give a signal and work properly. So it's not the sensors either.

    The problem seems to be that CMRI does not communicate the sensor status back to JMRI. Since I can operate the LED's without a problem, I'm wondering where I'm going wrong. The few google results available offer mostly the same info and do not mention issues like this. I can see activity in the CMRI console when operating the LED's, but nothing happens when I actuate a sensor manually (by putting a loco next to it). I've included my code below.

    And no, it's not the invert operation that's missing. It should still flip the block depending on what the sensor sees, just inverted.

    CMRI is nice because it's very cheap, but after messing with this for two days I think it might be more economical to just buy a couple of accessory decoders :')


    Code:
    #include <CMRI.h>
    #define CMRI_ADDR 1
    #define Block1Pin 7 // D7
    #define Block2Pin 8 // D8
    #define RedPin 2 //D2
    #define YellowPin 3 //D4
    #define GreenPin 4 //D5
    CMRI cmri; // defaults to a SMINI with address 0. SMINI = 24 inputs, 48 outputs
    bool Sensor1Value = digitalRead(!Block1Pin);
    bool Sensor2Value = digitalRead(!Block2Pin);
    
    void setup() {
     
      pinMode(RedPin, OUTPUT);
      pinMode(YellowPin, OUTPUT);
      pinMode(GreenPin, OUTPUT);
       pinMode(Block1Pin, INPUT_PULLUP);
      pinMode(Block2Pin, INPUT_PULLUP);
      Serial.begin(9600, SERIAL_8N2); // make sure this matches your speed set in JMRI
    //  Serial.println("CMRI");
    //  Serial.print("inputs_and_outputs");
    //  Serial.print("19-02-2021");
    }
    
    void loop() {
      // 1: build up a packet
      cmri.process();
     
      // 2: update outputs
      digitalWrite(RedPin, cmri.get_bit(0));
      digitalWrite(YellowPin, cmri.get_bit(1));
      digitalWrite(GreenPin, cmri.get_bit(2));
    
     
      // 3: update inputs (invert digitalRead due to the pullups)
      cmri.set_bit(0, ReadSensor(Block1Pin));
      cmri.set_bit(1, ReadSensor(Block2Pin));
    }
    
    bool ReadSensor(char InputPin) {
    bool SensorValue= digitalRead(InputPin);
      if(SensorValue == 0) {
    //    Serial.print("SensorValue:");
    //    Serial.println(SensorValue);
        return SensorValue;
      }
      if(SensorValue == 1) {
    //    Serial.print("SensorValue:");
    //    Serial.println(SensorValue);
        return SensorValue;
      }
    }
    
     
  2. Chris Hall

    Chris Hall TrainBoard Member

    188
    410
    11
    I'm by no means an arduino guru and have no idea what all the bool readSensor stuff is, but I managed to get my IR sensors working in JMRI with CMRI by just using

    cmri.set_bit(0, !digitalRead(Block1Pin));

    I have a node system up and running, each node measures block detection, both reed and IR sensors and operates lights. My code is not pretty but it works.
     
  3. Wadloper87

    Wadloper87 TrainBoard Member

    26
    2
    2
    I'm assuming your code is largely based on the example code provided with the library. Mine is too.

    My code does exactly the same (minus the inversion, although I've tried using it and the results are exactly the same: No change in JMRI). the only difference is it's able to read both sensors using one function (saves on having to type the same thing twice)

    I wonder what Arduino you are using. Since I'm using a nano I'm wondering if that is the source of my frustration.

    To be clear, I'm getting exactly the same result using the Inputs_and_outputs_ example sketch with only modified pin numbers.

    The weird thing to me is I'm seeing no output from the arduino on JMRI's CMRI console. I can see the data for the LED's being sent, but it's not receiving anything from the arduino. Even though I've tested using the arduino's serial monitor both sensors and the arduino's input are working as they should. But somewhere In converting it to the bits for CMRI, or CMRI sending the bits to the computer, it goes wrong.
     
  4. Wadloper87

    Wadloper87 TrainBoard Member

    26
    2
    2
    Just to simply verify the issue I've tried using the default example sketch. This is how it's supposed to work, and still I see no bits received in JMRI's CMRI console

    Code:
    #include <CMRI.h>
    CMRI cmri; // defaults to a SMINI with address 0. SMINI = 24 inputs, 48 outputs
    void setup() {
    Serial.begin(9600, SERIAL_8N2); // make sure this matches your speed set in JMRI
    for (int i=2; i<=5; i++) { pinMode(i, OUTPUT); }
    for (int i=6; i<=9; i++) { pinMode(i, INPUT); digitalWrite(i, HIGH); }
    }
    void loop() {
    // 1: build up a packet
    cmri.process();
    // 2: update outputs
    digitalWrite(2, cmri.get_bit(0));
    digitalWrite(3, cmri.get_bit(1));
    digitalWrite(4, cmri.get_bit(2));
    digitalWrite(5, cmri.get_bit(3));
    // 3: update inputs (invert digitalRead due to the pullups)
    cmri.set_bit(0, !digitalRead(6));
    cmri.set_bit(1, !digitalRead(7));
    cmri.set_bit(2, !digitalRead(8));
    cmri.set_bit(3, !digitalRead(9));
    }
    
     
  5. Wadloper87

    Wadloper87 TrainBoard Member

    26
    2
    2
    just to verify I am understanding correctly:
    the inputs are on card 3 of node 1.
    so output 1 is 1001
    and input 1 is also 1001
    or am I misreading the documentation and is input one on address 3001?
     
  6. Ash

    Ash TrainBoard Member

    106
    67
    8
    The numbering in my JMRI setup for System Names starts at 1, but Arduino code starts at 0.

    Tools / Tables / Turnouts shows System Names CT1001-CT1048 for outputs
    0-15 - turnout servos connected to PCA9685
    16-31 and 32-47 - MCP23017 - LEDs and relays

    Tools / Tables / Sensors shows System Names CS1001-CS1016 for inputs
    0-15 - MCP23017 - buttons and occupancy detectors (using bridge rectifier and optoisolator)

    CS1017-CS1024 for inputs 16-23 could also be used as CMRI can have 24 inputs

    I have three Arduino Mini boards connect to my RS485 buss. System names for second Arduino are CT2001-CT2048 and CS2001-CS2016.

    You might find something useful in the code for the PCA9685 and MCP23017 modules. Item 6 in this post: https://www.trainboard.com/highball...-out-card-for-jmri.116454/page-2#post-1141569

    The only difference between the code on each Arduino is the CMRI_ADDR number. All logic is handled in JMRI.

    Note that you can adjust the numbering/address of each MCP23017 module. I had swapped addresses 1 and 2 to use a board with a different layout. The code could be modified to use any mix of inputs or outputs boards.

    // Define MCP23017 modules
    mcp0.begin(1); // address 1 - mcp0 is the inputs module
    mcp1.begin(0); // address 0 - mcp1 - outputs
    mcp2.begin(2); // address 2 - mcp2 - outputs

    There are some translate-expressoarduino links in the CMRI_RS485.pdf file attached to post 36. This one should help with JMRI setup:
    https://translate.google.com/transl...8/conexao-e-configuracao-da-interface-48.html

    In reviewing my setup this evening, I found the setup on my test bench was not communicating to CMRI, as it had lost the node configuration. Edit / Preferences / Connections / CMRI / Configure Nodes. And I used the expressarduino page, but used the 9600 baud rate to match my code.

    You might notice that my code is using cmri.set_byte instead of cmri.set_bit -- interesting to see how reading the modules is handled as a byte -- 8 pins at a time, and checking the byte to see if there is a change that needs to be sent to JMRI.
     
  7. Chris Hall

    Chris Hall TrainBoard Member

    188
    410
    11
    Sorry, but this might be telling you to suck eggs, but when you set up the CMRI connection in JMRI did you change the Baud rate to 9600?

    I have a vague memory that I had a similar issue when I first started down this path and after days of checking connections, arduino code, swapping boards, it wasn't until I created a new CMRI connection that I found the little click box for additional connection settings and (by default) was set to 19,200. When I changed that to 9600 the boards burst into life and everything was good.
     
  8. Wadloper87

    Wadloper87 TrainBoard Member

    26
    2
    2
    Valid suggestion, I failed to mention I checked this :) I set the baud rate at the same rate used in the sketch (9600)
     
  9. Wadloper87

    Wadloper87 TrainBoard Member

    26
    2
    2
    I am currently connecting the sensors and LED's directly to the arduino pins. No Shift registers or multiplexers or anything like that in between. I wanted to get it working first before investing in expanding the system (and I was basically thinking of using one nano per module). But since this seems so unreliable I might save up for a first accesory decoder (or try to use a nano that way).

    I have checked the node configuration and this was still present in my setup.

    Last thing I did before going to bed was archive my .jmri folder and let JMRI create a new one, and I used a new build of JMRI, the 4.20 branch. I did see some improvement in the sense that the CMRI console in JMRI was now filled with receiving bytes. I slept very well!

    However when moving a hand or loco in front of any of the sensors, nothing in the received data changed so something is still amiss.
    Booted up again this morning, and now I am facing an empty console once again, but that might be related to the power outage I had just before bedtime.

    Another issue is now I have to add all my signal heads again, which I was hoping to avoid.

    the only thing left for me before I do a clean reinstall is to update the arduino IDE on the computer in question, and hope that fixes my issue. Still hoping that someone offers that obvious solution that I am still missing :p
     
  10. Chris Hall

    Chris Hall TrainBoard Member

    188
    410
    11
    I had a go with your code on an arduino uno, through CMRI on usb to JMRI. Admittedly I didn't use IR sensors, they were downstairs and I had two reed sensors set up for testing on my desk.

    Flashed it all up and everything works ok, my sensors in JMRI register when a magnet is placed nearby the reed switch and the LEDs function correctly.

    I'm picking you might have some sort of hardware issue, maybe through the shift register or a dodgy Nano?
     
  11. olequa

    olequa TrainBoard Member

    15
    7
    18
    You did turn on polling, right?
    George
     
  12. olequa

    olequa TrainBoard Member

    15
    7
    18
    Sorry, reading back through the thread I see that you are receiving data. Apparently you are not seeing a bit toggle in that data. Have a look at the cmri network metrics in cmri.
    George
     
  13. Wadloper87

    Wadloper87 TrainBoard Member

    26
    2
    2
    No, not using any shift registers. Just using a stock nano (old bootloader). Tried with 2 different nano's and have th e same result. I'm thinking it might be JMRI's issue, since I was shortly receiving data after making a new JMRI profile (moved the old one to a backup folder). However the next day (today) it was back to not receiving data.

    Will try with a fresh install on a different SD card and see what the results will be later.
     
  14. Wadloper87

    Wadloper87 TrainBoard Member

    26
    2
    2
    So it's not the code :D vindication

    it would be weird that both nano's apparently have this issue. I'm almost thinking SD card or JMRI install corruption. Could be, the SD card has been lying around for a while.
     
  15. Wadloper87

    Wadloper87 TrainBoard Member

    26
    2
    2
    I checked, and yes. Will check the network metrics per your suggestion.

    Thanks everyone so far! Will be back later with results
     
  16. olequa

    olequa TrainBoard Member

    15
    7
    18
    After the new install of JMRI are your signal heads still working as controlled from JMRI? When you look at the CMRI Command Monitor in JMRI does it show outbound data and inbound data? If nothing is happening it should show just two repeating messages, "Poll ua=1" and "Receive ua=1" with bytes following. Is that what you are seeing? With nothing but zeroes in the byte string?
    If you look at cmri/list assignments does that look right showing the inputs and the outputs as you would expect?
    I know this contraption works as I have one setup using a Nano which sends 44 bytes and polls 16 bytes on a 19200 serial connection. It works flawlessly. I'm using the same mechanism in the Nano as you are.
    Also, make sure you save your configuration religiously in JMRI. It's not good at protecting the user against data loss.
    Another thing, you will lose your cmri configuration completely if the serial connection is changed because a different usb port is connected. I brought this up in the groups.io jmri forum and received a reply that this is a known problem that is not easy to protect against.
    g
     
  17. Wadloper87

    Wadloper87 TrainBoard Member

    26
    2
    2
    Your comment about the USB issue is very helpful, as I've switched nano's and this inevitably leads to the issue you mention. I'll stick to using one nano. Also, that seems like it's not a great issue to run in to if you're a DCC++ user, since those are arduino's as well.

    About saving the config: I can confim. I wish it were a little clearer what I was saving from certain windows, but you live and you learn I guess.

    EDIT: another argument for using 1 node with USB to communicate with the rest over serial
     
    Last edited: Feb 20, 2021
  18. Wadloper87

    Wadloper87 TrainBoard Member

    26
    2
    2
    Well jeez, after fooling around some I discovered 2 errors in my original configuration.

    First was the node number. address 0 in the arduino is address 1 in JMRI. Discovered this after configuring an extra node and the terminal burst to life again.

    second error was the address used for the sensors. Instead of using 1001, 1002 , etc. the sensors worked using single digit addresses. If I understood correctly, 1001 and 1 are the same address. But it seems I misunderstood.

    anyhoo, sensors work. Yay!
     
    Chris Hall likes this.
  19. Chris Hall

    Chris Hall TrainBoard Member

    188
    410
    11
    Glad you got it working, took me a while to get my head around how everything is numbered in JMRI and on the arduino.
     
  20. Wadloper87

    Wadloper87 TrainBoard Member

    26
    2
    2
    Yeah, the weird thing is it only seems to work if I have a node with address 0 configured as well. without either one, it doesn't seem to work as expected. Will do some more trial and error research once I get into the layout again.
     

Share This Page