Go Down

Topic: XBee Serial communication (Read 626 times) previous topic - next topic

maigo

I am just getting started with Xbee so any help would be appreciated. I can connect my devices and data is transmitted but it comes out garbled. See example below.


PaulS

Quote
I can connect my devices and data is transmitted but it comes out garbled.

What data is transmitted by what code?

maigo

I am playing with some different code I put together to see how Arduino works so its very messy. It monitors the ambient temperature, ground temperature, ambient light and soil moisture. See below.

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

// which analog pin to connect
#define THERMISTORPIN A1         
// resistance at 25 degrees C
#define THERMISTORNOMINAL 10000     
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25   
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 5
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3950
// the value of the 'other' resistor
#define SERIESRESISTOR 10000 

// which analog pin to connect
#define THERMISTORPIN2 A2         
// resistance at 25 degrees C
#define THERMISTORNOMINAL2 1000     
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL2 25   
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES2 5
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT2 3100
// the value of the 'other' resistor
#define SERIESRESISTOR2 1000   

#define voltageFlipPin1 6
#define voltageFlipPin2 5
#define sensorPin 3

int flipTimer = 1000;
int dis;

int samples[NUMSAMPLES];
int samples2[NUMSAMPLES2];
int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the analog resistor divider

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  Serial.begin(9600);
  analogReference(EXTERNAL);
  pinMode(voltageFlipPin1, OUTPUT);
  pinMode(voltageFlipPin2, OUTPUT);
  pinMode(sensorPin, INPUT);
 
}

void setSensorPolarity(boolean flip){
  if(flip){
    digitalWrite(voltageFlipPin1, HIGH);
    digitalWrite(voltageFlipPin2, LOW);
  }else{
    digitalWrite(voltageFlipPin1, LOW);
    digitalWrite(voltageFlipPin2, HIGH);
  }
}

void loop() {
 
  lcd.setCursor(0, 0);
 
  uint8_t i;
  float average2;
  float average;

// take N samples in a row, with a slight delay
  for (i=0; i< NUMSAMPLES2; i++) {
   samples2 = analogRead(THERMISTORPIN2);
   delay(10);
  }

  // average all the samples out
  average2 = 0;
  for (i=0; i< NUMSAMPLES2; i++) {
     average2 += samples2;
  }
  average2 /= NUMSAMPLES2;

  //Serial.print("Average analog reading2 ");
  //Serial.println(average2);

  // convert the value to resistance
  average2 = 1023 / average2 - 1;
  average2 = SERIESRESISTOR2 / average2;
  //Serial.print("Thermistor resistance2 ");
  //Serial.println(average2);

  float steinhart2;
  steinhart2 = average2 / THERMISTORNOMINAL2;     // (R/Ro)
  steinhart2 = log(steinhart2);                  // ln(R/Ro)
  steinhart2 /= BCOEFFICIENT2;                   // 1/B * ln(R/Ro)
  steinhart2 += 1.0 / (TEMPERATURENOMINAL2 + 273.15); // + (1/To)
  steinhart2 = 1.0 / steinhart2;                 // Invert
  steinhart2 -= 273.15;                         // convert to C

  lcd.print("T=");
  lcd.print(steinhart2);
  Serial.print("Ground Temperature=");
  Serial.print(steinhart2);
  Serial.print('\n');

  delay(1000);

  // take N samples in a row, with a slight delay
  for (i=0; i< NUMSAMPLES; i++) {
   samples = analogRead(THERMISTORPIN);
   delay(10);
  }

  // average all the samples out
  average = 0;
  for (i=0; i< NUMSAMPLES; i++) {
     average += samples;
  }
  average /= NUMSAMPLES;

  //Serial.print("Average analog reading ");
  //Serial.println(average);

  // convert the value to resistance
  average = 1023 / average - 1;
  average = SERIESRESISTOR / average;
  //Serial.print("Thermistor resistance ");
  //Serial.println(average);

  float steinhart;
  steinhart = average / THERMISTORNOMINAL;     // (R/Ro)
  steinhart = log(steinhart);                  // ln(R/Ro)
  steinhart /= BCOEFFICIENT;                   // 1/B * ln(R/Ro)
  steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
  steinhart = 1.0 / steinhart;                 // Invert
  steinhart -= 273.15;                         // convert to C

  lcd.print(" ET=");
  lcd.print(steinhart);
  Serial.print("External Temperature=");
  Serial.print(steinhart);
  Serial.print('\n');
  delay(1000);
 
 
lcd.setCursor(0, 1);

  photocellReading = analogRead(photocellPin); 
  Serial.print("Light=");
  Serial.print(photocellReading);
  Serial.print('\n');
  lcd.print("L=");
  lcd.print(photocellReading);     // the raw analog reading

  delay(1000);

//
  setSensorPolarity(true);
  delay(flipTimer);
  int val1 = analogRead(sensorPin);
  delay(flipTimer); 
  setSensorPolarity(false);
  delay(flipTimer);
  // invert the reading
  int val2 = 1023 - analogRead(sensorPin);
  //
  reportLevels(val1,val2);
   
}


void reportLevels(int val1,int val2){
 
  int avg = (val1 + val2) / 2;
 
  String msg = " M=";
  msg += avg;
 
dis=avg; //May need to change to analogRead

  Serial.print("Moisture");
  Serial.println(msg);
  //Serial.print('\n');
  lcd.print(msg);
if (dis < 850)
{

lcd.print("Dry");
Serial.print("Dry");
Serial.print('\n');
Serial.print('\n');
delay(1000);

}
if (dis > 850)
{

lcd.print("OK");
Serial.print("OK");
Serial.print('\n');
Serial.print('\n');

delay(1000);

}
}

PaulS

It's unlikely that your code really looks like that. Modify your post. Select the code. Select the # icon. Save the changes.

Explain something, please:
Code: [Select]
#define voltageFlipPin1 6
#define voltageFlipPin2 5

Similar names. Both get numbered.

Code: [Select]
#define NUMSAMPLES 5
#define NUMSAMPLES2 5

Similar names. Only one gets numbered. Why?

Code: [Select]
int samples[NUMSAMPLES];
int samples2[NUMSAMPLES2];

Similar names. Only one gets numbered. Why?

maigo

Actually no, my code does look like that. Like I said, I am using the code to see how things work with xbee as it works fine to lcd and computer connected serial but when I run it to the serial through xbee it comes out strange. Regarding the questions:

#define voltageFlipPin1 6
#define voltageFlipPin2 5

Voltage is flipped between two digital pins to avoid electrolosis. The measurement is taken against the anologe pin.

#define NUMSAMPLES 5
#define NUMSAMPLES2 5
and
int samples[NUMSAMPLES];
int samples2[NUMSAMPLES2];

I am using two different thermistors to measure the temperature, both with different ohms.

Like I said, any help is appreciated.

PaulS

Quote
Like I said, any help is appreciated.

Like I said, you need to post your code correctly.


PaulS

Quote
Don't get you?

Quote
Modify your post. Select the code. Select the # icon. Save the changes.

It ain't rocket surgery.

maigo

I would love to see someone do surgery on a rocket....

Go Up