How to read XBee RSSI in API mode? - arduino

I am trying to test a XBee RSSI in API mode at receiving end, how can I retrieve the RSSI value of the receiving radio in arduino.
I configured both XBee in API-2 mode and are connected to arduino by pin 4-5(rxtx & txrx) to Xbee radios.
Sending frame code is like below and there is no problem in transmission at both ends,
uint8_t data[] = {'H','i'};
XBeeAddress64 addr64 = XBeeAddress64();
addr64.setMsb(0x00000000); // Msb address of receiver
addr64.setLsb(0x00000000); // Lsb address of receiver
ZBTxRequest zbTx = ZBTxRequest(addr64, data, sizeof(data));
xbee.send(zbTx);
delay(1000);
At the receiving end I tried pulseIn of arduino and .getRssi() of , The former function gives "0" in result while the later gives "102" but remains the same as I move the Xbee radios away from each other.
What should I need to do for getting correct RSSI at the receiving end..?

Hopefully this answer helps you and others.
Assuming you are using the following lib:
https://github.com/andrewrapp/xbee-arduino
and you have a series 1 module you can use the following test code for quick diagnostics. the commented parts can of course also be used if needed
#include <XBee.h>
#include <SoftwareSerial.h>
// XBee's DOUT (TX) is connected to pin 8 (Arduino's Software RX)
// XBee's DIN (RX) is connected to pin 9 (Arduino's Software TX)
SoftwareSerial serial1(8, 9); // RX, TX
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
Rx16Response rx16 = Rx16Response();
Rx64Response rx64 = Rx64Response();
// uint8_t xbeeOption = 0;
// uint8_t xbeeData = 0;
uint8_t xbeeRssi = 0;
void setup() {
Serial.begin(9600);
serial1.begin(9600);
xbee.setSerial(serial1);
}
void loop() {
xbee.readPacket(100);
if (xbee.getResponse().isAvailable()) {
Serial.println("Xbee available");
if (xbee.getResponse().getApiId() == RX_64_RESPONSE || xbee.getResponse().getApiId() == RX_16_RESPONSE) {
Serial.println("64 or 16");
if (xbee.getResponse().getApiId() == RX_16_RESPONSE) {
Serial.println("16");
xbee.getResponse().getRx16Response(rx16);
// xbeeOption = rx16.getOption();
//Serial.print("xbeeOption: "); Serial.println(xbeeOption);
//xbeeData = rx16.getData(0);
//Serial.print("xbeeData: "); Serial.println(xbeeData);
xbeeRssi = rx16.getRssi();
Serial.print("xbeeRssi: "); Serial.println(xbeeRssi);
}
else {
Serial.println("64");
xbee.getResponse().getRx64Response(rx64);
//xbeeOption = rx64.getOption();
//Serial.print("xbeeOption: "); Serial.println(xbeeOption);
//xbeeData = rx64.getData(0);
//Serial.print("xbeeData: "); Serial.println(xbeeData);
xbeeRssi = rx64.getRssi();
Serial.print("xbeeRssi: "); Serial.println(xbeeRssi);
}
}
}
If you use a series2 module there is only the way using the hardware pwm signal: In order for the RSSI pwm signal to be updated it needs to have received an API packet. Also, for the series 2 Xbee this applies only for the last hop of the packet, so from last router to destination. You need to use the XBee rssi pin and some coding depending on your appliance.
The rssi for distance is not very reliable and you will see a change perhaps every 10 to 15 meters while sending packets. So just moving a Xbee around on your workplace will not change the values.
EDIT:
When using a series 2 module there is the following possibility: connect the rssi pin of the xbee (6) to an Arduino pwm pin (eg 10) and measure the incoming signal, which could then be mapped to a quality or/and distance range. So writing your own rssi function. The usual xbee libs only support series1 modules.

Related

How do I get rid of the symbols behind my LoRa message reception?

A bit of context, I am participating in the UK CanSat competition. I am sorting out the communication between the CanSat and the ground station. We are using LoRa transceivers as required per the competition. We are using a raspberry pi pico as a emitter and an Arduino nano in the ground station as a receiver.
The LoRa module we are using is this one.
Long story short, when I receive the message in the ground station, there are multiple symbols, here is a picture:
Here is the code for the Arduino (receiver):
// Arduino9x_RX
// -*- mode: C++ -*-
// Example sketch showing how to create a simple messaging client (receiver)
// with the RH_RF95 class. RH_RF95 class does not provide for addressing or
// reliability, so you should only use RH_RF95 if you do not need the higher
// level messaging abilities.
// It is designed to work with the other example Arduino9x_TX
#include <SPI.h>
#include <RH_RF95.h>
#define RFM95_CS 10
#define RFM95_RST 9
#define RFM95_INT 2
// Change to .0 or other frequency, must match RX's freq!
#define RF95_FREQ 433.0
// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS, RFM95_INT);
// Blinky on receipt
#define LED 13
void setup() {
pinMode(LED, OUTPUT);
pinMode(RFM95_RST, OUTPUT);
digitalWrite(RFM95_RST, HIGH);
while (!Serial);
Serial.begin(9600);
delay(100);
Serial.println("Arduino LoRa RX Test!");
// manual reset
digitalWrite(RFM95_RST, LOW);
delay(10);
digitalWrite(RFM95_RST, HIGH);
delay(10);
while (!rf95.init()) {
Serial.println("LoRa radio init failed");
while (1);
}
Serial.println("LoRa radio init OK!");
// Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM
if (!rf95.setFrequency(RF95_FREQ)) {
Serial.println("setFrequency failed");
while (1);
}
Serial.print("Set Freq to: "); Serial.println(RF95_FREQ);
// Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on
// The default transmitter power is 13dBm, using PA_BOOST.
// If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then
// you can set transmitter powers from 5 to 23 dBm:
rf95.setTxPower(23, false);
}
void loop() {
if (rf95.available()) {
// Should be a message for us now
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
//char buf = (char*)buf;
uint8_t len = sizeof(buf);
if (rf95.recv(buf, &len)) {
digitalWrite(LED, HIGH);
Serial.print("Got: ");
Serial.println((char*)buf);
Serial.print("RSSI: ");
Serial.println(rf95.lastRssi(), DEC);
} else {
return;
}
}
}
Disclaimer this code was extracted from the internet.
And here is the code for the Raspberry pi pico (emitter):
import board
import busio
import time
import digitalio
import adafruit_rfm9x
import adafruit_bmp280
"Led configuration"
# Led amarillo
ledambar = digitalio.DigitalInOut(board.GP16)
ledambar.direction = digitalio.Direction.OUTPUT
# Led azul
ledazul = digitalio.DigitalInOut(board.GP17)
ledazul.direction = digitalio.Direction.OUTPUT
"LoRa"
# LoRa radio setup
spi = busio.SPI(clock=board.GP2, MOSI=board.GP3, MISO=board.GP4)
cs = digitalio.DigitalInOut(board.GP6)
reset = digitalio.DigitalInOut(board.GP7)
rfm9x = adafruit_rfm9x.RFM9x(spi, cs, reset, 433.0)
print("RFM9x radio ready")
"Not LoRa from here"
"BMP280 Sensor"
# BMP280 comunication start up
i2c = busio.I2C(scl=board.GP15, sda=board.GP14)
bmp280_sensor = adafruit_bmp280.Adafruit_BMP280_I2C(i2c, address=0x76)
# BMP280 sensor readings
def read_temperature():
return bmp280_sensor.temperature
def read_pressure():
return bmp280_sensor.pressure
while True:
ledazul.value = True
"LoRa"
rfm9x.send("Hello World, this is a test, why does this not work? im going mad!!!!")
print("Radio mensage sent")
"Not LoRa from here"
ledazul.value = False
cansat_temperature = read_temperature()
print("TEMPERATURE:")
print(cansat_temperature)
cansat_pressure = read_pressure()
print("PRESSURE:")
print(cansat_pressure)
print(" ")
time.sleep(2.5)
Disclaimer, this code is designed to do more things than just LoRa communication. The LoRa part of the code is below the tag: "LoRa".
If you have any recommendations on how to improve my code in any way, just let me know, it would be really helpful.
Thank you.
I have tried soldering the antennas, changing them, modifying the code, both in the pico and the Arduino, also tried changing the antennas but nothing worked.
You have a buffer over-read - a major security flaw, same as the infamous Heartbleed bug.
When you pass a char * to Serial.println, it will print the data until it finds a null character. Your python string does not contain such null termination, so the the print function keeps going through the memory until it finds one. You were lucky that it found one quite quickly. It could also go on much longer, depending on what is in the memory at that time.
A naive way to fix the issue would be to just append a null character to the python string:
rfm9x.send("Hello World, this is a test, why does this not work? im going mad!!!!\0")
However, if you were to receive partial data (or someone forgot to add a null character), the issue would reappear.
The proper way to fix the problem is to check how much data is received and only print that much. According to the documentation, the received number of octets is stored in the len parameter. Therefore, you can add null-termination on the receiving end like so:
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
// Leave space for null-termination
uint8_t len = sizeof(buf) - 1;
if (rf95.recv(buf, &len))
{
// Add null-termination at the end of the received data.
buf[len] = '\0';
// Print as before.
digitalWrite(LED, HIGH);
Serial.print("Got: ");
Serial.println((char*)buf);
Serial.print("RSSI: ");
Serial.println(rf95.lastRssi(), DEC);
}

radio control with arduino

Hi I am attempting to read from an rc transmitter using an Arduino Uno board, I have a signal pin connected from the receiver to pin 9 on the Arduino. Here is the code I would really appreciate some help all I am trying to achieve is the read the pmw from the receiver. I am able to plug a servo into the receiver and that works fine along with a motor I am just struggling when I try and use the Arduino with the receiver. When I run my program all I get in the Serial monitor are values such as 9991,9972,10030,10050 that are completely unrelated.
I want to have a pmw value that I can map to 0-255 in order to control a motor
My Circuit:
battery -> ESC(for BEC to regulate five volts back to receiver)-> receiver -> ch3 signal pin -> Arduino uno (pin9)
void setup() {
Serial.begin(9600);
}
void loop() {
int pwm = pulseIn(9, HIGH, 25000);
Serial.println(pwm);
delay (5);
}
You are using pulseIn which returns a time (in ms). The time being how long it waited for a HIGH signal. If you want the actual value, use analogRead. You can still use pulseIn, just don't use the return value
void setup() {
Serial.begin(9600);
}
void loop() {
byte pwm = analogRead(A5) / 4;
Serial.println(pwm);
delay(5);
}

SPI Arduino Interface with Absolute Encoder

I am trying to retrieve data from an RLS absolute rotary encoder via SPI through Arduino Uno. I have managed to learn the basics of SPI, but I can't seem to get this working. I keep printing the transfer data and keep getting 255.
I also use the recommended pins for Arduino Uno here: https://www.arduino.cc/en/reference/SPI
Here is the link to the Absolute Encoder DataSheet: https://resources.renishaw.com/en/details/data-sheet-orbis-true-absolute-rotary-encoder--97180
Any help is appreciated.
Here is my code I have been trying:
#include <SPI.h>
unsigned int data;
int CS = 10; //Slave Select Pin
// The SS pin starts communication when pulled low and stops when high
void setup() {
Serial.begin(9600);
RTC_init();
}
int RTC_init() {
pinMode(CS, OUTPUT);
SPI.begin();
SPI.setBitOrder(LSBFIRST); // Sets Bit order according to data sheet (LSBFIRST)
SPI.setDataMode(SPI_MODE2); // 2 or 3, Not sure (I have tried both)
digitalWrite(CS, LOW); // Start communications
/*
Commands List:
Command "1" (0x31) – position request (total: 3 bytes) + 4 for multi-turn
Command "3" (0x33) – short position request (total: 2 bytes) + 4 for multi-turn
Command "d" (0x64) – position request + detailed status (total: 4 bytes) + 4 for multi-turn
Command "t" (0x74) – position request + temperature (total: 5 bytes) + 4 for multi-turn
Command "v" (0x76) – serial number (total: 7 bytes)
*/
unsigned int data = SPI.transfer(0x33); // Data
digitalWrite(CS, HIGH); // End communications
return(data);
}
void loop() {
Serial.println(RTC_init());
}
First, you forgot to set your CS pin to output. That won't help.
void setup()
{
Serial.begin(9600);
digitalWrite(CS, HIGH); // set CS pin HIGH
pinMode(CS, OUTPUT); // then enable output.
}
SPI is a two way master/slave synchronous link, with the clock being driven by bytes being sent out by the master. This means that when transacting on SPI, you are expected to send out as many bytes as you want to receive.
As shown on the timing diagram on page 14 of the datasheet, you are expected to send one byte with the command, then as many null bytes as the response requires minus 1.
The diagram shows the clock being low at idle, and input bits being stable when the clock goes low, that's SPI mode 1.
To read the position, for a non-multiturn encoder.
unsigned int readEncoderPos()
{
// this initialization could be moved to setup(), if you do not use
// the SPI to communicate with another peripheral.
// Setting speed to 1MHz, but the encoder can probably do better
// than that. When having communication issues, start slow, then set
// final speed when everything works.
SPI.beginTransaction(SPISettings(1'000'000, MSBFIRST, SPI_MODE1));
digitalWrite(CS, LOW);
// wait at least 2.5us, as per datasheet.
delayMicroseconds(3);
unsigned int reading = SPI.transfer16(0x3300); // 0x3300, since we're sending MSB first.
digitalWrite(CS, HIGH);
// not needed if you moved SPI.beginTransaction() to setup.
SPI.endTransaction();
// shift out the 2 status bits
return reading >> 2;
}

Arduino RFID (Wiegand) Continuous reading

I have this RFID reader "Rosslare AY-X12", and it's working with Wiegand 26bit. I have an arduino mini Pro and connected together it's working fine but it only reads the card one time and then I have nothing.
When I put on the card arduino reads that card but only one time during the card is near by the reader and it again reads that card when I put off the card and then I put on. But I want to read that card continuously, I mean when the card is near by the Reader still reading the card, every 1ms reads that card.
Do you have any idea how to do that ? Is there any RFID arduino library which can do that? I had got the Mifare and its can do that. But this 125Khz reader which can communicate over Wiegand can't do that or I don't know how to do that.
I'm using this library : https://github.com/monkeyboard/Wiegand-Protocol-Library-for-Arduino
My previous answer was deleted. I am going to make another attempt to answer the questions.
Do you have any idea how to do that ?
This cannot be done by Arduino because Arduino in your case is just reading the D0 and D1 pulses from your RFID reader. Since your RFID reader Rosslare AY-X12 does not send out continuous output of wiegand protocol, there is no way Arduino can read more than what was not sent to it.
The common RFID readers will not send continuous data of the same card because in the common use case (entry/exit/attendance), normally one tap is to check-in and another tap is to check-out. If the RFID reader sends continuous data of the same card, the main system receiving the multiple wiegand data will be confused and will not be able to determine if the user actually wish to check-in or check-out.
Is there any RFID arduino library which can do that?
No. There is no such RFID Arduino library. If the RFID reader is not sending out continuous data, there is no way the receiver (Arduino) can receive them.
Is there a way to achieve this?
Yes, there are some readers that has the option to turn on the continuous output of data, for example 714-52 Mifare® ID Reader with selectable outputs. In its specification :
Continuous output with tag in field or single transmission
With this reader configured to continuous output, you can then use Arduino and the monkeyboard wiegand library to read the data.
I wrote my own wiegand code. Its not that difficult. I attached interrupts to the data pins and when they change I log the zero or one. You then build up the binary string and once timed out because no bits coming in. Then you convert the binary to decimal.
#include <LiquidCrystal.h>
int data0 = 2; //set wiegand data 0 pin
int data1 = 3; //set wiegand data 1 pin
unsigned long bit_holder; //unsigned long (positive 32 bit number)
unsigned long oldbit = 0;
volatile int bit_count = 0;
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
unsigned long badge;
unsigned int timeout;
unsigned int t = 800;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("Present Badge");
delay(2);
Serial.println("Present Badge");
pinMode(data0, INPUT);
digitalWrite(data0, HIGH);
pinMode(data1, INPUT);
digitalWrite(data1, HIGH);
attachInterrupt(0, zero, FALLING); //attach interrupts and assign functions
attachInterrupt(1, one, FALLING);
}
void zero(){
bit_count ++;
bit_holder = (bit_holder << 1) + 0; //shift left one and add a 0
timeout = t;
}
void one(){
bit_count ++;
bit_holder = (bit_holder << 1) + 1; //shift left one and add a 1
timeout = t;
}
void loop() {
timeout --;
if (timeout == 0 && bit_count > 0){
lcd.clear();
lcd.print("Dec:");
lcd.print(bit_holder);
lcd.setCursor(0,1);
lcd.print("Hex:");
lcd.print(String(bit_holder,HEX));
Serial.print("bit count= ");
Serial.println(bit_count);
Serial.print("bits= ");
Serial.println(bit_holder,BIN);
oldbit = bit_holder; //store previous this value as previous
bit_count = 0; //reset bit count
bit_holder = 0; //reset badge number
}
}
You may need to find a reader that offer a continuously reading, as I know almost of Wiegand Reader in the market can't perform a continuously reading because they have a "onboard" control that controls this...
Maybe you can try with Arduino Serial RFID Reader...
try a this timer libary Timer1 and mayby try this code it worked for me, my tags and cards now reads continuously.
Greetings from Denmark
Gregor
#include <Timer1.h>
//******************************************************************
// ATmega168, ATmega328:
// - Using Timer 1 disables PWM (analogWrite) on pins 9 and 10
// ATmega2560:
// - Using Timer 1 disables PWM (analogWrite) on pins 11 and 12
// - Using Timer 3 disables PWM (analogWrite) on pins 2, 3 and 5
// - Using Timer 4 disables PWM (analogWrite) on pins 6, 7 and 8
// - Using Timer 5 disables PWM (analogWrite) on pins 44, 45 and 46
//******************************************************************
unsigned int lastTime;
#include <SoftwareSerial.h>
SoftwareSerial RFID = SoftwareSerial(2,4);
char character;
String our_id;
void setup()
{
// Disable Arduino's default millisecond counter (from now on, millis(), micros(),
// delay() and delayMicroseconds() will not work)
disableMillis();
// Prepare Timer1 to count
// On 16 MHz Arduino boards, this function has a resolution of 4us
// On 8 MHz Arduino boards, this function has a resolution of 8us
startCountingTimer1();
lastTime = readTimer1();
Serial.begin(9600);
RFID.begin(9600);
}
void loop()
{
unsigned int now = readTimer1();
while (RFID.available()>0)
{
character = RFID.read();
our_id += character;
lastTime = now;
}
if (our_id.length() > 10) {
our_id = our_id.substring(1,13);
Serial.println(our_id);
our_id = "";
}
delay(1000);
}

Freescale Pressure Sensor MPL3115A2 I2C communication with Arduino

Does anyone have experience with the MPL3115A2 Freescale I2C pressure sensor?
I need to use it in a project concerning Arduino UNO r3, but I can't get communication between them correctly. Here is my code:
#include <Wire.h>
void setup(){
Serial.begin(9600);
/*Start communication */
Wire.begin();
// Put sensor as in Standby mode
Wire.beginTransmission((byte)0x60); //0x60 is sensor address
Wire.write((byte)0x26); //ctrl_reg
Wire.write((byte)0x00); //reset_reg
Wire.endTransmission();
delay(10);
// start sensor as Barometer Active
Wire.beginTransmission((byte)0x60);
Wire.write((byte)0x26); //ctrl_reg
Wire.write((byte)0x01); //start sensor as barometer
Wire.endTransmission();
delay(10);
}
void getdata(byte *a, byte *b, byte *c){
Wire.beginTransmission(0x60);
Wire.write((byte)0x01); // Data_PMSB_reg address
Wire.endTransmission(); //Stop transmission
Wire.requestFrom(0x60, 3); // "please send me the contents of your first three registers"
while(Wire.available()==0);
*a = Wire.read(); // first received byte stored here
*b = Wire.read(); // second received byte stored here
*c = Wire.read(); // third received byte stored here
}
void loop(){
byte aa,bb,cc;
getdata(&aa,&bb,&cc);
Serial.println(aa,HEX); //print aa for example
Serial.println(bb,HEX); //print bb for example
Serial.println(cc,HEX); //print cc for example
delay(5000);
}
The data I receive is : 05FB9 (for example). When I change the register address (see Wire.write((byte)0x01); // Data_PMSB_reg address), I expect the data to change, but it doesn't! Can you explain this to me?
You can find the documentation and datasheets on the NXP website.
I can't properly understand how they communicate with each other. I got communication between Arduino and some other I2C sensors with same communication protocol without any problem.
Your problem is likely due to the fact that the Freescale part requires Repeated-Start I2C communication to do reads. The original Arduino two-wire library (TWI library used by Wire), did not support Repeated-Start.
I know this because I had to rewrite TWI for one of my projects to support Repeated-Start (interrupt driven, both Master and Slave). Unfortunately I've never gotten around to uploading my code, but someone else did essentially the same thing here (at least for Master which is what you need):
http://dsscircuits.com/articles/arduino-i2c-master-library.html
Lose the Wire library and use their I2C library instead.

Resources