gw.present(sensor, S_LIGHT) - hangs on 4th sensor



  • I edited the RelayActuator sketch to include 4 relays, on pins 3 to 6. I was having an issue controlling them so dug further, I can only see 3 of the 4 sensors in MYSController and added some serial debugging to the sketch to pinpoint where things are going wrong.

    This is the output:

    send: 2-2-0-0 s=255,c=3,t=15,pt=2,l=2,sg=0,st=ok:0
    send: 2-2-0-0 s=255,c=0,t=17,pt=0,l=5,sg=0,st=ok:1.5.3
    send: 2-2-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=ok:0
    read: 0-0-2 s=255,c=3,t=15,pt=0,l=1,sg=0:0
    read: 0-0-2 s=255,c=3,t=6,pt=0,l=1,sg=0:M
    sensor started, id=2, parent=0, distance=1
    send: 2-2-0-0 s=255,c=3,t=11,pt=0,l=16,sg=0,st=ok:Desk Power Relay
    send: 2-2-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=ok:1.0
    Init Sensor : 1, pin 3
    before present...
    send: 2-2-0-0 s=1,c=0,t=3,pt=0,l=0,sg=0,st=ok:
    before pinMode...
    Sensor configured - OK!
    
    Init Sensor : 2, pin 4
    before present...
    send: 2-2-0-0 s=2,c=0,t=3,pt=0,l=0,sg=0,st=ok:
    before pinMode...
    Sensor configured - OK!
    
    Init Sensor : 3, pin 5
    before present...
    send: 2-2-0-0 s=3,c=0,t=3,pt=0,l=0,sg=0,st=ok:
    before pinMode...
    Sensor configured - OK!
    
    Init Sensor : 4, pin 6
    before present...
    

    The code was modified as follows:

      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
        Serial.print("Init Sensor : ");
        Serial.print(sensor);
        Serial.print(", pin ");
        Serial.println(pin);
    
        // Register all sensors to gw (they will be created as child devices)
        Serial.println("before present...");
        gw.present(sensor, S_LIGHT);
        // Then set relay pins in output mode
        
        Serial.println("before pinMode...");
        pinMode(pin, OUTPUT);   
        // Set relay to last known state (using eeprom storage) 
        digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
        Serial.println("Sensor configured - OK!"); 
        Serial.println();   
      } 
    

    On the 4th sensor, it never gets past the present line.


  • Mod

    How do you power your sensor, and what capactítor(s) are you using? (Step 5 in http://forum.mysensors.org/topic/666/how-to-ask-for-help )
    Adding a 100-250ms delay after each present seems to help people, you could see if that helps in your case as well.



  • HI, thanks for the prompt reply.

    I'm powering it via the 3.3v pin on an Arduino Nano (which is powered from the USB). I have a capacitor across the RF board (470uF).

    I added the delay(250) after the present, and the 4th completed this time (after about 2-3s).


  • Contest Winner

    @afulki What did you change in the default sketch i've tried to reproduce you issue with this sketch

    /**
     * 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 - Henrik Ekblad
     * 
     * DESCRIPTION
     * Example sketch showing how to control physical relays. 
     * This example will remember relay state after power failure.
     * http://www.mysensors.org/build/relay
     */ 
    
    #include <MySigningNone.h>
    #include <MyTransportNRF24.h>
    #include <MyTransportRFM69.h>
    #include <MyHwATMega328.h>
    #include <MySensor.h>
    #include <SPI.h>
    
    #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 4 // Total number of attached relays
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    
    // NRFRF24L01 radio driver (set low transmit power by default) 
    MyTransportNRF24 radio(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);  
    //MyTransportRFM69 radio;
    // Message signing driver (none default)
    //MySigningNone signer;
    // Select AtMega328 hardware profile
    MyHwATMega328 hw;
    // Construct MySensors library
    MySensor gw(radio, hw);
    
    void setup()  
    {   
      // Initialize library and add callback for incoming messages
      gw.begin(incomingMessage, AUTO, false);
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Relay", "1.0");
    
      // Fetch relay status
       for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
        Serial.print("Init Sensor : ");
        Serial.print(sensor);
        Serial.print(", pin ");
        Serial.println(pin);
    
        // Register all sensors to gw (they will be created as child devices)
        Serial.println("before present...");
        gw.present(sensor, S_LIGHT);
        // Then set relay pins in output mode
        
        Serial.println("before pinMode...");
        pinMode(pin, OUTPUT);   
        // Set relay to last known state (using eeprom storage) 
        digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
        Serial.println("Sensor configured - OK!"); 
        Serial.println();   
      } 
    }
    
    
    void loop() 
    {
      // Alway process incoming messages whenever possible
      gw.process();
    }
    
    void incomingMessage(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_LIGHT) {
         // Change relay state
         digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         gw.saveState(message.sensor, message.getBool());
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       } 
    }
    

    And my serial monitor shows this output

    send: 11-11-0-0 s=255,c=3,t=15,pt=2,l=2,sg=0,st=ok:0
    send: 11-11-0-0 s=255,c=0,t=17,pt=0,l=5,sg=0,st=ok:1.5.3
    send: 11-11-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=ok:0
    sensor started, id=11, parent=0, distance=1
    send: 11-11-0-0 s=255,c=3,t=11,pt=0,l=5,sg=0,st=ok:Relay
    send: 11-11-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=ok:1.0
    Init Sensor : 1, pin 3
    before present...
    send: 11-11-0-0 s=1,c=0,t=3,pt=0,l=0,sg=0,st=ok:
    before pinMode...
    Sensor configured - OK!
    
    Init Sensor : 2, pin 4
    before present...
    send: 11-11-0-0 s=2,c=0,t=3,pt=0,l=0,sg=0,st=ok:
    before pinMode...
    Sensor configured - OK!
    
    Init Sensor : 3, pin 5
    before present...
    send: 11-11-0-0 s=3,c=0,t=3,pt=0,l=0,sg=0,st=ok:
    before pinMode...
    Sensor configured - OK!
    
    Init Sensor : 4, pin 6
    before present...
    send: 11-11-0-0 s=4,c=0,t=3,pt=0,l=0,sg=0,st=ok:
    before pinMode...
    Sensor configured - OK!
    

    Just before this line

    sensor started, id=11, parent=0, distance=1
    

    i see 2 more lines in your log , did you add something before the for loop?



  • Hi BartE,

    This is the source, I switched around the relay high low, the name of the sketch and the false on the begin statement. I have no idea where those two extra lines come from, but will try to track them down.

    #include <MySigningNone.h>
    #include <MyTransportNRF24.h>
    #include <MyTransportRFM69.h>
    #include <MyHwATMega328.h>
    #include <MySensor.h>
    #include <SPI.h>
    
    #define RELAY_1  PD3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 4 // Total number of attached relays
    #define RELAY_ON 0  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 1 // GPIO value to write to turn off attached relay
    
    // NRFRF24L01 radio driver (set low transmit power by default) 
    MyTransportNRF24 radio(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);  
    //MyTransportRFM69 radio;
    // Message signing driver (none default)
    //MySigningNone signer;
    // Select AtMega328 hardware profile
    MyHwATMega328 hw;
    // Construct MySensors library
    MySensor gw(radio, hw);
    
    void setup()  
    {   
      // Initialize library and add callback for incoming messages
      gw.begin(incomingMessage, AUTO, false);
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Desk Power Relay", "1.0");
    
      // Fetch relay status
      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
        Serial.print("Init Sensor : ");
        Serial.print(sensor);
        Serial.print(", pin ");
        Serial.println(pin);
    
        // Register all sensors to gw (they will be created as child devices)
        Serial.println("before present...");
        gw.present(sensor, S_LIGHT);
        delay(250);
        // Then set relay pins in output mode
        
        Serial.println("before pinMode...");
        pinMode(pin, OUTPUT);   
        // Set relay to last known state (using eeprom storage) 
        digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
        Serial.println("Sensor configured - OK!");    
      }
    }
    
    
    void loop() 
    {
      // Alway process incoming messages whenever possible
      gw.process();
    }
    
    void incomingMessage(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_LIGHT) {
         // Change relay state
         digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         gw.saveState(message.sensor, message.getBool());
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       } 
    }
    
    

  • Hardware Contributor

    Could be unrelated but if all fails I had this issue with a normal reed switch sensor. Worked great for some time but always dead sooner or later. Never found out what the problem was but uploaded same sketch to another hardware and everything was ok.



  • @sundberg84

    I switched out both the radio and the nano, it seems to be the nano, the new one initialized fine with both radios. As a software guy my first response should have been that it was a hardware issue! 😈

    Thanks for the help!

    Gary



  • @BartE

    I added some extra logging (yes even more) and the 2 extra lines are sent during the gw.begin, I think the

    send: 3-3-0-0 s=255,c=0,t=17,pt=0,l=5,sg=0,st=ok:1.5.3
    

    Is telling the gateway about the arduino node (could be wrong still looking through the code).

    Gary



  • I spoke too soon, it's back to not initializing the 4th output, or rather no doing returning from gw.present();



  • I added a lot more debug output and find that it gets stuck in here:

    	while( ! ( get_status()  & ( _BV(TX_DS) | _BV(MAX_RT) ))) {
    		#if defined (FAILURE_HANDLING)
    			if(millis() - timer > 75){
    				errNotify();
    

Log in to reply
 

Suggested Topics

1
Online

11.2k
Users

11.1k
Topics

112.5k
Posts