Archive for April, 2013

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
11
Apr
13

Part 2: XRF, Teensy 2.0 and Xbee adaptor

So I had two XRFs modules, only one XBee adapter to use them with, which was unfortunately only working via serial, though still better than nothing.

I already had a Teensy, so I ordered a Xbee adaptor (http://www.pjrc.com/store/xbee_adaptor.html) for it and two XBBOs from Ciseco. I’d certainly wouldn’t need more after this. (LOL, do need, want a XBee Explorer from Sparkfun)

Finally arrived, wasn’t so fun soldering the SMT parts on the board with a soldering iron and far too thick solder.. but seems I managed fine.

The XRF, the soldered Adaptor and the Teensy with Sockets.

The three connected.

The thing is, the Teensy is sold with pins… for breadboard connecting. And the XBee Adaptor is sold also with pins.. why? Thankfully I had a normal teensy (without pins or sockets) so I had ordered sockets too. Learned my lesson from last time.

Soldering the XBBOs took a bit longer, they have a lot more components but thankfully no SMT.

XBBO from Ciseco

Sadly the Teensy is now quite bit bigger with the sockets. Maybe I’ll order a new one for smaller purpouses which I had originally in mind. Would also have been VERY nice if the XBBOs had USB support…

11
Apr
13

Part 1: XRF, Bee Adapter v1.4 and Arduino

Trying to slowly assemble the hardware to build a quadcopter (sigh), I got myself a pair of Ciseco’s XRF modules and a Xbee USB Dongle (Bee Adapter v1.4).

XBee Adapter Back

XRF and XBee Adapter

(You can read more about the XRF modules here: http://www.ciseco.co.uk/content/?p=1738)

The Ciseco XRFs are nice little buggers, they work at various frequencies, in the 1KHz range. I find this great because it’s far away from the 2.4GHz noise (microwave, wireless phone stations, wifi and bluetooth). Sure, it may be illegal or require a license in your country but who cares? (Actually, readers that live in more developed and orderly parts of the world, YOU should…)
The XRFs are very nice, since they have the form and pins of the Zigbees (Xbees, whatever), so you practically can use one wherever you’d use a Xbee. Just for a much lower price and various frequencies.

Since my project involved sometime being able to use an Android Phone with the XRFs, I bought the Bee Adapter from DX.com, which has serial and usb access. The Arduino I had bought a while ago, just upgraded it with a prototyping shield.

Notice something is missing? Yeah, it so happens that I completely forgot that to do anything with the modules, I’d need to connect the TWO of them. But I only bought one adapter. Genius, right?

Even so, they finally arrived. Shipping from the UK to Brazil is actually quite fast. From HK or US it’d probably get stuck in customs for a while…

The Bee Adapter was a bit different from the picture at DX, though seemed legit. Got two toggles that let you choose between High/Low, which seems to be power consumption, and 3v3/5v.

XBee Adapter Back

XBee Adapter Back

So! Let’s connect’em… Power levels fail via usb… power status led barely shines with the XRF module… Hmmm, XRF module must be using a lot of power! (shouldn’t but who knows..) So I plugged it in my newest Prototyping shield, wired up the serial connectors and blam… It worked fine with the Arduino. Tested it for the +++ command which get’s it to enter the config mode, it sends OK.

XBee Adapter Back

Arduino with Prototyping shield, Xbee Adapter and XRF module.

Great little adapter doesn’t work via USB… I’d guess it needs a current limiter, didn’t care to debug it…

Aaaaand there was the need for the other adapter more than ever, specially because the Xbee modules pins are not breadboard compatible…yay…




The Author

Older Ramblings

Tweets

Linux User Sig