Presentation is loading. Please wait.

Presentation is loading. Please wait.

Architectures and Applications for Wireless Sensor Networks (01204525) Sensor Node Programming II (UART and Radio) Chaiporn Jaikaeo

Similar presentations


Presentation on theme: "Architectures and Applications for Wireless Sensor Networks (01204525) Sensor Node Programming II (UART and Radio) Chaiporn Jaikaeo"— Presentation transcript:

1 Architectures and Applications for Wireless Sensor Networks (01204525) Sensor Node Programming II (UART and Radio) Chaiporn Jaikaeo chaiporn.j@ku.ac.th Department of Computer Engineering Kasetsart University

2 2 Outline UART communication UART communication Single-hop radio communication Single-hop radio communication

3 3 Connecting Mote to PC Via USB dongle Via USB dongle

4 4 UART API ( motelib/uart.h ) Enable UART (both TX and RX) Enable UART (both TX and RX) Send a single byte over UART Send a single byte over UART Send multiple bytes over UART Send multiple bytes over UART uartEnable(true,true); uint8_t buf[10]; : uartWrite(buf, 10); uartWriteByte(50);

5 5 UART API (cont'd) Check whether there is any data received from UART Check whether there is any data received from UART Read a single byte from UART input buffer Read a single byte from UART input buffer Send formatted string over UART using Standard I/O library Send formatted string over UART using Standard I/O library uint8_t byte = uartReadByte(); #include : printf("Hello, world\n"); printf("i = %d\n", i); if (uartInputLen() > 0)...

6 6 Processing UART Data on PC Locate UART device file Locate UART device file  Run dmesg to find out  Usually /dev/ttyUSB0 or /dev/ttyACM0 For textual data For textual data  Run terminal program such as screen, gtk-term, putty on UART device For binary data For binary data  Any UART library can be used  E.g., Python's serial package $ dmesg : [70063.712091] usb 4-1: new low speed USB device using uhci_hcd and.. [70063.871042] usb 4-1: config 1 interface 1 altsetting.. [70063.871056] usb 4-1: config 1 interface 1 altsetting.. [70063.895220] cdc_acm 4-1:1.0: ttyACM0: USB ACM device

7 7 USB-UART Implementation MoteLib provides UART-via-USB implementation (>= rev. 388) MoteLib provides UART-via-USB implementation (>= rev. 388)  Emulated using V-USB library by Objective Development  Based on AVR-CDC project (http://www.recursion.jp/avrcdc/) http://www.recursion.jp/avrcdc/ Requires no USB dongle Requires no USB dongle Build your app with UART_VIA_USB=1 Build your app with UART_VIA_USB=1 $ make UART_VIA_USB=1...

8 8 Example: sense-to-uart.c Sense light intensity every second; send values to display via UART Sense light intensity every second; send values to display via UART #include Timer t; void senseDone(uint16_t value); void sense(Timer *t); void boot() { uartEnable(true,true); timerCreate(&t); timerStart(&t, TIMER_PERIODIC, 1000, sense); } void senseDone(uint16_t value) { printf("Light = %d\n", value); actorSetState(ACTOR_0, 0); } void sense(Timer *t) { actorSetState(ACTOR_0, 1); sensorRequestAnalog(SENSOR_1, senseDone); }

9 9 Testing sense-to-uart Build with UART_VIA_USB option Build with UART_VIA_USB option Capture UART output using screen (or gtk-term or putty) Capture UART output using screen (or gtk-term or putty) $ make UART_VIA_USB=1 flash $ screen /dev/ttyACM0

10 10 Radio Communication Microchip's MRF24J40 controller Microchip's MRF24J40 controller IEEE 802.15.4 @ 2.4 GHz, 250 kbps IEEE 802.15.4 @ 2.4 GHz, 250 kbps 16 non-overlapping channels 16 non-overlapping channels 16-bit node ID 16-bit node ID 16-bit PAN (Personal Area Network) ID 16-bit PAN (Personal Area Network) ID

11 11 Configuring Radio Use config-mote.py script located in $MOTELIB_DIR/platforms/iwing- mrf/tools Use config-mote.py script located in $MOTELIB_DIR/platforms/iwing- mrf/tools Have mote enter bootloader, then run: Have mote enter bootloader, then run: E.g., set address to 234 and PAN ID to 555 E.g., set address to 234 and PAN ID to 555 Set channel to 0x1A Set channel to 0x1A $ cd $MOTELIB_DIR/platforms/iwing-mrf/tools $./config-mote.py $./config-mote.py --address 234 --panid 555 $./config-mote.py --channel 0x1A

12 12 Radio Message Format Type and application data are provided by the application via Radio API Type and application data are provided by the application via Radio API Type (1 byte) 802.15.4 Header Seq No. (1 byte) App Data (max ~100 bytes)

13 13 Radio API ( motelib/radio.h ) Broadcast a message (type=7) containing "HELLO" to all neighbors Broadcast a message (type=7) containing "HELLO" to all neighbors  Call txDone() when message has been sent  Use NULL when callback is not needed radioRequestTx(BROADCAST_ADDR, 7, "HELLO", 5, txDone); : void txDone(RadioStatus status) { : } radioRequestTx(BROADCAST_ADDR, 7, "HELLO", 5, NULL);

14 14 Radio API (cont'd) Set a handler to process received messages Set a handler to process received messages radioSetRxHandler(receive); : void receive(Address src, MessageType type, void *msg, uint8_t len) { : }

15 15 Example: sense-to-base.c Every second, each sensor node measures light intensity and reports to the base station Every second, each sensor node measures light intensity and reports to the base station  Assume base station has address = 0 Base station reports light measurements over UART Base station reports light measurements over UART Base station Sensor nodes measuring light intensity Sensor node

16 16 sense-to-base.c void sense(Timer *t) { actorSetState(ACTOR_0, 1); sensorRequestAnalog( SENSOR_1, senseDone); } void senseDone(uint16_t value) { radioRequestTx(0, 1, &value, sizeof(value), NULL); actorSetState(ACTOR_0, 0); } #include Timer t; void sense(Timer *t); void senseDone(uint16_t value); void boot() { timerCreate(&t); timerStart( &t, TIMER_PERIODIC, 1000, sense); }

17 17 base.c #include void receive(Address src, MessageType type, void *msg, uint8_t len) { if (type == 1) { printf("Node %d: Light = %d\n", src, *((uint16_t*)msg)); } void boot() { uartEnable(true,true); radioSetRxHandler(receive); }

18 18 Exercise: Voting Machine Create an application for wireless voting machine Create an application for wireless voting machine  Allow user to cast a vote using the USER button  Voting choices are: Red (1), Yellow (2), Green (3), or No Vote (0)  When the USER button is pressed,  Set LED status accordingly  Report current vote to the base station (#1) with message type 50


Download ppt "Architectures and Applications for Wireless Sensor Networks (01204525) Sensor Node Programming II (UART and Radio) Chaiporn Jaikaeo"

Similar presentations


Ads by Google