Hi friends :)
I want to share another Nanoframework project with you.
let’s start with the commutations Device!
Today commutations are a very important concept.
We have many devices and protocols to commutate with other things, for example, Bluetooth or Wi-Fi or wire….
Specially Commutation is important in IoT Projects when we want to control a remote car or lamp or ….
I worked before with esp32 Wi-Fi and simple wire.
So I decided to work with the new Module that name is NRF24L01_PA_LNA
NRF24L01_PA_LNA is a Wireless Module that works with SPI
I prepared a base Nanoframework library to control NRF24L01_PA_LNA. (AP.NanoFrameWork.NRF24L01PALNA)
Actually, I’m working on it and I will continue developing this library for Nanoframework.
Ok, let’s show you my library usage with a sample project:
First, we need two “esp32 wroom32” develop kits. (I have a develop kit v1 and develop kit v4)
we need two NRF24L01_PA_LNA Modules
Step1: Connect NRF24L01_PA_LNA to esp32 develop kits.
GND to GND
VCC to VCC 3.3
IRQ to GPIO 17
CE to GPIO 16
MOSI to MOSI (GPIO 23 or GPIO 13 ??)
MISO to MISO (GPIO 19 or GPIO 12 ??)
SS to SS (GPIO 15 or GPIO 5 ??)
When we look at esp32 pinouts, we find out it has 2 interfaces for SPI (VSPI, HSPI), The question here is that which interface must be connected to NRF24L01_PA_LNA ??!!!
To find the answer, let’s take a look at the Nanoframework spi samples
Hmm, with this sample I can’t understand which interface Nanoframework spi library is performed?!
After much R&D I found it; Nanoframework SPI library works with SPI1. So, we need to set PinFunction for SPI1.
I used VSPI. So, I should set PinFunction like this:
Now, we should initial SPI with This Parameter:
Note: this parameter is initialized according to NRF24L01PALNA Document
Then, with AP.NanoFrameWork.NRF24L01PALNA, we can send and receive data easily:
Initial:
Note: You can set PA and Data Rate on both sender and receiver
Sender:
Initial TX
or
Send Data
Receive:
Initial RX
Begin Receive
Some parameters are hardcoding in my library, but I will develop it that and make it more flexible
Tx
var gpioController = new GpioController();
var p1 = Configuration.GetFunctionPin(DeviceFunction.SPI1_CLOCK);
if (p1 != 18)
{
Configuration.SetPinFunction(18, DeviceFunction.SPI1_CLOCK);
Configuration.SetPinFunction(19, DeviceFunction.SPI1_MISO);
Configuration.SetPinFunction(23, DeviceFunction.SPI1_MOSI);
}
var irq = gpioController.OpenPin(17);
irq.SetPinMode(PinMode.InputPullUp);
irq.ValueChanged += Irq_ValueChanged;
var ce = gpioController.OpenPin(16);
ce.SetPinMode(PinMode.Output);
var csn = gpioController.OpenPin(5, PinMode.Output);
Thread.Sleep(200);
SpiDevice spiDevice;
SpiConnectionSettings connectionSettings;
SpiBusInfo spiBusInfo = SpiDevice.GetBusInfo(1);
Debug.WriteLine($"{nameof(spiBusInfo.ChipSelectLineCount)}: {spiBusInfo.ChipSelectLineCount}");
Debug.WriteLine($"{nameof(spiBusInfo.MaxClockFrequency)}: {spiBusInfo.MaxClockFrequency}");
Debug.WriteLine($"{nameof(spiBusInfo.MinClockFrequency)}: {spiBusInfo.MinClockFrequency}");
Debug.WriteLine($"{nameof(spiBusInfo.SupportedDataBitLengths)}: ");
foreach (var data in spiBusInfo.SupportedDataBitLengths)
{
Debug.WriteLine($" {data}");
}
connectionSettings = new SpiConnectionSettings(1, -1);
connectionSettings.ClockFrequency = 4_000_000;
connectionSettings.DataBitLength = 8;
connectionSettings.DataFlow = DataFlow.MsbFirst;
connectionSettings.Mode = SpiMode.Mode0;
// Then you create your SPI device by passing your settings
spiDevice = SpiDevice.Create(connectionSettings);
Thread.Sleep(50);
NRFActions nrf24 = new NRFActions(spiDevice, csn, ce);
////Read Status
////..................
nrf24.InitialTXMode(NRFActions.PAState.Min, NRFActions.DataRate.d1Mbps);
Thread.Sleep(100);
while (true)
{
nrf24.SendData("Hi My Name Is Alireza Paridar.)");
Thread.Sleep(1000);
}
RX:
var gpioController = new GpioController();
var irq = gpioController.OpenPin(17);
irq.SetPinMode(PinMode.InputPullUp);
irq.ValueChanged += Irq_ValueChanged;
var p1 = Configuration.GetFunctionPin(DeviceFunction.SPI1_CLOCK);
if (p1 != 18)
{
Configuration.SetPinFunction(18, DeviceFunction.SPI1_CLOCK);
Configuration.SetPinFunction(19, DeviceFunction.SPI1_MISO);
Configuration.SetPinFunction(23, DeviceFunction.SPI1_MOSI);
}
var ce = gpioController.OpenPin(16);
ce.SetPinMode(PinMode.Output);
// var csn = gpioController.OpenPin(15, PinMode.Output);
var csn = gpioController.OpenPin(5, PinMode.Output);
Thread.Sleep(200);
SpiDevice spiDevice;
SpiConnectionSettings connectionSettings;
Debug.WriteLine("Hello from sample for System.Device.Spi!");
// You can get the values of SpiBus
SpiBusInfo spiBusInfo = SpiDevice.GetBusInfo(1);
Debug.WriteLine($"{nameof(spiBusInfo.ChipSelectLineCount)}: {spiBusInfo.ChipSelectLineCount}");
Debug.WriteLine($"{nameof(spiBusInfo.MaxClockFrequency)}: {spiBusInfo.MaxClockFrequency}");
Debug.WriteLine($"{nameof(spiBusInfo.MinClockFrequency)}: {spiBusInfo.MinClockFrequency}");
Debug.WriteLine($"{nameof(spiBusInfo.SupportedDataBitLengths)}: ");
foreach (var data in spiBusInfo.SupportedDataBitLengths)
{
Debug.WriteLine($" {data}");
}
connectionSettings = new SpiConnectionSettings(1, -1);
connectionSettings.ClockFrequency = 4_000_000;//96000;//10_000_000;
connectionSettings.DataBitLength = 8;
connectionSettings.DataFlow = DataFlow.MsbFirst;
connectionSettings.Mode = SpiMode.Mode0;
// Then you create your SPI device by passing your settings
spiDevice = SpiDevice.Create(connectionSettings);
Thread.Sleep(50);
NRFActions nrf24 = new NRFActions(spiDevice, csn, ce);
nrf24.InitialRXMode(NRFActions.PAState.Min, NRFActions.DataRate.d1Mbps);
while (true)
{
Thread.Sleep(1000);
var buf = nrf24.ReciveData();
if(buf==null || buf.Length <= 0)
{
continue;
}
string res = "";
for (int i = 0; i < buf.Length; i++)
{
res += $"{buf[i].ToString()},";
}
Debug.WriteLine(res);
Debug.WriteLine("");
string txt = System.Text.Encoding.UTF8.GetString(buf, 0, buf.Length);
Debug.WriteLine(txt);
Debug.WriteLine("------------------");
}
I Have many ideas to create many projects with this Module, and I’m working on them. I will share the results when I finish them.
If you want, you can join me on GitHub to develop this library.
My GitHub (github.com/AlirezaP)
AlirezaP/AP.NanoFrameWork.NRF24L01PALNA (github.com)
NanoFrameWork makes it easy to deal with the hardware. Forget low-level c or c++ code, just focus on the Ideas ;)
Comments