Network Kernel Architectures and Implementation (01204423) Sensor Node Programming II (UART and Radio) Chaiporn Jaikaeo chaiporn.j@ku.ac.th Department of Computer Engineering Kasetsart University
Outline UART communication Single-hop radio communication 2
Connecting Mote to PC USB connector on IWING-MRF and IWING-JN 3.3V RX TX GND USB dongles 3.3V 5V RX TX GND USB Dongle based on Silicon Labs’s CP2102 ThaiEasyElec’s USB Dongle (FDTI) 3
UA UART RT API ( motelib/uart.h ) Enable UART (both TX and RX) uartEnable(true,true); Send a single byte over UART uartWriteByte(50); Send multiple bytes over UART uint8_t buf[10]; : uartWrite(buf, 10); 4
UART API (cont'd) Check whether there is any data received from UART if (uartInputLen() > 0) ... Read a single byte from UART input buffer uint8_t byte = uartReadByte(); Send formatted string over UART using Standard I/O library #include <stdio.h> : : printf("Hello, world\n"); printf("i = %d\n", i); 5
Processing UART Data on PC Locate UART device file Run dmesg to find out Usually /dev/ttyUSB0 or /dev/ttyACM0 $ 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 For textual data Run terminal program such as screen , gtk-term , putty on UART device For binary data Any UART library can be used E.g., Python's serial package 6
USB-UART Implementation MoteLib provides UART-via-USB implementation for IWING-MRF platform (>= rev. 388) Emulated using V-USB library by Objective Development Based on AVR-CDC project (http://www.recursion.jp/avrcdc/) Requires no USB dongle Build your app with UART_VIA_USB=1 $ make UART_VIA_USB=1 ... 7
Example: sense-to-uart.c Sense light intensity every second; send values to display via UART void senseDone( uint16_t value) #include <stdio.h> { #include <motelib/system.h> printf("Light = %d\r\n", value); #include <motelib/timer.h> actorSetState(ACTOR_0, 0); #include <motelib/uart.h> } #include <motelib/sensor.h> #include <motelib/actor.h> void sense(Timer *t) { Timer t; actorSetState(ACTOR_0, 1); void senseDone( uint16_t value); sensorRequestAnalog(SENSOR_1, senseDone); void sense(Timer *t); } void boot() { uartEnable(true,true); timerCreate(&t); timerStart(&t, TIMER_PERIODIC, 1000, sense); } 8
Testing sense-to-uart Build with UART_VIA_USB option $ make UART_VIA_USB=1 flash Capture UART output using screen (or gtk-term or putty) $ screen /dev/ttyACM0 9
Radio Communication IWING-MRF: Microchip's MRF24J40 controller IWING-JN: Built-in IEEE 802.15.4 @ 2.4 GHz, 250 kbps 16 non-overlapping channels 16-bit node ID 16-bit PAN (Personal Area Network) ID 10
Configuring Radio (IWING-MRF) Use config-mote.py script located in $MOTELIB_DIR/platforms/iwing- mrf/tools Have mote enter bootloader, then run: $ cd $MOTELIB_DIR/platforms/iwing-mrf/tools $ ./config-mote.py E.g., set address to 234 and PAN ID to 555 $ ./config-mote.py --address 234 --panid 555 Set channel to 0x1A $ ./config-mote.py --channel 0x1A 11
Configure Radio (IWING-JN) Set the following variables via make DEFAULT_ADDR DEFAULT_PANID DEFAULT_CHANNEL Example: $ make PLATFORM=iwing-jn DEFAULT_ADDR=50 12
Radio Message Format Type Seq No. App Data Check 802.15.4 Header (1 byte) (1 byte) (max ~100 bytes) sum Type and application data are provided by the application via Radio API 13
Radio API ( motelib/radio.h ) Broadcast a message (type=7) containing "HELLO" to all neighbors Call txDone() when message has been sent radioRequestTx(BROADCAST_ADDR, 7, "HELLO", 5, txDone); : void txDone(RadioStatus status) { : } Use NULL when callback is not needed radioRequestTx(BROADCAST_ADDR, 7, "HELLO", 5, NULL); 14
Radio API (cont'd) Set a handler to process received messages radioSetRxHandler(receive); : void receive(Address src, MessageType type, void *msg, uint8_t len) { : } 15
Example: sense-to-base.c 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 Sensor node Sensor nodes measuring light intensity 16
sense-to-base.c #include <motelib/system.h> void sense(Timer *t) #include <motelib/timer.h> { #include <motelib/radio.h> actorSetState(ACTOR_0, 1); #include <motelib/sensor.h> sensorRequestAnalog( #include <motelib/actor.h> SENSOR_1, senseDone); } Timer t; void sense(Timer *t); void senseDone( uint16_t value) void senseDone( uint16_t value); { radioRequestTx(0, 1, &value, void boot() sizeof (value), NULL); { actorSetState(ACTOR_0, 0); timerCreate(&t); } timerStart( &t, TIMER_PERIODIC, 1000, sense); } 17
base.c #include <stdio.h> #include <motelib/system.h> #include <motelib/radio.h> #include <motelib/uart.h> 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
Exercise: 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 19
Recommend
More recommend