Development - aliconnect/aliconnect.sdk GitHub Wiki

Voor het bouwen van een website op de PI met node hebben we express. Deze installeren we m.b.v. npm.

npm install express

Hierna kunnen we een klein website programma maken met hello world. Maak een bestand aan app.js en zet hierin volgende tekst.

var express = require('express');
var app = express();
app.get('/', function (req, res) {
       res.send('Hello World!');
});
app.listen(3000, function () {
       console.log('Example app listening on port 3000!');
});

Start de app door invoeren op de prompt

node app

Ga naar je PC en start een browser. Type hier in de addressbalk

http://192.168.1.132

In het scherm verschijnt Hello World!

Aansturen van IO

IO Aansturen vanuit je PI Pin layout van de PI Voor het aansturen van IO bekijken we eerste de pin layout van de J8 aansluiting.

Type

npm install onoff

Schakel de PI uit voordat je de spanning weghaalt

sudo shutdown -h now

Aansturen van een uitgang op de PI Verbind een LED X8-7 GPIO4 -

Enzovoort

Maak blink.js

var Gpio = require('onoff').Gpio; //include onoff to interact with the GPIO var LED = new Gpio(4, 'out'); //use GPIO pin 4, and specify that it is output var blinkInterval = setInterval(blinkLED, 250); //run the blinkLED function every 250ms

function blinkLED() { //function to start blinking if (LED.readSync() === 0) { //check the pin state, if the state is 0 (or off) LED.writeSync(1); //set pin state to 1 (turn LED on) } else { LED.writeSync(0); //set pin state to 0 (turn LED off) } }

function endBlink() { //function to stop blinking clearInterval(blinkInterval); // Stop blink intervals LED.writeSync(0); // Turn LED off LED.unexport(); // Unexport GPIO to free resources }

setTimeout(endBlink, 5000); //stop blinking after 5 seconds?

Inlezen van een ingang op de PI var Gpio = require('onoff').Gpio; //include onoff to interact with the GPIO var LED = new Gpio(4, 'out'); //use GPIO pin 4 as output var pushButton = new Gpio(17, 'in', 'both'); //use GPIO pin 17 as input, and 'both' button presses, and releases should be handled

pushButton.watch(function (err, value) { //Watch for hardware interrupts on pushButton GPIO, specify callback function if (err) { //if an error console.error('There was an error', err); //output error message to console return; } LED.writeSync(value); //turn LED on or off depending on the button state (0 or 1) });

function unexportOnClose() { //function to run when exiting program LED.writeSync(0); // Turn LED off LED.unexport(); // Unexport LED GPIO to free resources pushButton.unexport(); // Unexport Button GPIO to free resources };

process.on('SIGINT', unexportOnClose); //function to run when user closes using ctrl+c

⚠️ **GitHub.com Fallback** ⚠️