Tizen/Artik IoT Practice Part 1 IoT.js Application: Remote LED · Embedded Software Lab. @ SKKU 12...

12
Embedded Software Lab. @ SKKU 12 1 Sungkyunkwan University Tizen/Artik IoT Practice Part 1 IoT.js Application: Remote LED

Transcript of Tizen/Artik IoT Practice Part 1 IoT.js Application: Remote LED · Embedded Software Lab. @ SKKU 12...

Page 1: Tizen/Artik IoT Practice Part 1 IoT.js Application: Remote LED · Embedded Software Lab. @ SKKU 12 3 • fs:Support File System operations – fs.readFile(filename[, options], callback)

Embedded Software Lab. @ SKKU

12

1

Sungkyunkwan University

Tizen/Artik IoT Practice Part 1IoT.js Application: Remote LED

Page 2: Tizen/Artik IoT Practice Part 1 IoT.js Application: Remote LED · Embedded Software Lab. @ SKKU 12 3 • fs:Support File System operations – fs.readFile(filename[, options], callback)

Embedded Software Lab. @ SKKU

12

2Remote LED Example Overview

onIndex( )

onLight( )

onToggle( )

“on” or ”off”

Smart Phone

Artik board

LED

Page 3: Tizen/Artik IoT Practice Part 1 IoT.js Application: Remote LED · Embedded Software Lab. @ SKKU 12 3 • fs:Support File System operations – fs.readFile(filename[, options], callback)

Embedded Software Lab. @ SKKU

12

3

• fs: Support File System operations– fs.readFile(filename[, options], callback)

• Reads entire file asynchronously

• http: Support HTTP server and client enabling users to receive/send HTTP request easily– http.createServer([requestListener])

• Returns http.Server instance

Module (1/2)

* IoT.js API reference (modules)https://github.com/Samsung/iotjs/wiki/IoT.js%20API%20Reference

Page 4: Tizen/Artik IoT Practice Part 1 IoT.js Application: Remote LED · Embedded Software Lab. @ SKKU 12 3 • fs:Support File System operations – fs.readFile(filename[, options], callback)

Embedded Software Lab. @ SKKU

12

4

• gpio: Support GPIO control– gpio.initialize(callback)

• Initializes GPIO device. (This function must be called before other GPIO functions.)

– gpio.open(pinNumber, diriection[, mode][, callback])• Sets GPIO pin configuration.

– gpio.read(pinNumber[, callback])• Reads boolean value from a GPIO pin.

– gpio.write(pinNumber, value[, callback])• Writes out a boolean value to a GPIO pin.

Module (2/2)

Page 5: Tizen/Artik IoT Practice Part 1 IoT.js Application: Remote LED · Embedded Software Lab. @ SKKU 12 3 • fs:Support File System operations – fs.readFile(filename[, options], callback)

Embedded Software Lab. @ SKKU

12

5

1. Import modules required for the program.2. Predefine port number and LED’s pin number/mode.

Import Modules & Set Variables

server.js

Page 6: Tizen/Artik IoT Practice Part 1 IoT.js Application: Remote LED · Embedded Software Lab. @ SKKU 12 3 • fs:Support File System operations – fs.readFile(filename[, options], callback)

Embedded Software Lab. @ SKKU

12

6

1. Get a http.Server instance using createServer method.2. Pass a requestListener (Callback function) as a parameter of

http.createServer().– After using the createServer method, requestListener function is called when

server received request.

Create the Server Instance

The callback function calls the function such as onIndex(), onLight(), onToggle() according to the request’s url.

server.js

Page 7: Tizen/Artik IoT Practice Part 1 IoT.js Application: Remote LED · Embedded Software Lab. @ SKKU 12 3 • fs:Support File System operations – fs.readFile(filename[, options], callback)

Embedded Software Lab. @ SKKU

12

7

• onIndex()– Send contents of ‘index.html’ to the client.– In callback function, res object sets status code* and data (index.html),

and response process is terminated.

onIndex Function

* HTTP response status codehttps://en.wikipedia.org/wiki/List_of_HTTP_status_codes

- 200: OK- 500: Internal Server Error

server.js

Page 8: Tizen/Artik IoT Practice Part 1 IoT.js Application: Remote LED · Embedded Software Lab. @ SKKU 12 3 • fs:Support File System operations – fs.readFile(filename[, options], callback)

Embedded Software Lab. @ SKKU

12

8

• onLight()– Read the LED’s value and respond “on” or “off” message to the client.

onLight Function

server.js

Page 9: Tizen/Artik IoT Practice Part 1 IoT.js Application: Remote LED · Embedded Software Lab. @ SKKU 12 3 • fs:Support File System operations – fs.readFile(filename[, options], callback)

Embedded Software Lab. @ SKKU

12

9

• onToggle()– Reverse the LED’s value (on ↔ off).

onToggle Function

server.js

Page 10: Tizen/Artik IoT Practice Part 1 IoT.js Application: Remote LED · Embedded Software Lab. @ SKKU 12 3 • fs:Support File System operations – fs.readFile(filename[, options], callback)

Embedded Software Lab. @ SKKU

12

10

• gpio.initialize()– Initialize GPIO devices before using other GPIO functions.

• gpio.on()– If GPIO events value and first parameter are same, call the callback function.– If gpio.initialize() is successful, gpio.open() sets the LED pin configuration,

and server waits for the request using server.listen().

GPIO Initialize

server.js

Page 11: Tizen/Artik IoT Practice Part 1 IoT.js Application: Remote LED · Embedded Software Lab. @ SKKU 12 3 • fs:Support File System operations – fs.readFile(filename[, options], callback)

Embedded Software Lab. @ SKKU

12

11

• Execute server.js using iotjs.# ./build/arm-linux/release/bin/iotjs server.js

• LED status is changed and log is printed when toggle button isclicked in the client side.

Testing

Page 12: Tizen/Artik IoT Practice Part 1 IoT.js Application: Remote LED · Embedded Software Lab. @ SKKU 12 3 • fs:Support File System operations – fs.readFile(filename[, options], callback)

Embedded Software Lab. @ SKKU

12

12How To Identify the GPIO Pin Number

* GPIO Export Numbers (See Tables)https://developer.artik.io/documentation/developer-guide/gpio/kernel-gpio.html#gpio-export-numbers

GPIO pin number is listed in the tables of below webpage.

For example, GPIO pin number of J27 pin 8 can be found as shown in the figure.