{"id":1762,"date":"2022-11-18T14:08:11","date_gmt":"2022-11-18T22:08:11","guid":{"rendered":"http:\/\/thenscaler.com\/?p=1762"},"modified":"2023-04-05T09:09:00","modified_gmt":"2023-04-05T16:09:00","slug":"layout-control-nodes","status":"publish","type":"post","link":"https:\/\/thenscaler.com\/?p=1762","title":{"rendered":"Layout Control Nodes"},"content":{"rendered":"\n<p>I had thread going before the pandemic about <strong>Layout Control Nodes<\/strong> &#8212; I&#8217;ve been ruminating on this subject for a while: see <a href=\"https:\/\/thenscaler.com\/?p=1509\" data-type=\"post\" data-id=\"1509\">Building Blocks for Layout Control<\/a>, <a href=\"https:\/\/thenscaler.com\/?p=1552\" data-type=\"post\" data-id=\"1552\">Basic Layout Control Nodes<\/a> and <a href=\"https:\/\/thenscaler.com\/?p=1603\" data-type=\"post\" data-id=\"1603\">Additional LCN Components<\/a>. So let&#8217;s pick that back up and continue on. Put on your goggles, we&#8217;ll review old material then dive a little deeper into the subject.<\/p>\n\n\n\n<p><strong>Layout Control Nodes<\/strong> (LCNs) are dedicated to the physical layout &#8212; its turnouts, signals, lighting and animated accessories &#8212; leaving train control to other technologies. <\/p>\n\n\n\n<p>That does not mean that LCNs and train control cannot interact, only that they are independent of each other. Properly implemented, LCNs can <em>work cooperatively<\/em> with any train control system: DC, DCC, WiFi, Bluetooth, etc.<\/p>\n\n\n\n<div id=\"attachment_1577\" style=\"width: 880px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2019\/11\/basic-lcn-powered.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1577\" class=\"size-full wp-image-1577\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2019\/11\/basic-lcn-powered.jpg\" alt=\"\" width=\"870\" height=\"544\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2019\/11\/basic-lcn-powered.jpg 870w, https:\/\/thenscaler.com\/wp-content\/uploads\/2019\/11\/basic-lcn-powered-300x188.jpg 300w, https:\/\/thenscaler.com\/wp-content\/uploads\/2019\/11\/basic-lcn-powered-768x480.jpg 768w, https:\/\/thenscaler.com\/wp-content\/uploads\/2019\/11\/basic-lcn-powered-542x340.jpg 542w\" sizes=\"auto, (max-width: 870px) 100vw, 870px\" \/><\/a><p id=\"caption-attachment-1577\" class=\"wp-caption-text\">A Basic <strong>Layout Control Node<\/strong><\/p><\/div>\n\n\n\n<p>Here&#8217;s the concept:  take a basic microcontroller, such as an Uno or Nano, give it peripheral devices for communication and to provide ports for attaching your layout lighting, motors and electronics. That controller would then manage everything in a limited physical area &#8212; lets say 1 &#8211; 2 square meters. On any layout larger than a square meter or so, you can deploy multiple control nodes with reasonably localized wiring doing the work on your layout, instead of having all wiring leading back to a single central control point.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why would you want to do that? <\/h2>\n\n\n\n<p>This simple answer is that it enables you to do more, and, with good coding, allows you to <strong><em>create a layout that organically responds to trains and the passage of fast clock time<\/em><\/strong>. <\/p>\n\n\n\n<p>But, wait. Can&#8217;t I do that now? Fast clocks, DCC, JRMI, what&#8217;s missing?<\/p>\n\n\n\n<p>I suppose the best concepts to use here are ACCESSIBILITY, DEPTH and SCALING. <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>ACCESSIBILITY is a measure of how easy it is to use a tool, or more broadly, how easy it is to accomplish a functional goal, without having special expertise.<\/li>\n\n\n\n<li>DEPTH refers to the variety of objects and simultaneous actions supported. <\/li>\n\n\n\n<li>SCALING is the ability of a system to grow or shrink while maintaining the same level of performance. <\/li>\n<\/ul>\n\n\n\n<p>A fully animated museum layout is a wonder to behold.  So many things going on in perfect coordination. It takes substantial computing power with sophisticated communication and electrical switching technologies to make all that work. Add the ability to handle random elements, such as a decision made by a visitor by pushing a button, and the degree of complexity in decision making multiplies exponentially.<\/p>\n\n\n\n<p>Its hard to replicate that, even on a small scale, in your own basement. Why?  Because available tools are built around a single process &#8212; e.g., a single program, such as JRMI, running on a central computer &#8212; doing all the work. That is a classic strategy, going back to the roots of computing and, coupled with &#8220;event driven&#8221; software implementations, it will get you a long way.  <\/p>\n\n\n\n<p>But there are costs to that approach:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Accessibility <\/strong>is a big issue with centralized systems. How easy or difficult is it to program, use or later change? What level of programming knowledge, if any, is required to use or configure the system? Are the management tools graphical (GUI) or textual?<\/li>\n\n\n\n<li><strong>Depth <\/strong>in 2022 and beyond requires an automation system to <strong><em>support more than just turnouts and  signals<\/em><\/strong>. A system with <strong>depth <\/strong>includes automation support for layout lighting and a wide variety of layout accessories, including motorized accessories.<\/li>\n\n\n\n<li>Centralized systems usually deal with dumb attached objects, so it must be connected to everything and must micromanage everything. From the user perspective, you are potentially dealing with long lists of objects, with associated functional data, that are hard to organize and manage, impacting <strong>Accessibility<\/strong>, <strong>Scalability<\/strong> and <strong>Depth<\/strong>.<\/li>\n\n\n\n<li><strong>Scalability <\/strong>and <strong>Depth <\/strong>are limited by the speed and memory capacity of the controlling computer. If the central system has to manage too many objects, it will tend to become saturated and unresponsive. <\/li>\n\n\n\n<li>Significant changes to an established, centralized system can be difficult.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">The Principle of the Layout Control Node<\/h2>\n\n\n\n<p>The principle of the <strong>Layout Control Node<\/strong> is to subdivide the layout into smaller, semi-independent sections that are easier to create, wire, program, combine and use. The result:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Each section can be <strong>managed independently<\/strong>, with shorter object lists and the capacity to do more with the objects. <strong>Whatever you do in one section should not directly affect other sections.<\/strong> Just breaking your layout down into smaller independent modules vastly improves <strong>accessibility<\/strong>. <\/li>\n\n\n\n<li>While the scope of a section is limited by its physical size, the potential automation <strong>depth <\/strong>of each section is not limited by that. The depth of each section depends on the number and variety of supporting ports available for attaching layout objects. The more ports available, the more the processor can do.<\/li>\n\n\n\n<li>It is much easier to build your layout electronics one independent section at a time.  Since each section has its own processor, the system <strong>automatically scales<\/strong> as you add or remove sections. Because each node is logically and functionally independent, adding a node does not impose a significant burden on other nodes. Instead, <strong>each new node increases the amount of simultaneous activity the system can support<\/strong>.<\/li>\n\n\n\n<li>Each section manages its objects according to the rules you provide. Since each section has its own processor and rules set, the <strong>combined behaviors can be unexpectedly organic<\/strong>.<\/li>\n<\/ul>\n\n\n\n<p>An array of Layout Control Nodes, deployed in layout sections, is a multi-threaded, multi-processing engine that can give layout behavior an organic, natural quality because each node is responding to layout events in its own way. Behaviors like these aren&#8217;t easily programmed centrally; they are an emergent property of this kind of system.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2022\/11\/1024px-Ericsson_bakelittelefon_1931_sv.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"814\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2022\/11\/1024px-Ericsson_bakelittelefon_1931_sv.jpg\" alt=\"\" class=\"wp-image-1906\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2022\/11\/1024px-Ericsson_bakelittelefon_1931_sv.jpg 1024w, https:\/\/thenscaler.com\/wp-content\/uploads\/2022\/11\/1024px-Ericsson_bakelittelefon_1931_sv-300x238.jpg 300w, https:\/\/thenscaler.com\/wp-content\/uploads\/2022\/11\/1024px-Ericsson_bakelittelefon_1931_sv-768x611.jpg 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">These classic Western Electric Telephones at one time set the benchmark for communication.<br>By ProhibitOnions at English Wikipedia &#8211; Transferred from en.wikipedia to Commons by ProhibitOnions., Public Domain, https:\/\/commons.wikimedia.org\/w\/index.php?curid=4446552<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Communication Method<\/h2>\n\n\n\n<p>To paraphrase an old expression, &#8220;no person is an island,&#8221; the same is true of LCNs. When a train is running, what happens on an adjacent node may matter to another node. So our LCNs must communicate with each other.<\/p>\n\n\n\n<p>You want node-to-node communication to be wireless. Wired communication between multiple nodes would require require additional hardware to route communications; that sort of excess wiring and hardware is exactly what we want to avoid.<\/p>\n\n\n\n<p>Your wireless choices include WiFi, XBee and nRF24.  WiFi and XBee can transit significant distances, and to do that they have fairly high power consumption. nRF24 communications use the 2.4 GHz band, which is intended for short range use, giving good short range performance with lower power consumption. In a typical basement or even a 5000+ square foot club layout, the nRF radio provides the necessary range while saving power and leaving your WiFi for other uses.<\/p>\n\n\n\n<p> Accordingly, a Layout Control Node should include an nRF24L01+ radio as a basic component.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Not Use WiFi?<\/h2>\n\n\n\n<p>Some will argue for an ESP32 or similar IOT device as a node using built in WiFi.  That is a viable alternative, but not necessarily the best approach. Here&#8217;s why:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>WiFi is a relatively complex, processor intensive collection of protocols; the processing and memory burden impose by the network drivers is substantial. The design of the ESP32 is optimized for those tasks, but they still have an impact on the device&#8217;s overall performance as you add in additional tasks.<\/li>\n\n\n\n<li>WiFi requires an Access Point and Router. You can fabricate one, but most people will just use a standard WiFi router; for many, that means mixing your layout and household traffic unless you have a special, correctly configured AP\/Router for your trains. You would have the option of creating a mesh network, adding fault tolerance and simplifying adding and removing devices. So there are interesting options if you go WiFi.<\/li>\n\n\n\n<li>Nevertheless, the big reason to avoid WiFi for this application is power consumption: WiFi hardware is relatively power hungry, typically requiring 300 mA <em>or more<\/em> power to support each WiFi device.  That&#8217;s a lot of amps to consume before you&#8217;ve run a turnout motor or lit up some LEDS.  On a small layout with two control nodes talking to each other, no big deal. On a larger layout with 10 control nodes, you are burning 3 amps of layout power just to support WiFi. 3 amps could support over 100 LED devices, a far more entertaining use of the power!<\/li>\n<\/ol>\n\n\n\n<p>In contrast to WiFi, an nRFNetwork using nRF24L01+ radios:<\/p>\n<ul>\n<li>is self routing so it does not require an access point or router;<\/li>\n<li>Is a simple protocol requiring minimal processor support; and<\/li>\n<li>each device draws a mere 12 mA while listening, and 120 mA when transmitting. Overall power savings is 60 to 70% compared to WiFi. On a layout with lots of lighting, signals and turnout motors, the power savings matter.<\/li>\n<\/ul>\n<p>So I chose nRF for my layout network.&nbsp; You may choose differently.<\/p>\n\n\n\n<h2>nRF24L01+ Hardware<\/h2>\n\n\n\n<p>The nRF24L01+ chip is available on small, plug in cards with a built-in antenna.&nbsp; For greater range, you can use a version of the card with a connection for an external antenna. While outdoor railroading enthusiasts might need the extra range, for indoor use the cards with an integrated antenna are the right choice.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2019\/11\/nRF24L01.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"844\" height=\"394\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2019\/11\/nRF24L01.jpg\" alt=\"\" class=\"wp-image-1571\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2019\/11\/nRF24L01.jpg 844w, https:\/\/thenscaler.com\/wp-content\/uploads\/2019\/11\/nRF24L01-300x140.jpg 300w, https:\/\/thenscaler.com\/wp-content\/uploads\/2019\/11\/nRF24L01-768x359.jpg 768w\" sizes=\"auto, (max-width: 844px) 100vw, 844px\" \/><\/a><figcaption class=\"wp-element-caption\">This is a 3.3v device.  However, the data terminals are 5v tolerant so level shifting is not required to use this with a 5 volt Arduino.<\/figcaption><\/figure>\n\n\n\n<p>Connecting an nRF radio is straight forward. MISO, MOSI and SCK are connected to the SPI terminals with the same names on your board. CSN and CE can be any two other digital pins.&nbsp; Since the 3 SPI pins are shared, only two pins are used exclusively &#8212; CE &amp; CSN. If you use a Nano carrier board that includes a connector for an nRF radio, like the one shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2019\/11\/IO-Expansion.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"625\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2019\/11\/IO-Expansion.jpg\" alt=\"\" class=\"wp-image-1573\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2019\/11\/IO-Expansion.jpg 1000w, https:\/\/thenscaler.com\/wp-content\/uploads\/2019\/11\/IO-Expansion-300x188.jpg 300w, https:\/\/thenscaler.com\/wp-content\/uploads\/2019\/11\/IO-Expansion-768x480.jpg 768w, https:\/\/thenscaler.com\/wp-content\/uploads\/2019\/11\/IO-Expansion-542x340.jpg 542w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/a><\/figure>\n\n\n\n<p>&#8230; then the 3.3 volt terminal is connected to the 3.3v output from the Nano, CSN will be connected to D9 and CE will be connected to D10.<\/p>\n<p>Notice the capacitor below the lower right corner of the yellow header. This is here primarily because the source of the 3.3 volts is the USB chip on the underside of the Nano.&nbsp; That source has very limited current capacity and, while the nRF uses little power when idle or receiving, it needs 120 mA current to drive the transmitter, more current than the USB chip can provide. Without the capacitor reservoir, radio transmission would fail. The capacitor also serves to filter power supply noise. You need the capacitor when using the 3.3v output of any Arduino board.<\/p>\n\n\n\n<h3>Precautions<\/h3>\n\n\n\n<p>1.&nbsp; These devices are really static sensitive.&nbsp; Make sure you discharge all body static before handling one. The surest way to kill the card is a static discharge from your finger to the radio chip. I have a pile to dead radios to prove it.<\/p>\n<p>2.&nbsp; Along that line, these boards rarely survive being plugging in incorrectly. Make sure all pins are in the terminal block, in the right direction, before applying power.<\/p>\n<p>3.&nbsp;&nbsp; You may find these do not work well with some knock-off Arduinos.&nbsp; That is because one of the ways the knock-offs save costs is by using inferior USB chips. Some USB chips cannot support the power requirements of the nRF radio, even with the reservoir capacitor. If the USB chip tends to get hot when using an nRF radio, that is a clue that its a poor quality chip.<\/p>\n<p>4.&nbsp; If you create a custom board to host your radio, give it an independent circuit to provide regulated 3.3v power to the radio. That is the best way to ensure adequate power regardless of who made the Arduino board you are using.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Required Libraries<\/h3>\n\n\n\n<p>To use the radio you there are several required libraries:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>RF24 &#8211; Radio Driver, OSI layer 2 library for nrf24L01+ modules. To use the driver, include<strong> <\/strong><strong>RF24.h<\/strong> in your sketch after installing the library. <a rel=\"noreferrer noopener\" href=\"https:\/\/nrf24.github.io\/RF24\/\" data-type=\"URL\" data-id=\"https:\/\/nrf24.github.io\/RF24\/\" target=\"_blank\">Documentation<\/a>.<\/li>\n\n\n\n<li>RF24Network &#8211; OSI Layer 3 Networking for nrf24L01+ devices. Provides addressing and routing for your layout network. To use the driver, include <strong>RF24Network.h<\/strong>. <a rel=\"noreferrer noopener\" href=\"https:\/\/nrf24.github.io\/RF24Network\/\" data-type=\"URL\" data-id=\"https:\/\/nrf24.github.io\/RF24Network\/\" target=\"_blank\">Documentation<\/a>.<\/li>\n<\/ul>\n\n\n\n<p>Use the library manager in your Arduino IDE to obtain and manage these libraries. Other choices are available for OSI layer 3 networking, but for a model railroad the RF24Network is just right.<\/p>\n\n\n\n<h2>Creating a network<\/h2>\n<p>The RF24Network library allows you to create a routed mesh network for your nodes.<\/p>\n\n\n\n<p>The topology of the RF24Network is hub-and-spoke, using a MASTER node as the top hub uniting all the branches beneath it. The MASTER can have up to 5 children, as can each child. The depth of the topology can be up to 5 levels (including the MASTER), which results in up to <strong>781 addressable nodes<\/strong> on one radio channel. Really large networks can be achieved by running multiple parallel networks on different radio channels.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2019\/10\/Master-LCN.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"667\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2019\/10\/Master-LCN.jpg\" alt=\"\" class=\"wp-image-1542\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2019\/10\/Master-LCN.jpg 1000w, https:\/\/thenscaler.com\/wp-content\/uploads\/2019\/10\/Master-LCN-300x200.jpg 300w, https:\/\/thenscaler.com\/wp-content\/uploads\/2019\/10\/Master-LCN-768x512.jpg 768w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/a><figcaption class=\"wp-element-caption\">A Master Layout Control Node &#8212; eventually it will be install on a control panel.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Addressing<\/h3>\n\n\n\n<p>The address of a layout control node on an RF24Network is a number in <strong>OCTAL<\/strong> format that describes the route from the <strong>MASTER<\/strong> <strong>to reach the node.<\/strong><\/p>\n<p>The MASTER Node is always 00.<\/p>\n<p>The Master Node can have up to 5 children. The address of each child node is 1 through 5 with a leading 0 \u2014 01, 02, 03 , 04 and 05. These are referred to as Level 1 addresses.<\/p>\n<p>Each node can have 5 children of its own, whose addresses are Level 2. The address of each child is 01 through 05 preceding the address of its parent. So, the children of node 01 are 011, 021, 031, 041 and 051 respectively. The entire tree can have up to 4 levels below the Master. This can be illustrated as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/beaglebay.com\/duinogear\/wp-content\/uploads\/2021\/01\/mesh-network.jpg\" alt=\"\"\/><figcaption class=\"wp-element-caption\">An RF24Network address tree.<\/figcaption><\/figure>\n\n\n\n<p>The number of nodes you&#8217;ll use depends on the size and complexity of your layout. In most cases you&#8217;ll want to assign all 5 level 1 addresses before using level 2 or deeper addresses. On many if not most home layouts, you won&#8217;t need more than the 5 level 1 nodes<\/p>\n\n\n\n<p><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2022\/11\/16x10HO.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1856\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2022\/11\/16x10HO.jpg\" alt=\"\" width=\"1000\" height=\"659\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2022\/11\/16x10HO.jpg 1000w, https:\/\/thenscaler.com\/wp-content\/uploads\/2022\/11\/16x10HO-300x198.jpg 300w, https:\/\/thenscaler.com\/wp-content\/uploads\/2022\/11\/16x10HO-768x506.jpg 768w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/a>This HO layout would work well with 5 client nodes. The illustrations shows one way to divvy up the work.<\/p>\n\n\n\n<p>On a large layout, the assignment of node addresses should be directly related to the physical placement of the nodes, creating physical paths for messages to transit the layout from the Master out to the furthest edge and back.&nbsp; Level 1 nodes should be physically closest to the Master, while Level 2 nodes should be closer to their level 1 parents than to the Master, level 3 nodes be closer to level 2 parents than anything else, and so on.<\/p>\n\n\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2022\/11\/20x30-layout-room.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"1000\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2022\/11\/20x30-layout-room.jpg\" alt=\"\" class=\"wp-image-1851\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2022\/11\/20x30-layout-room.jpg 1000w, https:\/\/thenscaler.com\/wp-content\/uploads\/2022\/11\/20x30-layout-room-300x300.jpg 300w, https:\/\/thenscaler.com\/wp-content\/uploads\/2022\/11\/20x30-layout-room-150x150.jpg 150w, https:\/\/thenscaler.com\/wp-content\/uploads\/2022\/11\/20x30-layout-room-768x768.jpg 768w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/a><figcaption class=\"wp-element-caption\">This block diagram of a basement sized layout shows how addressing relates to the physical location of each node. Notice how the hub and spoke addressing matches the physical arrangement of the nodes.<\/figcaption><\/figure>\n\n\n\n<h3>Using the RF24 Radio Drivers<\/h3>\n\n\n\n<p>Put these lines at the top of your main sketch, before setup() and loop():<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;SPI.h&gt;\n\/\/ include the radio driver\n#include &lt;RF24.h&gt;\n\/\/ include the network driver\n#include &lt;RF24Network.h&gt;\n\/\/ create the radio object; CE and CSN can be any digital pins you choose\nRF24 radio(10, 9);  \/\/ nRF24L01 (CE,CSN)\n\/\/ create the network object and link the radio\nRF24Network network(radio);\n\/\/ address for this node      \nconst uint16_t this_node = 01;\n\/\/ channel chosen for this network\nconst uint8_t channel = 90; <\/code><\/pre>\n\n\n\n<p>In setup(), start the network with these lines:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ start SPI communications\nSPI.begin();\n\/\/ start the radio\nradio.begin();\n\/\/ start the network, specifying a channel and the address of this node\nnetwork.begin(channel, this_node); \/\/(channel, node address)\n\/\/ set speed to highest supported by the nRF24L01+\nradio.setDataRate(RF24_2MBPS); \/\/ Max baud rate\n\/\/ set power amp level to high; default power level is too low\n\/\/ For maximum range, use RF24_PA_MAX\nradio.setPALevel(RF24_PA_HIGH);<\/code><\/pre>\n\n\n\n<p>In loop(), call the update function at least once per iteration of the loop:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">network.update();<\/pre>\n\n\n\n<p>At this point you are ready to begin communicating with other nodes.&nbsp; Here is a basic function for sending a message to another node:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void sendMessage(uint16_t to_node, byte *data, int bytes){\n  RF24NetworkHeader header(to_node);\n  network.write(header, data, bytes); \/\/ Send the data\n}<\/code><\/pre>\n\n\n\n<p>Here is a basic function for receiving messages:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>boolean getNextMessage(uint16_t to_node, byte *data, int maxBytes){\nboolean msgReceived = false;\nnetwork.update(); \/\/ Process network traffic\nwhile( network.available() ) { \/\/ Is there any incoming data?\n  RF24NetworkHeader header;\n  network.read(header, data, maxBytes); \/\/ Read the incoming data\n  msgReceived = true;\n}\nreturn msgReceived;\n}<\/code><\/pre>\n\n\n\n<p>Readers with a lot coding experience may question the use of a while() loop; wouldn&#8217;t that cause multiple messages to overwrite each other?<\/p>\n\n\n\n<p>No, it will retrieve just one message. I can&#8217;t tell you exactly why you have to do it this way, but if you try the obvious if() clause instead, receiving fails.  My best guess is that multiple passes may be required to fetch all the data, and the header is used to differentiate messages and prevent overwrites. In any case, the while() loop is mandatory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Do You Use the Network?<\/h2>\n\n\n\n<p>What are you going to say and how are you going to say it? A network communications system has many moving parts; we have the OSI layers in place, and now we need a protocol for message payloads.<\/p>\n\n\n\n<p>I&#8217;ll talk about that next time.<\/p>\n\n\n\n<p>Until then, Happy Model Railroading!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I had thread going before the pandemic about Layout Control Nodes &#8212; I&#8217;ve been ruminating on this subject for a while: see Building Blocks for Layout Control, Basic Layout Control Nodes and Additional LCN Components. So let&#8217;s pick that back up and continue on. Put on your goggles, we&#8217;ll review old material then dive a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1542,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[67,35,21,34,49,15],"tags":[22,52,42,92,39,93],"class_list":["post-1762","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-code","category-electronics","category-layout-control","category-lighting-and-animation","category-model-railroading","category-planning","tag-arduino","tag-layout-wiring","tag-networking","tag-nrf24l01-2","tag-programming","tag-rf24network"],"_links":{"self":[{"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/posts\/1762","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thenscaler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1762"}],"version-history":[{"count":46,"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/posts\/1762\/revisions"}],"predecessor-version":[{"id":2053,"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/posts\/1762\/revisions\/2053"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/media\/1542"}],"wp:attachment":[{"href":"https:\/\/thenscaler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thenscaler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thenscaler.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}