problem to code?



  • 
    #include <MySigningNone.h>
    #include <MyTransportNRF24.h>
    //#include <MyTransportRFM69.h>
    #include <MyHwATMega328.h>
    #include <MySensor.h>
    #include <SPI.h>
    #include "Bounce2.h"
    
    //char sketch_name[] = "Multy-Relay";
    //char sketch_ver[]  = "0.1";
    
    #define RELAY_ON 0                      // switch around for realy HIGH/LOW state
    #define RELAY_OFF 1
    #define DEBUG On
    //
    MySensor gw;
    
    #define RADIO_ID 45                    // radio Id, whatever channel you assigned to
    #define noRelays 2
    const int relayPin[] = {3,4}; //  switch around pins to your desire
    const int buttonPin[] = {6,7}; //  switch around pins to your desire
    
    class Relay             // relay class, store all relevant data (equivalent to struct)
    {
    public: 
      int buttonPin;                    // physical pin number of button
      int relayPin;                     // physical pin number of relay
      byte oldValue;                    // last Values for key (debounce)
      boolean relayState;               // relay status (also stored in EEPROM)
    };
    
    Relay Relays[noRelays];
    Bounce debouncer[noRelays];
    MyMessage msg[noRelays];
    
    void setup()
    {
      gw.begin(incomingMessage, RADIO_ID, true);
      delay(500);
      gw.sendSketchInfo("Bar-Multy-Relay&Interruttori", "0.4");
      delay(500);
    
      // Initialize Relays with corresponding buttons
      for (int i = 0; i < noRelays; i++)
      {
        Relays[i].buttonPin = buttonPin[i];              // assign physical pins
        Relays[i].relayPin = relayPin[i];
        msg[i].sensor = i;                                   // initialize messages
        msg[i].type = V_LIGHT;
        debouncer[i] = Bounce();                        // initialize debouncer
        debouncer[i].attach(buttonPin[i]);
        debouncer[i].interval(5);
        pinMode(Relays[i].buttonPin, INPUT_PULLUP);
        pinMode(Relays[i].relayPin, OUTPUT);
        Relays[i].relayState = gw.loadState(i);                               // retrieve last values from EEPROM
        digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
        gw.send(msg[i].set(Relays[i].relayState ? true : false));                 // make controller aware of last status
        gw.present(i, V_LIGHT);                               // present sensor to gateway
        delay(250);
    
      }
    }
    
    void loop()
    {
      gw.process();
      for (byte i = 0; i < noRelays; i++)
      {
        debouncer[i].update();
        byte value = debouncer[i].read();
        //if (value != Relays[i].oldValue && value == 0)
        if (value != Relays[i].oldValue)
        {
          Relays[i].relayState = !Relays[i].relayState;
          gw.send(msg[i].set(Relays[i].relayState ? true : false));
          digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF);
          gw.saveState( i, Relays[i].relayState );
        }                 // save sensor state in EEPROM (location == sensor number)
    
        Relays[i].oldValue = value;
    
      }
    }
    
    // process incoming message
    void incomingMessage(const MyMessage &message)
    {
    
      if (message.type == V_LIGHT)
      {
        if (message.sensor < noRelays)            // check if message is valid for relays..... previous line  [[[ if (message.sensor <=noRelays){ ]]]
        {
          Relays[message.sensor].relayState = message.getBool();
          digitalWrite(Relays[message.sensor].relayPin, Relays[message.sensor].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
          gw.saveState( message.sensor, Relays[message.sensor].relayState ); // save sensor state in EEPROM (location == sensor number)
        }
      }
    }
    
    

    REport

    send: 44-44-0-0 s=255,c=3,t=15,pt=2,l=2,sg=0,st=fail:0
    send: 44-44-0-0 s=255,c=0,t=18,pt=0,l=5,sg=0,st=fail:1.5.4
    send: 44-44-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=fail:0
    repeater started, id=44, parent=0, distance=0
    send: 44-44-0-0 s=255,c=3,t=11,pt=0,l=25,sg=0,st=fail:Bar-Multy-Relay&Interrutt
    send: 44-44-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=fail:0.4
    send: 44-44-0-0 s=0,c=1,t=2,pt=2,l=2,sg=0,st=fail:0
    find parent
    send: 44-44-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,st=bc:
    send: 44-44-0-0 s=0,c=0,t=2,pt=0,l=0,sg=0,st=fail:
    send: 44-44-0-0 s=1,c=1,t=2,pt=2,l=2,sg=0,st=fail:0
    send: 44-44-0-0 s=1,c=0,t=2,pt=0,l=0,sg=0,st=fail:
    send: 44-44-0-0 s=0,c=1,t=2,pt=2,l=2,sg=0,st=fail:1
    send: 44-44-0-0 s=1,c=1,t=2,pt=2,l=2,sg=0,st=fail:1
    
    <<< reset button
    
    send: 44-44-0-0 s=255,c=3,t=15,pt=2,l=2,sg=0,st=fail:0
    send: 44-44-0-0 s=255,c=0,t=18,pt=0,l=5,sg=0,st=fail:1.5.4
    send: 44-44-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=fail:0
    repeater started, id=44, parent=0, distance=0
    send: 44-44-0-0 s=255,c=3,t=11,pt=0,l=25,sg=0,st=fail:Bar-Multy-Relay&Interrutt
    send: 44-44-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=fail:0.4
    send: 44-44-0-0 s=0,c=1,t=2,pt=2,l=2,sg=0,st=fail:1
    find parent
    send: 44-44-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,st=bc:
    send: 44-44-0-0 s=0,c=0,t=2,pt=0,l=0,sg=0,st=fail:
    send: 44-44-0-0 s=1,c=1,t=2,pt=2,l=2,sg=0,st=fail:1
    send: 44-44-0-0 s=1,c=0,t=2,pt=0,l=0,sg=0,st=fail:
    send: 44-44-0-0 s=0,c=1,t=2,pt=2,l=2,sg=0,st=fail:0
    send: 44-44-0-0 s=1,c=1,t=2,pt=2,l=2,sg=0,st=fail:0
    

    😤



  • alt text
    alt text


  • Hardware Contributor

    "st=fail" means the receiving node or gateway has problems sending ack back to the sending node.

    Its probably a hardware/radio issue (power and/or range). Try adding a capacitor (http://www.mysensors.org/build/connect_radio#connecting-a-decoupling-capacitor) to the receiving radio, change powersource, move receiver/sender closer to each other or build a repeater.



  • It might be the board you're using. I had similar issues when I was building my weather node with the same proto-board. After I switched to single sided board it worked immediately. The copper might reflect RF-energy back to the radio and your Arduino that interfere with the transmission.


  • Hardware Contributor

    Also the relays can cause problems - they cause spikes in the circuit which the radio dislikes. Try replacing the relays with leds or something and see what happens.



  • the problem was the radio module nrf24, replacing it I solved

    ps
    when you see lots of nrf24 not buy them on the cheap. thank you all for the support




Log in to reply
 

Suggested Topics

52
Online

11.4k
Users

11.1k
Topics

112.7k
Posts