Dear Friends,
In this opportunity despite of the "xbee pro s2b" (XBP24B7PIT) modules can communicate each other, the Rx value (0 - 1023) is different from the Tx analog value, additionally, the Rx value does not change although the Tx value changes.
Both modules are configured in API 2 (one as coordinator and the other as end device), have the same PAN ID, and so forth.
In order to reach this purpose I am using arduino boards UNO R3, xbee shields of DFRobot v1.4, and a Seedstudio relay shield.
The main purpose of the sketch is to control a relay if the analog value is in between a definite range, but by the moment I am trying to visualize the Rx value in the monitor to confirm the that Tx and Rx will be the same.
Sincerely I appreciate very much your help.
Thanks in advance.
Here the codes:
#include <XBee.h>
#include <Wire.h>
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle
ZBRxResponse rx = ZBRxResponse();
ModemStatusResponse msr = ModemStatusResponse();
int statusLed = 11;
int errorLed = 12;
int dataLed = 13;
int sensorValue = 0; // value read from the rx
void flashLed(int pin, int times, int wait) {
for (int i = 0; i < times; i++) {
digitalWrite(pin, HIGH);
delay(wait);
digitalWrite(pin, LOW);
if (i + 1 < times) {
delay(wait);
}
}
}
void setup() {
pinMode(statusLed, OUTPUT);
pinMode(errorLed, OUTPUT);
pinMode(dataLed, OUTPUT);
// start serial
Serial.begin(9600);
xbee.begin(Serial);
flashLed(statusLed, 3, 50);
}
// continuously reads packets, looking for ZB Receive or Modem Status
void loop() {
xbee.readPacket();
if (xbee.getResponse().isAvailable()) {
// got something
if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
// got a zb rx packet
// now fill our zb rx class
xbee.getResponse().getZBRxResponse(rx);
if (rx.getOption() == ZB_PACKET_ACKNOWLEDGED) {
// the sender got an ACK
flashLed(statusLed, 10, 10);
} else {
// we got it (obviously) but sender didn't get an ACK
flashLed(errorLed, 2, 20);
}
} 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
flashLed(statusLed, 10, 10);
} else if (msr.getStatus() == DISASSOCIATED) {
// this is awful.. flash led to show our discontent
flashLed(errorLed, 10, 10);
} else {
// another status
flashLed(statusLed, 5, 10);
}
} else {
// not something we were expecting
flashLed(errorLed, 1, 25);
}
} else if (xbee.getResponse().isError()) {
//nss.print("Error reading packet. Error code: ");
//nss.println(xbee.getResponse().getErrorCode());
}
// read the analog in value:
sensorValue = analogRead(rx.getData(0));
// print the results to the serial monitor:
Serial.print("sensor = ");
Serial.print(sensorValue);
// wait 50 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(500);
}
#include <avr/sleep.h>
#include <Wire.h>
#include "Sodaq_DS3231.h"
#include <XBee.h>
/*
Sends a ZB TX request with the value of analogRead(pin0) and checks the status response for success
*/
// create the XBee object
XBee xbee = XBee();
uint8_t payload[] = { 0, 0 };
// SH + SL Address of receiving XBee
XBeeAddress64 addr64 = XBeeAddress64(0x0013A200, 0x4098791E);
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
ZBTxStatusResponse txStatus = ZBTxStatusResponse();
int statusLed = 13;
int errorLed = 12;
void flashLed(int pin, int times, int wait) {
for (int i = 0; i < times; i++) {
digitalWrite(pin, HIGH);
delay(wait);
digitalWrite(pin, LOW);
if (i + 1 < times) {
delay(wait);
}
}
}
char weekDay[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
const int inputPin = 2;
const int analogInPin = A0; // Analog input pin that the sensor is attached to
int sensorValue = 0; // value read from the sensor
int outputValue = 0; // value output to the sensor (analog out)
const int powerPin = 8; // the number of the power pin
const uint8_t horaInicio = 9; // set the starting hour
const uint8_t horaFin = 18; // set the finish hour
volatile boolean flag;
void setup()
{
pinMode(statusLed, OUTPUT);
pinMode(errorLed, OUTPUT);
xbee.setSerial(Serial);
pinMode (inputPin, INPUT_PULLUP);
goToSleep();
Serial.begin(9600);
Wire.begin();
rtc.begin();
pinMode(powerPin, OUTPUT);
}
void loop ()
{
if (flag)
{
doSomething();
flag = false;
goToSleep();
powerSensor();
}
}
void setFlag ()
{
flag = true;
}
void goToSleep ()
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
attachInterrupt(0, setFlag, LOW); // pin D2
sleep_mode(); //sleep now
//now sleep until LOW interrupt
sleep_disable();
detachInterrupt(0);
}
void powerSensor ()
{
DateTime now = rtc.now(); //get the current date-time
if ((horaInicio <= now.hour()) && (now.hour() <= horaFin))
{
//power on
digitalWrite (powerPin, HIGH);
}
else
{
//power off
digitalWrite (powerPin, LOW);
}
}
void doSomething()
{
DateTime now = rtc.now(); //get the current date-time
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.date(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print(' ');
Serial.print(weekDay[now.dayOfWeek()]);
Serial.println();
delay(1000);
// read the analog in value:
sensorValue = analogRead(analogInPin);
// change the analog out value:
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
delay (50);
// print the results to the serial monitor:
Serial.print("sensor = ");
Serial.print(sensorValue);
// wait 5 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(50);
// break down 10-bit reading into two bytes and place in payload
payload[0] = sensorValue >> 8 & 0xff;
payload[1] = sensorValue & 0xff;
xbee.send(zbTx);
// flash TX indicator
flashLed(statusLed, 1, 10);
// 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
flashLed(statusLed, 5, 50);
} else {
// the remote XBee did not receive our packet. is it powered on?
flashLed(errorLed, 3, 50);
}
}
} else if (xbee.getResponse().isError()) {
//nss.print("Error reading packet. Error code: ");
//nss.println(xbee.getResponse().getErrorCode());
} else {
// local XBee did not provide a timely TX Status Response -- should not happen
flashLed(errorLed, 2, 50);
}
delay(500);
}
sketch_sensor7.1.ino (4.19 KB)
Series2_Rx_1.1.ino (2.63 KB)