18
Architectures and Architectures and Applications for Wireless Applications for Wireless Sensor Networks (01204525) Sensor Networks (01204525) Sensor Node Programming II Sensor Node Programming II (UART and Radio) (UART and Radio) Chaiporn Jaikaeo [email protected] Department of Computer Engineering Kasetsart University

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

Embed Size (px)

Citation preview

Architectures and Applications for Architectures and Applications for Wireless Sensor Networks Wireless Sensor Networks

(01204525)(01204525)

Sensor Node Programming IISensor Node Programming II(UART and Radio)(UART and Radio)

Chaiporn [email protected]

Department of Computer EngineeringKasetsart University

2

OutlineOutline UART communicationUART communication Single-hop radio communicationSingle-hop radio communication

3

Connecting Mote to PCConnecting Mote to PC Via USB dongleVia USB dongle

4

UART API (UART API (motelib/uart.hmotelib/uart.h))

Enable UART (both TX and RX)Enable UART (both TX and RX)

Send a single byte over UARTSend a single byte over UART

Send multiple bytes over UARTSend multiple bytes over UART

uartEnable(true,true);

uint8_t buf[10];:uartWrite(buf, 10);

uartWriteByte(50);

5

UART API (cont'd)UART API (cont'd) Check whether there is any data Check whether there is any data

received from UARTreceived from UART

Read a single byte from UART input Read a single byte from UART input bufferbuffer

Send formatted string over UART Send formatted string over UART using Standard I/O libraryusing Standard I/O library

uint8_t byte = uartReadByte();

#include <stdio.h>::printf("Hello, world\n");printf("i = %d\n", i);

if (uartInputLen() > 0) ...

6

Processing UART Data on Processing UART Data on PCPC Locate UART device fileLocate UART device file

Run Run dmesgdmesg to find out to find out Usually Usually /dev/ttyUSB0 /dev/ttyUSB0 oror /dev/ttyACM0 /dev/ttyACM0

For textual dataFor textual data Run terminal program such as Run terminal program such as screenscreen, ,

gtk-termgtk-term, , puttyputty on UART device on UART device For binary dataFor binary data

Any UART library can be usedAny UART library can be used E.g., Python's E.g., Python's serialserial package 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

USB-UART ImplementationUSB-UART Implementation MoteLib provides UART-via-USB MoteLib provides UART-via-USB

implementation (>= rev. 388)implementation (>= rev. 388) Emulated using V-USB library by Emulated using V-USB library by

Objective DevelopmentObjective Development Based on AVR-CDC project (Based on AVR-CDC project (

http://www.recursion.jp/avrcdc/http://www.recursion.jp/avrcdc/) ) Requires no USB dongleRequires no USB dongle Build your app with Build your app with UART_VIA_USB=1UART_VIA_USB=1

$ make UART_VIA_USB=1 ...

8

Example: Example: sense-to-uart.csense-to-uart.c Sense light intensity every second; Sense light intensity every second;

send values to display via UARTsend values to display via UART#include <stdio.h>#include <motelib/system.h>#include <motelib/timer.h>#include <motelib/uart.h>#include <motelib/sensor.h>#include <motelib/actor.h>

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

Testing Testing sense-to-uartsense-to-uart Build with UART_VIA_USB optionBuild with UART_VIA_USB option

Capture UART output using Capture UART output using screenscreen (or (or gtk-termgtk-term or or puttyputty))

$ make UART_VIA_USB=1 flash

$ screen /dev/ttyACM0

10

Radio CommunicationRadio Communication Microchip's MRF24J40 controllerMicrochip's MRF24J40 controller IEEE 802.15.4 @ 2.4 GHz, 250 kbpsIEEE 802.15.4 @ 2.4 GHz, 250 kbps 16 non-overlapping channels16 non-overlapping channels 16-bit node ID16-bit node ID 16-bit PAN (16-bit PAN (PPersonal ersonal AArea rea NNetwork) IDetwork) ID

11

Configuring RadioConfiguring Radio Use Use config-mote.pyconfig-mote.py script located script located

in in $MOTELIB_DIR/platforms/iwing-$MOTELIB_DIR/platforms/iwing-mrf/toolsmrf/tools

Have mote enter bootloader, then Have mote enter bootloader, then run:run:

E.g., set address to 234 and PAN ID E.g., set address to 234 and PAN ID to 555to 555

Set channel to 0x1ASet 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

Radio Message FormatRadio Message Format

Type and application data are Type and application data are provided by the application via Radio provided by the application via Radio APIAPI

TypeType(1 byte)(1 byte)802.15.4 Header802.15.4 Header Seq No.Seq No.

(1 byte)(1 byte)App DataApp Data

(max ~100 bytes)(max ~100 bytes)

13

Radio API Radio API ((motelib/radio.hmotelib/radio.h)) Broadcast a message (type=7) Broadcast a message (type=7)

containing "HELLO" to all neighborscontaining "HELLO" to all neighbors Call txDone() when message has been Call txDone() when message has been

sentsent

Use NULL when callback is not neededUse 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

Radio API (cont'd)Radio API (cont'd) Set a handler to process received Set a handler to process received

messagesmessages

radioSetRxHandler(receive);:void receive(Address src, MessageType type, void *msg, uint8_t len){ :}

15

Example: Example: sense-to-base.csense-to-base.c Every second, each sensor node measures Every second, each sensor node measures

light intensity and reports to the base light intensity and reports to the base stationstation Assume base station has address = 0Assume base station has address = 0

Base station reports light measurements Base station reports light measurements over UARTover UART

Base stationBase station

Sensor nodes measuring light intensitySensor nodes measuring light intensity

Sensor nodeSensor node

16

sense-to-base.csense-to-base.cvoid 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 <motelib/system.h>#include <motelib/timer.h>#include <motelib/radio.h>#include <motelib/sensor.h>#include <motelib/actor.h>

Timer t;void sense(Timer *t);void senseDone(uint16_t value);

void boot(){ timerCreate(&t); timerStart( &t, TIMER_PERIODIC, 1000, sense);}

17

base.cbase.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: Exercise: Voting MachineVoting Machine Create an application for wireless Create an application for wireless

voting machinevoting machine Allow user to cast a vote using the USER Allow user to cast a vote using the USER

buttonbutton Voting choices are: Voting choices are: Red (1)Red (1), , Yellow (2)Yellow (2), ,

Green (3)Green (3), or No Vote (0), or No Vote (0) When the USER button is pressed, When the USER button is pressed,

Set LED status accordinglySet LED status accordingly Report current vote to the base station (#1) Report current vote to the base station (#1)

with message type 50with message type 50