Saturday, November 26, 2011

Testing the XBee on Wise Clock 4

To test the XBee on Wise Clock 4 I used the sketch below (upload it to the board; display is not required).

int nCount = 0;
void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.println("WC4 XBee ready for comm..."); 
}


void loop()
{
  // read from port 1, report characters on port 0:
  while (Serial1.available())
  {
    char inChar = Serial1.read();
    Serial.print(inChar); 
  }
  Serial1.print("WC4 XBee transmit ");
  Serial1.print(nCount++);
  Serial1.print(", ");
  Serial1.println(millis());
  delay(3000);
}

Notice the use of Serial1 instance to talk to the XBee, since this is connected to the second hardware serial port of the ATmega644P (D10/D11, pins 16/17). Had Sanguino offered no support for the second USART, we could have used NewSoftwareSerial library.

The second XBee is linked directly to a terminal (e.g. HyperTerminal in Windows, or Terminal panel in the X-CTU application from Digi). I personally used the XBee Adapter from Adafruit, connected through the FTDI cable to the PC as shown here.

Once this XBee test passed, you can adapt the Wise Clock 4 software to display any message sent to it from a second XBee. A simple hack is to add to the function fetchMessage() in file WiseClock4.cpp, as shown below:

void WiseClock4::fetchMessage()
{
  // new code for XBee............
  // read from XBee into the "personalized message" buffer;
  char* Ptr1 = &personalMsg[0];
  if (Serial1.available())
  {
    while (Serial1.available())
    {
      *Ptr1++ = Serial1.read();
    }
    *Ptr1 = 0;
  }

  // existing code................
  strcpy(msgLine, "      ");

Now, instead of the personalized message (read from file message.txt on the SD card) you will get whatever is sent from the terminal application (second XBee).

Don't forget to add this line in function setup (file WiseClock4.pde):

  Serial1.begin(9600);


No comments:

Post a Comment