Pages: 1 [2]   Go Down
Author Topic: A story of three xbees; two emitters and one hub  (Read 1098 times)
0 Members and 1 Guest are viewing this topic.
Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3942
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

A start delimiter is just a special character of your choosing that serves to mark the start of a message. I chose an asterisk (*) but any character can be used, as long as it's not also used in the data being sent. Having said that, since we are sending binary data, we have a problem with that because there's no guarantee that a byte of "myData" in the example code above won't look like an asterisk. As as simplifying assumption, we could dispense with checking for a "premature" start delimiter, and see how things work just relying on knowing that the messages are a constant length. I'd expect that it would actually work fairly well. This is the kind of robustness that can be had with API mode. There is at least one very good library out there to help operate in API mode (here), but in sticking to transparent mode, we get to do it ourselves, or take the easy way out and live with a weaker design.
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Offline Offline
Full Member
***
Karma: 0
Posts: 126
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

eh, not having much luck. the LCD prints out a -257 and nothing changes.

here the code:


Emitter:

Code:
//Transmitter modules. These are the bike units intended to show signal strength indication in correlation to a central networked hub.
//x bees have been configured with personal ID's to avoid interference with other networks (encryption).
//optimal xbee use is outdoors, no wall interference/metal(close contact). range is 300ft at optimal conditions.
//





const int rssi_pin = 9; //RSSI Pin of the XBee Module
 int rssiDur; //variable to read pin
byte inches_val;

//LED pins//////////////////////////
const int led3 = 3;
const int led4 = 4;
const int led5 = 5;
const int led6 = 6;
const int led7= 7;
const int led8= 8;
const int led10= 10;
const int led11= 11;
const int led12= 12;
const int led13= 13;
/////////////////////////////////////

///Averager Variables///////////////////////////////

//this is intended to place first 10 read incoming RSSI values in an array and average. smoother readings

const int numReadings = 10;

int readings[numReadings];      // the readings from the RSSI
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
byte average = 0;                // the average


///////////////////////////////////////////////////
const byte nodeID = 1;
    int myData;


void setup(){
  
   for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;

pinMode (led3, OUTPUT);
pinMode (led4, OUTPUT);
pinMode (led5, OUTPUT);
pinMode (led6, OUTPUT);
pinMode (led7, OUTPUT);
pinMode (led8, OUTPUT);
pinMode (led10, OUTPUT);
pinMode (led11, OUTPUT);
pinMode (led12, OUTPUT);
pinMode (led13, OUTPUT);
  
   //Serial1.begin(38400);
  Serial.begin(38400); //Xbee communicating at this baud
}




void loop(){


 rssiDur = pulseIn(rssi_pin, LOW, 200); //this is set to 200ms with ATRP @ 2
inches_val = rssiDur; // XBee times out 10-12 inches apart, so added 11
inches_val = rssiDur + 24; // XBee times out 10-12 inches apart, so added 11
min(inches_val, 1);
//min(inches_val, 18);
total= total - readings[index];        
  // read from the sensor:  
  readings[index] = inches_val;
  // add the reading to the total:
  total= total + readings[index];      
  // advance to the next position in the array:  
  index = index + 1;                    

  // if we're at the end of the array...
  if (index >= numReadings)              
    // ...wrap around to the beginning:
    index = 0;                          

  // calculate the average:
  average = total / numReadings;        
  


    Serial.write('*');            //message start delimiter
    Serial.write(nodeID);
    Serial.write(average >> 8);    //most significant byte
    Serial.write(average & 0xFF);  //least significant byte
delay(100);
// value 10 = 1 meter
if (average > 24 && average < 124){ //1-5 meters
 // Serial.write(1);
   digitalWrite(led3, HIGH);
       digitalWrite(led4, HIGH);
        digitalWrite(led5, HIGH);
        digitalWrite(led6, HIGH);
        digitalWrite(led7, HIGH);
        digitalWrite(led8, HIGH);
        digitalWrite(led10, HIGH);
        digitalWrite(led11, HIGH);
        digitalWrite(led12, HIGH);
        digitalWrite(led13, HIGH);
        
}
  if (average > 124 && average < 224){ //5-10 meters
  //Serial.write(2);
   digitalWrite(led3, HIGH);
       digitalWrite(led4, HIGH);
        digitalWrite(led5, HIGH);
        digitalWrite(led6, HIGH);
        digitalWrite(led7, HIGH);
         digitalWrite(led8, HIGH);
        digitalWrite(led10, HIGH);
        digitalWrite(led11, HIGH);
        digitalWrite(led12, LOW);
        digitalWrite(led13, LOW);
        
  }
  if (average > 224 && average < 324){ //10- 15 meters
  //Serial.write(3); //this is 10 meters. write to reciever
   digitalWrite(led3, HIGH);
       digitalWrite(led4, HIGH);
        digitalWrite(led5, HIGH);
        digitalWrite(led6, HIGH);
        digitalWrite(led7, HIGH);
         digitalWrite(led8, HIGH);
         digitalWrite(led10, LOW);
        digitalWrite(led11, LOW);
        digitalWrite(led12, LOW);
        digitalWrite(led13, LOW);
        
  }
  if (average > 324 && average < 424){ //15-20 meters
  //Serial.write(4);
    digitalWrite(led3, HIGH);
       digitalWrite(led4, HIGH);
        digitalWrite(led5, HIGH);
        digitalWrite(led6, HIGH);
        digitalWrite(led7, LOW);
           digitalWrite(led8, LOW);
        digitalWrite(led10, LOW);
        digitalWrite(led11, LOW);
        digitalWrite(led12, LOW);
        digitalWrite(led13, LOW);
        
  }
  if (average > 424 ){ //20-30 meters
 // Serial.write(5);
   digitalWrite(led3, HIGH);
       digitalWrite(led4, HIGH);
        digitalWrite(led5, LOW);
        digitalWrite(led6, LOW);
        digitalWrite(led7, LOW);
         digitalWrite(led8, LOW);
        digitalWrite(led10, LOW);
        digitalWrite(led11, LOW);
        digitalWrite(led12, LOW);
        digitalWrite(led13, LOW);
      
    }
    
    //isolated instances below
  
  if (average == 24 ){ //if their is no signal
  Serial.write(9);
   digitalWrite(led3, LOW);
       digitalWrite(led4, LOW);
        digitalWrite(led5, LOW);
        digitalWrite(led6, LOW);
        digitalWrite(led7, LOW);
         digitalWrite(led8, LOW);
        digitalWrite(led10, LOW);
        digitalWrite(led11, LOW);
        digitalWrite(led12, LOW);
        digitalWrite(led13, LOW);
}

}

reciever :

Code:
#include <LiquidCrystal.h>

const int ledPin = 13;
//const int signalStrengthPin = 9;

//byte incomingByte, val_read;
byte val_read;

int emitter2;


    byte nodeID;
 
    int average;
//int RSSIpin = 9;
//unsigned long rssiDur;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()
{
  Serial.begin(38400);
 lcd.begin(16,2);
lcd.noAutoscroll();
    
  pinMode(ledPin, OUTPUT);
 // pinMode(signalStrengthPin, INPUT);
}

void loop()
{


 //rssiDur = pulseIn(RSSIpin, LOW, 200);
  // print signal strength
  //Serial.println(rssiDur);
 
  if (Serial.available()>0)
  {
   // if ((int(val_read) == 'B'))
    
val_read = Serial.read();
 if (val_read == '*') {                     //wait for start delimiter
        nodeID = Serial.read();
        average = Serial.read() << 8;    //msb
        average += Serial.read();        //lsb
        
   while (val_read == '*' && nodeID == 1)    //message received and decoded, continue processing it...
    lcd.noAutoscroll();
    lcd.setCursor(0,0);
lcd.print("Bike1 = ");
lcd.print(average);

 }
 
  while (val_read == 'A' && nodeID ==2) {                     //wait for start delimiter
        
lcd.noAutoscroll();
      lcd.setCursor(0,1);
lcd.print("Bike2 = ");
lcd.print(average);
  }
}

  }
« Last Edit: November 09, 2012, 04:31:07 pm by danieljay » Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3942
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try the following receiver code. I might also slow the transmitters down to once every two seconds, just for testing purposes.

Code:
#include <LiquidCrystal.h>

int val_read;
byte nodeID;
int average;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()
{
    Serial.begin(38400);
    lcd.begin(16,2);
}

void loop()
{
    val_read = Serial.read();
    if (val_read == '*') {               //wait for start delimiter
        nodeID = Serial.read();
        average = Serial.read() << 8;    //msb
        average += Serial.read();        //lsb

        //message received and decoded, continue processing it...
        if (nodeID == 1) {
            lcd.setCursor(0, 0);
            lcd.print("Bike1 = ");
            lcd.print(average, DEC);
            lcd.print("   ");
        }
        else if (nodeID == 2) {
            lcd.setCursor(0, 1);
            lcd.print("Bike2 = ");
            lcd.print(average, DEC);
            lcd.print("   ");
        }
    }
}
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Offline Offline
Full Member
***
Karma: 0
Posts: 126
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

No go. I tried and also rearranged the emitter code as shown below :

Code:
const int rssi_pin = 9; //RSSI Pin of the XBee Module
 int rssiDur; //variable to read pin
byte inches_val;

//LED pins//////////////////////////
const int led3 = 3;
const int led4 = 4;
const int led5 = 5;
const int led6 = 6;
const int led7= 7;
const int led8= 8;
const int led10= 10;
const int led11= 11;
const int led12= 12;
const int led13= 13;
/////////////////////////////////////

///Averager Variables///////////////////////////////

//this is intended to place first 10 read incoming RSSI values in an array and average. smoother readings

const int numReadings = 10;

int readings[numReadings];      // the readings from the RSSI
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
byte average = 0;                // the average


///////////////////////////////////////////////////
const byte nodeID = 2;
    int myData2;


void setup(){
 
   for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;

pinMode (led3, OUTPUT);
pinMode (led4, OUTPUT);
pinMode (led5, OUTPUT);
pinMode (led6, OUTPUT);
pinMode (led7, OUTPUT);
pinMode (led8, OUTPUT);
pinMode (led10, OUTPUT);
pinMode (led11, OUTPUT);
pinMode (led12, OUTPUT);
pinMode (led13, OUTPUT);
 
 
  Serial.begin(38400); //Xbee communicating at this baud
}

void loop(){


 rssiDur = pulseIn(rssi_pin, LOW, 200); //this is set to 200ms with ATRP @ 2
//inches_val = rssiDur + 24; // this is 24inches / 2 feet
//min(inches_val, 18);
total= total - readings[index];         
  // read from the sensor: 
  readings[index] = rssiDur;
  // add the reading to the total:
  total= total + readings[index];       
  // advance to the next position in the array: 
  index = index + 1;                   

  // if we're at the end of the array...
  if (index >= numReadings)             
    // ...wrap around to the beginning:
    index = 0;                           

  // calculate the average:
  average = total / numReadings;         
 


if (average > 24 && average < 124){ //1-5 meters
 // Serial.write(1);
   digitalWrite(led3, HIGH);
       digitalWrite(led4, HIGH);
        digitalWrite(led5, HIGH);
        digitalWrite(led6, HIGH);
        digitalWrite(led7, HIGH);
        digitalWrite(led8, HIGH);
        digitalWrite(led10, HIGH);
        digitalWrite(led11, HIGH);
        digitalWrite(led12, HIGH);
        digitalWrite(led13, HIGH);
       
}
  if (average > 124 && average < 224){ //5-10 meters
  //Serial.write(2);
   digitalWrite(led3, HIGH);
       digitalWrite(led4, HIGH);
        digitalWrite(led5, HIGH);
        digitalWrite(led6, HIGH);
        digitalWrite(led7, HIGH);
         digitalWrite(led8, HIGH);
        digitalWrite(led10, HIGH);
        digitalWrite(led11, HIGH);
        digitalWrite(led12, LOW);
        digitalWrite(led13, LOW);
       
  }
  if (average > 224 && average < 324){ //10- 15 meters
  //Serial.write(3); //this is 10 meters. write to reciever
   digitalWrite(led3, HIGH);
       digitalWrite(led4, HIGH);
        digitalWrite(led5, HIGH);
        digitalWrite(led6, HIGH);
        digitalWrite(led7, HIGH);
         digitalWrite(led8, HIGH);
         digitalWrite(led10, LOW);
        digitalWrite(led11, LOW);
        digitalWrite(led12, LOW);
        digitalWrite(led13, LOW);
       
  }
  if (average > 324 && average < 424){ //15-20 meters
  //Serial.write(4);
    digitalWrite(led3, HIGH);
       digitalWrite(led4, HIGH);
        digitalWrite(led5, HIGH);
        digitalWrite(led6, HIGH);
        digitalWrite(led7, LOW);
           digitalWrite(led8, LOW);
        digitalWrite(led10, LOW);
        digitalWrite(led11, LOW);
        digitalWrite(led12, LOW);
        digitalWrite(led13, LOW);
       
  }
  if (average > 424 ){ //20-30 meters
 // Serial.write(5);
   digitalWrite(led3, HIGH);
       digitalWrite(led4, HIGH);
        digitalWrite(led5, LOW);
        digitalWrite(led6, LOW);
        digitalWrite(led7, LOW);
         digitalWrite(led8, LOW);
        digitalWrite(led10, LOW);
        digitalWrite(led11, LOW);
        digitalWrite(led12, LOW);
        digitalWrite(led13, LOW);
     
    }
   
    //isolated instances below
   
  if (average == 24 ){ //if their is no signal
 // Serial.write(5);
   digitalWrite(led3, LOW);
       digitalWrite(led4, LOW);
        digitalWrite(led5, LOW);
        digitalWrite(led6, LOW);
        digitalWrite(led7, LOW);
         digitalWrite(led8, LOW);
        digitalWrite(led10, LOW);
        digitalWrite(led11, LOW);
        digitalWrite(led12, LOW);
        digitalWrite(led13, LOW);
}

 Serial.write('*');            //message start delimiter
    Serial.write(nodeID);
    Serial.write(average >> 8);    //most significant byte
    Serial.write(average & 0xFF);  //least significant byte
delay(200);
}
 
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
Serial.write('*');            //message start delimiter
    Serial.write(nodeID);
    Serial.write(average >> 8);    //most significant byte
    Serial.write(average & 0xFF);  //least significant byte
Send 4 bytes as fast as possible. Which isn't very fast.

Code:
    val_read = Serial.read();
    if (val_read == '*') {               //wait for start delimiter
        nodeID = Serial.read();
        average = Serial.read() << 8;    //msb
        average += Serial.read();        //lsb
Expect all the data to arrive nearly instantaneously. Nope. Not going to happen (sorry, Jack).

You need to, of you are going to read 5 bytes, wait for there to be 4 bytes to read. Serial.available() is a good idea.
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3942
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Expect all the data to arrive nearly instantaneously. Nope. Not going to happen (sorry, Jack).

Ummm yeah that'd be a good point, no apology necessary, Paul. That's what I get for writing untested vapor code on the fly and not thinking enough about it.

Actually, if the XBee's serial interface baud rate were slow enough, then the serial-to-RF data packetization might allow it to work. Not that that would be good design or anything smiley-red
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Pages: 1 [2]   Go Up
Jump to: