connectivity for local sensors and actuators using nrf24l01+

51
1 / 51 Local Connectivity nRF24L01+ Eueung Mulyana https://eueung.github.io/012017/nrf24 CodeLabs | Attribution-ShareAlike CC BY-SA

Upload: eueung-mulyana

Post on 13-Apr-2017

120 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Connectivity for Local Sensors and Actuators Using nRF24L01+

1 / 51

Local Connectivity

nRF24L01+Eueung Mulyanahttps://eueung.github.io/012017/nrf24CodeLabs | Attribution-ShareAlike CC BY-SA

Page 2: Connectivity for Local Sensors and Actuators Using nRF24L01+

Outline

Introduction

Getting Started - Preparation

Getting Started - Code & Play

Simple Remote Control

Gateway

2 / 51

Page 3: Connectivity for Local Sensors and Actuators Using nRF24L01+

3 / 51

Page 4: Connectivity for Local Sensors and Actuators Using nRF24L01+

Introduction

4 / 51

Page 5: Connectivity for Local Sensors and Actuators Using nRF24L01+

5 / 51

nRF24L01+nRF24L01+ is a highly integrated, ultra

low power (ULP) 2Mbps RFtransceiver IC for the 2.4GHz ISM

(Industrial, Scienti�c and Medical)band 2.400 - 2.4835GHz.

The Nordic nRF24L01+ integrates a complete 2.4GHz RFtransceiver, RF synthesizer, and baseband logic including thehardware protocol accelerator (Enhanced ShockBurst)supporting a high-speed SPI interface for the applicationcontroller.

With peak RX/TX currents lower than 14mA, a sub uA powerdown mode, advanced power management, and a 1.9 to 3.6Vsupply range, the nRF24L01+ provides a true ULP solutionenabling months to years of battery life from coin cell or AA/AAAbatteries.

Ref: Nordic Semiconductor

Page 6: Connectivity for Local Sensors and Actuators Using nRF24L01+

6 / 51

nRF24L01+

1. ISM Frequency Band at 2.400 - 2.4835 GHz (Spacing at 1 or2 MHz, GFSK)

2. 126 RF Channels3. Air Data Rate Con�gurable to 2 Mbps (Options: 250 kbps, 1

Mbps)4. 4-Pin Hardware SPI5. 5V Tolerant Inputs6. 6 Data Pipe MultiCeiver for 1:6 star networks

Notes: Power still at 3.3V!

Page 7: Connectivity for Local Sensors and Actuators Using nRF24L01+

Pin Map

7 / 51

Page 8: Connectivity for Local Sensors and Actuators Using nRF24L01+

Important Notes

Radio is sensitive to Noises! Make sure that the circuit (wire,solder, etc.) is stable.

Anything �uxtuates is bad!

8 / 51

Page 9: Connectivity for Local Sensors and Actuators Using nRF24L01+

Preparation

Getting Started

9 / 51

Page 10: Connectivity for Local Sensors and Actuators Using nRF24L01+

This setup is for demo purpose only. Can be any MCUs.10 / 51

GettingStarted

Arduino IDE, NodeMCU &Nano

1. Install RF24 Library2. Prepare the First Node - NodeMCU3. Prepare the Second Node - Arduino Nano

Page 11: Connectivity for Local Sensors and Actuators Using nRF24L01+

RF24 Library

11 / 51

Page 12: Connectivity for Local Sensors and Actuators Using nRF24L01+

RF24 Library

12 / 51

Page 13: Connectivity for Local Sensors and Actuators Using nRF24L01+

First Node - NodeMCU

13 / 51

Page 14: Connectivity for Local Sensors and Actuators Using nRF24L01+

First Node - NodeMCU14 / 51

Page 15: Connectivity for Local Sensors and Actuators Using nRF24L01+

Second Node - Nano

15 / 51

Page 16: Connectivity for Local Sensors and Actuators Using nRF24L01+

Second Node - Nano16 / 51

Page 17: Connectivity for Local Sensors and Actuators Using nRF24L01+

Code & Play

Getting Started

17 / 51

Page 18: Connectivity for Local Sensors and Actuators Using nRF24L01+

Simple Transmit & Receive

NodeMCU - Transmit | Nano - Receive

Ref: Example Sketches

18 / 51

Page 19: Connectivity for Local Sensors and Actuators Using nRF24L01+

#include <SPI.h> #include "nRF24L01.h"#include "RF24.h"

RF24 myRadio (2, 15);

byte addresses[][6] = {"1Node"}; int dataTransmitted;

void setup() { Serial.begin(115200); delay(1000); Serial.println(F("RF24/Simple Transmit data Test"));

dataTransmitted = 100;

myRadio.begin(); myRadio.setChannel(108); myRadio.setPALevel(RF24_PA_MIN);

myRadio.openWritingPipe( addresses[0]); delay(1000);}

void loop() { myRadio.write( &dataTransmitted, sizeof(dataTransmitted) );

Serial.print(F("Data Transmitted = ")); Serial.print(dataTransmitted); Serial.println(F(" No Acknowledge expected")); dataTransmitted = dataTransmitted + 1; delay(500);}

19 / 51

NodeMCU

Page 20: Connectivity for Local Sensors and Actuators Using nRF24L01+

20 / 51

NodeMCUSerial

1384, room 16 tail 8chksum Data Transmitted = 100 No Acknowledge expectedData Transmitted = 101 No Acknowledge expectedData Transmitted = 102 No Acknowledge expectedData Transmitted = 103 No Acknowledge expectedData Transmitted = 104 No Acknowledge expectedData Transmitted = 105 No Acknowledge expected...

Page 21: Connectivity for Local Sensors and Actuators Using nRF24L01+

#include <SPI.h> #include "nRF24L01.h"#include "RF24.h"

RF24 myRadio (7, 8);

byte addresses[][6] = {"1Node"}; int dataReceived;

void setup() { Serial.begin(115200); delay(1000); Serial.println(F("RF24/Simple Receive data Test"));

myRadio.begin(); myRadio.setChannel(108); myRadio.setPALevel(RF24_PA_MIN);

myRadio.openReadingPipe(1, addresses[0]); myRadio.startListening();

}

void loop() { if (myRadio.available()) { while (myRadio.available()) { myRadio.read( &dataReceived, sizeof(dataReceived) ); }

Serial.print("Data received = "); Serial.println(dataReceived); } }

21 / 51

Nano

Page 22: Connectivity for Local Sensors and Actuators Using nRF24L01+

22 / 51

NanoSerial

RF24/Simple Receive data TestData received = 100Data received = 101Data received = 102Data received = 103Data received = 104Data received = 105...

Page 23: Connectivity for Local Sensors and Actuators Using nRF24L01+

RF24 Sample Code

23 / 51

Page 24: Connectivity for Local Sensors and Actuators Using nRF24L01+

RF24 Sample Code - GettingStarted

24 / 51

Page 25: Connectivity for Local Sensors and Actuators Using nRF24L01+

#include <SPI.h>#include "nRF24L01.h"#include "RF24.h"

byte addresses[][6] = {"1Node","2Node"};

RF24 radio(2,15);

bool radioNumber = 0;bool role = 1;

/**********************************************************/void setup() { Serial.begin(115200); Serial.println(F("RF24/examples/GettingStarted")); Serial.println(F("*** PRESS 'R' to begin receiving from the other node"));

radio.begin(); radio.setChannel(108); radio.setPALevel(RF24_PA_MIN);

if(radioNumber){ radio.openWritingPipe(addresses[1]); radio.openReadingPipe(1,addresses[0]); }else{ radio.openWritingPipe(addresses[0]); radio.openReadingPipe(1,addresses[1]); } radio.startListening();}

void loop() {

/****************** Ping Out Role ***************************/ if (role == 1) { radio.stopListening();

Serial.println(F("Now sending"));

unsigned long start_time = micros(); //radio.write( &start_time, sizeof(unsigned long));

if (!radio.write( &start_time, sizeof(unsigned long) )){ Serial.println(F("failed")); 25 / 51

NodeMCU

Page 26: Connectivity for Local Sensors and Actuators Using nRF24L01+

26 / 51

NodeMCUSerial

^$#%$#@*&%)# Why??

Nevermind for now!

Unplug NodeMCU, Plug-In Nano ..

Now sendingfailedFailed, response timed out.Now sendingfailedFailed, response timed out.Now sendingfailedFailed, response timed out.Now sendingfailedFailed, response timed out.Now sendingfailedFailed, response timed out.Now sendingfailedFailed, response timed out....

Page 27: Connectivity for Local Sensors and Actuators Using nRF24L01+

#include <SPI.h>#include "nRF24L01.h"#include "RF24.h"

byte addresses[][6] = {"1Node","2Node"};

RF24 radio(7,8);

bool radioNumber = 1;bool role = 0;

/**********************************************************/void setup() { Serial.begin(115200); Serial.println(F("RF24/examples/GettingStarted")); Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));

radio.begin(); radio.setChannel(108); radio.setPALevel(RF24_PA_MIN);

if(radioNumber){ radio.openWritingPipe(addresses[1]); radio.openReadingPipe(1,addresses[0]); }else{ radio.openWritingPipe(addresses[0]); radio.openReadingPipe(1,addresses[1]); } radio.startListening();}

void loop() {... ...}

27 / 51

Nano

Page 28: Connectivity for Local Sensors and Actuators Using nRF24L01+

28 / 51

NanoSerial

Get Back to NodeMCU, Switch It On!

RF24/examples/GettingStarted** PRESS 'T' to begin transmitting to the other node

# After NodeMCU Switched ONSent response 9284083Sent response 10286475Sent response 11288847Sent response 12291268Sent response 13293653...

Page 29: Connectivity for Local Sensors and Actuators Using nRF24L01+

29 / 51

NodeMCUSerial - Take 2

Find Another Serial Console..

Now It Looks Good.. Explain!

Now sendingSent 18612291, Got response 18612291, Round-trip delay 1828 microsecondsNow sendingSent 19614686, Got response 19614686, Round-trip delay 1840 microsecondsNow sendingSent 20617552, Got response 20617552, Round-trip delay 1803 microsecondsNow sendingSent 21619866, Got response 21619866, Round-trip delay 1800 microsecondsNow sendingSent 22622153, Got response 22622153, Round-trip delay 1806 microsecondsNow sendingSent 23624535, Got response 23624535, Round-trip delay 1840 microseconds...

Page 30: Connectivity for Local Sensors and Actuators Using nRF24L01+

Simple Remote Control

30 / 51

Page 31: Connectivity for Local Sensors and Actuators Using nRF24L01+

NodeMCU - Remote Controller31 / 51

Page 32: Connectivity for Local Sensors and Actuators Using nRF24L01+

Nano - Local Controller32 / 51

Page 33: Connectivity for Local Sensors and Actuators Using nRF24L01+

Simple Remote Control

33 / 51

Page 34: Connectivity for Local Sensors and Actuators Using nRF24L01+

#include <SPI.h> #include "nRF24L01.h"#include "RF24.h"

RF24 myRadio (2, 15); const int SW1 = 5;

byte addresses[][6] = {"1Node"}; int dataTransmitted; int button;

void setup() { pinMode(SW1, INPUT); dataTransmitted = 10; button = 0;

Serial.begin(115200); delay(1000);

myRadio.begin(); myRadio.setChannel(108); myRadio.setPALevel(RF24_PA_MIN);

myRadio.openWritingPipe( addresses[0]); delay(1000);}

void loop() { int newButton = digitalRead(SW1); if (newButton != button) { button = newButton;

if (button == HIGH){ dataTransmitted = 20; } else { dataTransmitted = 10; } myRadio.write( &dataTransmitted, sizeof(dataTransmitted) ); Serial.print(F("Data Transmitted = ")); Serial.println(dataTransmitted); } 34 / 51

NodeMCU

Page 35: Connectivity for Local Sensors and Actuators Using nRF24L01+

35 / 51

NodeMCUSerial

After Some ON-OFFs

1384, room 16 tail 8chksum Data Transmitted = 20Data Transmitted = 10Data Transmitted = 20Data Transmitted = 10Data Transmitted = 20Data Transmitted = 10Data Transmitted = 20

Page 36: Connectivity for Local Sensors and Actuators Using nRF24L01+

#include <SPI.h> #include "nRF24L01.h"#include "RF24.h"

RF24 myRadio (7, 8); const int LED = 2;

byte addresses[][6] = {"1Node"}; int dataReceived;

void setup() { pinMode(LED, OUTPUT);

Serial.begin(115200); delay(1000);

myRadio.begin(); myRadio.setChannel(108); myRadio.setPALevel(RF24_PA_MIN);

myRadio.openReadingPipe(1, addresses[0]); myRadio.startListening();}

void loop() { if (myRadio.available()) { while (myRadio.available()) { myRadio.read( &dataReceived, sizeof(dataReceived) ); }

Serial.print("Data received = "); Serial.println(dataReceived);

if (dataReceived == 10) { digitalWrite(LED, LOW); } else { digitalWrite(LED, HIGH); } } } 36 / 51

Nano

Page 37: Connectivity for Local Sensors and Actuators Using nRF24L01+

37 / 51

NanoSerial

After Some ON-OFFs

Data received = 20Data received = 10Data received = 20Data received = 10Data received = 20Data received = 10Data received = 20Data received = 10Data received = 20Data received = 10

Page 38: Connectivity for Local Sensors and Actuators Using nRF24L01+

Simple Remote Control

38 / 51

Page 39: Connectivity for Local Sensors and Actuators Using nRF24L01+

Nano - Attach a Device39 / 51

Page 40: Connectivity for Local Sensors and Actuators Using nRF24L01+

Nano - Attach a Device

40 / 51

Page 41: Connectivity for Local Sensors and Actuators Using nRF24L01+

Connecting to Blynk Cloud

Gateway

41 / 51

Page 42: Connectivity for Local Sensors and Actuators Using nRF24L01+

Notes

This is only an example of integration of local-connected sensorsand actuators to other (cloud-based) services. This is applicable

not only for Blynk or Firebase, but also for other services.

42 / 51

Page 43: Connectivity for Local Sensors and Actuators Using nRF24L01+

#include <SPI.h> #include "nRF24L01.h"#include "RF24.h" #include <ESP8266WiFi.h>#include <BlynkSimpleEsp8266.h>

RF24 myRadio (2, 15);

const int SW1 = 5;

byte addresses[][6] = {"1Node"}; int dataTransmitted; int button;

char auth[] = "c5d0dea217cd49539d7bed14d1234567";char ssid[] = "emAP-01";char pass[] = "1010101010";

BLYNK_WRITE(V1){ int pinValue = param.asInt();

if (pinValue == HIGH){ dataTransmitted = 20; } else { dataTransmitted = 10; } myRadio.write( &dataTransmitted, sizeof(dataTransmitted) );

Serial.print(F("pinValue = ")); Serial.println(pinValue);

Serial.print(F("Data Transmitted = ")); Serial.println(dataTransmitted);}

void setup() { Serial.begin(115200); Blynk.begin(auth, ssid, pass); delay(1000);

pinMode(SW1, INPUT); 43 / 51

NodeMCU

Page 44: Connectivity for Local Sensors and Actuators Using nRF24L01+

Blynk Button with Virtual Pin V144 / 51

Page 45: Connectivity for Local Sensors and Actuators Using nRF24L01+

45 / 51

NodeMCUSerial

After Some ON-OFFs via PhysicalButton and Blynk Virtual Button

1384, room 16 Data Transmitted = 20Data Transmitted = 10pinValue = 1Data Transmitted = 20pinValue = 0Data Transmitted = 10pinValue = 1Data Transmitted = 20pinValue = 0Data Transmitted = 10pinValue = 1Data Transmitted = 20pinValue = 0Data Transmitted = 10pinValue = 1Data Transmitted = 20Data Transmitted = 20Data Transmitted = 10pinValue = 0Data Transmitted = 10Data Transmitted = 20pinValue = 1Data Transmitted = 20pinValue = 0Data Transmitted = 10Data Transmitted = 10

Page 46: Connectivity for Local Sensors and Actuators Using nRF24L01+

Refs/Resources

46 / 51

Page 47: Connectivity for Local Sensors and Actuators Using nRF24L01+

Refs/Resources1. Nordic Semiconductor2. Example Sketches @arduino-info3. Connecting the Radio | MySensors4. nRF24/RF24: Optimized fork of nRF24L01 for Arduino & Raspberry Pi/Linux Devices

47 / 51

Page 48: Connectivity for Local Sensors and Actuators Using nRF24L01+

NodeMCU V1.0 Pin Map

48 / 51

Page 49: Connectivity for Local Sensors and Actuators Using nRF24L01+

Nano V3.0 Pin Map

49 / 51

Page 50: Connectivity for Local Sensors and Actuators Using nRF24L01+

Nano V3.0 Pin Map (?)50 / 51

Page 51: Connectivity for Local Sensors and Actuators Using nRF24L01+

51 / 51

ENDEueung Mulyanahttps://eueung.github.io/012017/nrf24CodeLabs | Attribution-ShareAlike CC BY-SA