Tizen/Artik IoT Practice Part 1 IoT.js Application: Remote LED · Embedded Software Lab. @ SKKU 12...
Transcript of Tizen/Artik IoT Practice Part 1 IoT.js Application: Remote LED · Embedded Software Lab. @ SKKU 12...
Embedded Software Lab. @ SKKU
12
1
Sungkyunkwan University
Tizen/Artik IoT Practice Part 1IoT.js Application: Remote LED
Embedded Software Lab. @ SKKU
12
2Remote LED Example Overview
onIndex( )
onLight( )
onToggle( )
“on” or ”off”
Smart Phone
Artik board
LED
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
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)
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
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
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
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
Embedded Software Lab. @ SKKU
12
9
• onToggle()– Reverse the LED’s value (on ↔ off).
onToggle Function
server.js
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
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
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.