RF 433 MHz sensor to control RF sockets.



  • Hello,
    I would like to make sensor to control RF 433 MHz sockets. I have connected RF433 transmitter and receiver to node. I use Ago Control as a controller of gateway. I want to get RF codes from node and store them in controller. I want to control RF sockets by sending codes from controller

    Now I have created sketch to transmit code from controller to node. The message code consists of RF code and period. I would like to split this message. I have problem with convert string to integer.
    This is actual code:

    #include <Relay.h>
    #include <SPI.h>
    #include <EEPROM.h>
    #include <RF24.h>

    #include <RemoteReceiver.h>
    #include <RemoteTransmitter.h>

    #define PIN_TRANSMITTER 8

    Sensor gw;

    void setup()
    {
    gw.begin(28);

    // Send the sketch version information to the gateway and Controller
    gw.sendSketchInfo("RF433", "1.0");

    // Register all sensors to gw (they will be created as child devices)
    gw.sendSensorPresentation(PIN_TRANSMITTER, S_IR);

    }

    void loop()
    {
    if (gw.messageAvailable()) {
    message_s message = gw.getMessage();
    sendRfMessage(message);
    }
    }

    void sendRfMessage(message_s message) {
    if (message.header.messageType==M_SET_VARIABLE &&
    message.header.type==V_IR_SEND) {
    unsigned long incomingRfCode = atoi(String(message.data).substring(0,6).c_str());
    unsigned int incomingPeriod = atoi(String(message.data).substring(6).c_str());
    // Change relay state
    //digitalWrite(message.header.childId, incomingRfCode);
    // Write some debug info
    //Serial.print("Incoming change for relay on pin:");
    Serial.println(String(message.data).substring(0,6));
    Serial.println(message.data);
    Serial.println(incomingRfCode);
    Serial.println(incomingPeriod);
    RemoteTransmitter::sendCode(PIN_TRANSMITTER, incomingRfCode, incomingPeriod, 3);
    }
    }

    If I send to node this message: 28;8;1;32;333333111

    Node prints this:
    333333
    333333111
    5653
    111

    So there is problem with convert string to integer.
    If I remove command c_str() like this:
    unsigned long incomingRfCode = atoi(String(message.data).substring(0,6));
    I can't compile code and I get this message:
    _433mhz.ino: In function 'void sendRfMessage(message_s)':
    _433mhz:40: error: cannot convert 'String' to 'const char*' for argument '1' to 'int atoi(const char*)'

    Anyone know how to convert str to int properly?


  • Contest Winner

    @lasso said:

    The message code consists of RF code and period.

    Can you clarify?
    Does this mean that you are using the period (ASCII 46) to delimit your message or it is part of your message?



  • I mean, construction of message is (string) "RF code" + "period". For example: if RF code is 123456 and period is 200, message looks like this: 123456200.
    OK I fixed problem with string to integer conversion by using command toInt() . Code looks like this now:
    String sendingRfMessage = String(message.data);
    unsigned long sendingRfCode = sendingRfMessage.substring(0,sendingRfMessage.length()-3).toInt();
    unsigned int sendingPeriod = sendingRfMessage.substring(sendingRfMessage.length()-3).toInt();

    Program below relays RF 433 commands from RF 433 remote to Gateway and from Gateway to RF 433 sockets.
    If I send message 28;8;1;32;356428203 (RF code is 356428 and period is 203) to node from Gateway, then my socket turns on. If I push button on RF 433 remote which turns on the same socket, then I get this message: 28;2;1;33;356428203 from node on my Gateway.
    This program works, but I have stability problem. If I push buttons on my RF 433 remote several times at the begging program works correctly and I get messages with codes and periods on Gateway. But after some time program stops and I can't get messages from node and send messages to node. After this I can only reset arduino
    BulldogLowell, do You know why I have this problem?

    #include <Relay.h>
    #include <SPI.h>
    #include <EEPROM.h>
    #include <RF24.h>

    #include <RemoteReceiver.h>
    #include <RemoteTransmitter.h>

    #define PIN_TRANSMITTER 8
    #define RECEIVER_CHILD_ID 2
    Sensor gw;

    void setup()
    {
    gw.begin(28);
    gw.sendSketchInfo("RF433", "1.0");
    gw.sendSensorPresentation(PIN_TRANSMITTER, S_IR);
    RemoteReceiver::init(0, 3, getRfMessage);

    }

    void loop()
    {
    if (gw.messageAvailable()) {
    message_s message = gw.getMessage();
    sendRfMessage(message);
    }
    }

    void sendRfMessage(message_s message) {
    if (message.header.messageType==M_SET_VARIABLE &&
    message.header.type==V_IR_SEND) {
    String sendingRfMessage = String(message.data);
    unsigned long sendingRfCode = sendingRfMessage.substring(0,sendingRfMessage.length()-3).toInt();
    unsigned int sendingPeriod = sendingRfMessage.substring(sendingRfMessage.length()-3).toInt();

     Serial.println(sendingRfCode);
     Serial.println(sendingPeriod);
     RemoteReceiver::disable();
     interrupts();
     RemoteTransmitter::sendCode(PIN_TRANSMITTER, sendingRfCode, sendingPeriod, 3);
     RemoteReceiver::enable();
    

    }
    }

    void getRfMessage(unsigned long incomingRfCode, unsigned int incomingPeriod){
    String gettingRfMessage = String(incomingRfCode)+String(incomingPeriod);
    Serial.println(gettingRfMessage.toInt());
    gw.sendVariable(RECEIVER_CHILD_ID, V_IR_RECEIVE, gettingRfMessage.toInt());
    }


  • Contest Winner

    @lasso

    Sorry, I can help you debug the C but not the RF 😞

    there is a lot of knowledge here for that, I am sure you will get some good help.



  • Thank You for response.
    This is actual code: http://wklej.org/id/1365746/
    I noticed that, the problem occurs when node receiving code from RF433 remote. After start arduino program works good (diode connected to pin 13 lights up). When I push button on RF433 remote several times diode turns off and then I can't send codes from gateway to node. When I push buttons on RF433 remote diode blinks and codes are transmitted to gateway correctly. After a few more pushes of the button on remote diode connected to pin 13 turns on and then I can't send codes to node from gateway and get codes from node to gateway.


  • Admin

    Try avoid using String class. It allocates memory and you might end up overwriting program.



  • Hek, thank for your response. I have to send two information to node (and from node): RF433 code and period. I would like to avoid sending this information in two messages (e.g. as V_VAR1 and V_VAR2), so I joined this informations in one by using class String. Maybe do You have idea how to avoid String class in this program?


  • Admin

    @lasso

    atoi, strcpy, itoa



  • OK, I stopped using String Class, but program works in the same way.On start Arduino, program works well and I can get and send RF433 codes. After some pushes a button on RF433 remote, LED on Arduino (pin 13) turns off and then I can't send messages from gateway to node, but I can still get codes of buttons on gateway from node. I think that, maybe this problem is connected with interrupts because NRF24 and receiver RF433 modules are using interrupts. IRQ pin from NRF24 module is connected to pin 2 (int.0) in Arduino Pro Mini and data pin from receiver RF433 module is connected to pin 3 (int.1) in Arduino.
    @hek Do You have any idea why this program doesn't work properly?

        #include <Relay.h>
        #include <SPI.h>
        #include <EEPROM.h>  
        #include <RF24.h>
        
        #include <RemoteReceiver.h>
        #include <RemoteTransmitter.h>
        
        #define TRANSMITTER_PIN 8
        #define RECEIVER_INTERRUPT 1
        #define RF433_CHILD_ID 0
        Sensor gw;
        
        void setup()  
        {  
          gw.begin(28);
          gw.sendSketchInfo("RF433", "1.0");
          gw.sendSensorPresentation(RF433_CHILD_ID, S_IR);
          RemoteReceiver::init(RECEIVER_INTERRUPT, 3, getRfMessage);
        }
        
        void loop() 
        {
          if (gw.messageAvailable()) {
            message_s message = gw.getMessage(); 
            sendRfMessage(message);
          }
        }
        
        void sendRfMessage(message_s message) {
          if (message.header.messageType==M_SET_VARIABLE &&
              message.header.type==V_IR_SEND) {
        
             char sendingRfCode[7]; 
             char sendingRfPeriod[4];
             strncpy(sendingRfCode, message.data, 6);
             strncpy(sendingRfPeriod, message.data+6, 3);
             sendingRfCode[6] = 0;
             sendingRfPeriod[3] = 0;
             Serial.println(atol(sendingRfCode));
             Serial.println(atol(sendingRfPeriod));
             RemoteReceiver::disable();
             interrupts();
             delay(200);
             RemoteTransmitter::sendCode(TRANSMITTER_PIN, atol(sendingRfCode), atol(sendingRfPeriod), 3);
             RemoteReceiver::enable();
           }
        }
        
        void getRfMessage(unsigned long incomingRfCode, unsigned int incomingRfPeriod){
          
          char gettingRfMessage[10];
          sprintf(gettingRfMessage, "%lu%u", incomingRfCode, incomingRfPeriod);
          Serial.println(incomingRfCode);
          Serial.println(incomingRfPeriod);
          Serial.println(gettingRfMessage);
          gw.sendVariable(RF433_CHILD_ID, V_IR_RECEIVE, gettingRfMessage);
          delay(200);
        }

  • Admin

    yup, in the example you attach it to INT1 (=GPIO pin 3).

    You can safely detach IRQ pin from radio if you want. It is not used for anything right now.

    Or change RECEIVER_INTERRUPT to 0 and attach receiver to GPIO pin 2.



  • @hek
    I detached IRQ pin from pin 2 in Arduino and attached to pin 2 data line from RF433 receiver. After that, I changed RECEIVER_INTERRUPT to 0. Program works in the same way. The problem has not been solved.


  • Admin

    If I understand the RemoteReceiver code correctly your callback will be called in an interrupt. This means you should do as little as possible there. Definetly not serial print or send any data over radio.

    I propose you just save the incoming values and a new flag like this:

    setup() {
        RemoteReceiver::init(RECEIVER_INTERRUPT, 3, newRf);
    }
    
    volatile bool receivedNew = false;
    volatile unsigned long incomingRfCode;
    volatile unsigned int incomingRfPeriod;
    
    void newRf(unsigned long _incomingRfCode, unsigned int _incomingRfPeriod){
         incomingRfCode = _incomingRfCode;
         incomingRfPeriod = _incomingRfPeriod;
         receivedNew=true;
    }
    

    And in you main loop check if received new is true.

    loop() {
         if(receivedNew) {
             getRfMessage(incomingRfCode, incomingRfPeriod);
             receivedNew = false;
         }
    }
    

    Note this is not tested and should just give you an hint how to handle it without doing lots of stuff when executing inside an interrupt.



  • @hek
    Wow it's working 👍. Hek thank you very much, you are the master 😄
    This is working code:
    #include <Relay.h>
    #include <SPI.h>
    #include <EEPROM.h>
    #include <RF24.h>

        #include <RemoteReceiver.h>
        #include <RemoteTransmitter.h>
        
        #define TRANSMITTER_PIN 8
        #define RECEIVER_INTERRUPT 1
        #define RF433_CHILD_ID 0
        Sensor gw;
        
        volatile bool receivedNew = false;
        volatile unsigned long incomingRfCode;
        volatile unsigned int incomingRfPeriod;
        
        void setup()  
        {  
          gw.begin(28);
          gw.sendSketchInfo("RF433", "1.0");
          gw.sendSensorPresentation(RF433_CHILD_ID, S_IR);
           delay(200);
          RemoteReceiver::init(RECEIVER_INTERRUPT, 3, newRf);
        }
        
        void loop() 
        {
          if (gw.messageAvailable()) {
            message_s message = gw.getMessage(); 
            sendRfMessage(message);
          }
          if(receivedNew) {
            getRfMessage(incomingRfCode, incomingRfPeriod);
            receivedNew = false;
          }
        }
        
        void sendRfMessage(message_s message) {
          if (message.header.messageType==M_SET_VARIABLE &&
              message.header.type==V_IR_SEND) {
        
             char sendingRfCode[7]; 
             char sendingRfPeriod[4];
             strncpy(sendingRfCode, message.data, 6);
             strncpy(sendingRfPeriod, message.data+6, 3);
             sendingRfCode[6] = 0;
             sendingRfPeriod[3] = 0;
             Serial.println(atol(sendingRfCode));
             Serial.println(atol(sendingRfPeriod));
             RemoteReceiver::disable();
             interrupts();
             delay(200);
             RemoteTransmitter::sendCode(TRANSMITTER_PIN, atol(sendingRfCode), atol(sendingRfPeriod), 3);
             RemoteReceiver::enable();
           }
        }
        
        void newRf(unsigned long _incomingRfCode, unsigned int _incomingRfPeriod){
             incomingRfCode = _incomingRfCode;
             incomingRfPeriod = _incomingRfPeriod;
             receivedNew=true;
        }
        
        void getRfMessage(unsigned long incomingRfCode, unsigned int incomingRfPeriod){
          char gettingRfMessage[10];
          sprintf(gettingRfMessage, "%lu%u", incomingRfCode, incomingRfPeriod);
          Serial.println(incomingRfCode);
          Serial.println(incomingRfPeriod);
          Serial.println(gettingRfMessage);
          gw.sendVariable(RF433_CHILD_ID, V_IR_RECEIVE, gettingRfMessage);
        }


  • @lasso Are you learning the codes with the receiver inorder to know what to send to them ? I would really like to react on my 433mhz sensors i have for doors and i am interested how to do that and i found your post and i am trying to figure out if i could use some of this code for that purpose, what's your take on that ?



  • any idea about this?

    variable or field 'sendRfMessage' declared void



  • @lasso @hek
    any idea about this?

    variable or field 'sendRfMessage' declared void



  • Did you ever get this working?

    What pins did you attache the transmitter to and what pin did you attach the receiver to?

    Is that the latest code?



  • Hello,
    this is my version "Home Easy Plug-in switch (HE877)". scatch.
    Arduino emulates remote control Elro AB440
    AVI, thanks for the inspiration.

    https://codebender.cc/sketch:92208



  • @lasso Like to try your RF 433 Mhz Sensor sketch but can't find a working instance od Relay.h.
    Can you help?



  • "can't find a working instance od Relay.h."

    Having the same issue here, perhaps it's for version 1.3 of mysensors?


  • Admin

    @cdr

    Yes, 1.3 had a Realy-class.


  • Plugin Developer

    Does anyone have a working version of this for 2.x?


Log in to reply
 

Suggested Topics

  • 87
  • 6
  • 5
  • 5
  • 3
  • 7

76
Online

11.4k
Users

11.1k
Topics

112.7k
Posts