Let’s keep going: Reporting onboard LED status to server.
Please ensure your have read Getting Started – Adding a Device before reading this section.
Step 11: Configuring Network for Development Board
Please ensure your commputer have already connnected to the hotspot for further operation.
- Visit console.ruff.io via your browser.(Visit 192.168.78.1 if domain name is not available).
- Fill in SSID and password for wireless network as the following figure. (Only 2.4G is supported)
Step 12: Writing an Application
Modify the index.js
file under src
folder and report onboard LED status to server with http
module. ;
var http = require('http');
var options = {
host: 'httpbin.org',
path: '/post',
method: 'POST',
headers: {
}
};
function postState(state) {
options.headers['Content-Length'] = state.length;
var req = http.request(options, function(res) {
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.write(state);
req.end();
}
$.ready(function (error) {
if (error) {
console.log(error);
return;
}
$('#button').on('push', function() {
console.log('Button pushed.');
$('#led-r').turnOn(function() {
postState('turn on');
});
});
$('#button').on('release', function() {
console.log('Button released.');
$('#led-r').turnOff(function() {
postState('turn off');
});
});
});
$.end(function () {
$('#led-r').turnOff();
});
httpbin used here is a online HTTP testing server.
Step 13: Deploying the Application
Run the following command to deploy and start your application:
rap deploy -s
Create a new command line window, change the directory to the application folder, and run the following command:
rap log
The console outputs the following content once the connection between
rap
and the development board is set up.Connection established.
Pushing the button, turns the red onboard LED on and the log will be
Button pushed.
BODY: {
"args": {},
"data": "turn on",
"files": {},
"form": {},
"headers": {
"Connection": "close",
"Content-Length": "7",
"Host": "httpbin.org"
},
"json": null,
"origin": "58.246.140.200",
"url": "http://httpbin.org/post"
}Releasing the button, turns the red onboard LED off and the log will be
Button released.
BODY: {
"args": {},
"data": "turn off",
"files": {},
"form": {},
"headers": {
"Connection": "close",
"Content-Length": "8",
"Host": "httpbin.org"
},
"json": null,
"origin": "58.246.140.200",
"url": "http://httpbin.org/post"
}
Next Step
Visit Ruff Developer Guide - Application Development to know more about Ruff application development.