RN-42 Bluetooth to PC Demo [P8X32A]

posted in: Tutorials 0
RN-42 Bluetooth to PC Demo

This demo provides a two-way communication channel for a remote device, which has two functions. One is to send system telemetry from on-board sensors. The other is to receive and act upon known commands from a remote serial terminal or command interface, including a PC or Smartphone application.

While this demo application is small, it represents a scalable real-world application where you might be receiving weather or sensor telemetry from a farm or industrial control system and need to be able to send commands to such a system, perhaps to open or close a valve in response to data received.

What’s Required

  • (1) Propeller Board of Education
  • (1) RN-42 Bluetooth Module
  • (1) ADC0831 8-bit A/D Converter
  • (1) DS1620 Digital Thermometer
  • (1) PING))) Ultrasonic Sensor + Mounting Bracket
  • (1) Photoresistor
  • (1) 10K, 1/4W Resistor
  • (1) 4.7K, 1/4W Resistor
  • (1) Li-ion Power Pack
  • (2) High-Capacity Li-ion Cell
  • (1) Veho 360 Speaker
  • (1) Veho Speaker Stand for Propeller BOE
  • (1) 2 GB microSD Card (or similar)

You will also need some wires, some sort of mounting plate or platform to mount everything on and optionally a USB Adapter for copying WAV files to the microSD card, which can be 2GB or smaller.

Assemble the Bluetooth Unit

Following the schematic diagram (included in the ZIP file below), you should connect the hardware as indicated. Each device has an associated test program so that you can verify proper operation of the individual components, which greatly simplifies debugging issues. There are test programs for the following components:

  • DS1620
  • ADC0831 & CdS Photocell
  • PING))) Sensor
  • RN-42 Bluetooth Module
  • Servo
  • LEDs
Figure 1

Connect the RN-42 Bluetooth Module, ADC0831 and DS1620 as shown in Figure 1, using the schematic diagram for reference. Be sure the voltage jumper on the RN-42 is set to 5V and not 3.3V. Only configuration jumper 4 (default baud rate) should be shorted, forcing the baud rate to 9600 bps. Once connected, run the test programs for each component, individually, before moving on.

For this demo, a custom base was laser cut for the project. The Propeller BoE and the PING))) Mounting Bracket are both attached to this base. They could have also been mounted to a BoE-Bot chassis or another home-made frame/chassis. The Li-ion Power Pack is optional. You can power the demo from a wall adapter, if you prefer.

Figure 2

The PING))) Sensor connects to the servo header P18 while the servo connects to P19 (see Figure 2). Be sure the voltage jumper for these ports are set to 5V and not VIN. Also be sure when the servo turns that the bracket doesn’t hit anything on the board.

Figure 3

P12 connects to R1 on the VGA socket, P11 connects to G1 and P10 connects to B1. In this manner you have control of three different color LEDs which can be selectively turned on/off (see Figure 3).

Figure 4

The photoresistor is connected to a 10K resistor forming a voltage divider which is fed into the input of the ADC0831. See Figure 4 and the schematic at the beginning of the demo SPIN code for the connections to the RN-42, ADC0831, DS1620 and the VGA LEDs. It’s a tight fit, but everything does fit on the breadboard are of the Propeller BoE.

Figure 5

You will need a USB adapter or some other means to download the WAV files onto the microSD card. Once the files are on the card, install it into the microSD card socket on the Propeller BoE (see Figure 5). After installing the Veho Speaker Stand and Speaker, plug the 1/8” phone plug into the audio jack on the Propeller BoE. Be sure the card is plugged in all the way and that the Veho Speaker is turned on (Blue LED will light up).

Communicating with the Demo Unit

Communication with the demo unit is done by connecting via Bluetooth connection from your PC. Before this can be done you will have to pair the RN-42 Bluetooth Module with your PC. How this is done varies with your Bluetooth driver and operating system and goes beyond the scope of this project.

Download the RN-42 Bluetooth to PC Demo V1.0 program to your Propeller BoE. Once connected to Bluetooth, you can open the Parallax Serial Terminal (or any terminal) to the Bluetooth COM Port created by your O/S Bluetooth driver. The port speed should be set to 9600 bps. Be sure the Propeller BoE power switch is in the second position (2) so the PING))) Sensor and servo are powered up.

Figure 6

Once connected, the blue LINK LED on the RN-42 Bluetooth Module will light up. At this point you should be getting telemetry from the demo unit as shown in Figure 6. Temperature, light level and distance as determined by the PING))) Sensor. You can also send commands to the demo unit. The demo unit understands the following
commands sent from the terminal:

  • !clr – clear the terminal screen, and refresh the telemetry data
  • !svo1 – move the servo to position one (90 degrees right)
  • !svo2 – move the servo to position two (center position)
  • !svo3 – move the servo to position three (90 degrees left)
  • !snd# – where # is a number from 0-9 corresponding to a WAV file to play
  • !inr0 – turn Red LED off
  • !inr1 – turn Red LED on
  • !ing0 – turn Green LED off
  • !ing1 – turn Green LED on
  • !inb0 – turn Blue LED off
  • !inb1 – turn Blue LED on

Each command starts with an exclamation point. The next three letters are the command. Three letters were chosen to keep things simple for the parser code. Some commands have a parameter that follows. Once the full command is understood it is executed. No carriage return and / or linefeed are required. All commands are lowercase.

The only command that would affect the telemetry is the “svo” command, since the direction the PING))) Sensor faces may change the distance seen. Watch the video on the main page of this project for an example of how everything works. The video shows all commands as well as disconnecting and reconnecting.

The Details

All the objects used for the various functions were obtained from the Propeller Library (built into the IDE), the OBEX (Object Exchange) or the Parallax Forums. Everything works together with very little work to balance out resources. All required objects are included in the code ZIP file. The telemetry code is launched into a new cog. Its function is to read the sensors and send the data, wait one second and repeat. It runs completely independent of the other functions. Each of the three telemetry sections calls a method in the appropriate object, setting a variable and then formatting that data out to the terminal.

The command parser evaluates incoming commands and executes them when a complete command is detected. It does this by first evaluating every character starting with the “!” character. Once this character is received the next character is checked against possible matches. If at any point the next character is not a match the whole evaluation resets. In the parser section the various levels are indented in such a way that commands are evaluated on through until completion or until a wrong character.

For example, once the “!” is received the next character is compared against “c”, “s” or “i”. If “s” is received the next character is compared against “v” or “n”. If “v” is received the next character is compared against “o”. If it does not match the parser won’t evaluate again until “!” is received again. This level-nested evaluation is an easy way to parse commands, even if they have similar characters at the beginning as the “svo” and “snd” commands do.

When there is no connection via Bluetooth the telemetry won’t be sent due to flow control. Likewise, since no commands can be received everything remains as it was until a connection is established and valid commands received.

For Your Information

This project is made up by adding code from more than 7 different objects and modified portions of the demos to get the desired results. To make things easier, constants for each object were placed in multiple CON sections, keeping them isolated. The start methods were placed as needed and initialization code was placed at the beginning to ensure everything started up properly. Despite of the apparent complexity of having over 7 objects running in the demo code, there are only 3 PUB and one PRI method, and two of these contain only one line of code.

Things You Can Try

Change the commands used to activate various functions. You can do this by studying the structure of the parser. Indentation is important to how things are executed (and evaluated) in SPIN, so watch the level of indentation of each sub-section of the parser evaluations. You could also add commands, or add/change the WAV files loaded on the microSD card.

The Test_microSD.spin file makes use of a command to detect when a WAV file has stopped playing. You could use this to enhance the “snd” commands in this demo.

Resources

RN-42 Bluetooth to PC Demo – Video

XBee 802.15.4 to PC Demo – Tutorial

Discuss this tutorial on Savage///Chats


RN-42 Bluetooth to PC Demo by Chris Savage is licensed under CC BY 4.0

PLEASE FEEL FREE TO LEAVE YOUR COMMENTS, QUESTIONS, SUGGESTIONS OR FEEDBACK ON THIS POST.

Leave a Reply