Created
March 2, 2014 10:51
-
-
Save ddewaele/9304853 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Include the software serial port library */ | |
#include <SoftwareSerial.h> | |
/* | |
On a mac , connect with the bluetooth module like this: | |
$ screen /dev/tty.HC-06-DevB | |
To check the available bluetooth devices : | |
$ ls /dev/tty.* | |
/dev/tty.Bluetooth-Modem /dev/tty.SerialPort | |
/dev/tty.Bluetooth-PDA-Sync /dev/tty.SerialPort-1 | |
/dev/tty.HC-06-DevB | |
Type CTRL-A-D to exit the screen | |
[detached] | |
References | |
http://www.exp-tech.de/service/datasheet/HC-Serial-Bluetooth-Products.pdf | |
http://www.ebay.com/itm/HC-05-Bluetooth-Transceiver-Host-Slave-Master-Module-Wireless-Serial-6pin/221303627009?_trksid=p2047675.m1850&_trkparms=aid%3D222002%26algo%3DSIC.FIT%26ao%3D1%26asc%3D18058%26meid%3D2539213225991316226%26pid%3D100011%26prg%3D8304%26rk%3D2%26rkt%3D5%26sd%3D221286562520%26 | |
https://play.google.com/store/apps/details?id=com.techbitar.android.Andruino | |
http://www.instructables.com/id/Cheap-2-Way-Bluetooth-Connection-Between-Arduino-a/?ALLSTEPS | |
*/ | |
/* DIO used to communicate with the Bluetooth module's TXD pin */ | |
#define BT_SERIAL_TX_DIO 10 | |
/* DIO used to communicate with the Bluetooth module's RXD pin */ | |
#define BT_SERIAL_RX_DIO 11 | |
/* Initialise the software serial port */ | |
SoftwareSerial BluetoothSerial(BT_SERIAL_TX_DIO, BT_SERIAL_RX_DIO); | |
char command[20]; | |
int index=0; | |
void setup() | |
{ | |
/* Set the baud rate for the hardware serial port */ | |
Serial.begin(9600); | |
/* Set the baud rate for the software serial port */ | |
BluetoothSerial.begin(9600); | |
Serial.println("Waiting ..."); | |
} | |
/* Main loop that will pass any data to and from the Bluetooth mode to the | |
host PC */ | |
void loop() | |
{ | |
int photocellReading = analogRead(0); | |
// Serial.print("Analog reading = "); | |
// Serial.println(photocellReading); // the raw analog reading | |
//Serial.println(millis()); | |
if (millis()%1000==0) { | |
// Serial.println("Every sec"); | |
BluetoothSerial.write(reading.c_str()); | |
//BluetoothSerial.write(photocellReading); | |
BluetoothSerial.write("\r\n"); | |
} | |
String reading = String(photocellReading); | |
/* If data is available from the Bluetooth module then pass it on to the | |
hardware serial port. */ | |
if (BluetoothSerial.available()) { | |
Serial.println("BluetoothSerial.available()"); | |
int incomingByte = BluetoothSerial.read(); | |
command[index] = incomingByte; // Store it | |
index++; // Increment where to write next | |
command[index] = '\0'; // Null terminate the string | |
Serial.println(String("User wrote int : ") + incomingByte); | |
Serial.println(String("User wrote char : ") + (char)incomingByte); | |
Serial.write(incomingByte); // prints out the letter | |
//Serial.println(String(incomingByte)); // prints out the int | |
Serial.println(); // blank line | |
if (incomingByte == 'R') { | |
Serial.println("RESET"); | |
} | |
if (incomingByte == '\r') { | |
Serial.println("Found enter .... the command entered was " + String(command)); | |
index=0; | |
} | |
if (incomingByte == '\n') { | |
Serial.println("Found linefeed"); | |
} | |
if (incomingByte == '\r\n') { | |
Serial.println("Found enter/linefeed"); | |
} | |
//Serial.write(BluetoothSerial.read()); | |
} | |
/* If data is available from the hardware serial port then pass it on | |
to the Bluetooth module. */ | |
if (Serial.available()) | |
BluetoothSerial.write(Serial.read()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment