pHAT DAC on OSMC

Our new media center (after some blue smoke on the TV side and resulting that the TV doesn’t send any information over the HDMI i²c-bus) is a RaspberryPi 2 with a [pHAT DAC] running [OSMC].

It is a little bit different than the [guide] on the Pimonori side and also the ones from the [HifiBerry]site [2](same chip on HifiBerryDAC) itself didn’t work.

So here is what I did by using all aforementioned guides:
sudo nano /boot/config.txt

Uncomment the dtoverlay=lirc-rpi:gpio_out_pin=17,gpio_in_pin=18' and add dtoverlay=hifiberry-dac.

Uncomment and change dtparam=audio=on.

Download the correct device tree overlay to  /boot/overlays/ from [ osmc-forum ] (it seems the overlay is broken).
Then reboot with sudo reboot

Within Settings > System > Audio output > Audio output device select ALSA: Default (snd_rpi_dac Analog) and you should hear the acquainted clicking noise.

One remark: This is what I did to get working audio output, not sure if it is the most simple way, but hey it works.

Weiterlesen

Raspberry Pi + TI Sensortag + Plotly

In the last post (Raspberry Pi 2 TI Sensortag) I recapped the possibility to use Raspberry Pi to connect with BLE to a TI Sensortag using the bluepy-library.

A logical next step is to push the data to the „cloud“.

First register under https://plot.ly/ go to settings and note your API Key and your tokens (create two).

To use it with the Raspberry Pi ssh into it (or use a terminal) and install the prerequistes:
sudo apt-get install python-dev
sudo apt-get install python-pip
sudo pip install rpi.gpio
sudo pip install plotly

The next step is to create a script which uploads the streaming data plot.ly, it is heaviliy based on this intructable from plotlygraphs:

import plotly.plotly as py
from plotly.graph_objs import Scatter, Layout, Figure, Data
import time
import datetime
import sensortag

username = 'your_username'
api_key = 'your_api_key'
stream_token1 = 'one_unique_token'
stream_token2 = 'another_unique_token'

py.sign_in(username, api_key)

trace1 = Scatter(
    x=[],
    y=[],
    name='Ambient Temp.',
    stream=dict(
        token=stream_token1,
        maxpoints=5000
    )
)
trace2 = Scatter(
    x=[],
    y=[],
    name='IR Temp.',
    stream=dict(
        token=stream_token2,
        maxpoints=5000
    )
)

layout = Layout(
    title='Raspberry Pi Streaming Sensor Data',
    showlegend=True,
    autosize=True
)

data = Data([trace1,trace2])
fig = Figure(data=data, layout=layout)

print(py.plot(fig, filename='Raspberry Pi Streaming Example Values'))

# activate sensortag
intervall = 20 # seconds
print "Push button on sensortag"
time.sleep(2.0)
tag = sensortag.SensorTag('BC:6A:29:AC:53:D1')

time.sleep(1.0)
tag.IRtemperature.enable()
print("Connected")

i = 0
stream1 = py.Stream(stream_token1)
stream1.open()

stream2 = py.Stream(stream_token2)
stream2.open()


#the main sensor reading loop
while True:
    sensor_data = tag.IRtemperature.read()
    now = datetime.datetime.now()
    stream1.write({'x': now, 'y': sensor_data[0]})
    stream2.write({'x': now, 'y': sensor_data[1]})
    i += 1
        # wait for new measurement
    tag.waitForNotifications(intervall)
    if i > 100:
        break

stream1.close()
stream2.close()
tag.disconnect()
print("Finished!")

The result looks somthing like this.

Raspberry Pi 2 BLE TI Sensor Tag

Install necessary packages:
sudo apt-get install bluez-utils libopenobex1 build-essential libglib2.0-dev libdbus-1-dev

Restart the bluetooth service
sudo service bluetooth restart

Scan for BLE enabled devices connected to the Raspberry Pi:
hciconfig --all

hci0: Type: BR/EDR Bus: USB
BD Address: 00:1A:7D:DA:71:0C ACL MTU: 310:10 SCO MTU: 64:8
UP RUNNING PSCAN
...

Scan for BLE devices:
sudo hcitool lescan

BC:6A:29:AC:53:D1 SensorTag
F4:F9:51:C7:4C:23 (unknown)
BC:6A:29:AC:53:D1 (unknown)
BC:6A:29:AC:53:D1 SensorTag
BC:6A:29:AC:53:D1 (unknown)
...

It shows two devices, but we are only interrested in the Sensor Tag. The adress is BC:6A:29:AC:53:D1 (this address may be different with each device).
So we know bluetooth is working and the mac address of the sensor tag.

Next step is to download/install the great bluepy library from Ian Harvey:
git clone https://github.com/IanHarvey/bluepy.git
cd bluepy/bluepy
make

To read now sensor data from the sensortag, use the example script sensortag.py:
usage: sensortag.py [-h] [-n COUNT] [-t T] [-T] [-A] [-H] [-M] [-B] [-G] [-K]
[--all]
host

positional arguments:
host MAC of BT device

optional arguments:
-h, --help show this help message and exit
-n COUNT Number of times to loop data
-t T time between polling
-T, --temperature
-A, --accelerometer
-H, --humidity
-M, --magnetometer
-B, --barometer
-G, --gyroscope
-K, --keypress
--all

I.e. to read the temperature five times in an interval of 0.5 seconds we use:
python sensortag.py BC:6A:29:AC:53:D1 -n 5 -t 0.5 -T
then press the button on the side of the sensortag
Connecting to BC:6A:29:AC:53:D1
('Temp: ', (31.71875, 30.4396374686782))
('Temp: ', (31.75, 28.29441505369789))
('Temp: ', (31.71875, 27.21886394070981))
('Temp: ', (31.71875, 27.21886394070981))
('Temp: ', (31.71875, 28.54570004702458))

It is also possible (without any changes to the sensortag.py-script) to use it as a python script:
import time
import sensortag

tag = sensortag.SensorTag('BC:6A:29:AC:53:D1')

time.sleep(1.0)
tag.IRtemperature.enable()
for i in range(5):
tag.waitForNotifications(1.0)
print tag.IRtemperature.read()
tag.disconnect()
del tag

One remark if you want to import the module from another folder I recommend adding to the __init__.py file the line:
from . import *
Than it is possible to import the package with realtive importing like:
from bluepy.bluepy import sensortag