Archive for the 'Uncategorized' Category

11
Apr
13

Part 3: XRF BACONZ(Beacons)

Let me start this post sharing my “Lessons learned”:

1. Arduino IDE sucks… still better having it than trying to program MSPs from Ti on Linux manually, without any IDE, but still… Arduino sucks. I hate when tab is replaced with spaces, why use tab then? just use your damn spaces. And I never get the double quotes to work, I always have to ctrl+c, ctrl+v them. I ended up editing the code in Gedit and kept pasting it to the IDE.

2. DON’T try to use the same Arduino installation for both Teensy and Arduino Duemillanove. You probably will more often than not choose the wrong library from the Examples menu.

3. Very nice discovering that the NewSoftwareSerial is in Arduino Core since 1.0!

4. Gnoduino is pretty, has better editor than Arduino IDE, and doesn’t work (on Arch at least). Something about a flash error. Will try more later.

So, why I’m sharing these? Well, if you own a Teensy and an Arduino and want to quickly write some code that kinda works on both for testing, say… XRF modules (hehehe) you’ll probably know by now at least two of the things from above.

But I digress. So, I wanted to test the comunication and range of the XRFs so I wrote these little hacks that pretty much work out of the box. (XRFs are configured for 868MHz and 9600 Baud serial from factory, don’t remember wireless bitrate.)

Teensy code is built around Serial(yay) and the Uart library that comes with Teensy. Teensy has a special Uart port that is not used for programming (bothersome in Arduino) http://www.pjrc.com/teensy/td_uart.html

#define LED_PIN 11
#define BAUD 9600

HardwareSerial Uart = HardwareSerial();

void setup()
{
	pinMode(LED_PIN, OUTPUT);

	digitalWrite(LED_PIN, HIGH);
	delay(1000);
	pinMode(LED_PIN, LOW);

	Serial.begin(BAUD);	// USB, communication to PC or Mac
	Serial.println("SETUP teensy");

	Uart.begin(BAUD);	// UART, communication to Dorkboard
}

char beacon[]="abcdef";
char answer[]="fedcba";

unsigned i = 0;
long led_on_time=0;
char input[]="000000";

void loop()
{
	unsigned char c;

	if (Uart.available()) {
		digitalWrite(LED_PIN, HIGH);
		led_on_time = millis()+200;

		c = Uart.read();
		if (c == beacon[0]){
			input[0] = c;
			i=1;
		}else if(i > 0){
			input[i] = c;
			i++;
		}
		if ( i >= strlen(beacon)){
			if (strcmp(input, beacon) == 0){
				digitalWrite(LED_PIN, HIGH);
				led_on_time = millis()+500;

				Uart.write(answer);
				Serial.println("Got beacon!");
			}else{
				strcpy("000000",input);
			}
			i=0;
		}
	}
	if (millis() > led_on_time) {
		digitalWrite(LED_PIN, LOW);
	}
}

http://pastebin.com/mnQVDXBk

I’m using the Teensy as the beacon receiver. When it receives the expected message it flashes the LED and sends the specified answer. (Receives “abcdef” and sends “febcba” back)

The Arduino code is pretty much the same thing but uses the SoftwareSerial on ports 2 and 3.

#include <SoftwareSerial.h>

#define SEND_DELAY 5000
#define LED_PIN 13

SoftwareSerial mySerial(2, 3); // RX, TX

void setup()  
{
  pinMode(LED_PIN, OUTPUT);
  
  digitalWrite(LED_PIN, HIGH);
  delay(1000);
  digitalWrite(LED_PIN, LOW);

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  Serial.println("SETUP Arduino");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
}

char beacon[]="abcdef";
char answer[]="fedcba";

char input[]="000000";

unsigned long timeLastSend = 0;
unsigned long led_on_time = 0;
unsigned i=0;

void loop() // run over and over
{
	unsigned char c;

	if (millis() > timeLastSend){
		mySerial.write(beacon);

		timeLastSend = millis()+SEND_DELAY;

		led_on_time = millis()+200;
		digitalWrite(LED_PIN, HIGH);

		Serial.println("Sending beacon...");
	}

	if (millis() > led_on_time){
		digitalWrite(LED_PIN, LOW);
	}
		

	if (mySerial.available()){
		c = mySerial.read();

		Serial.print("r:");
		Serial.print(c);

		if ( c == answer[0]){
			i=1;
			input[0]=c;
		}else if(i > 0){
				input[i] = c;
				i++;
		}
	}
	if ( i >= strlen(beacon)){
			if (strcmp(input, answer) == 0){
			digitalWrite(LED_PIN, HIGH);
			led_on_time = millis()+700;
			Serial.println("GOT BEACON");
		}else{
			Serial.println("beacon failed...");
			strcpy("000000",input);
		}
		i=0;
	}
}

http://pastebin.com/PRzdJc9X

These sources aren’t exactly well tested and thoughtfully written. I thought of making both send and flash only when a beacon was received, would be simpler than answering beacons with another text. Possibly could also have used the same code on both of them.

Please comment if I’ve commited a serious mistake 😀

I tested them at home with no clear sight but with open path for about 10-15 meters. The Arduino was connected to my notebook and the Teensy to my S3 through OTG.

I still have to discover how to customize bitrate of the XRFs and test greater distances with direct path but haven’t had the chance.

EDIT: I know how to edit the bitrates and other config, just enter “+++” on the serial with the XRF, write nothing for a second at it should enter config mode, answering with a OK. Then send your favorite “AT” commands… just haven’t had the opportunity to reprocude these steps for various reasons. (Correct me if any of those directions was wrong hahaha)

Advertisements
10
Aug
11

Android arduino communication

Now to the fun part!

In the previous post I explained how to interface and configure a Bluetooth Module with a Bus Pirate.

Here I will show how to toggle the Arduinos LED through an Android app!

Note that I’m using a Arduino Duemilanove.

You’ll need to connect the module to the Arduino. The +5V and GND pins are already there. The digital 0 and 1 pins are reserved for the PC/Arduino interface through the FTDI.
I used the digital pins 2 and 3 for the communication with the module.

The rxPin(2) is connected to the tx pin of the module.
The txPin(3) is connected to the rx pin of the module.

Arduino Code
Available here:
http://pastebin.com/EjCFqAVR

Notes:
I could have implemented it without the use of
NewSoftSerial but didn’t. The delay when reading was added because of the baud rate configuration problem I was having, at 1200 bauds. At 9600 it is perhaps not needed. There is no need to actually return if the message was understood, I added it to experiment on possible information polling events.


Here is a short python code I wrote for my Ubuntu to test the communication.

Python test code
Available here:
http://pastebin.com/saFwneNa
Ubuntu will automatically ask if you want to pair with the device. It usually is called linvor and has the pin 1234 as default.

I based my code on the examples found here:
http://people.csail.mit.edu/albert/bluez-intro/c212.html(which also finds your device by name and gives you it’s mac address)


As for the Android code, you must add the permissions for bluetooth connections to the Manifest.

<uses-permission android:name=”android.permission.BLUETOOTH” />
<uses-permission android:name=”android.permission.BLUETOOTH_ADMIN” />

BLUETOOTH_ADMIN is only needed if you are to set your device visible or manage paired devices.

My main and only java class is available here:
http://pastebin.com/tkqhi1S2
(wasn’t going to get indented here in WordPress)

It should be noted that after creating a socket with

UUID mUUID = UUID.fromString(“00001101-0000-1000-8000-00805F9B34FB”);
BluetoothSocket mBTSock = device.createRfcommSocketToServiceRecord(mUUID);

which uses the default device UUID, you must still connect the socket.
It seems a socket cannot be reused after closing it. Probably would have to open a new socket.
Note that this implementation is far from being totally bug and idiot proof. I do the most of the error checks which probably would go wrong, but there are always more to add.

I used the DeviceListActivity from the BluetoothChat example available in the Android SDK. This class needs the res/layout/device_list.xml and res/layout/device_name.xml. I used it so the user can select a paired device from the list and it returns the Bluetooth address(mac) to the calling activity, so it knows to which device to connect.

Here is the whole project:
http://dl.dropbox.com/u/19070953/Wordpress/Arduino-Android/btcon.tar.gz

So, I hope this works for you. Will not run on Android Emulator(I think, since Bluetooth is not available) and was built/tested for android 2.1(SDK 7)

Obs.:
Sorry for the embedded images for the code. WordPress without decent plugins does not allow code to be displayed properly. And the android code was too long to paste together.




The Author

Older Ramblings

Tweets

Linux User Sig