[Raspberry Pi – Arduino ]Lamp Switch[/caption]
A month ago we planned to manage our Arduino irrigator (aka Irrigatorino) through the NRF24l01+ wireless module. Our little project has been subjected to a little change, since we decided to make our life easier (buahahah…) and remote control a light over a browser (yes, mobile too). Surprisingly, a lot of people liked the little demo I’ve made with some raw code and asked for a tutorial, so here we are:

Ladies and (more probably) Gentleman, I’m glad to present you the lamp driven by a browser switch!

Hardware:

To test the wiring we suggest you to use the ping/pong test you can find in the RF24 libraries (both, of the raspberry and the arduino).

Raspberry Pi

The connection between the Raspberry Pi and the NRF24l01+ module was a little bit tricky, so you have to double ( or triple) check the wiring before giving up. Here is the wiring scheme, the Raspberry Pi is a revision 2, and the module is the + (plus) version.

Raspberry Pi (rev2) – NRF24l01+ Diagram

Arduino

The setup is easy and similar to a mix of our tutorials about Irrigatorino and NRF24l01+ Tests.
Just keep in mind that you are playing with dangerous voltage, so if you get struck, it’s only your fault. I would suggest, anyway, to check if everything (especially the relay board) is working with a multimeter, before connecting to a real lamp.

Arduino Nano – NRF24l01+ – Lamp Diagram

NOTE ON PACKET LOSS:

If you are encountering problems while receiving or transmitting, like packet loss, consider adding a capacitor of 10 μF between the pin 1 and 2 of the NRF24l01+, with ground capacitor side on pin 1. You should do this on both the modules, on raspberry and arduino.

Capacitor on NRF24l01+

Software

Our plan to communicate between all this stuff is to use a Node.js application to run a binary ‘sudo ./remote -m 81‘ and ‘sudo ./remote -m 80‘ sending a message to the Arduino with the text 81 and 80 . The message is composed by pin identifier (8) and action to perform (0|1|2). After the action has been read, all the other numbers identify the pin which the action refers to.

When the arduino receive it, it then sends back an acknowledgment packet to the raspberry with the same message (yeah, like a parrot), except for action 2, which will not perform any action, but will reply with actual state of the pin. In the meantime, if the message the arduino receive is 1 it will close a relay (turning on a light, like a good parrot), 0 will open it (I doubt a parrot could be so clever).

Sketching

The sketch for the Arduino Nano is nothing more than a Getting Started demo of the RF24 library. We just modified it to switch the relay on in case it receive a message with 1 as text, or open it otherwise. Of course you’ll need the RF24 library to make it work.

Arduino sketch

C

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

/*

This program is free software; you can redistribute it and/or

modify it under the terms of the GNU General Public License

version 2 as published by the Free Software Foundation.

*/

/*

Hack.lenotta.com

Modified code of Getting Started RF24 Library

It will switch a relay on if receive a message with text 1,

turn it off otherwise.

Edo

*/

#include <SPI.h>

#include "nRF24L01.h"

#include "RF24.h"

#include "printf.h"

intrelay=8;

//

// Hardware conf

//

// Set up nRF24L01 radio on SPI bus plus pins 9 & 10

RF24 radio(9,10);

//

// Topology

//

// Radio pipe addresses for the 2 nodes to communicate.

constuint64_t pipes[2]={0xF0F0F0F0E1LL,0xF0F0F0F0D2LL};

char*convertNumberIntoArray(unsignedshortnumber,unsignedshortlength){

char*arr=(char*)malloc(length*sizeof(char)),*curr=arr;

do{

*curr++=number%10;

number/=10;

}while(number!=0);

returnarr;

}

unsignedshortgetId(char*rawMessage,unsignedshortlength){

unsignedshorti=0;

unsignedshortid=0;

for(i=1;i<length;i++){

id+=rawMessage[i]*pow(10,i-1);

}

returnid;

}

unsignedshortgetMessage(char*rawMessage){

unsignedshortmessage=rawMessage[0];

return(unsignedshort)message;

}

unsignedshortgetLength(unsignedintrudeMessage){

unsignedshortlength=(unsignedshort)(log10((float)rudeMessage))+1;

returnlength;

}

voidsetup(void)

{

//

// Print preamble

//

Serial.begin(57600);

pinMode(relay,OUTPUT);

digitalWrite(relay,HIGH);

printf_begin();

printf("\nRemote Switch Arduino\n\r");

//

// Setup and configure rf radio

//

radio.begin();

// radio.setAutoAck(1); // Ensure autoACK is enabled

radio.setRetries(15,15);

radio.openWritingPipe(pipes[1]);

radio.openReadingPipe(1,pipes[0]);

radio.startListening();

radio.printDetails();

}

intgetState(unsignedshortpin){

booleanstate=digitalRead(pin);

returnstate==true?0:1;

}

voiddoAction(unsignedshortid,unsignedshortaction){

if(action==0){

digitalWrite(id,HIGH);

}else{

digitalWrite(id,LOW);

}

}

voidsendCallback(unsignedshortcallback){

// First, stop listening so we can talk

radio.stopListening();

// Send the final one back.

radio.write(&callback,sizeof(unsignedshort));

printf("Sent response.\n\r");

// Now, resume listening so we catch the next packets.

radio.startListening();

}

voidperformAction(unsignedshortrawMessage){

unsignedshortaction,id,length,callback;

char*castedMessage;

length=getLength(rawMessage);

castedMessage=convertNumberIntoArray(rawMessage,length);

action=getMessage(castedMessage);

id=getId(castedMessage,length);

if(action==0||action==1){

callback=action;

doAction(id,action);

}elseif(action==2){

callback=getState(id);

}

sendCallback(callback);

}

voidloop(void)

{

// if there is data ready

if(radio.available())

{

// Dump the payloads until we've gotten everything

unsignedshortmessage;

booldone;

// char * new;

unsignedshortrawMessage;

done=false;

while(radio.available())

{

// Fetch the payload, and see if this was the last one.

radio.read(&rawMessage,sizeof(unsignedlong));

// Spew it

printf("Got message %d...",rawMessage);

performAction(rawMessage);

delay(10);

}

}

}

Enabling Raspberry Pi GPIO and Installing Node.js

First we enable the Raspy GPIO:

Shell

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

sudo apt-getinstall python-dev

#make sure to COMMENT the lines with spi-bcm2708 and i2c-bcm2708 modules

#Pretty similar except for the python-dev package taken from the repos.

#source: Adafruit.com

sudo apt-getupdate

sudo apt-getinstall python-dev

sudo apt-getinstall python-rpi.gpio

#add this two modules: i2c-bcm2708 i2c-dev

sudo nano/etc/modules

sudo apt-getinstall python-smbus

sudo apt-getinstall i2c-tools

# do COMMENT the lines with spi and i2c modules

sudo nano/etc/modprobe.d/raspi-blacklist.conf

Let’s install node.js.
If you don’t need the latest version you can install it from the repos:

install nodejs

Shell

1

sudo apt-getinstall nodejs

Otherwise you can follow this instructions (under Ubuntu, Mint… section) to compile the latest version.

The Lamp Switch Application

For Raspberry Pi 2 users: some of our readers pointed out that this library is a bit outdated for the Raspberry 2 and it’s encountering some problems. Until we have time to update the article, please refer to this post on the Raspberry Official Forum to solve the “Preparing interface” stuck problem (kudos to Andrei).

What we need now is to write an application that will send a message to the arduino using the NRF24l01+ and the Raspi. So the first thing to do is download the RF24 library ported for Raspberry Pi. I would suggest to use git.

Install RF24 Raspberry library

Shell

1

2

3

4

5

6

7

8

9

10

11

#install git

sudo apt-getinstall git

#move to home

cd~

#clone the RF24 repos from edoardoo's github fork

git clonehttps://github.com/edoardoo/RF24RaspberryCommunicator.git

#retrieve the RF24 libs

cd~/RF24RaspberryCommunicator/

git submodule init

git submodule update

Now let’s install the library:

installing libraries

Shell

1

2

3

#install the library

cd~/RF24RaspberryCommunicator/RF24

sudo makeinstall

At this point, with the RF24 library installed, you can use the compiled remote application or compile it yourself with make command:

Shell

1

2

3

4

5

6

7

8

9

10

11

12

13

14

cd~/RF24RaspberryCommunicator

#optional: compile the remote application:

makeclean&&make

# Use remote command to send message to arduino (use sudo!)

# Message should be in this format: [id pin number][action to do]

# We've set relay at pin 8 and actions are:

# 0: turn off

# 1: turn on

# 2: get the actual state

#this message will turn the light on:

sudo./remote-m81

If everything went well you should be able to turn on (and off) the relay at this point using the sudo./remote -m 81 application, hearing the famous ‘click’ from the relay board.

The Node.js Lamp Application

At this point the only thing left is to build a little node.js server and bind a click on a client browser to an exec function on the raspberry server.

public (folder) => contains remoteClient.js, the javascript code loaded on the client and bootstrap folder for styling

remote.js => the actual node app

To run the application you need to:

Run the node application

Shell

1

2

3

4

5

6

7

8

9

10

11

12

#install the dependencies

cd~/RF24RaspberryCommunicator/LightSwitchInNode/RemoteRaspberry

#but first clean the npm cache

sudo npm cache clean

npm install

#nom nom nom installing stuff

#run the application

cd../

sudo node lightSwitch.js

Authors note (31/3/2014)

We love the way you support each other in the comments! It’s really a satisfaction for us.
We can’t be always here to answer your questions or doubts, and all your contributions are a really great value you add to this website.
Thank you all!

Update (10/5/2014)

We’ve refined a bit the code, making it more clean and definitely more readable. All you have to do now to add new buttons, is to create a new button in the node app views giving it a data-command tag. The value of that tag will be automagically passed to the old switch -f binary (now called remote -m). Of course you still need to tell arduino how to interpret the message. No more c++ recompiling. Plus, we’ve made the web interface give you a visual feedback when the raspberry receive the ack package from arduino.

Update (27/8/2014)

Looks like the Rf24 library author has changed the folder’s name. I’ve tried to fix the article with the new names. Didn’t have time to check it yet. Let me know if everything is ok. Edo.

Update (2/12/2014)

I’ve cleaned and updated the code to the latest RF24 libraries from TMRh20. I’ve managed to submit a bug (issue #46)of the RF24 lib to the TMRh20 github account, and apply the workaround he proposed. Unfortunately it looks like the modules needs time between communications, it’s a known bug that right now we don’t have time to fix. If you would like to fork the RF24RaspberryCommunicator project, magically fix it and make a pull request, we would love it. Right now, since we do it for free, we don’t have time to fix it. Hope you like it. Edo.

Thank you!
I would think about an address field inside the message.
If you have to do something a little more complicated, I would look at esp8266 which has a much better protocol (WIFI WPA2) and it’s more reliable, based on my experience.

I recently purchased an Elduino kit (RPi v3 Model B and an LCD Touchscreen), some 3rd party Arduino Nanos and a number of nRF24L01+ modules.

I am happy to report that I finally managed to get communication working between the RPi v3 + nRF and the Arduino Nano + nRF late last night.

All of my issues were with respect to the RPi, so I will share what I did to get it working here:

1) Start with a fresh install of Raspbian Jessie on the RPi v3
2) After first boot up, go into Preferences -> Raspberry Pi Configuration and turn on SPI in the interfaces tab (SPI is off by default)
3) Slightly modified terminal steps to above:

sudo apt-get install python-dev

#make sure to COMMENT the lines with spi-bcm2708 and i2c-bcm2708 modules. File was empty I believe, so just continue
sudo nano /etc/modprobe.d/raspi-blacklist.conf

#be sure to have i2c-dev module in the /etc/module. i2c-dev was already there
sudo nano /etc/modules

6) Open /RF24Communicator/RF24/RPi/bcm2835.h Change the #define BCM2835_PERI_BASE from 0x20000000 to 0x3F000000. You can verify where spi mem by opening /proc/iomem. In mine, there is a line that say:

3f204000-3f204fff : /soc/spi@7e204000

In bcm2835.h, I can see that BCM2835_SPIO_BASE is declared as:

#define BCM2835_SPIO_BASE (BCM2835_PERI_BASE + 0x204000)

So, I assume that 0x3f000000 is the correct setting for RPi v3

Save change to bcm2835.h. This fixed stalling at “preparing interface”

7) Open /RF24Communicator/RF24/makefile Change the -march to -march=armv8-a Change the -mtune to -mtune=cortex-a53 Save file

8) Open /RF24Communicator/makefile Change the -march to -march=armv8-a Change the -mtune to -mtune=cortex-a53 Save file

9) Open /RF24Communicator/remote.cpp Add #include at the top of the file. This fixes compiler error associated with sleep(1). Also, change the RF24 radio(CE pin, CSn Pin, SPI Speed) a few lines down from the top of the file to match your pin assignments. My wiring for your reference is the following:

# Use remote command to send message to arduino (use sudo!)
# Message should be in this format: [id pin number][action to do]
# We’ve set relay at pin 8 and actions are:
# 0: turn off
# 1: turn on
# 2: get the actual state

#this message will turn the light on:
sudo ./remote -m 81

I had some LEDs connected to my Arduino nano on pins 2, 3, 4, and 5, with 150Ohms to limit current. I modified the Arduino sketch with the following change in the setup function:

Thank you Derek for sharing your solution!
I’ll edit the post as soon as I can to integrate your solution. May I ask you if you verified all the procedure a second time? Being able to repeat all the steps with the same results is very important as you understand. Thank you again for your contribution.

My rf24l01 is working Rpi-2 good. But when I move rf24l01 from Rpi-2 to Rpi-3 it is not working now. Rpi-2 bcm 2836 and #define BCM2835_PERI_BASE 0x3F000000 . Rpi – 3 bcm2837 so that which PERI_BASE address Should I use ? Pls help me.

I Have a problem with the raspberry and module nrf24l01, I think is about this post.

When I send a parameter its work without problem, but when i use this some times to receive and send the stop of work and I need reset raspberry to came back to work. I dont no why this happing can you help me?

Its power issue:
1. Start from rpi supply,
2. Cable to supply power to rpi ( don’t use cheap Chinese)
3. If can do then best to use voltage regulators and power from 5v to regulator tk
Stepdown to 3.3v.

I had this issue for nearly 6 months, regulator and power cable was the fix for me

I’m using a Raspberry Pi 2 Model B and an Arduino Uno Rev. 3 and trying to make this work between the NRF24L01+ on each end. I have followed all the directions on here, and then continued on to the link provided for the RPi 2s (https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=99227&p=689610) which fixed several issues. I am still getting stuck at the “Preparing Interface” stage after I run “sudo ./remote -m 81”.

So far, I have updated the bcm285.h file, updated the MakeFiles, and added the “#include ” in Remote.cpp. Just curious to see if there has been any ground made on this error for RPi 2 B.

Hello!
Im having problems trying to make Arduino and Rpi comunicate. It seems both have the NRF propperly connected and both have the correcto libraries, I even updated my old RF24 one to the latest at https://github.com/edoardoo/RF24

As far as i can tell, i have an adress on the arduino, and i have the rpi sending data.. We have the same RF_CH and followed instructions step by step.
At the moment ive run out of any ideas to make it work after trying everything on the posts.
¿Any idea of what is the problem?

Cool project ! But if i want to control another device, i create another like that (arduino + nrf4l )
Ex: i have 2 arduino nano
Arduino 1: i use relay pin 7 and 8 to control two devices.
Arduino 2: i use rely pin 5 and 6 to control two device.
But i don’t understand why raspi just connect one in two device. if it worked when i sent message sudo ./remote -m 71 but 51 not. Sometime 51 worked but 71 or 81 did not. Do you have any idea to improve that? Thanks

As a thank you to your work I will comment an error in the Arduino code. I put your remote in a infinite loop and get a memory fault: the call to malloc (inside convertNumberIntoArray) eats the memory slowly but finally blocking the program. It must be replaced by something like:

I was having problem with the receiver logging garbage data to the console (serial monitor) after it had been on for a while and tried soldering the 10uF caps to pins 1 and 2 with the ground on pin 1 and that only made matters worse. Who knows whats happening with the sender.

Hi! I know this is kinda old but I have been trying to get this to work on a RP 2 B and an Arduino Uno. I’m a little confused as to how I am supposed to use VIM. Also, everything seems to work until I try sudo ./remote m- 81 at which point I get an error that ./remote command can’t be found. I’m inexperienced and sure it’s something silly but any help would be appreciated.

Sorry for late reply. You don’t have to use VIM, you can use whatever text editor you like (SublimeText, Emacs, Vi, Notepad…).
For the second error it looks like you are not in the right path to launch the command.

First off: This project totally got me set up and ready to go with these awesome NRF24L0+ radios. I love it. Thanks so much for the tutorial/code.

Next! When I uploaded the default code to my arduino (using an Uno) and run the command sudo ./remote -m 80 from the Raspberry Pi the arduino received it but didn’t seem to do anything.

So, I added some debug printf statements after the if (radio.available() ) { … } to see if the arduino radio has data … and it does! However, after three transmit cycles from the Raspberry Pi radio the arduino seemed to lock up and would remain that way until a reset.

I then added a printf statement after the while (radio.available () ){ … } statement and observed that the code was never executed!

I decided to remove the while loop altogether to see if that would fix the problem and voila! the problem is gone and I can now successfully turn on and off the LED I attached. I don’t know if that’s unique to me or if I did something else wrong, but I wanted to post this comment here in case anyone else runs into a similar problem.

It’s about 1am here so I’m going to head to bed … but I’m going to try and troubleshoot the code a little bit tomorrow to see what went wrong.

Here is my present code that works (notice where I commented out the while loop. didn’t work otherwise):

hi again… I finally solved almost all my problems, and now I’m testing my own project.
And now I’d need to exec a function from a web page (probably from an script.js). Since you’re using node.js to do it, my question is: Do I need to build another node.js server to do it? Or is it possible to do it just using: exec.execFile(‘../myProgram[…] directly on the script code??.
Thanks a lot again for your help

I have been using nrf24l01 module between arduino and raspberry pi. Pin connections are correct. when I write sudo ./remote -m 81 comment raspberry pi is waiting and when i touch the nrf24l01 raspberry send message. And when i touch nrf24l01 I am exposed to static electricity. And also cables in contact

Hi,
I’ve taken the freedom to edit a bit the comment to make it more instantly understandable.
Yes, the command works.
Please, use a link to an image, not to a search page (didn’t know which image you was referring to). I STRONGLY recommend to use the schematics I’ve posted on the article.

hi
this ins nearly the stuff i looked for but i have several questions, first sorry about my english 😉
well i want to connect my pi with fhem over my home-installed wifi with the arduino nanos.
the pi is with lan cable and the arduinos with the nrf24l01+ connected to the home-network. so i can put the arduinos with sensors and other stuff in every room with any cable – thats the plan… is it nessasary to use the nrf24l01 on both the pi and the arduino to communicate???
how should the code looke like? i’ m not good in programming (newby) maybe you. an help a little? thanks a lot!!!

Thank you Edo and all for such an excellent project. Really good work.
Appreciate you are very busy, but I wonder if you manage to
fix a unique id bug on toggle. Currently, if I toggle one button, both of them are changing status (toggled).

Hi Alex,
I’ve been chasing this kind of issue for a while… up to the day I read in a thread where the transmitting power was set to MAX and in the condition where both nRF devices were somewhat close one to the other. I added the following to both device code (Ardunio and Pi):

radio.setPALevel(RF24_PA_MIN);

This would reduce the transmit power level and (I guess) prevent saturation of the receiver antenna. Then it started to run/work as a charm.

Initially the devices where about 50cm to each other. Still with RF24_PA_MIN, devices are 10 meters away and work perfectly.

This is _Great_! I’m having a small issue though… i’m trying to run the example pingpair_dyn.py in the examples_RPi/ directory and i’m getting an error:

ImportError: No Module named RF24

I’m assuming this has something to do with python not knowing where to find the RF24 libraries? I’ve been banging my head against the wall for a couple hours with this, and am sure there is a simple solution. any tips?

Good to know. Don’t have time to check, but be careful because with the updated code library reference might not work.
Just for knowledge, inside the make file (few lines actually) there are the right code and options to use. Plus, it’s included the clean option (make clean).
Have fun!

Hi. Some time ago, with the ancient libraries, I was able to clone your job by myself. But now I’m using raspbmc in the raspberry, and I can’t get the nrf24 working fine anymore. I’ve been trying with xbian too, and neither worked. So I’m gonna do it configuring before the nrf24 and later trying to install xbmc.
So I just wanna ask which distro are you using in the raspberry to get all working.
By the way, my raspberry it’s a B tapeamos, not the newer B+.
Thanks a lot in advance

Looks like, I did get it to kind of work.. In previous tries today, I wasn’t able to make it work on the raspberry..
I have the NRF24 working successfully between arduinos, but when I try to transfer between an arduino and the RPi I get no success.

Please, do not add your personal website (with sponsors) to our comments, it will be considered spam. Add your github account if you want to share code. Just to mention, forking project, fixing bugs and making pull request is the right way to proceed.

The problem you are encountering, for instance, is the same I’m facing trying to use the new RF24 libraries.

I’m sorry, you’re right. The guys developing RF24 libs are changing some stuff, and after some tries I can see there’s no way to make it work with some easy hack. Let me work on it. It may take a while, but since it’s not a source of income, we’re doing it for free and for personal pleasure, right now it’s kind of a challenge to find the time to debug.
Sorry again.

hoo! jaja it was my mistake, but yes the name of the library is RF24, it seems like all going well, I’ve already sended data from an arduino to my rasp, but I want to do the same thing like your post, but, on the contrary, the transmitter (arduino), and reciver (raspberry pi)

Hi,
I tried to make a receiver using the same library but I can’t make it working..

I don’t understand why it seems that the application doesn’t take the settings writed in the start of the block:
ie:
in the block start I see:
pipes = [[0xf0, 0xf0, 0xf0, 0xf0, 0xe1], [0xf0, 0xf0, 0xf0, 0xf0, 0xd2]]
radio.setDataRate(NRF24.BR_250KBPS)
and other things.
When I start the script I see a warning message:
nrf24.py 373: RuntimeWarning: This channel is already in use, continuing anyway. USE GPIO.setwarnings(False) to disable warnings. GPIO.setup(self.ce_pin, GPIO.OUT)
Then I see the initial settings differs: I see RX ADDRP0-1= 0x0000000000 0x0000000000
data rate 1mbps
etc.
Then I see a bunch of random strange chars..
I have to say that, using the RF24 c++ version I have the same effect (bunch of chars).
Have you got some ideas about my problem?

Great project,
I only got one problem left and would be really thankful if someone could help me: When i start the server with “sudo node lightSwitch.js” i can access the website via another computer. But when i click one of the two buttons the terminal on the raspberry gives me an error: “librf24.so.1 no such file or directory”. In the RF24 Library i have a librf24.so.1.0 and a librf24-bcm.so.1.0. I have no idea what i have to do to get this going. Maybe theres something i need to do different since i have the B+ Raspberry version.
Thank you!
Fabian

Any thoughts on this is much appreciated.
Adjusted the remote.cpp code (First change the “__msleep(10)” to “sleep(0.01)” and the “__millis()” to “millis()”) but got a few errors during the check.
Arduino: 1.0.6 (Mac OS X), Board: “Arduino Nano w/ ATmega328”
remote.cpp: In function ‘bool switchLight(int)’:
remote.cpp:57: error: ‘sleep’ was not declared in this scope
remote.cpp: In function ‘int main(int, char**)’:
remote.cpp:87: error: ‘getopt’ was not declared in this scope
remote.cpp:95: error: ‘optarg’ was not declared in this scope
The mod code is now:
//Let’s take the time while we listen
unsigned long started_waiting_at = millis();
bool timeout = false;
while ( ! radio.available() && ! timeout ) {
sleep(0.01);
if (millis() – started_waiting_at > 1000 )
timeout = true;
Not sure what is meant by …’sleep’ was not declared & how to fix it.
Also saw the ‘get opt’ ‘optarg’ errors. Again not sure what this means & now to fix them.
That code is as:
while(( choice = getopt( argc, argv, “m:”)) != -1){
if (choice == ‘m’){

for anyone having trouble..the -bcm library files uses a different pin out. Also if you aren’t receiving data on the arduino, try increasing the data rate to 2mb on the wifi transmission. For some reason this works better than the 1mb setting.

I am noob as well lol when it comes to linux however I try my best to understand the code so I get a better idea of what the code is doing so I can fix any problem. Anyway I am also trying to figure how to use the express3-handlebars also the express3-handlebars is changed to express-handlebars so the code node.js needs some cleaning. I cant seem to run the npm install for some reason looks like my nodejs is not installed correctly or its using the different version than what it is suppose to be. I will try re-formatting rpi and doing this over so I don’t have any unnecessary stuff in there. If someone can help on this matter I would really appreciate. I get some warning message when I run the npm install and then some error message

again any help will be greatly appreciated thank. I would really love to have it controlled through the app. Also I did manage to run a different project of the express-handlebars and it worked I was able to go to the webpage. So I am sure that the stuff works.

I am an idiot forgot to copy the remote file to the root directory of the lightSwitch.js
got it working now.
I will try to redo all from fresh install and provide steps for people who is having trouble.
For now I guess try installing the latest update and dont forget to put the remote file inside the directory with the lightSwitch.js and route.js files

one more thing if you run the server and exit out of it then you can not run the server again. you need to restart rpi. If there is a better way please do tell. Thanks

Heyoo!
This project looks awesome, but I’m afraid that I’m a complete noob when it comes to anything Linux. You mention that some folders have changed names, but I assume that the code is still the old one?

Hi! I see a lot of interest, great! I don’t have time now to update the article in a more “nooby” way. In the next week I hope I’ll be able to provide a new compiled version of the remote.cpp bin and check the code. It did work for Ralf(another generous user of the blog), so I think it hasn’t to be that hard to find a way to make it works, so if you want to find the solution to your problem while you wait I think you can reach it.
Bests

This is currently not stable enough.
Although the switch programm is working for me, it stops responding after a few on off switches.

I assume this is some kind of timing issue when the rpi and the arduino talk…
I have to blame the arduinos, since the opening the serial from the ide does some kind of reset on the arduino , i can definetely get this up and running again, turning the relay on and off … for a while… then it just stops communicating back to the rpi.
In that scenario resetting the arduino (by opening a serial to the com port) , will then allow me to run the
sudo ./remote switch -m 1
from the pi again until it times out all the time again to get the response from the arduino.

Would it make sense to put some delays in the code to delay sending the response, or delay the acknowledge on the pi or similar.
Any ideas?

Have you tried changing the arduino? And powering the arduino from a different source like a phone charger?
It looks like is going to idle, but this is not a known feature of arduino. I did not have a similar issue, not even with different arduinos.
Make this tries and let men know.

Glad you solved, next thing could have been to test if the computer which power the arduino was sending to idle the usb port.
Anyway, have fun with your light and thank a lot for your donation, it has been really appreciated.

it was something on the arduino itself.
tried with different chargers, and different chargers to the usb , and or power over the usb serial.
worked fine with this little guy, and no change in code.http://www.pololu.com/docs/0J61

Thanks again for this tutorial.

I cannot get your Nodejs application to work and i have problems to install the npm properly

Thanks a lot for the nice documentation.
I was wondering whether you could help.
Sorry for being such a noob , but i hope somebody can give me some help.

i tried to compile the remote application running on the Pi as described , but i am getting a few errors.
I am beginning to think that the repository might have changes a bit , and therefore i will need to do something else, i just don’t know where to go.

The switch application includes a lot of other libraries as well which are not included in the repository mentioned in the article, so they must have gotten some other libraries and have just not updated the website for one reason or the other.

Hi Ralf! Just in time. Actually we were using the same repo we posted. I’ve updated the article with the changes the librarie’s author made. Please check if it work, it should just be a matter of folder’s name.
Thank you for proposing a fix.

I totally removed the folder and started from scratch using the updated instructions but I get exact same error. I tried modifying the make file to remove the ‘-bcm’ which appeared to create the same lib name as th example byut on running ,/remote got same error as original post.

Do you mind if I quote a few of your articles as long as I provide credit and sources back
to your site? My blog is in the exact same niche as yours and my visitors would genuinely benefit from some of the information you present here.

Hello and thank you for the great post. I am a newbie to the raspberry pi and arduino, so forgive my lack of understanding. I was able to follow everything until the “To run the application you need to:”. I was able to type “./remote -m 0 or 1” and turn on and off the light, but I haven’t been able to run the Node.js application. Below are the errors I got.

#install the dependencies
cd ~/RF24/librf24-rpi/librf24/examples/LightSwitchInNode/RemoteRaspberry
###(((okay, made it this far)))

#but first clean the npm cache
sudo npm cache clean
###(((at this point I get an error that states, sudo: npm: command not found)))

npm install
###(((i get an error that states, bash: npm: command not found)))

Hi! It looks like you don’t have node and npm (which is node package manager) installed. Try to install it ( http://joshondesign.com/2013/10/23/noderpi ) and retry.
So it is like “Hey Npm, clean your cache you dirty!” but no application named Npm is present in your raspy, so raspy says “Npm? Who’s this Npm? I can’t find him.”. Just a little example on how to interpret error messages.
Let us know how it goes.

Thanks a lot for your instructions and script! Everything worked well. I was able to use your script with python using the subprocess.call function and it worked well to send a message from the raspberry pi to the arduino.

subprocess.call([“./remote”, “-m”, “0”])

But I’m not able to use the answer (from the Arduino to my raspberry pi) in my python program. Does anyone know how to read and use the arduino’s message in a python program on the raspberry pi?

1) Automatic irrigation system: The arduino is connected to the pump and moisture sensor inside. I want to send wireless a command from the Raspberry Pi inside to the Arduino outside to activate the pump. I can do that with your remote.cpp file and it work well:

subprocess.call([“./remote”, “-m”, “0”])

Then I want the arduino to answer with the values of the moisture sensor. But I can’t use these values in my python code, I can received them with your remote.cpp, but I can’t use them in my python program….

2) Automatic curtain: The Arduino control the motor to open or close the curtain. The Raspberry Pi is connected to internet. I’m able to send the command from the Raspberry Pi to the Arduino, but I want to use the answer from the Arduino to know if the curtain are open or closed.

So my two projects are working only as a 1 way wireless communication…

Thanks again for your help, I whish I can share my projet if some people are interested and if it’s working 🙂

HI Jean-Maurice, I working for complete home automation. Where all the electric goods will be controlled by mobile . same configuration i m using . Rpi+arduino+many sensor+mobile app+ dimmer + web APIs + DVR.

Also I m thinking about for commercial production if project will be successful ..

i’m working on a very similar project (Automatic irrigation system) than yours and having the same troubles you got (sending back info from moisture sensor to the raspberry doesn’t work for me)
did you finally manage it ?
I would really appreciate if you could share your sources

Hi Neeraj,
I got the same problem. In my case, it was a power problem. I solved it by connecting a .1uF to 10 uF (MicroFarad) capacitor between the module and the arduino (and also between the module and the raspberry) from +3.3V to Gnd (Watch + and – !)

Are you trying to send encrypted messages? If so, the easiest thing to do is to either copy a cipher from some source and provide both ends with the key. In other words, the only way to encrypt/decrypt is to do so on the two computers (pis, arduinos, etc). There are lots of algorithms on the web you can try.

Hello there! I am in the middle of nowhere, trying to make Atmega8A and RPi talk to each other with a help from NRF2401A.

The thing, that troubles me the most is slightly different architecture of my radio modules (provided by local company). I found similar ones within SparkFun resources: https://www.sparkfun.com/products/152 . I also figured out how to use “DATA” pin (according to Nordic sheets, you just plug it to both MISO and MOSI pins through a 10k resistor).

Knowing all this, I still cannot establish a working connection. The rpi-hub example gives me “Recv: size=127 payload= pipe=7” no matter what. I thousand-checked all the hardware, and I am pretty certain about software on Atmega side.

Could you please give me any hints on porting your library to NRF2401A modules? Is there something I can do to determine the centre of the problem?

Good day,
First of all, great tutorial, I have it working using “./remote -m 1”, however, as soon as I type “sudo npm cache clean” or “npm install” I get the message “npm: command not found”. Any suggestions?
Thanks in advanced.

When i start now the app, the button is shown as a checkbox and the slider i can’t see (but is included in page).

I’m new in javascript, jquery and node.js, can you help me with this problem. in another app (also node.js based) i do this by the same way,
but there i have a html file with all the code in it and not separated.

Hi! Glad you liked it! To reach the app from the internet: register no-ip account, install noip2 client on raspberry, log on the noip2 client with you credentials, open the choosen port on the router .
Ex: if 192.168.1.2:3000 is your app raspy port, then you should open port 3000 on the router telling him to send connections on 192.168.1.2 on port 3000. You will then be able to connect on example.no-ip.org:3000.

I don’t see any point where that file is included. Are you sure you made it from a clean download of the libraries? If you are using raspberry (or any arm compatible), you can find a compiled version on the github repo: https://github.com/edoardoo/RF24RaspberryCommunicator

Nice work guys this is exactly what i am looking for at the moment. I have it working with a Pro Mini embedded within a light switch. However when i try to run it as a service i keep getting errors which i believe are related to file paths.

Being a bit of a noob in the Linux area i’m fumbling my way through but cant seem to work out how to resolve the issue. I’ve tried using /etc/init.d creating a service and also trying to run it using “forever”. I’ve messed about with the path variables but it only ever works if ran using the command as described in the post from the LightSwitchInNode folder.

Since I would like to have a kind of log of what I’m doing, I made it run on a screen instance.
So:
1- create a file called bootScript.sh, write inside:!/bin/bashcd/home/user/folderOfNodeApplication/sudo node remote.js

First, as you may know caps lock is equivalent to yell, and this is not a nice behaviour while asking help.
For what concern you errors, the first usually happen when your arduino IDE is unable to find the printf library, check at beginning for the include.
About the other errors, we recently used the same code to prepare another arduino, and everything worked flawlessy. Maybe you should send us the error.

I’m sorry about my behaviour. Thanks for your help. how to overcome about error “printf”. I have not understood how to check at beginning for the include. About 1 month ago, I have added printf library for arduino. If you have a printf library to surmount this error, Can you give me it? Thanks a lot.

Mohit#
Comply error in sketch in Arduino, error about printf. IDE belongs to Raspberry. if you have a printf library. Can you send me a link to download. I have also a question. What program do we use to make Switch.cpp

Here you can find all the informations needed for the printf library http://playground.arduino.cc/Main/Printf
To write Switch.cpp code you can use whatever text editor you prefer (vim, emacs, sublime, nano… even, not recommended, Word)
Just save the file with .cpp extension and compile it.

We are sending integers because it’s faster. I think that if you need to send a text you can just wrap it up in double apexes.
Remember to double check types, since you are in C (like done = radio.read( &message, sizeof(unsigned long) );)
Plus, don’t forget you have to compare strings instead of integers -> http://www.wikihow.com/Compare-Two-Strings-in-C-Programming

I have the same problem. I had it working compiling just fine. Then one day it just started spitting out the same errors. I have RF24.h in place, I’ve even reinstalled the RF24 library a couple of times with no success.

I can not figure out what’s wrong. (I’m a raspberry and linux beginner).

I’m building a system where my Pi (central unit) needs to connect to multiple Arduinos wirelessly.

So I have one Pi and 3 Arduinos set up.

Each Arduino has a variable called ID to help identify it.

If my Pi want to talk to Arduino with ID = 1, it will send a message with the format : 1XXXX
If it wants to talk to Arduino with ID =1, the message sent out will be : 2XXXX

Even though all units are kept in the same room for now, I’m noticing major packet drops.

One of my observations is this : when the Pi transmits, all the Arduinos receive, but if the ID does not match, they do nothing, only the Arduino with the correct ID will give an Acknowledgement back, but it turns out the Pi does not receive the Acknowledgement.

What I can’t figure out is, whether my modules are faulty, or are they not designed to work this way ..??

I have implemented this for a home automation system.
This works but the problem is it works intermittently.

At times it works really smooth.
The next moment it stops working.
After some investigation I found that the NRF24l01 on arduino receives the request works on it and sense the response back to NRF24l01 on raspberry. But the raspberry is not able to receive it resulting in packet drops.

Sorry for the delay, it has been a very busy period.
Don’t know actually what could be the problem . I didn’t have any kind of problem like your.
Before editing the code, since everything is working fine for me, I would suggest:
1- double check wires (it happened to us faulty cables drive crazy)
2- check the modules (they are really cheap and it’s always good to have one more for backup)
3- check the arduino (it happened to us to lose a whole day with a broken arduino… )
4- do not place the arduino nor the transmitter near wifi routers (same frequency 2.4GHZ)

Hi. Thanks a lot for sharing your jobs. I’ve done your project, in order to test miself!!. But now I have to implement my own solution. I want to control some blinds, each one with an arduino one, with a raspberry pi. But my problem is that I’m gonna use some wall blind switch. So I would need to transmit with the arduino to the raspberry when the switch its been activated, in order to memorize the new blind position on the raspberry. My first solution to do that it’s to have two different programs on the raspberry: One always running, just to listen when some arduino sends info to the raspberry with his new position. And another one program to control the blinds movement from remote.
Do you think that’s a good solution?? Maybe I could have some collission problems when both programs will be running?
Thanks a lot for you help

Hi Oscar,
hopefully I’ve understood the problem, please correct me if I’m wrong.
Since you are sending a message to the arduino, I suppose that the message is different when opening and closing the blinds.
Said that, the arduino actually replies with the same message you’ve sent (ack package) when he receive the message correctly.
Now, your raspberry know that your blind is being opened and can hold and manage this information. The same happen when you are closing the blinds. What you might to achieve is to have a “callback” when the blinds are effectively closed. Without creating two programs, you could use the same, given the fact the opening/closing the blinds will be eventually a locking command during all the time of the operations. Which means that when you are opening the blinds you can’t turn the lights on, for instance. This solution simply tell the raspberry to ping the arduinoBlinds every 5 seconds after you’ve pressed the action and send a message, like “What are you doing?” and he answers: 00) Closed .10)I was open and I’m closing .01) I was closed and I’m opening. 11) I’m open. When the raspberry receive the last command it will stop to ping.

Let’ make an example.
Raspberry knows that arduBlinds is 00 (Because of a ping or a previous known state saved, for instance, on a db). So Raspberry tells him “11”. Then Raspberry start to pings arduBlinds with “What are you doing?”. At first the arduBlinds will say “01” for the time it is opening the blinds and then, when finished, it will say “11”. At this point Raspberry stop to ping.

I don’t have libraries with me, but I suppose you have to switch mode from listening to sending.

Thanks for your time, unfortunately I don’t explain myselfarruinó. nk I can write all the process between arduino and the raspberry. My problem is when I use the wall switch, not via the raspberry. Every blind has it’s own switch, connected to the arduino to avoid conflict between switch and online orders. So, if I act the switch, the arduino can send a message to the raspberry with the new blind position. But then I need to Have the raspberry always listening for a message from the arduino ? . and another program to operate the blinds ‘online’??. Thanks a lot again

Hi! I know this is somewhat off topic but I was wondering if you knew where I could find
a captcha plugin for my comment form? I’m using the
same blog platform as yours and I’m having trouble finding
one? Thanks a lot!

Hi Mohit!
We are happy that you finally managed to make them communicate! We did have the same problems, which turned out it was just a matter of clean implementation and wiring.
About the succes rate, we’ve found that turning on the light in the same room as the Raspy was almost instantaneous. Maybe you have some device which is generating some intereferences with your modules. Remember that the frequency is 2.4 Ghz, which is the same as wifi and microwave oven (and some other devices). Try to change channel frequency and, if you are brave enough, have a look at some antenna mods you find around for 2.4ghz (remember I’ve seen this module with an antenna mod too).
Let us now how it goes!

I have the same issue here. Maybe 1 in 50 attempts will actually succeed.
Have tested at millimeters apart as well as a few meters.
Also disconnected all 2.4GHz devices (WAP, Cordless phone etc) as well as checked, checked and rechecked the pinouts and it still won’t work consistently. Have also tried a second Nano, and also replaced the NRF24I01+ at both ends.
Only thing I haven’t replaced is the Pi itself, but have tested the SPI by looping it back and that works fine.

At a bit of a loss as to why it won’t work. Only thing I can think of is to start again, reimport libraries and reimage the SD on the Pi in case there is any sort of conflict.

If anyone else has any other suggestions I will be happy to hear them.

So I went ahead and recreated the config and had the same issue, but stumbled across the cause of the issue by dumb luck on the pingpong test.
Seems if I am connected to the serial interface of the Arduino tests fail, however, if I am not connected it all works fine.
I will try and dig into why, but at least it is working for now. If I can’t fix it, it will make debugging other issues a little harder but I can live with that.

Hi!
I would suggest a possible cause.
Recenlty we were working on a project with 433MHZ module. We needed to listen carefully the waves received from another Arduino with the TX module. I noticed that when connected to the computer the received message was a lot disturbed by the dirty DC passed with the USB cable (even if the computer was on battery), but when the tx arduino was attached to a 12v battery, then the received signal was really clean.

That said, since I didn’t find this problem with my computer nor the raspberry or the chinese usb adapter the arduino is powered by, you could try attach the arduino to a normal usb adapter and the raspberry too.

Hi,
thanks for this great project. I succeed with it. But I have one strange behaviour. I succeed to send the on (1) and off (2) to arduino from Raspberry Pi. But when the Raspberry Pi get back the response it never receive the same information. I would expect that when Raspberry Pi send “1” to arduino then arduino send back again “1”. But I never get this back on Raspberry Pi. I always get some different strange number (0, 1678878, 245584 etc…) but never 1. I also tryed to send same static number from the Arduino to Pi but I never receive the correct number on Pi. Do you have any idea where could be the problem?

Yes, i have moved to the next steps. I have test “sudo ./switch -f on” and i get “Failed, response timed out”.

I have double check the connections, but it is ok. Maybe my problem is in the Arduino side (i have use Arduino uno). Right now, the Arduino sketch is showing a message when is compiled, it says “printf_begin was not declared in this scope”.

Have a double check at the wires first. We’ve exchanged pins so many times! We have a UNO around, so if you want we can have look. I don’t see the point why it should not work anyway. Keep in mind that we are planning a huge step up on this project in the next months. Plus, we have to work too 😀 So we’ll not have so much time to support all the requests of help we receive. Said that, since we would have really appreciated an help from someone who finally made it, we’ll reply as soon as we can, trying to keep everything updated.
Have a nice day!

Hi Ishu, I succeed to run the switch through PHP script using the exec() function and also .sh file in between the switch and PHP script. Part on which I spend the most of the time was correct rights of all the files and also the fact that I succeed only in case that www-data account has been set to work without password. PHP script then run the .sh file with one parameter which can be then passed to switch -f . When switch example is slightly modified then it can sand many kinds of commands to arduino. If more info neede let me know and I can add some more tips.

Hi. Great project, thx! I have done everything and now i can switch one relay from Pi 🙂 I’m trying to add some line to turn on second relay on board but need some help. Could You guys give me some tips? How should Arduino Sketch and switch.cpp looks like? Thank You 🙂

Holaaaa Tom!
You need to pass a different message to arduino for each relay you want to switch.
Let’s say “1” is “turn on the R1 (relay 1)” and “3” is “turn on the R2 (relay 2)”.
Remember you need a message to turn them off too. So, you’ll define “0” as a message to turn off R1, and “2” to turn off R2.
With this pattern you’ll have all couples, with a pair and an odd number. The pair switch off, the odd switch on.
Like this:
R1 | R2 | R3
—————————
0 – 1 | 2 – 3 | 3 – 4
off-on | off-on | off-on

We are working on something similar, but more “modular” on our side. More in the next week!

maybe someone can help me. i dont get it. i suppose, that may this is the reason:

pi@raspbmc ~$ sudo ldconfig -v | grep librf
ldconfig: Path /lib/arm-linux-gnueabihf' given more than once
ldconfig: Path /usr/lib/arm-linux-gnueabihf’ given more than once
librf24.so.1 -> librf24.so.1.0

i also checked “sudo modinfo i2c-dev”, witch results in “module i2c-dev not found”.

I think this is a great project. I am working on a project that would send text from tweets from the pi to an arduino with an lcd screen using the nrf24l01. Every example I find out there for the pi is sending numeric information. I saw the serialchat example in the arduino and I am having a tough time replicating something similar to send a string across the radio. Anyway you could point me in the right direction?

Its a wonderful project and has a huge potential, I am from India, I am working on the same kind of project. Your project is a great help to us, My question is can we use nRF24LE so that data sent can be encrypted and to avoid arduino nano. Thank you once again for the resources.

I digged around and I found some material to work on. First of all I wuold use AES simmetric key to do the job. Here some reference to a library written for NRF24LE1.
I don’t understand why the WiFi module alone… if you want to explain us you project or link something we would be grateful!

I don’t understand why the WiFi module alone… if you want to explain us you project or link something we would be grateful!

Indeed, I don’t understand neither 😀 . That module (the NRF24LE1) doesn’t seems to have a micro-processor you could use as an arduino’s one. Plus,I think that if you would use it for encryption it would be better to keep it separated from other calculus, which right now belongs to arduino.

If we’ve misunderstood please correct us, since we are looking for an “encrypted” solution too.
Have a nice day.

I’ve been working with the same project for the past 2 months and got the rpi and the arduino uno talking to each other via the RF24 unit(s). I also managed to get some PHP + MySql codes working to turn the relay OFF @ ON respectively.

But the only thing that halted my project is the RF24 security, which if i’m not mistaken, transfers the data forth and back in plain form. I thought that it would be ‘dangerous’ if someone could just send a forged packet and my arduino would blindly accept it. So I’ve been googling around for a library that could be used both in RPI & Arduino Uno to encrypt the packets with no luck.

So does the RF24 unit has any security implementation inside that i can use?.

Hi Mr.F. I don’t think the little RF24 has the dedicated hardware which can provide encryption. You’ll have to do develop it software-side both on the Raspberry Pi (not a problem) and the arduino (can be a problem).
Like you, I mind very much about security, but for this little test I didn’t bother too much actually. Do you have so much “nerd” neighbours???

Actually it is a bidirectional communication. Indeed, we’ve made arduino reply with the same message he received, to check if communication worked, but you can make him respond whatever you need.
On line 92 of Arduino sketch you can changeradio.write(&message,sizeof(unsignedlong));
to deliver anything you want.
Have nice coding!

ENG
Hi Angelo,
we’ve seen your awesome project on youtube. The road we’ve taken is definitely more “wireless” 😀
Js instructions are interpreted by a binary file pre compiled written in C++. This binary interface itself with RF24 libraries written in C (If I remember well) which probably (almost surely) have a python sub layer.
To be able to use those libraries you should unmount them or look for the pre porting version. From there, I think the rest is pretty easy.
Next time could be better to write in english, so all the world can take advantage from your doubts and projects.
Thank you again for you participation!

In these days I found the time to write down a wrapper to send data from Raspberry through nRF24L01. It’s simply to use, it accepts three parameters.. the sender address, the receiver address and the message coded in long long unsigned int.

Thanks to this wrapper the only thing you have to do is to configure the receiver, in my case an Arduino Uno, in order to understand your messages.

By continuing to use the site, or scrolling, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.