Does the RF24 library (from TMRh20) work on STM32F103?

Can someone help me port this library?
madias
Posts: 813
Joined: Mon Apr 27, 2015 11:26 am
Location: Vienna, Austria

Re: Does the RF24 library (from TMRh20) work on STM32F103?

Post by madias » Wed Oct 05, 2016 8:56 am

Basic communication demos works out of the box with this library. You can try out the "scanner demo" so you can test each module alone.
I always use this adapters:
https://www.aliexpress.com/item/10pcs-N ... 18663.html

Code: Select all

radio.setPALevel(RF24_PA_LOW)
is a good idea for testing.
Beware: Most of the ali/ebay cheap nrf24l01 models are fakes (google for details). A important thing is that

Code: Select all

radio.setPALevel(RF24_PA_MAX)
do NOT work with this fake modules.

41300895
Posts: 28
Joined: Tue Oct 25, 2016 12:19 pm

Re: Does the RF24 library (from TMRh20) work on STM32F103?

Post by 41300895 » Tue Oct 25, 2016 5:13 pm

Hey everybody, I found this code, which used TMRh20 RF24 library:

Code: Select all

#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
#include <DHT.h>

// The DHT data line is connected to pin 2 on the Arduino
#define DHTPIN 2

// Leave as is if you're using the DHT22. Change if not.
//#define DHTTYPE DHT11   // DHT 11 
#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

// PIR variables
byte pirPin = 9;
int pirCalibrationTime = 30;

// Photocell variable
byte photocellPin = A3;

// Magnetic Door Sensor variable
byte switchPin = 12;

// Radio with CE & CSN connected to pins 7 & 8
RF24 radio(7, 8);
RF24Network network(radio);

// Constants that identify this node and the node to send data to
const uint16_t this_node = 1;
const uint16_t parent_node = 0;

// Time between packets (in ms)
const unsigned long interval = 1000;  // every sec

// Structure of our message
struct message_1 {
  float temperature;
  float humidity;
  byte light;
  bool motion;
  bool dooropen;
};
message_1 message;

// The network header initialized for this node
RF24NetworkHeader header(parent_node);

void setup(void)
{
  // Set up the Serial Monitor
  Serial.begin(9600);

  // Initialize all radio related modules
  SPI.begin();
  radio.begin();
  delay(5);
  network.begin(90, this_node);

  // Initialize the DHT library
  dht.begin();
  
  // Activate the internal Pull-Up resistor for the door sensor
  pinMode(switchPin, INPUT_PULLUP);

  // Calibrate PIR
  pinMode(pirPin, INPUT);
  digitalWrite(pirPin, LOW);
  Serial.print("Calibrating PIR ");
  for(int i = 0; i < pirCalibrationTime; i++)
  {
    Serial.print(".");
    delay(1000);
  }
  Serial.println(" done");
  Serial.println("PIR ACTIVE");
  delay(50);
}

void loop() {

  // Update network data
  network.update();

  // Read humidity (percent)
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit
  float f = dht.readTemperature(true);
  
  // Read photocell
  int p = analogRead(photocellPin);
  // Testing revealed this value never goes below 50 or above 1000,
  //  so we're constraining it to that range and then mapping that range
  //  to 0-100 so it's like a percentage
  p = constrain(p, 50, 1000);
  p = map(p, 50, 1000, 0, 100);
  
  // Read door sensor: HIGH means door is open (the magnet is far enough from the switch)
  bool d = (digitalRead(switchPin) == HIGH);
  
  // Read motion: HIGH means motion is detected
  bool m = (digitalRead(pirPin) == HIGH);
  
  // Headers will always be type 1 for this node
  // We set it again each loop iteration because fragmentation of the messages might change this between loops
  header.type = '1';

  // Only send values if any of them are different enough from the last time we sent:
  //  0.5 degree temp difference, 1% humdity or light difference, or different motion state
  if (abs(f - message.temperature) > 0.5 || 
      abs(h - message.humidity) > 1.0 || 
      abs(p - message.light) > 1.0 || 
      m != message.motion ||
      d != message.dooropen) {
    // Construct the message we'll send
    message = (message_1){ f, h, p, m, d };

    // Writing the message to the network means sending it
    if (network.write(header, &message, sizeof(message))) {
      Serial.print("Message sent\n"); 
    } else {
      Serial.print("Could not send message\n"); 
    }
  }

  // Wait a bit before we start over again
  delay(interval);
}

This was originally written for arduino + nrf24l01 + raspberry pi. I already made this system run. But I want to change this system into stm32f103 (blue pill board)+ nrf24l01 + raspberry pi. Thanks for your supports, I found a RF24.h that is compatible with STM32F103, but how about RF24Network.h? Is it compatible with STM32F103 too? If not, please show me what should I modify to make it works with stm32f103? If possible, can you port above code so that it can work with stm32f103 board? Thanks very much, I'm very appreciated! Please help me out, I just a mere student and really love electronics but I found it so difficult!

acronis
Posts: 122
Joined: Thu Mar 02, 2017 12:32 pm

Re: Does the RF24 library (from TMRh20) work on STM32F103?

Post by acronis » Thu Jul 27, 2017 6:24 am

Hello.
How and what kind of contacts (PIN) to connect the nrf24l01 to the Board STM32F407VET6 (black) to make it work ?

ChrisMicro
Posts: 299
Joined: Fri Mar 24, 2017 4:51 pm
Location: Germany

Re: Does the RF24 library (from TMRh20) work on STM32F103?

Post by ChrisMicro » Thu Jul 27, 2017 8:58 am

You can simply plug it in:
Image

acronis
Posts: 122
Joined: Thu Mar 02, 2017 12:32 pm

Re: Does the RF24 library (from TMRh20) work on STM32F103?

Post by acronis » Thu Jul 27, 2017 9:13 am

Thank you.
Is there a working example to work on SPI3 with this Board ?

// Radio with CE & CSN connected to pins 7 & 8
RF24 radio(7, 8);

How to write for this Board this line ?
What pins should I write ?

Post Reply