<< Chapter < Page Chapter >> Page >
Now that you can send packets between nodes, you will put content into those packets. Data is entered through the keyboardand sent over the air when pressing enter. The receiver prints the received data on the screen. You are thus building a wireless chat program.
Because you want to type to and read from both motes, you need to have two USB programmers.

Running the code

  • Copy the code presented below Alternatively, this code is available in the downloadable source code . Open source_code/iar_v4.11/lab_ezwsn.eww with IAR. The project corresponding to this section is called txrx_chat . .
  • Compile and download the code onto both boards (Ctrl+D) .
  • Connect each board to a computer using the USB programmer and open PuTTY. Type something and hit enter, the message should appear on the other side's screen.
#include "radios/family1/mrfi_spi.h" #include "mrfi.h"uint8_t index_output = 9; mrfiPacket_t packetToSend;int main(void) {BSP_Init(); MRFI_Init();P3SEL |= 0x30; // P3.4,5 = USCI_A0 TXD/RXD UCA0CTL1 = UCSSEL_2; // SMCLKUCA0BR0 = 0x41; // 9600 from 8Mhz UCA0BR1 = 0x3;UCA0MCTL = UCBRS_2; UCA0CTL1&= ~UCSWRST; // Initialize USCI state machine IE2 |= UCA0RXIE; // Enable USCI_A0 RX interruptMRFI_WakeUp(); MRFI_RxOn();index_output=0; __bis_SR_register(GIE+LPM4_bits);} void MRFI_RxCompleteISR(){ uint8_t i;P1OUT ^= 0x02; mrfiPacket_t packet;MRFI_Receive(&packet); char output[]= {" "}; for (i=9;i<29;i++) { output[i-9]=packet.frame[i];if (packet.frame[i]=='\r') {output[i-9]='\n';output[i-8]='\r';} }TXString(output, (sizeof output)); }#pragma vector=USCIAB0RX_VECTOR __interrupt void USCI0RX_ISR(void){ char rx = UCA0RXBUF;uint8_t i; packetToSend.frame[index_output]=rx; index_output++;if (rx=='\r' || index_output==29) { packetToSend.frame[0]=28; MRFI_Transmit(&packetToSend, MRFI_TX_TYPE_FORCED); index_output = 9;for(i=9;i<29;i++) { packetToSend.frame[i]=' '; }P1OUT ^= 0x01; }P1OUT ^= 0x02; TXString(&rx, 1); }

Some keys for understanding the code:

  • Lines 9-15 configure the UART module used to communicate over the serial port. In particular, line 17 enables interrupts for incoming traffic, i.e. when youwrite onto PuTTY.
  • Lines 21-36. Function MRFI_RxCompleteISR is called when a packet is received. The red LED is switched on (Line 26), and an empty output string ismodified with the received characters before being sent.
  • Lines 37-55. Function USCI0RX_ISR is called when you enter a character on PuTTY. This 8-bit character is stored at the right byte in the outgoing packet(line 45). When you hit enter or you have type 29 consecutive characters (44), the frame is sent and the output buffer is initialized for subsequent text.

If you have multiple motes and multiple computers, all messages typed on one computer will appear on all screen. To reduce the broadcast group, you may wish to switch to another frequency channel using the code described in 4.2. Simple Tx/Rx .

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Ezwsn: experimenting with wireless sensor networks using the ez430-rf2500. OpenStax CNX. Apr 26, 2009 Download for free at http://cnx.org/content/col10684/1.10
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Ezwsn: experimenting with wireless sensor networks using the ez430-rf2500' conversation and receive update notifications?

Ask