Notes - aliconnect/aliconnect.sdk GitHub Wiki

Overnemen scherm Shut down PI form command window

sudo shutdown -h now

I2C IO Expansion with MCP23017 MCP23017-E/SP I/O Expander, 16bit, 1.7 MHz, I2C, Serial, 1.8 V, 5.5 V, DIPI2C bus, 16 In/Out, 2x Interupt

http://ww1.microchip.com/downloads/en/devicedoc/20001952c.pdf

Create Input

PI Pin Layout ?

Power voltage regulation L7805CV Linear Voltage Regulator, 7805, Fixed, Positive, 10V To 35V In, 5V And 1.5A Out, TO-220-3

OKI-78SR-5 https://nl.farnell.com/murata-power-solutions/oki-78sr-5-1-5-w36h-c/dc-dc-converter-5v-1-5a/dp/2812636

3,91

Strompi 2 Electronic Housing, For Raspberry Pi-BC, Lower Part, Upper Part, Cover & PCB Holder, Light Gray

27,49

node app

De weberver werkt nu op je PI

Use Browser special functions

To use browser special function teh website should by served over a secure connection with a SSL certificate. The special functions are.

  • Geolocation � requires secure origins as of M50
  • Device motion / orientation
  • EME
  • getUserMedia
  • AppCache
  • Notifications

For testng purposes the are seferal options

  • Secure the server with a publicly-trusted certificate. If the server is reachable from the Internet, several public CAs offer free, automatically-renewed server certificates. http://localhost is treated as a secure origin, so if you're able to run your server from localhost, you should be able to test the feature on that server.
  • You can run chrome with the --unsafely-treat-insecure-origin-as-secure="http://example.com" flag (replacing "example.com" with the origin you actually want to test), which will treat that origin as secure for this session. Note that on Android and ChromeOS this requires having a device with root access/dev mode. (This flag is broken in Chrome 63 but fixed in Chrome 64 and later. Prior to Chrome 62, you must also include the --user-data-dir=/test/only/profile/dir to create a fresh testing profile for the flag to work.)
  • Create a self-signed certificate for temporary testing. Direct use of such a certificate requires clicking through an invalid certificate interstitial, which is otherwise not recommended. Note that because of this interstitial click-through (which also prevents HTTPS-response caching), we recommend options (1) and (2) instead, but they are difficult to do on mobile. See this post on setting up a self-signed certificate for a server for more information on how to do this.
  • An alternative approach is to generate a self-signed root certificate which you place into the trust store of the developer PC/devices, and then issue one or more certificates for the test servers. Trusting the root certificate means that Chrome will treat the site as secure and load it without interstitials or impacting caching. One easy way of setting up and using a custom root certificate is to use the open source mkcert tool.
  • On a local network, you can test on your Android device using port forwarding to access a remote host as localhost.

I2C Expansion board

Adres selectie MCP23017

Overnemen van het PI scherm

Activeer I2C op de PI

Nu kunnen we een testprogramma maken en starten

Voorbeeld voor het gebruik var MCP23017 = require('node-mcp23017'); var mcp = new MCP23017({ address: 0x20, //default: 0x20 device: '/dev/i2c-1', // '/dev/i2c-1' on model B | '/dev/i2c-0' on model A debug: true //default: false }); /* By default all GPIOs are defined as INPUTS. You can set them all the be OUTPUTs by using the pinMode-Methode (see below), You can also disable the debug option by simply not passing it to the constructor or by setting it to false / //set all GPIOS to be OUTPUTS for (var i = 0; i < 16; i++) { mcp.pinMode(i, mcp.OUTPUT); //mcp.pinMode(i, mcp.INPUT); //if you want them to be inputs //mcp.pinMode(i, mcp.INPUT_PULLUP); //if you want them to be pullup inputs } mcp.digitalWrite(0, mcp.HIGH); //set GPIO A Pin 0 to state HIGH mcp.digitalWrite(0, mcp.LOW); //set GPIO A Pin 0 to state LOW / to read an input use the following code-block. This reads pin Nr. 0 (GPIO A Pin 0) value is either false or true */ mcp.digitalRead(0, function (err, value) { console.log('Pin 0', value); }); Voorbeeld (Blink 16 LEDs)

var MCP23017 = require('node-mcp23017');

var mcp = new MCP23017({ address: 0x20, //all address pins pulled low device: '/dev/i2c-1', // Model B debug: false });

/* This function blinks 16 LED, each hooked up to an port of the MCP23017 */ var pin = 0; var max = 16; var state = false;

var blink = function() { if (pin >= max) { pin = 0; //reset the pin counter if we reach the end }

if (state) { mcp.digitalWrite(pin, mcp.LOW); //turn off the current LED pin++; //increase counter } else { mcp.digitalWrite(pin, mcp.HIGH); //turn on the current LED console.log('blinking pin', pin); } state = !state; //invert the state of this LED };

//define all gpios as outputs for (var i = 0; i < 16; i++) { mcp.pinMode(i, mcp.OUTPUT); }

setInterval(blink, 100); //blink all LED's with a delay of 100ms

Servo aansturen

Instellen vast IP adres op PI Het instellen van een vast IP adres op de PI doen we in de dhcpcd.conf file.

Installeer nodejs en npm We installeren node js en npm. Node is onze webserver en websocket server.

sudo apt-get update --fix-missing sudo apt-get install -y nodejs npm 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!

De weberver werkt nu op je PI

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

Automatisch starten van app.js bij aanzetten PI Hiervoor moeten we een service installeen genaamd forever.

sudo npm -i install forever g

Nu kunnen we kijken of de app start met

forever start app.js

We stoppen de service met

forever stop app.js

Dit commando plaatsen we in de autostart. Dit bestand passen we aan met nano.

sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart Voer nu als laatste commando in @forever start app.js Sla op met [CTRL]+[O], [Enter]

Sluit af met [CTRL]+[X]

Na het opstarten zal de app starten. Type daarvoor

reboot

Aanpassen pin bezetting servo-blaster cd cd PiBits/ServoBlaster/user sudo nano servod.c Pas aan in de file static char *default_p1_pins = "7,11,12,13,15,16,18,22" in static char *default_p1_pins = "12" Sluit af met [CTRL]+[X], [Y], [Enter]

Bestand maken en installeren en voor het ingaan van de wijzigingen moet je opnieuw opstarten

make sudo make install reboot

Belangrijke functies zijn niet meer beschikbaar over HTTP. Hiervoor is een beveiligde SSL verbinding nodig. Hieronder vallen

Geolocation — requires secure origins as of M50 Device motion / orientation EME getUserMedia AppCache Notifications Voor testen bestaan volgende mogelijkheden

Secure the server with a publicly-trusted certificate. If the server is reachable from the Internet, several public CAs offer free, automatically-renewed server certificates.

http://localhost is treated as a secure origin, so if you're able to run your server from localhost, you should be able to test the feature on that server.

You can run chrome with the --unsafely-treat-insecure-origin-as-secure="http://example.com" flag (replacing "example.com" with the origin you actually want to test), which will treat that origin as secure for this session. Note that on Android and ChromeOS this requires having a device with root access/dev mode. (This flag is broken in Chrome 63 but fixed in Chrome 64 and later. Prior to Chrome 62, you must also include the --user-data-dir=/test/only/profile/dir to create a fresh testing profile for the flag to work.)

Create a self-signed certificate for temporary testing. Direct use of such a certificate requires clicking through an invalid certificate interstitial, which is otherwise not recommended. Note that because of this interstitial click-through (which also prevents HTTPS-response caching), we recommend options (1) and (2) instead, but they are difficult to do on mobile. See this post on setting up a self-signed certificate for a server for more information on how to do this.

An alternative approach is to generate a self-signed root certificate which you place into the trust store of the developer PC/devices, and then issue one or more certificates for the test servers. Trusting the root certificate means that Chrome will treat the site as secure and load it without interstitials or impacting caching. One easy way of setting up and using a custom root certificate is to use the open source mkcert tool.

On a local network, you can test on your Android device using port forwarding to access a remote host as localhost.

Adres selectie MCP23017

Instellen configuratie

sudo raspi-config

5 Interfacing Options P5 I2C

5 Interfacing Options P2 SSH

5 Interfacing Options P3 VNC

Uitschakelen van PI middels de prompt

sudo shutdown -h now

Opstarten van de website in kiosk mode

In terminal

sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart In editor invoeren

@lxpanel -–profile LXDE-pi @pcmanfm –-desktop -–profile LXDE-pi #@xscreensaver -no-splash #@point-rpi @xset s off @xset -dpms @xset s noblank @sed -i 's/"exited_cleanly":false/"exited_cleanly":true/' /home/pi/.config/chromium/Default/Preferences @chromium-browser --start-fullscreen --disable-session-crashed-bubble --noerrdialogs --no-default-browser-check --no-first-run --disable-infobars -- kiosk https://aliconnect.nl/aliconnector

Overnemen van het PI scherm

Activeer I2C op de PI

sudo raspi-config

5 Interfacing Options P5 I2C Kijk met WinSCP of bestand /dev/i2c-1 bestaat. Dit bestand maken we lees en schrijfbaar.

sudo chmod o+rw /dev/i2c* Nu kunnen we een testprogramma maken en starten

Voorbeeld voor het gebruik var MCP23017 = require('node-mcp23017'); var mcp = new MCP23017({ address: 0x20, //default: 0x20 device: '/dev/i2c-1', // '/dev/i2c-1' on model B | '/dev/i2c-0' on model A debug: true //default: false }); /* By default all GPIOs are defined as INPUTS. You can set them all the be OUTPUTs by using the pinMode-Methode (see below), You can also disable the debug option by simply not passing it to the constructor or by setting it to false / //set all GPIOS to be OUTPUTS for (var i = 0; i < 16; i++) { mcp.pinMode(i, mcp.OUTPUT); //mcp.pinMode(i, mcp.INPUT); //if you want them to be inputs //mcp.pinMode(i, mcp.INPUT_PULLUP); //if you want them to be pullup inputs } mcp.digitalWrite(0, mcp.HIGH); //set GPIO A Pin 0 to state HIGH mcp.digitalWrite(0, mcp.LOW); //set GPIO A Pin 0 to state LOW / to read an input use the following code-block. This reads pin Nr. 0 (GPIO A Pin 0) value is either false or true */ mcp.digitalRead(0, function (err, value) { console.log('Pin 0', value); }); Voorbeeld (Blink 16 LEDs)

var MCP23017 = require('node-mcp23017');

var mcp = new MCP23017({ address: 0x20, //all address pins pulled low device: '/dev/i2c-1', // Model B debug: false });

/* This function blinks 16 LED, each hooked up to an port of the MCP23017 */ var pin = 0; var max = 16; var state = false;

var blink = function() { if (pin >= max) { pin = 0; //reset the pin counter if we reach the end }

if (state) { mcp.digitalWrite(pin, mcp.LOW); //turn off the current LED pin++; //increase counter } else { mcp.digitalWrite(pin, mcp.HIGH); //turn on the current LED console.log('blinking pin', pin); } state = !state; //invert the state of this LED };

//define all gpios as outputs for (var i = 0; i < 16; i++) { mcp.pinMode(i, mcp.OUTPUT); }

setInterval(blink, 100); //blink all LED's with a delay of 100ms

Servo aansturen

Remote IO Configuration

Install node module (is part of the aim-control module)

npm install node-mcp23017 --save

Configure the I2C address of the Romete IO module

  • Connect A0,A1 en/of A2 met de GND of de VDD (+3.3V) om een binaire code te maken

Code table

000 = 001 = 010 = 011 = 100 = 101 = 110 = 111 = Adres 20

After configuring the module connect it to the I2C bus. Check connected modules with the command

i2cdetect -y 1

GITHUB CLONING NOT REQUIRED

sudo apt-get install git
git clone https://github.com/richardghirst/PiBits.git cd PiBits cd ServoBlaster cd user make sudo make install
⚠️ **GitHub.com Fallback** ⚠️