Dieses Blog durchsuchen

Seiten

Labels

Raspberry (8) RPI (8)
Posts mit dem Label RPI werden angezeigt. Alle Posts anzeigen
Posts mit dem Label RPI werden angezeigt. Alle Posts anzeigen

Dienstag, 5. März 2019

Win10 SSH to RPI with key


1.) Create a Key on the RPI and import it


ssh-keygen -b 4096

ssh-copy-id -i .ssh/id_rsa.pub pi@rpi3 


(rpi3 is in this case my Raspberry )

2.) Copy the private key from rpi


([Username] is the windows username)

~/.ssh/id_rsa      to   c:\Users\[Username]\.ssh\id_rsa
~/.ssh/id_rsa.pub  to   c:\Users\[Username]\.ssh\id_rsa.pub


3.) Remove the Keys from the rpi and check if the authorized_keys updated

pi@rpi3:~/.ssh $ rm id_rsa
pi@rpi3:~/.ssh $ rm id_rsa.pub
pi@rpi3:~/.ssh $ ll
total 8
-rw------- 1 pi pi 733 Mar  5 22:15 authorized_keys
-rw-r--r-- 1 pi pi 222 Mar  5 22:15 known_hosts
pi@rpi3:~/.ssh $

4.) Activate ssh_agent in the Windows Services and set it to automatic


5.) Start the Agent in Gui or with command net start ssh-agent 

net start ssh-agent 

6.) Import the Key to the agent

 ([Username] is the windows username)

ssh-add c:\Users\[Username]\.ssh\id_rsa_rpi3


7.) To connect without password to the RPI over windows SSH use this command

ssh  pi@rpi3 


 

Freitag, 15. Juni 2018

How to connect a OLed (I2c) to Raspberry and programing with mono (c#)

 

This little blog shows you how to connect a 0.96" OLed to the Raspberry and programming with C#.

First of all we need the right OLed with the I2C interface, in my case i used a 0,96 Inch Oled for ~3€ in Ebay like this. It is controlled by a SSD1306 controller the hardware documentation can be found here: https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf



Now we connect the OLed to the Raspberry as follow:


The hardware is ready but now we need the code and libraries now. In my case i use the WiringPi library in combination with Mono. I love mono and it work's pretty well with my Rasperry zero W.

Install Mono and WiringPi

see this links to install WiringPi and Mono
http://wiringpi.com/download-and-install/
https://www.mono-project.com/download/stable/#download-lin-raspbian 

 After installed, check the follow:

Mono


WiringPi



Setup I2C on the Raspberry


Enable I2C run:  sudo raspi-config









Now close and reboot
After this the I2C module is available, please chek it with lsmod:




The code


My C# code is a small example and paints a rectangle and elipse on the 0.96 Inch Display with 128x64 Pixel. I use WiringPi function as an DllImport to communicate
You can download or clone the Visual Studio 2017 Project from here:
https://github.com/maza70/Oled_SSD1306








Donnerstag, 1. Februar 2018

Power consumption of the Raspberry Zero W

Here's my test results from the current consumption of the Raspberry Zero W

  • WiFi connected and cpu IDLE: 0,7 W
  • Wi-Fi network and CPU load: 1,2 W
  • WiFi connected, HDMI out, cpu IDLE: 0,6 W
    /usr/bin/tvservice -o
  • WiFi connected, HDMI out, cpu load: 1,1 W
Since I do not need HDMI, I switch it permanently off in the /etc/local.rc


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
 
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi
## MZ
/usr/bin/tvservice -o
exit 0


Alexa communicates with Raspberry


All based on Raspberry Zero W with an "Debian Stretch" installed!

It takes a little bit of knowledge to connect Alexa to the Raspberry.
The most important thing to know is that the echo is quite stupid and almost does not do it on its own.
The echo only responds to the keyword Alexa and then sends the spoken word to the server.
As in our case the communication looks, I show here in a small diagram.

 

As we can see, Raspberry has to be reachable over the internet, with the Fritzbox you can do it easily.
After that we have to forward the port 443 for SSL to our Raspberry,
for this we have to use the standard port because Amazon is very bitchy and only allows SSL over 443. (Grrrr)
When we reach our PI over the Internet via ssl, we can get started on Skill development.

First we register at https://developer.amazon.com and then go to Alexa and to "Get Started".


Next, we'll start a new skill..


In my case, the skill is called Yoda and is German.


After saving we come to the "interaction model"

To test I enter in the "Intent schema" the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
  "intents": [
    {
      "intent": "AMAZON.CancelIntent"
    },
    {
      "intent": "AMAZON.HelpIntent"
    },
    {
      "intent": "AMAZON.PauseIntent"
    },
    {
      "intent": "AMAZON.ResumeIntent"
    },
    {
      "intent": "AMAZON.StopIntent"
    },
    {
      "intent": "MeinIntent"
    }
   ]
}

In the "Sample Utterances" I enter the following (it this case, its scrap but for testing it is enough ):

1
2
3
4
MeinIntent hallo wie gehts
MeinIntent hallo wie geht es dir
MeinIntent wie gehts
MeinIntent wie geht es


After saving, we come to the "Configuration" and enter the following, please note that you the Https address adapts to your!



Now we have to teach Amazon that our SSL certificate is accepted, since it is a self-singed certificate.

For this we take the content of certificate.pem that we already have as in the blog post
Lighttp and SSL have generated and copy the content into the input box.




So, now it's time to test!
We specified the endpoint under "Configuration" and now we have to fill it with life. 
Create the file yoda.php as follows:

1
pico /var/www/html/yoda/yoda.php

Now copy the following PHP code in.
1
  
1
2
3
4
5
6
7
8
9
<?php
header ("Content-Type: application/json;charset=UTF-8" );
http_response_code(200);
echo '{"version": "1.0","response": {"outputSpeech": {"type": "PlainText","text": "Am Ende sind Feiglinge die, die der Dunklen Seite folgen."},
        "card": {
            "type": "Simple",
            "title": "Am Ende sind Feiglinge die, die der Dunklen Seite folgen."
        }
       },"shouldEndSession": true}';

To test you call your HTTPS adress it in your browser (in my case i use Chrome).
Chrome shows a warning message because the certificate is "Self Signed". 
Confirm that he should load the page and the result should look like this.



Now we almost made it, only the final test is missing.



So, if now have a "Service Response" after clicking on "Ask Yoda", Alexa is connected to the Raspberry and all paths are open to us. Now go to your echo and say "Alexa start Yoda" and Alexa will answer you. :-)

Raspberry Lighttp and SSL

All based on Raspberry Zero W with an "Debian Stretch" installed!
 
Since the Amazon Echo server only wants to communicate via https, I have to use for the Lighttp server a  "self-singed ceriticate".

First, let's start with a configuration for the certificate (please replace everything between <..> with your values):

1
2
3
4
cd ~
mkdir ssl
cd ssl
pico ssl.cfg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[req]
distinguished_name = req_distinguished_name
x509_extensions = v3_req
prompt = no
 
[req_distinguished_name]
C = [[dein land=""]]
ST = [[dein bundesland=""]]
L = [[deine stadt=""]]
O = keine
CN = [[dein skillname=""]]
 
[v3_req]
keyUsage = keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @subject_alternate_names
 
[subject_alternate_names]
DNS.1 = [[domain die von außen aufgerufen wird z.b.23213qwesd.myfritz.net]]
DNS.2 = [[interne domain z.b. rpi-zero]]

Now we execute the following commands:
1
  
1
2
3
4
5
openssl genrsa -out private-key.pem 2048
openssl req -new -x509 -days 365 -key private-key.pem -config ssl.cfg -out certificate.pem
cat private-key.pem  certificate.pem > lighttp.pem
sudo mkdir /etc/lighttpd/ssl
sudo cp lighttp.pem /etc/lighttpd/ssl/


Almost done now we have to configure Lighttpd:

1
sudo pico /etc/lighttpd/lighttpd.conf

Enter the following at the end of the file

1
2
3
4
$SERVER["socket"] == "0.0.0.0:443" {
 ssl.engine = "enable"
 ssl.pemfile = "/etc/lighttpd/ssl/lighttp.pem"
}

Now restart the server

1
sudo service lighttpd restart


So the certificate is installed!

Mittwoch, 31. Januar 2018

MPD auf dem Raspberry

All based on Raspberry Zero W with an "Debian Stretch" installed!

In meinem letzten blog hab ich eine Verbindung zwischen Amazon Echo und dem Raspberry Zero W hergestellt. Jetzt möchtich ich natürlich auch spachgesteuert meine Musik abspielen über eine Bluetooth Verbindung zwischen Rpi und Echo.



Installation of MPD



sudo apt-get install mpd mpc
sudo cp /etc/mpd.conf /etc/mpd.org
sudo pico /etc/mpd.conf


Ersetzte folgenden Abschnitt in der mpd.conf


audio_output {
        type            "alsa"
        name            "My ALSA Device"
#       device          "hw:0,0"        # optional
#       mixer_type      "hardware"      # optional
#       mixer_device    "default"       # optional
#       mixer_control   "PCM"           # optional
#       mixer_index     "0"             # optional
}

Durch


audio_output {
        type            "pulse"
        name            "MPD Pulse Output"
        server          "localhost"
}

Nun müssen wir den Service neu starten und die rechte setzten

sudo service mpd restart
sudo chgrp audio /var/lib/mpd/music
sudo chmod g+w /var/lib/mpd/music

Kopiert nun eure Musik nach

/var/lib/mpd/music

Nun über mpc die Datenbank aktualisieren


mpc --wait update



Beispiel einer playlist für den MPD


Pop.m3u 
Rio Reiser - König Von Deutschland.mp3
Red Hot Chili Peppers - Aeroplane.mp3
Stevie Nicks - Imperial Hotel.mp3
Laura Branigan - Self Control.mp3
Chaka Khan - Eye To Eye.mp3
Cyndi Lauper - Money Changes Everything.mp3
Herbert Grönemeyer - Alkohol.mp3
Fox The Fox - Precious Little Diamond.mp3
Melanie C - Northern Star.mp3
Silbermond - Nichts passiert.mp3
Frankie Goes To Hollywood - Relax.mp3
Michael Jackson - Earth Song.mp3

Wenn die Playliste erstellt wurde kann sie mit folgenden Kommandos benützt werden



mpc lsplaylists
mpc load Pop
mpc play 
 

Dienstag, 30. Januar 2018

Raspberry Bluetooth Pulseaudio and Alexa

All based on Raspberry Zero W with an "Debian Stretch" installed!
 
Hi, Now that I spent some nights connecting my Raspberry Zero W to the echo via Bluetooth, I decided to write this blog.

What do you need:
  • a Raspberry Pi Zero W
  • Amazon Echo
  • I hope only a fraction of the time I needed
I start with this introduction from the ground state after the installation. For the basic device I wrote this blog: Basic installation


Here we go!

1.) Bluetooth installation

It is important to assign the user groups. Unfortunately, this is missing in many descriptions in the INet.

1
2
3
sudo apt-get install bluetooth bluez bluez-tools bluez-firmware alsa-tools
sudo gpasswd -a pi bluetooth
sudo gpasswd -a root bluetooth

2.) Pulseaudio installation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
sudo apt-get install pulseaudio pulseaudio-module-bluetooth
sudo gpasswd -a pi pulse-access
sudo gpasswd -a pi lp
sudo gpasswd -a pi pulse
sudo gpasswd -a pulse lp
sudo gpasswd -a pulse audio
sudo gpasswd -a pulse bluetooth
sudo gpasswd -a pulse input
sudo gpasswd -a root audio
sudo gpasswd -a root pulse
sudo gpasswd -a root pulse-access
cd ~
sudo cp pulse /etc/init.d
sudo chmod +xwr,+xr,+xr /etc/init.d/pulse
sudo update-rc.d pulse defaults
sudo update-rc.d pulse enable

configure Pulse as service

1
2
sudo cp /etc/pulse/system.pa /etc/pulse/system.org
sudo pico /etc/pulse/system.pa

Now enter the following at the end of the file:
1
2
3
4
5
6
7
8
9
10
11
12
13
## --Ich start
### Automatically load driver modules for Bluetooth hardware
.ifexists module-bluetooth-policy.so
 load-module module-bluetooth-policy
.endif
 
.ifexists module-bluetooth-discover.so
  load-module module-bluetooth-discover
.endif
 
##wird benötigt damit man über TCP/IP auf Pulse zugreifen kann
load-module module-native-protocol-tcp auth-anonymous=1 auth-ip-acl=127.0.0.1
## --Ich ende 

3.) Now it's time

We start now the Pulseaudio service and see if everything is there.

1
sudo service pulse start 
If it looks like that, everything is good :-)
Now we have to register our Raspberry on the Alexa, you should see your bluetooth controller in /var /lib/bluetooth/.
 If you want to give your controller a special name, edit  /var/lib/bluetooth/<Bluetooth MAC>/settings

In my case, I call my Bluetooth controller Hugo.

1
2
[General]
Name = Hugo

So now execute the following commands:
1
2
sudo service bluetooth restart
bluetoothctl

The following output should appear:


Now enter the following commands:

1
2
scan on
agent on

Start now on the phone the Alexa App and go into the Bluetooth device and then press "NEW DEVICE".
After some time bluetoothctl shows the following line:
 
The Mac address will be different for you, so replace it with your own in the following commands!



1
2
3
pair FC:A1:83:B5:8C:E7
trust FC:A1:83:B5:8C:E7
exit
1
  
The following should be displayed:


We are now creating the configuration of Pulse for the user PI.

1
2
cd ~
pico .asoundrc

Fügt folgenden Inhalt ein:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
pcm.pulse {
    type pulse
}
 
ctl.pulse {
    type pulse
}
 
pcm.!default {
    type pulse
}
 
ctl.!default {
    type pulse
}

Reboot PI

sudo reboot

Now we can connect our Raspberry to the Alexa, now enter the following commands:

1
2
3
bluetoothctl
connect FC:A1:83:B5:8C:E7
exit

Now comes the final test:
1
2
3
sudo install mpg321
mpg321 test.mp3

1
<span style="color: red;"> </span>
Important when bluetooth play the sound only 1-3 second, the problem is the UART driver.
You must disable it in /boot/config.txt with enable_uart=0. Edit or add this line at /boot/conig.txt: 

1
 
1
 

If everything is well you should hear something on your echo :-) 

 

Debugging Bluetooth helps you if somehow not work. 

For Bluetooth with the command:


1
2
sudo sed -i 's/bluetoothd/bluetoothd \-d/g' /lib/systemd/system/bluetooth.service
sudo systemctl daemon-reload


yodablue.py

The Bluetoothctl tool is not so handy for automation, while the other tools like bt-device on my Rpi not work i write a little python script to connect and disconnect from a device.

Please install the follow Python packages with Pip:

1
2
3
4
5
sudo apt-get install libdbus-1-dev
sudo apt-get install libdbus-glib-1-dev
sudo pip install dbus-python
sudo pip install bluetool
sudo apt-get install python-bluez


connect to device : python yodablue.py -c FC:A1:83:B5:8C:E7
disconnect from device : python yodablue.py -d FC:A1:83:B5:8C:E7

This is the code from yodablue.py to connect and disconnect :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/python
from bluetool import Bluetooth
import sys, getopt
 
def main(argv):
 
  bluetooth = Bluetooth()
  res = False
  if len(sys.argv) != 3:
     print 'yodablue.py -c |-d <mac-adress>'
     sys.exit(2)
  if (sys.argv[1] =='-c'):
    print 'connect to',sys.argv[2]
    res = bluetooth.connect(sys.argv[2])
  if (sys.argv[1] =='-d'):
    print 'disconnect from',sys.argv[2]
    res = bluetooth.disconnect(sys.argv[2])
  if res == False:
    print 'fail'
    sys.exit(1)
  else:
    print 'OK'
    sys.exit(0)
 
if __name__ == "__main__":
  main(sys.argv[1:])
 
</mac-adress>


Rasberry Zero W basic installation

All based on Raspberry Zero W with an "Debian Stretch" installed!

 
Here's an introduction on how to get your new Raspberry Zero W up and running without using a monitor.


Download the software we need:
  1. Download image „Raspbian Stretch Lite“
    https://www.raspberrypi.org/downloads/raspbian/
  2. Win32DiskImager
     
Now connect the SD card for the Raspberry to the computer, using a card reader and write the image to the SD card using Win32DiskImager.





Now we have to teach the written image that the Raspberry automatically connects to our Wifi network when booting and the SSH server is started. We can do that with two files in the boot partition.

1.) Create the file wpa_supplicant.conf on the drive named Boot and add the following content (Please change the SSID and PSK to the settings of your WIFI network).

wpa_supplicant.conf:
1
2
3
4
5
6
7
8
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
 
network={
  ssid="Der ssid deines Wifi"
  psk="Dein geheimes Passwort"
  scan_ssid=1
}

2.) Create an empty file named ssh in the root of the partition.

After that it should look like this on the drive.


3.)
Now we insert the SD card into the RPI and provide it with power. After some time he should have automatically connected to the Wifi network and be found under the name raspberry. 
(This may take a few minutes for update the DNS)
You can then just test if everything works, by a ping of the DNS name raspberry:

1
ping raspberry


4.)  Access SSH over the standard User:
user: pi
password: raspberry


5.) After login over SSH we should start these commands

1
2
3
sudo apt-get update
sudo apt-get upgrade
sudo raspi-config

Now we configure our raspberry with assistance of  raspi-config :
  • at first set a new Password
  • set an new Hostname: Network Options ->Hostname
  • configure WIFI contry: Localisation Options-> Change Wi-fi Country
  • Expand SD-Card: Advanced Options->Expand Filesystem
  • Configure the memory: Advanced Options->Memory Split  16MB 
  • close Raspi-config and reboot.

So now your Raspberry should be ready,
have fun with your new gadget!

Matthias Zartmann