Premiers pas avec BeagleBone Black

From air
Revision as of 16:00, 30 November 2015 by Donsez (talk | contribs) (→‎Activation des 5 UART supplémentaires)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


Démarrage

Connecter l'USB du BeagleBone Black. Une fois qu'il est visible (comme un disque : /media/BEAGLEBONE), on peut se connecter dessus : http://192.168.7.2

L'environnent de développement Cloud9 est aussi accessible depuis http://192.168.7.2:3000


Des que cela fonctionne, on peut y aller par ssh (mot de passe vide)

ssh root@192.168.7.2

Il n'y a pas de mot de passe. Par sécurité, mettre un mot de passe ou déployer une clé publique pour SSH.

Test des GPIO depuis le shell

Test des LED USR0123

cd /sys/class/leds
ls -F
cd beaglebone\:green\:usr0
ls -F
cat trigger 
echo none > trigger
echo oneshot > trigger
echo heartbeat > trigger
echo 1 > brightness 
echo 0 > brightness 
echo timer > trigger
echo 100 > delay_on
echo 900 > delay_off


Test des GPIO (write)

# attach a LED at gpio1_28 (1*32+28=60) with a 220 ohms resistor
cd /sys/class/gpio
ls -F
echo 60 > export
cd gpio60
echo out > direction
echo 1 > value
cd ..
echo 60 > unexport

Test des GPIO (analog read)

TBD


Test des GPIO (digital read) : mettre un bouton en pullup sur 60 9_28 ? (=1*32+28)

#!/bin/sh
#
# Read a GPIO input 
# Author : http://wh1t3s.com/2009/05/14/reading-beagleboard-gpio/

GPIO=$1

cleanup() { # Release the GPIO port
  echo $GPIO > /sys/class/gpio/unexport
  echo ""
  echo ""
  exit
}

# Open the GPIO port
#
echo "$GPIO" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio${GPIO}/direction

trap cleanup SIGINT # call cleanup on Ctrl-C

THIS_VALUE=`cat /sys/class/gpio/gpio${GPIO}/value`
LAST_VALUE=$THIS_VALUE
NEWLINE=0

# Read forever

while [ "1" = "1" ]; do
  # next three lines detect state transition
  if [ "$THIS_VALUE" != "$LAST_VALUE" ]; then
    EV="|"
  else
    EV=""
  fi

  # "^" for high, '_' for low
  if [ "1" = "$THIS_VALUE" ]; then
    EV="${EV}^"
  else
    EV="${EV}_"
  fi
  echo -n $EV

  # sleep for a while
  sleep 0.05

  # wrap line every 72 samples
  LAST_VALUE=$THIS_VALUE
  THIS_VALUE=`cat /sys/class/gpio/gpio${GPIO}/value`
  NEWLINE=`expr $NEWLINE + 1`
  if [ "$NEWLINE" = "72" ]; then
    echo ""
    NEWLINE=0
  fi

done

cleanup # call the cleanup routine
chmod 755 ./read_gpio.sh
./read_gpio.sh 60

Ressources

Test des GPIO en Python

import os
import time
def test(command):
print command
os.popen(command)
test(“echo 60 > /sys/class/gpio/export”)
test(“echo high > /sys/class/gpio/gpio60/direction”)
test(“echo 1 > /sys/class/gpio/gpio60/value”)
value = 1
for i in range(0,10):
if value == 1:
value = 0
else:
value = 1
test(“echo ” + str(value) + ” > /sys/class/gpio/gpio60/value”)
time.sleep(2)
test(“echo 60 > /sys/class/gpio/unexport”)

Ressources

C++

Ressources

Configuration Réseau

Sur le BeagleBone

route add default gw 192.168.7.1

Mettre un DNS dans /etc/resolv.conf (par exemple : cat /etc/resolv.conf sur le PC, vous prenez la 1ère ligne "nameserver X.X.X.X" et vous la mettez dans /etc/resolv.conf sur le BeagleBone).

nameserver 192.168.7.1
nameserver 8.8.8.8


Sur le PC qui sert de passerelle (celui où est connecté l'USB) :

sudo iptables --append FORWARD --in-interface eth2 -j ACCEPT
sudo iptables --table nat --append POSTROUTING --out-interface eth1 -j MASQUERADE

Note : sur ma machine, eth1 = l'I/F réseau du PC, et eth2 = la connexion filaire via USB (faire "ifconfig -a" pour vérifier, sinon adapter les commandes ci-dessus).

Vérifier que /proc/sys/net/ipv4/ip_forward contient "1" . Sinon:

sudo bash -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

Et voilà... Depuis le BeagleBone, je peux pinger n'importe quelle adresse

ping beaglebone.org
ping 8.8.8.8
nslookup www.google.com

Configuration du temps

Attention : les mises à jour et les installations via npm requièrent une horloge mise à jour

voir http://derekmolloy.ie/automatically-setting-the-beaglebone-black-time-using-ntp/

date
ntpdate -b -s -u pool.ntp.org
date

Ajout d'une clé Wifi

https://learn.adafruit.com/setting-up-wifi-with-beaglebone-black/overview

Activation des 5 UART supplémentaires

http://beaglebone.cameon.net/home/serial-ports-uart

Ajouter à /boot/uboot/uEnv.txt avec vim/nano

##Enable on-board serial ports http://beaglebone.cameon.net/home/serial-ports-uart
optargs=quiet drm.debug=7 capemgr.enable_partno=BB-UART1,BB-UART2,BB-UART4,BB-UART5

puis

reboot
ls -l /dev/ttyO*

Node.js

Attention : les mises à jour et les installations via npm requièrent une horloge mise à jour

Node.js est installé par défaut. Il sert le serveur web embarqué. La bibliothèque bonescript donne accès aux GPIO de la carte.

Led toggling

Copier ce code dans un fichier blinking.js pris dans le livre Bad to the Bone: Crafting Electronics Systems with BeagleBone and BeagleBone Black.


var b = require('bonescript');

//Old bonescript defines 'bone' globally
var pins = (typeof bone != 'undefined') ? bone : b.bone.pins;

var ledPin = pins.P8_13;
var ledPin2 = pins.USR3;

b.pinMode(ledPin, b.OUTPUT);
b.pinMode(ledPin2, b.OUTPUT);

var state = b.LOW;
b.digitalWrite(ledPin, state);
b.digitalWrite(ledPin2, state);

setInterval(toggle, 1000);

function toggle() {
    if(state == b.LOW) state = b.HIGH;
    else state = b.LOW;
    b.digitalWrite(ledPin, state);
    b.digitalWrite(ledPin2, state);
}
node blinkled.js

Led on / off

Un autre exemple

var led = process.argv[2];
var mode = process.argv[3];

var b = require('bonescript');

b.pinMode(led, b.OUTPUT);

if(mode == "ON") state = b.HIGH; else state = b.LOW;

b.digitalWrite(led, state);
node led.js USR0 ON
node led.js USR1 OFF
node led.js USR2 ON
node led.js USR3 OFF

Analog read

Un autre exemple

var b = require('bonescript');
var apin = process.argv[2];
b.analogRead(apin, printStatus);
function printStatus(x) {
    console.log( x.value);
}
node analogread.js P9_33
node analogread.js P9_35
node analogread.js P9_37
node analogread.js P9_39
node analogread.js P9_36
node analogread.js P9_38
node analogread.js P9_40

JDK

Télécharger le JDK d'Oracle : JDK 7 version "Linux ARM v6/v7 Soft Float ABI".

Le copier sur le Beagle via scp; exemple :

scp jdk-7u45-linux-arm-vfp-sflt.tar.gz root@192.168.7.2:/home/root

Décompresser le tar.gz

export PATH=$PATH:/home/root/jdk1.7.0_45/bin
export JAVA_HOME=/home/root/jdk1.7.0_45

java -version

Ajouter les 2 exports au .bashrc

OpenHAB

Télécharger OpenHAB

Le copier sur le Beagle via scp; exemple :

scp openhab.tgz root@192.168.7.2:/home/root

Décompresser le tgz

cd openhab
bin/start.sh

Naviguer sur http://192.168.7.2:8080/openhab.app?sitemap=demo

Il reste plus qu'à ajouter des règles avec le binding exec pour contrôler les GPIO du Beaglebone.



Liens:

Node-RED

Attention : les mises à jour et les installations via npm requièrent une horloge mise à jour

Voir http://nodered.org/docs/hardware/beagleboneblack.html

Charger et déployer ce flow pour faire clignoter les LED USR2 et USR3 du BBB

[{"id":"b1fce44.9ce3c18","type":"inject","name":"on","topic":"","payload":"1","repeat":"","once":false,"x":127.16666412353516,"y":109.16666412353516,"wires":[["b2a66e5e.6da6f"]]},{"id":"591aef4d.b55f38","type":"inject","name":"off","topic":"","payload":"0","repeat":"","once":false,"x":127.16666412353516,"y":149.16666412353516,"wires":[["b2a66e5e.6da6f"]]},{"id":"36f2a960.164f76","type":"inject","name":"tick","topic":"","payload":"","repeat":"1","once":false,"x":127.16666412353516,"y":49.166664123535156,"wires":[["7ee460bc.df48e"]]},{"id":"55249ec9.21e61","type":"debug","name":"","active":true,"x":567.1666641235352,"y":89.16666412353516,"wires":[]},{"id":"7ee460bc.df48e","type":"function","name":"Toggle USR3 LED on input","func":"\nvar pin = \"USR3\"\nvar b = context.global.bonescript;\ncontext.state = context.state || b.LOW;\n\nb.pinMode(pin, b.OUTPUT);\n\n(context.state == b.LOW) ? context.state = b.HIGH : context.state = b.LOW;\nb.digitalWrite(pin, context.state);\n\nreturn msg;","outputs":1,"x":347.16666412353516,"y":69.16666412353516,"wires":[["55249ec9.21e61"]]},{"id":"b2a66e5e.6da6f","type":"function","name":"Set USR2 LED on input","func":"\nvar pin = \"USR2\";\nvar b = context.global.bonescript;\n\nb.pinMode(pin, b.OUTPUT);\n\nvar level = (msg.payload === \"1\")?1:0;\nb.digitalWrite(pin, level);\n\nreturn msg;","outputs":1,"x":347.16666412353516,"y":129.16666412353516,"wires":[["55249ec9.21e61"]]}]


http://nodered.org/docs/hardware/arduino.html Charger et déployer ce flow pour faire clignoter la LED 13 de l'Arduino branché sur le port USB du BBB

npm install firmata

Le port est /dev/ttyACM0

[{"id":"d7663aaf.47194","type":"arduino-board","repeat":"25","device":"/dev/ttyACM0"},{"id":"8c09ca6c.a975d","type":"arduino out","name":"","pin":"13","state":"OUTPUT","arduino":"d7663aaf.47194","x":509.16667556762695,"y":162.16666984558105,"wires":[]},{"id":"e37b6a97.610968","type":"inject","name":"tick","topic":"","payload":"","repeat":"0.5","once":false,"x":116.16668319702148,"y":62.16666507720947,"wires":[["60b4aeaa.800d58"]]},{"id":"60b4aeaa.800d58","type":"function","name":"Toggle output on input","func":"\n// initialise level as a context variable if currently undefined \n// (context variables persist between calls to the function)\ncontext.level = context.level || false;\n\n// if it's a 0 make it a 1 else make it a 0...\ncontext.level = !context.level;\n\n// set the payload to the level and return\nmsg.payload = context.level;\nreturn msg;","outputs":1,"x":298.1666793823242,"y":113.16665458679199,"wires":[["8c09ca6c.a975d"]]}]

Arduino

Attention : les mises à jour et les installations via npm requièrent une horloge mise à jour

avec Firmata et Node.js : TBD avec Node.js#Arduino_avec_Firmata

Le port de l'Arduino est /dev/ttyACM0

http://www.instructables.com/id/How-to-make-a-BeagleBone-and-an-Arduino-communicat/

Future carte Arduino TRE : http://beagleboard.org/blog/2013-10-03-beagleboardorg-collaborates-with-arduino/

XBMC

OpenCV

GStreamer

MQTT

En Python avec Paho Python

Il faut installer le package Adafruit_BBIO (lien)

import time

import Adafruit_BBIO.ADC as ADC

import paho.mqtt.client as mqtt

sensor_pin = 'P9_40'

ADC.setup()

mqttc = mqtt.Client()

mqttc.connect("m2m.eclipse.org", 1883, 60)

mqttc.loop_start()

while True:

  reading = ADC.read(sensor_pin)

  millivolts = reading * 1800  # 1.8V reference = 1800 mV

  temp_c = (millivolts - 500) / 10

  print('mv=%.2f C=%.2f' % (millivolts, temp_c))

  mqttc.publish("bbbexample/tmp36/mv","%.2f" % millivolts);

  mqttc.publish("bbbexample/tmp36/c","%.2f" % temp_c);

  time.sleep(1)[a]

BBB contrôlant Dagu Wild Thumper 6WD Chassis

TBD