Maxbotix RS232 distance sensor - UPDATE - working



  • I have a Maxbotix distance sensor that talks over RS232. Every second, it sends the distance in MM in this format: "Rxxx\n" where xxx is a number in MM.

    Can I make this work easily with RS232? The sensor also supports analog and pulse methods. However, the sensor is inside of a septic tank. And, to change it to pulse or analog, I would have to open up the tank and get the stinky sensor out of it, solder on another wire, and put it back. I really do not feel like trying to get thing thing out of the tank to modify it.


  • Mod

    You can use SoftwareSerial to turn any two io pins on the Arduino into a serial port. Then it should be easy to read the measurements.



  • I've got a MAX232, which is apparently needed because the Maxbotix sensor has an inverted output. I've connected it to the TX and RX ports on my Nano. All of the SoftwareSerial examples I see use digital pins on the arduino, not the RX and TX lines.

    Should I connect the RX and TX on the MAX232 to digital ports instead, or is there a way to read the RX port with SoftwareSerial? Or, do I not need SoftwareSerial to read the RX port?



  • Ok, I am making some progress. I connected the MAX232 to D3 and D4, and I'm able the read the output from the sensor. One thing to note is that the MAX232 module I bought requires a null modem cable if you're not connecting it to a PC. I just popped the plug apart on the end of my cable and swapped pins 2 and 3 and then it worked.

    But, I'm having an issue. When I read the values from the sensor, I get an alternating correct reading, followed by a reading of "0". The raw output does not have a 0 in it. Here's my code, followed by the output:

    void loop() { // run over and over
      if (mySerial.available() > 0) {
        mm = mySerial.parseFloat();
        inches = mm/25.4;
        Serial.print(mm);
        Serial.print(" mm, ");
        Serial.print(inches);
        Serial.println(" inches");
      }
    }
    

    And here's the output:
    Maxbotix RS232 Mysensors Sketch
    812.00 mm, 31.97 inches
    0.00 mm, 0.00 inches
    812.00 mm, 31.97 inches
    0.00 mm, 0.00 inches
    812.00 mm, 31.97 inches
    0.00 mm, 0.00 inches
    812.00 mm, 31.97 inches
    0.00 mm, 0.00 inches
    812.00 mm, 31.97 inches

    I know I can put an if statement to filter out the zero reading. But, I'm wondering what the root cause of this is and if I can fix it a better way.



  • Ok, I put in something to filter out the zero readings. Here's the code below, which is working. I have a couple of questions though:

    1. I used code from the DistanceSensor example. It sends in cm if metric, and inches if not. It appears the controller plugin for vera expects cm, as if I create a trigger for if the distance goes above or below, it asks for it in cm. Should I change this?
    2. The controller plugin ONLY gives the option to create a trigger in cm. If the sensor is sending in inches, I'm assuming the controller plugin will interpret this number in cm, instead of knowing to convert it. Is this true?

    Anyway, here's what I have as it sits. I'm almost thinking it might be better to just remove the non-metric code and only send in cm. Thoughts?

    /*
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Jay Austad
     * 
     * DESCRIPTION
     * This sketch provides an example how to implement a distance sensor using Maxbotix sensor with RS232 output 
     * http://www.mysensors.org/build/xxx
    
     The circuit:
     * RX is digital pin 3 (connect to RXD of MAX232)
     * TX is digital pin 4 (connect to TXD of MAX232)
    
     Note:
     Not all pins on the Mega and Mega 2560 support change interrupts,
     so only the following can be used for RX:
     10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69
    
     Not all pins on the Leonardo support change interrupts,
     so only the following can be used for RX:
     8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
    
     This example uses code written by Henrik EKblad, Tom Igoe, Mikal Hart, and Jay Austad
     */
    
    #include <SoftwareSerial.h>
    #include <SPI.h>
    #include <MySensor.h>  
    
    #define CHILD_ID 1
    
    MySensor gw;
    MyMessage msg(CHILD_ID, V_DISTANCE);
    boolean metric = true; 
    SoftwareSerial mySerial(3, 4); // RX, TX
    float mm;
    float inches;
    float dist;
    float lastDist;
    
    void setup() {
      gw.begin();
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Distance Sensor", "1.0");
    
      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID, S_DISTANCE);
      metric = gw.getConfig().isMetric;
    
      Serial.println("Maxbotix RS232 Mysensors Sketch");
    
      // set the data rate for the SoftwareSerial port
      mySerial.begin(9600);
    }
    
    void loop() { // run over and over
      if (mySerial.available() > 0) {
        mm = mySerial.parseFloat();
        if (mm != 0) {
          inches = mm/25.4;
          dist = metric?mm:inches;
          Serial.print(dist);
          Serial.println(metric?" mm":" in");
        }
      
        if (dist != lastDist) {
          gw.send(msg.set(dist, 2));
          lastDist = dist;
        }
      }
    }
    


  • Can anyone give insight to my questions in the previous post?


  • Hero Member

    @signal15 the sketch will be more universal if you leave the non metric stuff in. personally I never use non metric and think the controller should handle this. Not to offend anybody but I think that a good (metric/ si) standard is the way to go.
    Translating into curious other units should be done by the user interface.



  • @AWI

    I agree that it would be nice if it was up to the controller to handle it, and then the units for different measurements sent to it were standardized on (like mm for distance). Also, it would be nice if the controller would allow selection of metric/imperial either by device, or sensor type. For example, I want my distance sensors in mm, but my temperatures in F.


Log in to reply
 

Suggested Topics

1
Online

11.4k
Users

11.1k
Topics

112.7k
Posts