MEASURE XBEE RSSI PWM WIDTH... not working.. any help appreciated

Hi all, I'm new here.

Ok.. before the obvious question of "there has been many topics on that, just do a search"... I have to say I did, and my whole day was spent debugging why it is not working, but the other people using the same method is working....

So basically.... I'm now calling for help.. desperate.

1.What I want to do. -Get RSSI values from XBee for Localization.
2. Why use PWM, rather than the ATDB method? - I want more resolution... ATDB command returns range of 256 values only.
3. What have I tried - pulseIn fucntion... and it is causing me much headache as to WHY hasn't it work.

The pulsein function gets the duration.. it is NOT working.... returns 0 values... when I get like a room far.. only it returned 100+- value... when I close in, it suddenly jumps to 0 again.

I thought it's because of pulseIn can't detect the pulses?

From Xbee datasheet,

PWM operates at 12MHz, period is 200us...

This is another baffling stuff to me.. Period according to frequency... 1/12M=0.08333us, Period according to statemet is 200us.. @@

my code

#include <XBee.h>
#include <SoftwareSerial.h>
#include <stdlib.h>
#include <stdio.h>


//Declare pins for software serial
byte ssRX = 8;  
byte ssTX = 9;

//New object software serial
SoftwareSerial nss(ssRX,ssTX);

//create an Xbee object
XBee xbee = XBee();

//VARIABLES
byte atCmd[5];
byte atCmdValue[8];
byte atCmdValueIndex=0;
byte atCmdValueSize;
byte textInput=0;
byte textHex[20];
byte temp1;
byte dbCommand[]={'D','B'};
byte p0Command[]={'P','0'};
byte payload[]={'P','I'};
unsigned long duration[10];

//aT command TX and RX class
AtCommandRequest atRequest =0;
AtCommandResponse atResponse =AtCommandResponse();
//response
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle 
ZBRxResponse rx = ZBRxResponse();
ModemStatusResponse msr = ModemStatusResponse();


// SH + SL Address of receiving XBee
XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x403e0f30);
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
ZBTxStatusResponse txStatus = ZBTxStatusResponse();


//SETUP==================================================
void setup()
{
  pinMode(12,INPUT);
  
  Serial.begin(9600); //begin hardware serial
  //start soft serial for XBee
  nss.begin(9600);
  xbee.begin(nss);
  Serial.println("Loading...Please Wait...");
  delay(3000);
  Serial.println("Press X to configure commands, S to send commands, R to receive payload.");
  
}
//=====================================================
//==========================================================
void atSendCommand() {
  //Serial.println("Sending command to XBee");
  
  //send the command
  xbee.send(atRequest);
  
  //wait for AT response, timeout 5seconds
  if(xbee.readPacket(5000)) {
    //got response, so check whether is it an AT response
    if(xbee.getResponse().getApiId() == AT_COMMAND_RESPONSE) {
      xbee.getResponse().getAtCommandResponse(atResponse);
      
      if(atResponse.isOk()) {
        //Serial.print("Command [");
        //Serial.write(atResponse.getCommand()[0]);
        //Serial.write(atResponse.getCommand()[1]);
        //Serial.println("] was successfull!");
        
        if(atResponse.getValueLength() > 0) {
          //Serial.print("Command value length is ");
          //Serial.println(atResponse.getValueLength(),DEC);
          Serial.print("Command value is : ");
          
          for(int i = 0;i< atResponse.getValueLength();i++){
            Serial.print(atResponse.getValue()[i],DEC);
            Serial.print(" ");
          }
          
          Serial.println("");
        }
      }
      else {
        Serial.print("Command return error code: ");
        Serial.println(atResponse.getStatus(),HEX);
      }
    }
    else {
      Serial.print("Expected AT response but got ");
      Serial.print(xbee.getResponse().getApiId(), HEX);
    }
  } else {
    //at command failed
    if(xbee.getResponse().isError()){
      Serial.print("Error readin packet. Error code: ");
      Serial.println(xbee.getResponse().getErrorCode());
    }
    else {
      Serial.print("NO RESPONSE FROM RADIO");
    }
  }
  
}
    

//=====================================================
//==========================================================
void atCommandInput()
{
 Serial.println("Enter 2 letter command : ");
 Serial.print("Letter 1 of command : ");
 while(Serial.available()==0);
 textInput=Serial.read();
 Serial.flush();
 atCmd[0]=textInput;
 Serial.write(atCmd[0]);
 Serial.println("");
 Serial.print("Letter 2 of command : ");
 while(Serial.available()==0);
 textInput=Serial.read();
 Serial.flush();
 atCmd[1]=textInput;
 Serial.write(atCmd[1]);
 atRequest.setCommand(atCmd);
 Serial.println("");
 Serial.println("Query(Y) or Setting(N)?");
 while(Serial.available()==0);
 textInput=Serial.read();
 Serial.flush();
 if(textInput=='Y') {
   atRequest.clearCommandValue();
   memset(atCmdValue,'\0',sizeof(atCmdValue));
   memset(textHex,'\0',sizeof(textHex));
   Serial.println("Query Selected.");
 }
 else if(textInput=='N') {
   atCmdValueIndex=0;
   Serial.print("Enter value in hex : ");
   while(textInput!='Z') {
     if(Serial.available()>0) {
       textInput=Serial.read();
       Serial.flush();
       if(textInput!='Z') {
         textHex[atCmdValueIndex]=textInput;
         Serial.print(textHex[atCmdValueIndex],HEX);
         Serial.print(" ");
         atCmdValueIndex++;
       }
     }
   }
   atCmdValueSize=strlen((char*)textHex);
   
   hextobyte((char*)textHex,atCmdValue);
   Serial.println("");
   for(int i=0;i<(atCmdValueSize/2);i++) {
     Serial.print(atCmdValue[i]);
     Serial.print(" ");
   }
   Serial.println("");
   atRequest.setCommandValue(atCmdValue);
   atRequest.setCommandValueLength(sizeof(atCmdValue));
 }
 
}

//=====================================================
//==========================================================

my code.. continued.. too long.. sigh..

//==========================================================
void hextobyte(char *text,byte *bytes) {
  
  unsigned int i, t, hn, ln;

        for (t = 0,i = 0; i < atCmdValueSize; i+=2,++t) {

                hn = text[i] > '9' ? text[i] - 'A' + 10 : text[i] - '0';
                ln = text[i+1] > '9' ? text[i+1] - 'A' + 10 : text[i+1] - '0';

                bytes[t] = (hn << 4 ) | ln;
        }

   
  
    
  return;
}
//=====================================================
//==========================================================
void xbSendPayload() {
  
  xbee.send(zbTx);
  
  // after sending a tx request, we expect a status response
  // wait up to half second for the status response
  if (xbee.readPacket(500)) {
    // got a response!

    // should be a znet tx status            	
    if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
      xbee.getResponse().getZBTxStatusResponse(txStatus);

      // get the delivery status, the fifth byte
      if (txStatus.getDeliveryStatus() == SUCCESS) {
        // success.  time to celebrate
        Serial.println("TX Success!");
      } else {
        // the remote XBee did not receive our packet. is it powered on?
        Serial.println("TX Delivery Failure, is it powered on?");
      }
    }
  } else if (xbee.getResponse().isError()) {
    Serial.print("Error reading packet.  Error code: ");  
    Serial.println(xbee.getResponse().getErrorCode());
  } else {
    // local XBee did not provide a timely TX Status Response -- should not happen
    Serial.println("TX Delivery Status TimeOut.");
  }

  



}


//=====================================================
//==========================================================
void xbReceivePayload()
{
  xbee.readPacket();
  
 if (xbee.getResponse().isAvailable()) {
      // got something
      //Serial.println("Got something..");
      
      if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
        // got a zb rx packet
        //Serial.println("Got a ZB RX Packet..");
        
        // now fill our zb rx class
        //Serial.println("Filling rx...");
        xbee.getResponse().getZBRxResponse(rx);
            
        if (rx.getOption() == ZB_PACKET_ACKNOWLEDGED) {
            // the sender got an ACK
            Serial.println("Sender get ACK.");
        } else {
            // we got it (obviously) but sender didn't get an ACK
            Serial.println("Sender did not get ACK.");
        }
        // set dataLed PWM to value of the first byte in the data
        
      } else if (xbee.getResponse().getApiId() == MODEM_STATUS_RESPONSE) {
        xbee.getResponse().getModemStatusResponse(msr);
        // the local XBee sends this response on certain events, like association/dissociation
        
        if (msr.getStatus() == ASSOCIATED) {
          // yay this is great.  flash led
          Serial.println("Modem Response : ASSOCIATED");
        } else if (msr.getStatus() == DISASSOCIATED) {
          // this is awful.. flash led to show our discontent
          Serial.println("Modem Response : DISASSOCIATED");
        } else {
          // another status
          Serial.println("Modem Response : UNKNOWN");
        }
      } else {
      	// not something we were expecting
        Serial.println("Not ZB or MST response.");    
      }
    } else if (xbee.getResponse().isError()) {
      Serial.print("Error reading packet.  Error code: ");  
      Serial.println(xbee.getResponse().getErrorCode());
    }
}
  
//=====================================================
//==========================================================



void loop()
{
  
   Serial.println("Preparing... You have 5 seconds to position..");
   delay(5000);
   Serial.println("Starting log...");
   atRequest.setCommand(dbCommand);
   atRequest.clearCommandValue();
   
  while(1) {
    if(nss.available()>0)
     {
      
          while(digitalRead(12)==HIGH);
          duration[0]=pulseIn(12,HIGH,1000); //get the duration.. it is NOT working.... returns 0 values... when I get like a room far.. only it   
                                                            //returned 100+- value... when I close in, it suddenly jumps to 0 again.
          
      
      
        Serial.println(duration[0],DEC);
      
      //xbReceivePayload();
      atSendCommand();
      delay(80);
      
      nss.flush();
      
    }
  }   

}
  1. Why use PWM, rather than the ATDB method? - I want more resolution... ATDB command returns range of 256 values only.

Breaking a half-assed guess as to RSSI into 256 values is still going to have huge amounts of discrepancy in the distances you try to discern from those values.

Like printing a float to 12 decimal places, you can not make an inaccurate value more accurate by reading it a different way.

PaulS:

  1. Why use PWM, rather than the ATDB method? - I want more resolution... ATDB command returns range of 256 values only.

Breaking a half-assed guess as to RSSI into 256 values is still going to have huge amounts of discrepancy in the distances you try to discern from those values.

Like printing a float to 12 decimal places, you can not make an inaccurate value more accurate by reading it a different way.

So... what you mean is... even if PWM values may have a higher resolution.. it still won't give a higher accuracy?

I need the resolution so that I can discern smaller changes in distances. Right now, if I'm using the DB command, it's sensitivity is 10CM per dB unit. When it's close. When it's further away, it' resolution increases...

I have to have a higher sensitivity/resolution distance per unit change.... so that is why I am making the PWM RSSI work, since it says in the datasheet, it is able to give a resolution of 2400 counts...

The 256 steps that the AT command splits the RSSI value up into is already more steps that is reasonable.

I need the resolution so that I can discern smaller changes in distances.

It is a complete fallacy that RSS! correlates to distance.

Understood.