2BIT || ! 2BIT 👨‍💻
  • Home
  • Blog
  • About
  • Social
  • Affiliate ​Store
  • Home
  • Blog
  • About
  • Social
  • Affiliate ​Store

Smart your home up with Raspberry pi, Nodejs, Bluetooth, Serial and RF24

1/14/2018

 
As part of my SmartHomeDIY project (open source), I need as small and efficient server which could also talk to my Arduino stations that are using RF24 module, so for the rescue came my Raspberry PI and it's serial GPIO pins.
PI Server connected to BT module over UART
All of my Arduino stations are talking to each other with the RF24 module (connected via SPI) and I needed a fast lightweight server (and small in size) to talk to that network.
I wanted to use the PI to do that job and I knew I could use it's GPIOs as serial (usually used for console logging in Hardware development stages)
So I figured why not connect a serial device to the PI GPIOs and through this device I will talk to my network.
​So I figured up the following setup
Dispatch station bridging BT to RF24
I connected the PI to a serial BT device called Bluetooth HC-05
This device can talk via serial and forward everything to the other side BT device which is connected to an Arduino which in turn checks the commands and forward these commands to the specified Arduino station via the RF24 module, we will talk about the protocol and the BT device configuration in a moment.
Picture
AliExpress.com Product - Raspberry Pi 3 Starter Kit Original Raspberry Pi 3 + 3.5 inch Touchscreen + 9-layer Acrylic Case + 2.5A Power Plug + Heat Sink
AliExpress.com Product - Original Raspberry Pi 3 Model B Board 1GB RAM Quad Core 1.2GHz 64bit CPU with WiFi & Bluetooth Function made in UK Free Shipping

nodejs, pi and serial

To use the PI serial you will first need to disable PI console prints to serial, to do that go here
To know more about pi serial read here
If cannot disable serial for pi read more here
As a summary here is what you will have to do:
1. download a utility

2.  disable serial

3. ​check status

​You will need to connect the bluetooth module to the PI GPIOs as shown above - here is a closeup
BE SURE TO DOUBLE CHECK BECAUSE THE PI DOES NOT HAVE PROTECTION CIRCUITRY - YOU CAN BURN YOUR PI FOREVER!
SERIAL Connection
Note that there are at least 2 types of revision for the PI board - so make sure which one you have - each one has a different GPIO layout

REV 1

REV 1

Rev 2

REV 2

Configuring the BT modules to talk to each other

 To config the 2 BT modules to talk to each other and act as an over the air UART we will need to enter the AT command configuration mode, to do that we will give power to HC-05 while key pin is connected to Arduino pin (9) and pin (9) is already HIGH (or other pin you wish to use or any HIGH level source).
  1. Note the BT serial baud-rate is correct - regular operation (getting and sending data) is used on a different baud-rate then the configuration mode, for configuration mode use: 38400 for operation mode you can configure what ever you need.
  2. Note that the console is at "Both NL & CR" state (console can be any serial terminal such as putty or the Arduino IDE serial monitor
We will set: 
  1. Master and Salve to be binded and using auto connect mode
  2. The salve will be set to AT+ CMODE:2  (Which means "Slave-Loop" - which will make the auto connection work). 
  3. the module on the PI side will be Master, to do that use: AT+ROLE=1
  4. AT+BIND=YOUR_BT_MAC_ADDRESS (to get the master address use: AT+ADDR?)
  5. The dispatch station will use: AT+ROLE=2 and then AT+CMODE=2

After all these commands the BT modules should be binded and auto connect always.

For more AT Commands info look:
  1. Here
  2. And here
  3. And here
  4. And here
  5. And the attached AT commands HC_05 reference PDF at the bottom.
​​​
After all the configurations, if everything is fine, the BT modules will blink at a slower rate, which indicates that the modules are in sync and connected to each other. note that every time you will reset the modules they will blink fast until connected to each other and in sync, I am using that indication a lot, for example to see if the modules are in range when installed in my house walls.
AliExpress.com Product - HC05 HC-05 master-slave 6pin JY-MCU anti-reverse, integrated Bluetooth serial pass-through module, wireless serial

talking to the serial from node

After everything is connected and configured all we have to do is to send the data (commands) in my case through the serial (UART) port to the dispatch station
To do that will we have to use node serial module
https://www.npmjs.com/package/serialport
Install it as any node module.

And here is an code example how to use it 

Every command that we will send from this serial site will go through BT module and received on the other BT module and the Arduino on the dispatch station side will received that data as any other serial data, from there that station can parse that and forward it via SPI to the other stations
Here is the code for the dispatch station.
Important parts of the code are highlighted here.

AliExpress.com Product - Free shipping 1pcs/lot New FT232RL FT232 USB TO TTL 5V 3.3V Download Cable To Serial Adapter Module For Ardui USB TO 232

Comments are closed.
    AliExpress.com Product - Free shipping! Microcontroller learning kit starter and proficient 24 interactive lessons for arduino
    View my profile on LinkedIn

    Categories

    All
    ARDUINO
    ELECTRONICS
    Embedded
    Raspberry Pi
    STM32F103C8T6
    TOOLS

    RSS Feed

Don't forget 📝

If you like what your read and it helped you trough the development and professional 
​growing 
​journey, please subscribe and share.

Get in touch 👇