Hi Kiss and All,
I too am working on a piece of software that incorporates Arduino with Android via Bluetooth (working in Eclipse). I have downloaded several bluetooth apps on the android that are displaying my sensor values.
My difficulty, I believe, in finalizing this system, is in figuring out how to open the correct com port to read the bluetooth values into the openFrameworks program. What is the name of the Comm port for a bluetooth connection? Struggling with listing the ports in my code.
Any suggestions from someone who has successfully linked Arduino to Android via bluetooth would be very helpful. Thanks!
Here is what I have so far, just not sure what to call for the Com port:
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
// initialize the accelerometer
ofxAccelerometer.setup();
ofBackground(0);
ofSetWindowTitle("St. Oswalds Kiosk");
width = ofGetWidth();
height = ofGetHeight();
// read the directory for the images
// we know that they are named in seq
ofDirectory dir;
count = 0;
//load the images in the file - replace with sd card images
int nFiles = dir.listDir("/storage/sdcard1/SD"); //here, the images are in a folder on the card called "SD"
//int nFiles = dir.listDir("/storage/sdcard1"); //these are directly in the sd card, but they read extra files and create black spaces
if(nFiles) {
for(int i=0; i<dir.numFiles(); i++) {
// add the image to the vector
string filePath = dir.getPath(i);
images.push_back(ofImage());
images.back().loadImage(filePath);
}
}
else printf("Could not find folder\n");
// initialize all of the Ball particles
for(int i=0; i<NUM_POINTS; i++) balls[i].init();
// Serial Stuff
//font.loadFont(“DIN.otf”, 64);
bSendSerialMessage = false;
serial.listDevices();
serial.enumerateDevices();
//vector <ofSerialDeviceInfo> deviceList = serial.getDeviceList();
string serialID;
vector<ofSerialDeviceInfo> deviceList = serial.getDeviceList();
string deviceLine;
for(int i=0; i<deviceList.size();i++){
deviceLine = deviceList[i].getDeviceName().c_str();
//if(deviceLine.substr(0,12) == "tty.usbmodem"){
serialID = "/dev/" +deviceLine;
cout<<"arduino serial = "<<serialID<<endl;
//}
}
// this should be set to whatever com port your serial device is connected to.
// (ie, COM4 on a pc, /dev/tty.... on linux, /dev/tty... on a mac)
// arduino users check in arduino app....
int baud = 9600;
serial.setup(0, baud); //open the first device
//serial.setup("COM4", baud); // windows example
//serial.setup("/dev/tty.usbserial-A4001JEC", baud); // mac osx example
//serial.setup("/dev/ttyUSB0", baud); //linux example
// nTimesRead = 0;
// nBytesRead = 0;
// readTime = 0;
// memset(bytesReadString, 0, 4);
}
//--------------------------------------------------------------
void ofApp::update() {
for(int i=0; i<NUM_POINTS; i++) balls[i].update();
/*
//SERIAL STUFF
if (bSendSerialMessage){
// (1) write the letter "a" to serial:
//serial.writeByte('a');
// (2) read
// now we try to read 3 bytes
// since we might not get them all the time 3 - but sometimes 0, 6, or something else,
// we will try to read three bytes, as much as we can
// otherwise, we may have a "lag" if we don't read fast enough
// or just read three every time. now, we will be sure to
// read as much as we can in groups of three...
nTimesRead = 0;
nBytesRead = 0;
int nRead = 0; // a temp variable to keep count per read
unsigned char bytesReturned[3];
memset(bytesReadString, 0, 4);
memset(bytesReturned, 0, 3);
while( (nRead = serial.readBytes( bytesReturned, 3)) > 0){
nTimesRead++;
nBytesRead = nRead;
};
memcpy(bytesReadString, bytesReturned, 3);
bSendSerialMessage = false;
readTime = ofGetElapsedTimef();
}
*/
}
//--------------------------------------------------------------
void ofApp::draw() {
// we need some images if not return
if((int)images.size() <= 0) {
ofSetColor(255);
ofDrawBitmapString("No Images...", 150, ofGetHeight()/2);
return;
}
//my attempt at righting vertical images, (not working)
if (images[count].height < images[count].width){
images[count].rotate90(1);
}
// make images fullscreen, correct aspect ratio, centred
int newWidth = ofGetWidth();
float scaleFactor = (float)ofGetWidth() / (float)images[count].width;
int newHeight = (int)(images[count].height * scaleFactor);
int destX = (width - newWidth) / 2;
int destY = (height - newHeight) / 2;
ofSetColor(255);
//This draws the current image (of the count state) to the screen
images[count].draw(destX, destY, newWidth, newHeight);
//images[count].draw(0, 0);
// info to print to screen
ofSetColor(200);
string info;
info += "tap the screen to advance the images";
ofDrawBitmapString(info, 15, 20);
for(int i=0; i<NUM_POINTS; i++) balls[i].draw();
/*
//Serial Stuff
if (nBytesRead > 0 && ((ofGetElapsedTimef() - readTime) < 0.5f)){
ofSetColor(0);
} else {
ofSetColor(220);
}
string msg;
msg += “click to test serial:\n”;
msg += "nBytes read " + ofToString(nBytesRead) + “\n”;
msg += "nTimes read " + ofToString(nTimesRead) + “\n”;
msg += "read: " + ofToString(bytesReadString) + “\n”;
msg += "(at time " + ofToString(readTime, 3) + “)”;
//font.drawString(msg, 50, 100);
*/
}