Ce site contient essentiellement des notes de travail. Le contenu est en constante évolution, et loin d'être achevé. (+ d'infos)
La plupart des documentations informatiques sont orientées Debian / Ubuntu.

Electronique/Hardware/Communication/XRF/Testeur RSSI arduino (XRF v1.3)

De Ordinoscope.net
Sauter à la navigation Sauter à la recherche

Introduction

La version 1.5 du XRF intégrera le RSSI en mode PWM sur la pin 6 (optionel). Les version antérieures peuvent fournir le RSSI, à condition d'utiliser un mode dédié. Ce code a été écrit pour des Arduinos UNO ou équivalents.

Code

Ce code a fonctionné pendant près d'une semaine, jusqu'à ce que je change de fréquence sur mes modules XRF. Depuis, plus moyen d'obtenir des valeurs régulières. Les XRF fonctionnent très aléatoirement, voire plus du tout.

This code did work for almost one week, until I did change the frequency on my XRF modules. From this moment, no way to get reliable values. XRF modules seem to work randomly, even not at all.

Slave

/*
 * 2012-04-16 - Jean-Christophe Heger
 *
 * This code use the dedicated RSSI test mode on an XRF transmitter < v1.5.
 * The slave mode (ATNT4) is a loobpack, that resend the recieved RSSI.
 * Nothing can be get on this mode. It only has to be up and running.
 *
 * Reminder: if you dit change the frequency, don't forget you also must
 * change the antenna size. Look at the following we page for sizes:
 * http://www.ordinoscope.net/index.php/Electronique/Hardware/Communication/XRF/Taille_des_antennes
 *
 * Requirements:
 * - Arduino 0023 (NewSoftSerial won't be compatible with Arduino 1.0)
 * - XRF module
 * - Xbee shield
 * - NewSoftSerial: http://arduiniana.org/libraries/newsoftserial/
 *
 * The Seeedstudio's shield must be set for using pins 11 and 12. This means
 * that both switched are on the left.
 *
 * References:
 * - http://openmicros.org/index.php/articles/84-xrf-basics/114-xrf-at-command-reference
 * - http://www.seeedstudio.com/depot/xbee%C3%82%C2%AE-shield-p-419.html?cPath=132_134
 * - http://www.seeedstudio.com/wiki/index.php?title=XBee%C2%AE_Shield
 */
 
#include <NewSoftSerial.h>

#define PIN_XRF_RX 11
#define PIN_XRF_TX 12
#define PIN_LED 13

#define PRINT   0
#define NOPRINT 1
#define NOLN    2

NewSoftSerial XRF (PIN_XRF_RX, PIN_XRF_TX);

void xrf_print_info () {
  String r;
  
  Serial.println ();
  Serial.println ("Reading XRF settings");
  
  xrf_send ("+++", NOLN|NOPRINT);
  
  r = xrf_send ("ATNT", NOPRINT);
  Serial.print ("Node type: ");
  Serial.println (r);

  r = xrf_send ("ATID", NOPRINT);
  Serial.print ("PAN ID: ");
  Serial.println (r);

  r = xrf_send ("ATCH", NOPRINT);
  Serial.print ("Frequency: ");
  Serial.println (r);

  r = xrf_send ("ATPL", NOPRINT);
  Serial.print ("Power level: ");
  Serial.println (r);

  r = xrf_send ("ATEE", NOPRINT);
  Serial.print ("Encryption: ");
  Serial.println (r);
  
  r = xrf_send ("ATDN", NOPRINT);

  delay (1000);
  Serial.println ();
}

String xrf_send (String msg, int opts) {
  unsigned char c;
  String r;
  
  while (XRF.available ()) XRF.read ();
  
  if (!(opts & NOPRINT)) Serial.print ("Sending [");
  
  if ((opts & NOLN)) {
    if (!(opts & NOPRINT)) Serial.print (msg);
    XRF.print (msg);
  } else {
    if (!(opts & NOPRINT)) {
      Serial.print (msg);
      Serial.print ("\\n");
    }
    XRF.println (msg);
  }

  if (!(opts & NOPRINT)) Serial.println ("]");
  
  while (!XRF.available ());
  while (c != 0xD) {
    if (XRF.available ()) {
      c = (unsigned char) XRF.read ();
      r += c;
    }
  }
  if (!(opts & NOPRINT)) Serial.println (r);
  return r;
}

void setup () {
  unsigned char c;
  
  // init LED
  pinMode (PIN_LED, OUTPUT);
  
  // init serial
  Serial.begin (115200);
  Serial.println ("XRF RSSI Tester - slave");
  
  // init XRF
  pinMode (PIN_XRF_RX, INPUT);
  pinMode (PIN_XRF_TX, OUTPUT);
  XRF.begin (9600);

  delay (3000);  // wait for XRF to warm up
  xrf_print_info ();

  xrf_send ("+++", NOLN|NOPRINT);
  xrf_send ("ATNT4", PRINT);
  xrf_send ("ATDN", PRINT);

  Serial.println ("Ready");
}

void loop () {
  digitalWrite (PIN_LED, HIGH);
  delay (500);  
  digitalWrite (PIN_LED, LOW);
  delay (500);  
}

Master

/*
 * 2012-04-16 - Jean-Christophe Heger
 *
 * This code use the dedicated RSSI test mode on an XRF transmitter < v1.5.
 * The master mode (ATNT3) is the one that prints data. A loobback must
 * obviously be alive and in the range.
 *
 * Highest RSSI is around 20, and lowest will be arround 90.
 *
 * Reminder: if you dit change the frequency, don't forget you also must
 * change the antenna size. Look at the following we page for sizes:
 * http://www.ordinoscope.net/index.php/Electronique/Hardware/Communication/XRF/Taille_des_antennes
 *
 * Requirements:
 * - Arduino 0023 (NewSoftSerial won't be compatible with Arduino 1.0)
 * - XRF module
 * - Xbee shield
 * - NewSoftSerial: http://arduiniana.org/libraries/newsoftserial/
 *
 * The Seeedstudio's shield must be set for using pins 11 and 12. This means
 * that both switched are on the left.
 *
 * References:
 * - http://openmicros.org/index.php/articles/84-xrf-basics/114-xrf-at-command-reference
 * - http://www.seeedstudio.com/depot/xbee%C3%82%C2%AE-shield-p-419.html?cPath=132_134
 * - http://www.seeedstudio.com/wiki/index.php?title=XBee%C2%AE_Shield
 */

#include <NewSoftSerial.h>

#define PIN_XRF_RX 11
#define PIN_XRF_TX 12
#define PIN_LCD    9
#define PIN_LED    13

#define PRINT   0
#define NOPRINT 1
#define NOLN    2

NewSoftSerial XRF (PIN_XRF_RX, PIN_XRF_TX);
NewSoftSerial LCD (PIN_LCD, PIN_LCD);

String buffer = "";

void lcdClear () {
  LCD.write (0xFE);
  LCD.write (0x01);
  delay(10);
}

//position = line 1: 0-15, line 2: 16-31, 31+ defaults back to 0
void lcdGoto (int position) {
  if (position < 16) {
    LCD.write (0xFE);
    LCD.write (position + 128);
  } else if (position<32) {
    LCD.write (0xFE);
    LCD.write (position + 48 + 128);
  } else {
    lcdGoto (0);
  }
  delay(10);
}

void xrf_print_info () {
  String r;
  
  Serial.println ();
  Serial.println ("Reading XRF settings");
  
  xrf_send ("+++", NOLN|NOPRINT);
  
  r = xrf_send ("ATNT", NOPRINT);
  Serial.print ("Node type: ");
  Serial.println (r);

  r = xrf_send ("ATID", NOPRINT);
  Serial.print ("PAN ID: ");
  Serial.println (r);

  r = xrf_send ("ATCH", NOPRINT);
  Serial.print ("Frequency: ");
  Serial.println (r);

  r = xrf_send ("ATPL", NOPRINT);
  Serial.print ("Power level: ");
  Serial.println (r);

  r = xrf_send ("ATEE", NOPRINT);
  Serial.print ("Encryption: ");
  Serial.println (r);
  
  r = xrf_send ("ATDN", NOPRINT);

  delay (1000);
  Serial.println ();
}

String xrf_send (String msg, int opts) {
  unsigned char c;
  String r;
  
  while (XRF.available ()) XRF.read ();
  
  if (!(opts & NOPRINT)) Serial.print ("Sending [");
  
  if ((opts & NOLN)) {
    if (!(opts & NOPRINT)) Serial.print (msg);
    XRF.print (msg);
  } else {
    if (!(opts & NOPRINT)) {
      Serial.print (msg);
      Serial.print ("\\n");
    }
    XRF.println (msg);
  }

  if (!(opts & NOPRINT)) Serial.println ("]");
  
  while (!XRF.available ());
  while (c != 0xD) {
    if (XRF.available ()) {
      c = (unsigned char) XRF.read ();
      r += c;
    }
  }
  if (!(opts & NOPRINT)) Serial.println (r);
  return r;
}

void setup () {

  // init LED
  pinMode (PIN_LED, OUTPUT);
  
  // init serial
  Serial.begin (115200);
  
  // init LCD
  pinMode (PIN_LCD, OUTPUT);
  LCD.begin (9600);
  lcdClear ();
  lcdGoto (0);
  LCD.print ("XRF RSSI Tester");
  Serial.println ("XRF RSSI Tester - master");

  // init XRF
  pinMode (PIN_XRF_RX, INPUT);
  pinMode (PIN_XRF_TX, OUTPUT);
  XRF.begin (9600);

  delay (5000);  // wait for XRF to warm up
  xrf_print_info ();

  lcdGoto (16);
  LCD.print ("Init XRF        ");
  Serial.println ("Init XRF");

  lcdGoto (16);
  LCD.print ("Sending [+++]   ");
  xrf_send ("+++", NOLN|NOPRINT);

  lcdGoto (16);
  LCD.print ("Sending [ATNT3] ");
  xrf_send ("ATNT3", PRINT);

  lcdGoto (16);
  LCD.print ("Sending [ATDN]  ");
  xrf_send ("ATDN", PRINT);

  lcdGoto (16);
  LCD.print ("Ready           ");
  Serial.println ("Ready");
}

void loop () {
  static boolean lcdOn = 0;
  
  if (XRF.available ()) {
    unsigned char c = XRF.read ();
    if (c == 'a') {
      buffer = c;
      Serial.println (c);
    } else {
      buffer += c;
      Serial.print (c);
    }
  }
  
  if (buffer.length() == 12) {
    digitalWrite (PIN_LED, HIGH);
    
    if (lcdOn == 0) {
      lcdGoto (16);
      LCD.print ("M:      S:      ");
      lcdOn = 1;
    }

    String node  = buffer.substring (0, 8);
    String value = buffer.substring (9, 12);
    
    if (node == "aSSRSSIS") {
      if (lcdOn) {
        lcdGoto (19);
        LCD.print (value);
      }
    } else if (node == "aMMRSSIM") {
      if (lcdOn) {
        lcdGoto (27);
        LCD.print (value);
      }
    }

    buffer = "";
    digitalWrite (PIN_LED, LOW);
  }
}