Problem with adding sonar sensor to another sketch



  • Code below. I got a Maxbotix distance sensor working over RS232. I added some code afterwards to support a second distance sensor, the standard one in the examples. I put it in inclusion mode (Vera) and it said one device found. However, it doesn't show up, and the serial.pring messages from the new sensor always shows 0cm. Am I missing something in this code???

    The Maxbotix sensor still works just fine. It's only the new one that doesn't show up in my controller and shows 0cm in the serial output.

    /*
     * 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
     
     Receives from the hardware serial, sends to software serial.
     Receives from software serial, sends to hardware serial.
    
     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>  
    #include <NewPing.h>
    
    #define CHILD_ID1 1
    #define CHILD_ID2 2
    
    #define TRIGGER_PIN  5  // Arduino pin tied to trigger pin on the ultrasonic sensor.
    #define ECHO_PIN     6  // Arduino pin tied to echo pin on the ultrasonic sensor.
    #define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
    unsigned long SLEEP_TIME = 1000; // Sleep time between reads (in milliseconds)
    
    MySensor gw;
    NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    MyMessage msg1(CHILD_ID1, V_DISTANCE);
    MyMessage msg2(CHILD_ID2, V_DISTANCE);
    
    boolean metric = true; 
    SoftwareSerial mySerial(3, 4); // RX, TX
    float mm1;
    float cm1;
    float inches1;
    float dist1;
    float lastDist1;
    float mm2;
    float cm2;
    float inches2;
    float dist2;
    float lastDist2;
    
    
    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_ID1, S_DISTANCE);
      gw.present(CHILD_ID2, 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
      //Maxbotix sensor, child ID 1
      if (mySerial.available() > 0) {
        mm1 = mySerial.parseFloat();
        if (mm1 != 0) {
          inches1 = mm1/25.4;
          cm1 = mm1/10;
          dist1 = metric?cm1:inches1;
          Serial.print(dist1);
          Serial.println(metric?" cm":" in");
        }
      
        if (dist1 != lastDist1) {
          gw.send(msg1.set(dist1, 2));
          lastDist1 = dist1;
        }
      }
    //sonar sensor, child ID 2
      int dist2 = metric?sonar.ping_cm():sonar.ping_in();
      Serial.print("Ping: ");
      Serial.print(dist2); // Convert ping time to distance in cm and print result (0 = outside set distance range)
      Serial.println(metric?" cm":" in");
    
      if (dist2 != lastDist2) {
          gw.send(msg2.set(dist2));
          lastDist2 = dist2;
      }
    
      gw.sleep(SLEEP_TIME);
      
    }
    

  • Mod

    This part is strange:

    float mm2;
    float cm2;
    float inches2;
    float dist2;
    float lastDist2;
    

    mm2, cm2 and inches2 are not used, so they can be removed
    dist2 is (re)defined in the loop so it should not be defined here. Remove it.
    lastDist2 should be int, not float

    But none of that should affect the ability to read the distance. Maybe double/triple check the wiring?



  • @mfalkvidd

    I fixed that, but it didn't make a difference. Interestingly, the ECHO pin was stuck at 2.5 volts. I don't know if that's right, but it didn't seem right to me. I did some searching and got it working. Here's the code I added to reset the sensor and the output below it. It's interesting that this fixes it, and it has to do it every time I reset the Arduino. Maybe the five for $5 sensors I got on Amazon were that cheap for a reason...

    int uS = sonar.ping();
      if (uS==0)
      {
        Serial.println("MAX: resetting sensor");
        pinMode(ECHO_PIN, OUTPUT);
        delay(150);
        digitalWrite(ECHO_PIN, LOW);
        delay(150);
        pinMode(ECHO_PIN, INPUT);
        delay(150);
      }
    

    sensor started, id=2, parent=0, distance=1
    send: 2-2-0-0 s=255,c=3,t=11,pt=0,l=15,sg=0,st=ok:Distance Sensor
    send: 2-2-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=ok:1.0
    send: 2-2-0-0 s=1,c=0,t=15,pt=0,l=0,sg=0,st=ok:
    send: 2-2-0-0 s=2,c=0,t=15,pt=0,l=0,sg=0,st=ok:
    Maxbotix RS232 Mysensors Sketch
    MAX: resetting sensor
    MAX: resetting sensor
    MAX: resetting sensor
    MAX: resetting sensor
    Ping: 77 in
    send: 2-2-0-0 s=2,c=1,t=13,pt=2,l=2,sg=0,st=ok:77
    Ping: 76 in
    send: 2-2-0-0 s=2,c=1,t=13,pt=2,l=2,sg=0,st=ok:76
    Ping: 76 in
    Ping: 77 in
    send: 2-2-0-0 s=2,c=1,t=13,pt=2,l=2,sg=0,st=ok:77
    Ping: 76 in
    send: 2-2-0-0 s=2,c=1,t=13,pt=2,l=2,sg=0,st=ok:76
    Ping: 77 in
    send: 2-2-0-0 s=2,c=1,t=13,pt=2,l=2,sg=0,st=ok:77
    Ping: 77 in
    Ping: 78 in
    send: 2-2-0-0 s=2,c=1,t=13,pt=2,l=2,sg=0,st=ok:78
    Ping: 76 in
    send: 2-2-0-0 s=2,c=1,t=13,pt=2,l=2,sg=0,st=ok:76
    Ping: 76 in
    Ping: 77 in



  • Very weird. I deleted the node from my controller, and then went into inclusion mode again because it didn't find the sensor. It found both of my distance sensor, but the controller is showing distances like this for the new sonar sensor: 1107296276 cm

    I have the parent device set to imperial, and the Maxbotix sensor is working as it should. The messages printed to the serial console show that it's sending the right distance... But the gateway is showing this (look at the 2;2;1 lines):

    2;1;1;0;13;36.42
    2;1;1;0;13;36.46
    2;2;1;0;13;1107296260
    3;0;1;0;0;80.9
    3;1;1;0;0;103.5
    2;2;1;0;13;1107296265
    2;1;1;0;13;36.42
    2;2;1;0;13;1107296263
    2;2;1;0;13;1107296277



  • When you include sensors, is the gateway notified whether the data being sent from the sensor is an int or a float? I'm wondering that maybe since I originally was declaring dist2 as a float, maybe the gateway still thinks this and it's somehow corrupting the number before it sends it to the controller.

    I need to go hook up the serial port to the sensor and the gw to see if I can figure out where the bad data is coming from.



  • @mfalkvidd

    Interesting. I changed the child ID of the problem sensor to one I haven't used yet. So here's what my sensor is sending (ignore the 37.xx readings, those are for the sensor that's working):

    Ping: 22 in
    send: 2-2-0-0 s=3,c=1,t=13,pt=2,l=2,sg=0,st=ok:22
    37.05 in
    send: 2-2-0-0 s=1,c=1,t=13,pt=7,l=5,sg=0,st=ok:37.05
    Ping: 23 in
    send: 2-2-0-0 s=3,c=1,t=13,pt=2,l=2,sg=0,st=ok:23
    37.05 in
    Ping: 22 in
    send: 2-2-0-0 s=3,c=1,t=13,pt=2,l=2,sg=0,st=ok:22

    And here's what I'm seeing on port 5003 on my gateway:
    2;3;1;0;13;1107296278
    2;1;1;0;13;37.05
    2;3;1;0;13;1107296279
    2;3;1;0;13;1107296278

    Why is my sensor sending 22, and the gateway is sending 1107296278 to my controller?


  • Mod

    What gateway are you using? There have been several similar problems with esp8266-based gateways.



  • Interesting:

    https://forum.mysensors.org/topic/2934/solved-wifi-gateway-motion-sensor-value-1107296256-instead-of-tripped/2
    https://forum.mysensors.org/topic/2737/2-channel-relais-with-2-buttons-sending-strange-values-for-on-off/15

    Is this a bug in 1.5?

    If I update my gw to 2.0b, will it still read from 1.5 sensors? Do I need to upgrade the code on my Vera to the dev branch as well?



  • @mfalkvidd

    ESP8266 gateway. :/ See above, I found the threads on it.



  • I upgraded the gateway to the 2.0b with the ESP8266OTA sketch. Problem solved. It does seem to work fine with the gateway on 2.0b, but sensors and controller on 1.5 code.


Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.