1. Home
  2. Docs
  3. Sensor Node
  4. Introduction
  5. Get started

Get started

Let’s consider the following example to understand the basic logic of the SNODE Arduino library.

There are three nodes each of which has one or more sensors. Node 1 has three sensors (Button 1, Button 2, Button 3). Node 2 has two sensors (Button and Led). Node 3 has a sensor (RGB Led). The figure below shows the structure of the Node network with sensors.

Figure 8 – Node network

Each node must be assigned to a unique id as mentioned earlier. In our case, let it be the following IDs: Node ID = 50, Node ID = 60, Node ID =70.

Sensor addresses are automatically assigned during their registration.  And their value will be such as those are shown in the figure below.

Task:Three nodes are connected to the same network.

  • Button Sensor = 1 of Node ID = 50 should turn on and off Led Sensor = 2 of Node Id = 60.
  • Button Sensor = 2 of Node ID = 50 should turn on and off Green Led Sensor = 1 of Node Id = 70.
  • Button Sensor = 3 of Node ID = 50 should turn on and off Red Led Sensor = 1 of Node Id = 70.
  • Button Sensor = 1 of Node ID = 60 should turn on and off Blue Led Sensor = 1 of Node Id = 70.

On the picture below with the help of arrows is shown schematic connection between sensors (one sensor is controlled by another) of each of the node.

Note : Node ID = 60 has two types of sensors (input and output). The Led Sensor is output. The Button Sensor is input.

Figure 9 – Node network diagram

A short video below shows how the SNODE network works.

 

 

The following figure is the pin connections of Arduino boards (Nano and Micro), MCP2515 CAN bus shield, Button and Led Modules.

Full connection

Source code

The full source code could be found here.

Download or Clone the library from GitHub with:

git clone https://github.com/okrutax/senodes.git

Let’s consider the source code of the example of Node ID 60 (Button and Led Sensors).

Open the snode_one_button example in PlatformIO IDE.

If you want to run the code on another arduino board, uncomment the necessary one in the file.

The table below shows the pin to pin connection (Arduino Nano, Micro and MCP2515 CAN Bus Shield).

Arduino NanoArduino MicroThe MCP2515 CAN Bus Shield
5 V5 VVCC
GNDGNDGND
D10SSCS
D12MISOSO
D11MOSISI
D13SCKSCK

The SNODE library uses the standard SPI interface. Hardware Abstraction Layer (HAL) could be found here.

Add header files:

#include <Arduino.h>
#include "snode.h"

Initialization of the SNODE library and registration of node sensors:

#define CAN_FRAME_SOURCE_ID  (60)

#define SNODE_BUTTON         (PD2)
#define SNODE_LED            (PD3)

/**************************************************************************/

void setup()
{
  Serial.begin(115200);

  pinMode(SNODE_BUTTON, INPUT_PULLUP);
  pinMode(SNODE_LED, OUTPUT);

  SNODE_Init((uint16_t)CAN_FRAME_SOURCE_ID);
  SNODE_Registration((PSNODE_SENSOR_REG)&SNODE_Button); //! The Button Address is 0x01.
  SNODE_Registration((PSNODE_SENSOR_REG)&SNODE_LedSensor); //! The Led Address is 0x02.
}

 

The SNODE_Registration function is used to register sensors of the node. The pointer to the SNODE_SENSOR_REG structure is used as the input parameter of the function (const PSNODE_SENSOR_REG pSensor). The struct of the Button Sensor is as follows.

SNODE_ERROR SNODE_GetButtonData(void * data);

/**************************************************************************/

SNODE_SENSOR_REG SNODE_Button =
{
  .addr        = 0x00,
  .type        = SNODE_BUTTONS_SENSOR_TYPE,
  .mode        = SNODE_INPUT_SENSOR_MODE_TYPE,
  .subscriber  = SNODE_NONE_SENSOR_TYPE,
  .callback = SNODE_GetButtonData,
};

/**************************************************************************/

SNODE_ERROR SNODE_GetButtonData(void * data)
{
  uint32_t *pButton = (uint32_t *) data;

  *pButton = !digitalRead(SNODE_BUTTON);

  return SNODE_SUCCESS;
}

The struct of the Led Sensor is as follows.

SNODE_ERROR SNODE_SetLedData(void *data);

/**************************************************************************/

SNODE_SENSOR_REG SNODE_LedSensor =
{
  .addr        = 0x00,
  .type        = SNODE_LED_SENSOR_TYPE,
  .mode        = SNODE_OUTPUT_SENSOR_MODE_TYPE,
  .subscriber  = SNODE_BUTTONS_SENSOR_TYPE, // Output interface should be subscribed to the other input or control interfaces.
  .callback = SNODE_SetLedData,
};

/**************************************************************************/

SNODE_ERROR SNODE_SetLedData(void * data)
{
  PSNODE_SENSOR_CALLBACK_DATA  pSensorCallbackData = (PSNODE_SENSOR_CALLBACK_DATA) data;

  if ( pSensorCallbackData->type & SNODE_BUTTONS_SENSOR_TYPE )
  {
    if ( 50 == pSensorCallbackData->id )
    {
      if ( 1 == pSensorCallbackData->addr )
      {
        if ( pSensorCallbackData->data )
        {
          digitalWrite(SNODE_LED, HIGH);
        }
        else
        {
          digitalWrite(SNODE_LED, LOW);
        }
      }
    }
  }

  return SNODE_SUCCESS;
}

 

The SNODE_ProcessPacket function should be called in a loop in order to procedure the SNODE Engine (receive and send frames, multi-frames, timings, etc.).

 void loop()
{
  SNODE_ProcessPacket();
}

Open the snode_led example in PlatformIO IDE.

Open the snode_buttons example in PlatformIO IDE.