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)


0 Responses to “Part 3: XRF BACONZ(Beacons)”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


The Author

Older Ramblings

Tweets

Linux User Sig

%d bloggers like this: