|
Intro to Physical Computing Syllabus Research & Learning Other Class pages
ITP Help Pages |
BluetoothOverviewThere are a few methods of wireless serial communication: Infrared (IR) Radio/RF Bluetooth is a high-speed RF communication protocol intended for connection between various mobile devices. The high speeds allow it to carry rich data like digitized audio and video. Once two devices make contact over a Bluetooth connection, the radios handle error correction, noise, and crosstalk for you. Its range is limited to about 10m (but some claim up to 100m!). It is primarily designed for one-to-one connections, and once two devices are connected they ignore all other Bluetooth devices nearby until the connection is broken. Bluetooth devices can be considered modems because they convert electrical signals (TTL serial carried over wires) into Bluetooth serial carried over radio signals and back. A modem is a device that converts one type of signal into another, and connects one object to one other object. Your home cable/DSL modem is one example - it takes the digital data from your home computer, converts it to a signal that can be carried across the cable line, and connects to another modem on the other end. That modem is connected to your ISP’s network. All modems are designed to open a connection to another modem, negotiate the terms of data exchange, carry on an exchange, then disconnect. To do this, they have to have two operating modes, usually referred to as command mode, in which you talk to the modem, and data mode, in which you talk through the modem. Like modems, Bluetooth serial devices have two modes: command mode and data mode. A Bluetooth radio has a unique address that other devices can use to identify it. To begin communication, the radio scans the area around it. If it discovers other devices, it can request a connection and begin conversing. Different modules have different commands, but in general you can:
In order to use Bluetooth for physical computing, you’ll need a Bluetooth serial module. The one you'll use here is Sparkfun’s Bluetooth Mate. If you’re communicating from one microcontroller to the other, you’ll need two Bluetooth modules. Otherwise, you can use your laptop with a Bluetooth receiver (many have them built in, but if yours doesn’t, you can buy a Bluetooth adapter). The Bluetooth Mate has two interfaces: two of its pins, marked RX and TX, are an asynchronous serial port that can communicate with a microcontroller. It also has a radio that communicates using the Bluetooth communications protocol. It acts as a modem, translating between the Bluetooth and regular asynchronous serial protocols. Bluetooth can carry many other services besides serial data, but this device can only carry serial data. To use it, you’ll need to tell your computer that you want to make a serial connection. On a PC or Mac with Bluetooth built in, you’ll establish and configure the serial connection using the Bluetooth control panel. If you have an adapter, you’ll be able to define a new serial port after installing the drivers according to the radio’s instructions. To switch the Bluetooth Mate from data mode to command mode, send the string $$$. To exit command mode, send the string ---\r (the \r is a carriage return, ASCII 13), and the Bluetooth modem switches back to data mode. In this lab, you'll learn how to how to use a Bluetooth Mate with a Lily Pad Arduino. You will be able to receive serial values through Bluetooth to Processing wirelessly. Table of Contents (hide) 1. PartsFor this lab you will need to have the following parts:
2. Pair Bluetooth and Laptop (MAC OS)First, plug your FTDI board into your LilyPad. The first time you use the Bluetooth Mate with your laptop, you need to introduce them to each other by using pairing. Once your computer has made contact with the Bluetooth module, you can connect to it like any other serial port. After choosing your board and serial port as usual, upload your code to your Lily Pad Arduino. //Arduino code void setup() { Serial.begin(115200); //The Bluetooth Mate has a baud rate of 115200, faster than the 9600 we're used to. } void loop() { // } After uploading your sketch, you can unplug the FTDI board and plug in the Bluetooth Mate and lithium battery. The start LED on the Bluetooth Mate should start blinking. The pins on the Bluetooth Mate match with the Lily Pad's, so don't worry about the connection. Mac Users: Now Turn on your Bluetooth settings and choose "Set Up Bluetooth Device" option. Your machine will start searching for Bluetooth devices around you. Your Bluetooth device will be "FireFly-xxxx". It will show up as the port called FireFly-XXX-SPP. After double clicking your device, laptop will try to pair with bluetooth. It is going to ask you to enter "0000". However, since we are not using a keyboard device, we need to go to "Passcode Options" and click "Do not use passcode with this device". 3. Pair Bluetooth and Laptop (Windows 7)For Windows 7 users, click on the "Show Hidden Icons" icon in the taskbar to get to the "Bluetooth Devices". Click the "Bluetooth Devices Icon" and from the menu, choose "Open Settings". In the "Settings" window, choose the "Options" pane, and make sure that you've allowed other devices to find this computer, and that you've allowed them to connect as well. Then click the "COM ports" pane. If you've paired with you Bluetooth module before, there will be two ports indicated for it, one outgoing and one incoming. You're not using those though, because Windows tries to initiate contact when you do. You want the Bluetooth modem to initiate contact. Add a new incoming port. Note the port number, then open that port in your serial terminal program at 115200 bps. 4. Add a PhotocellWe'll use a photocell here as an example of a way to get interesting data in to then sound out wirelessly via Bluetooth. Hook up the photocell like you do any other analog input sensor, similar to the FSRs in the Analog In lab. 5. Program your Lily PadNow, plug in your FTDI board again, and program your Lily Pad Arduino with the following code: //Arduino Code: int photocellPin = 0; // Analog input pin that the photocell is attached to int val = 0; // value read from the analog sensor void setup() { Serial.begin(115200); } void loop() { val = analogRead(photocellPin); Serial.println(val); Serial.flush(); delay(200); } Test this code with the FTDI board on, and try to see if you are receiving any values on the Arduino serial monitor. Then you can switch to your Bluetooth device and battery. Attention: If you are not using call and response method, you should put a delay at the end of your code. Otherwise, because of data overflowing, Bluetooth locks itself. Unlocking is possible, however we don't want that for now. 6. Program your Processing SketchStart your Processing sketch and type in following code to list available serial ports: //Processing Code import processing.serial.*; void setup() { //list all the available serial ports println(Serial.list()); } When you run this code, you should see values like this: Look for the port that has tty and firefly in it, and note the number for the following example code: //Processing code import processing.serial.*; Serial myPort; // Create object from Serial class int val; // Data received from the serial port void setup() { size(200, 200); println(Serial.list()); //list all the available serial ports //replace the 8 in the next line with your serial port number from the list above. String portName = Serial.list()[8]; //My device is using "port 8" on my laptop myPort = new Serial(this, portName, 115200); //115200 is the data rate associated with this Bluetooth module } void draw() { if ( myPort.available() > 0) { // If data is available, String myString = myPort.readStringUntil('\n'); //reads serial port until end of line if (myString != null) { //checks if myString is null myString = trim(myString); val = int(myString); //converts string to integer background(255); // Set background to white if (val < 150) { // If the serial value is 0, fill(0); // set fill to black } else { // If the serial value is not 0, fill(204); // set fill to light gray } rect(50, 50, 100, 100); } } } When you first run this code, you will see a screen like this: You need to type in "1234" as your passcode. After clicking "Pair", according to your sensor values, you will be able to see color changes on your rectangle. Also, take a look at the "Connect" LED on your Bluetooth Mate - it should turn on and stay on as soon as you have established a connection between Bluetooth and your laptop. |