Itead RBoard and RMini Example Sketch



  • Hello All,
    I've been using an Iteadstudio RBoard and RMini for MySensor nodes. These boards are perfect for MySensors as they are cheap and comes with nRF24 socket. I'm not sure why there isn't more mention of them here!

    Anyway, here is a simple sketch I put together for both boards, it follows the same basic pattern as the standard 'Relay With Button Actuator' example.

    I tested this with MyController.org and it all worked as expect, however I am a coding novice, so any suggested improvements would be most welcome.

    Cheers!

    /**
     * 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 - Alex Scott
     * 
     * DESCRIPTION
     * Example MySensor sketch for both the Itead RBoard and RMini
     * 
     * The code sends and receives commands from the GW unit.
     * This unit does the following.
     *     - Receives incoming commands to turn on or off the relays
     *     - Local input switches sends change to GW
     *     - Switch's ACK back from GW sets the corresponding relay i.e.
     *       if GW is down, local relay will not turn on.
     *       This can be good as you know your connected to
     *       the GW... However if you want the swtich-to-relay
     *       to be linked even if the GW is down, then change the
     *       enable the #define below
     *     - Option to save / restore relay states from EEPROM
     *     - Also a MySensors repeater node
     *
     */ 
    
    /*** Set Itead board type  ***/
    //#define RBOARD 1
    #define RMINI 1
    
    /*** Set direct switch-to-relay link if desired ***/
    //#define DIRECT_SW_TO_R
    
    /*** Set to save / restore relay sate from EEPROM  ***/
    //#define USE_EEPROM
    
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <MyTransportNRF24.h>
    #include <Bounce2.h>
    
    #define SKETCH_VER        "1.0"                 // Sketch version
    #define NODE_ID           AUTO                  // Set number if static node ID if not assigned by HA
    #define NODE_REPEATER     true                  // Set true if a repeater node (i.e. always turned on)
    #define CHILD_NAME        "Switch & Relay"      // Optional child sensor name
    #define MSG_NO_ACK        false
    #define MSG_ACK           true
    
    #ifdef RBOARD
      #define SKETCH_NAME   "RBoard"
      #define NRF24_CE      8                       // RBoard nRF24 CE pin
      #define NRF24_CS      9                       // RBoard nRF24 CS pin
      byte switchInputs[] = {14, 15, 16, 17};       // input switches A0 - A3
      byte relayOutput[]  = {4, 5, 6, 7};           // relay output D4 - D7
    #elif RMINI
      #define SKETCH_NAME   "RMini"
      #define NRF24_CE      9                       // RMini nRF24 CE pin
      #define NRF24_CS      10                      // RMini nRF24 CS pin
      byte switchInputs[] = {A0};                   // input switche A0
      byte relayOutput[]  = {4};                    // relay output D4
    #endif
      
    #define DEBOUNCE_TIME   5
    #define NUM_SWITCHES    sizeof(switchInputs)/sizeof(switchInputs[0])  // Number of input switches
    #define NUM_RELAYS      sizeof(relayOutput)/sizeof(relayOutput[0])    // Number of relay channels
    
    char childName[] = "Relay  ";                                         // Send child sensor name, leave space on end for No.
    Bounce *switchDebounce;                                               // Create pointer to bouncer object
    MyTransportNRF24 transport(NRF24_CE, NRF24_CS, RF24_PA_LEVEL);        // Set non-default CE and CS pins for RBoard
    MySensor gw(transport);
    
    
    
    // ************* Setup ***************
    
    void setup(void) {
      for (byte i = 0; i < NUM_SWITCHES; i++) {                           // Configure switch inputs
        pinMode(switchInputs[i], INPUT_PULLUP);                           // Set internal pullup resistor
      }
    
      for (byte i = 0; i < NUM_RELAYS; i++) {                             // Configure outputs to relays
        pinMode(relayOutput[i], OUTPUT);
    #ifdef USE_EEPROM
        digitalWrite(relayOutput[i], gw.loadState(i));                    // Set output to state in EEPROM
    #else
        digitalWrite(relayOutput[i], LOW);                                // Just set relay off
    #endif
      }
      
      switchDebounce = (Bounce *) malloc(sizeof(Bounce) * NUM_SWITCHES);  // Allocated memory for bouncer objects
      
      for (byte i = 0; i < NUM_SWITCHES; i++) {                           // Attach switches to bouncer objects
        switchDebounce[i].attach(switchInputs[i]);
        switchDebounce[i].interval(DEBOUNCE_TIME);
      }
    
      gw.begin(incomingMessage, NODE_ID, NODE_REPEATER);                  // RX message handler, node ID, node repeater
      gw.sendSketchInfo(SKETCH_NAME, SKETCH_VER);                         // Send sketch info
    
      for (byte i = 0; i < NUM_SWITCHES; i++) {                           // Present the switch / relay child sensors to the GW
        childName[(sizeof(childName))-2] = '0'+i;                         // Insert switch / relay number into end of child sensor char array
        gw.present(i, S_LIGHT, childName);
      }  
    }
    
    
    // ************* Main Loop ***************
    
    void loop(void) {
      gw.process();                                                       // Pump the MySensor network
    
      for(int i = 0; i < NUM_SWITCHES; i++) {                             // Check for debounce switch changes
        if(switchDebounce[i].update()) {                                  // Cycle through debounce objects looking for switch change
          bool state = switchDebounce[i].read();
          MyMessage tmpMsg;                                               // Build a new message and send switch state to GW
          tmpMsg.setSensor(i);
          tmpMsg.setType(V_LIGHT);
          tmpMsg.set(state);
    #ifdef DIRECT_SW_TO_R
          digitalWrite(relayOutput[i], state);                            // Set corresponding relay, no via GW ACK message
          gw.send(tmpMsg, MSG_NO_ACK);                                    // Send message, no ACK
      #ifdef USE_EEPROM
          gw.saveState(i, state);
      #endif
    #else
          gw.send(tmpMsg, MSG_ACK);                                       // Send message, ask for ACK, which in turn will set the relay
    #endif                                                                // No EEPROM saving here as we haven't changed to relay state
        }
      }
    }
    
    
    // ********* Handle Incoming MySensor Messages ********
    
    void incomingMessage(const MyMessage &message) {
      if (message.isAck()) {
         Serial.println("This is an ack from gateway");
      }
    
      if (message.type == V_LIGHT) {
        int relayNum = message.sensor;                                    // get child / sensor ID from received message
        if (relayNum >= 0 && relayNum < NUM_RELAYS) {                     // Valid Relay Number?
          digitalWrite(relayOutput[relayNum], message.getBool());         // Set relay output to the received status
    #ifdef USE_EEPROM
          gw.saveState(relayNum, message.getBool());                      // Save relay state to EEPROM
    #endif
         }
      }
    }
    


  • @Alphalove
    I highly recommend itead products, especially for those just getting into MySensors. They remove alot of the pitfalls that electronic noobs (such as myself) encounter when building their first sensor nodes. I used the RMini to build my first relay and it could not have been easier. Many of the troubleshooting problems posted on the forum have to do with setting up relays and these boards eliminate many of those issues.

    I also recommend the nano shield from itead to any entry level electronics noobs for setting up early sensor nodes who are not ready for intricate soldering. It provides a nRf socket and a power jack and breaks out all of the nano pins with individual power and ground headers. Just plug in a nano and nRf and program from USB and power the entire setup through a single jack - perfect for beginners.



Suggested Topics

67
Online

11.4k
Users

11.1k
Topics

112.7k
Posts