ZB1 XBee Transmitter
This program configures an XBee radio via the serial port,
reads the voltage value on analog pin 0 (PC0) and transmits
an ASCII string corresponding to the measured voltage.
This program is adapted from the "XBee Analog Duplex Transmission" examples
in "Making Things Talk", O'Reilly, First Edition, Sep-2007. See pages
193-206.
This program transmits data to be displayed by the ZB1
XBee receiver program
ZB1 XBee Transmitter
/*
Adapted from the "XBee Analog Duplex Transmission" example in "Making
Things Talk", O'Reilly, First Edition, Sep-2007. See pages 193-206.
This sketch configures an XBee radio via the serial port,
reads the voltage value on analog pin 0 (PC0) and transmits
an ASCII string corresponding to the measured voltage.
(* jcl *)
*/
#define SENSOR_PIN 0 // input sensor
#define THRESHOLD 10 // how much change you need to see on
// the sensor before sending
#define STATUS_LED 7 // ZB1 (PD7)
int lastSensorReading = 0; // previous state of the switch
void setup() {
// configure serial communications:
// Compensating for the 12MHz XTAL
// 12800 = (16/12) * 9600
Serial.begin(12800);
// configure LED output pin
pinMode(STATUS_LED, OUTPUT);
blink(3);
set_xbee_destination_address();
blink(3);
}
void set_xbee_destination_address() {
long count = 0;
// put the radio in command mode:
Serial.print("+++");
// wait for the radio to respond with "OK\r"
char thisByte = 0;
while (thisByte != '\r') {
if (Serial.available() > 0) {
thisByte = Serial.read();
}
if (count++ > 100000) break;
}
if (count <= 100000) {
// set the destination address, using 16-bit addressing.
// if you're using two radios, one radio's destination
// should be the other radio's MY address, and vice versa:
Serial.print("ATDH0, DL5678\r");
// set my address using 16-bit addressing:
Serial.print("ATMY1234\r");
// set the PAN ID. If you're working in a place where many people
// are using XBees, you should set your own PAN ID distinct
// from other projects.
Serial.print("ATID1111\r");
// put the radio in data mode:
Serial.print("ATCN\r");
} else {
blink(5);
}
}
void _blink(int t) {
digitalWrite(STATUS_LED, HIGH);
delay(t);
digitalWrite(STATUS_LED, LOW);
delay(t);
}
void blink(int n) {
for (int i = 0; i < n; i++) {
_blink(50);
}
delay(200);
}
void loop() {
// listen for incoming serial data:
// listen to the potentiometer:
char sensorValue = readSensor();
// if there's something to send, send it:
if (sensorValue > 0) {
Serial.print(sensorValue, DEC );
Serial.print("\r");
}
}
char readSensor() {
char message = 0;
// read the sensor:
int sensorReading = analogRead(SENSOR_PIN);
// look for a change from the last reading
// that's greater than the threshold:
if (abs(sensorReading - lastSensorReading) > THRESHOLD) {
message = sensorReading/4;
lastSensorReading = sensorReading;
_blink(20);
}
return message;
}