Showing posts with label rpi. Show all posts
Showing posts with label rpi. Show all posts

Saturday, July 31, 2021

Separate Zwave gateway or hub with MQTT integration to Home Assistant

This post is based on the excellent tutorial found here.

I want to run my Zwave stick from another RPi than my HA installation and want all the data to become available within HA through MQTT.

In short, these are the major steps to take:

  1. Ensure you have a running HA & MQTT setup, mine are separate Docker images
  2. Check that your Zwave stick is being found on the RPi
  3. Install Docker on your RPi
  4. Install ZWavejs2MQTT
  5. Start your Docker image
  6. Configure Zwave
  7. Do a discovery and check whether your stick and potentially 1 or more Zwave sensors or actors are being found
  8. Configure MQTT (disable the Gateway functionality in this step)
  9. Configure the Gateway (enable the functionality again)
  10. Check within HA if a new entity for MQTT has been found, with your sensors or actors

Monday, March 11, 2019

Making a Wifi router from your RPi and force traffic of your Kodi through it

Sometimes, you can run out of bandwidth in your ethernet connected home network. Yet, connecting to a Wifi hotspot somewhere and serving other devices in your network that are bandwidth intensive (like your Kodi) can overcome this issue.

I have the following devices in scope for this:

  • My regular Internet router is connected through a wired connection (Internal network 192.168.0.0/24)
  • A RPi1 that is connected through eth0 (wired) to the home network (IP 192.168.0.30). It has also a Wifi interface wlan0 that is connected to the public internet.
  • Another RPi2 that is serving Kodi to a TV also connected through eth0 to the home network (IP 192.168.0.40).
RPi1 will be the router that will connect to the Internet through wlan0. RPi2 will be configured to route all of the Internet requests to RPi1 over wired LAN.
Please note that an alternative way to configure Wifi on a RPi (e.g. serving Kodi from OSMC) can be found in this blogpost.
  1. Configure RPi1 to access the Wifi hotspot
    1. Scan your environment for the Wifi network
    2. iwlist wlan0 scan
    3. Edit the Wlan configuration
    4. sudo vi /etc/wpa_supplicant/wpa_supplicant.conf
      Add this config:
      network={
      ssid="ssid"
      scan_ssid=1
      key_mgmt=WPA-EAP
      group=CCMP TKIP
      eap=PEAP
      identity="username"
      password="password"
      phase1="peapver=0"
      phase2="MSCHAPV2"
      }
      
    5. Save and test your config
    6. wpa_cli -i wlan0 reconfigure
    7. Check if your wlan0 device has received an IP address
    8. ifconfig wlan0 or wpa_cli -i wlan0 status
      Output:
      wlan0: flags=-28605  mtu 1500
              inet 151.164.43.34  netmask 255.255.255.0  broadcast 151.164.43.255
              ether b8:27:aa:aa:ff:c1  txqueuelen 1000  (Ethernet)
              RX packets 256109  bytes 319396252 (304.6 MiB)
              RX errors 0  dropped 0  overruns 0  frame 0
              TX packets 187107  bytes 24877570 (23.7 MiB)
              TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    9. Install a cron task to regularly reconfigure the wlan0 interface if the Wifi connection gets disconnected
  2. Create a router of your RPi1
    1. vi /etc/iptables/rules.v4
      Add this config:
      *nat
      -A POSTROUTING -s 192.168.0.0/24 -o wlan0 -j MASQUERADE
      COMMIT
      
      *filter
      -A INPUT -i lo -j ACCEPT
      # allow ssh, so that we do not lock ourselves
      -A INPUT -i eth0 -p tcp -m tcp --dport 22 -j ACCEPT
      # allow incoming traffic to the outgoing connections,
      # et al for clients from the private network
      -A INPUT -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
      -A OUTPUT -p icmp --icmp-type 8 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
      -A INPUT -p icmp --icmp-type 0 -m state --state ESTABLISHED,RELATED -j ACCEPT
      # prohibit everything else incoming
      #-A INPUT -i eth0 -j DROP
      COMMIT
    2. Store your config
    3. iptables-restore < /etc/iptables/rules.v4
    4. Check if the rules are in effect
    5. root@hass:~# iptables -L
      Chain INPUT (policy ACCEPT)
      target     prot opt source               destination         
      ACCEPT     all  --  anywhere             anywhere            
      ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh
      ACCEPT     all  --  anywhere             anywhere             state NEW,RELATED,ESTABLISHED
      ACCEPT     icmp --  anywhere             anywhere             icmp echo-reply state RELATED,ESTABLISHED
      
      Chain FORWARD (policy ACCEPT)
      target     prot opt source               destination         
      
      Chain OUTPUT (policy ACCEPT)
      target     prot opt source               destination         
      ACCEPT     icmp --  anywhere             anywhere             icmp echo-request state NEW,RELATED,ESTABLISHED
      
  3. On your RPi2 add a static route to use RPi1 as the gateway for Internet traffic
    1. Test what external IP is being used on RPi2
    2. root@kodi1:/lib# curl ifconfig.me
      64.160.13.75
    3. Add the route
    4. route add -net default gw 192.168.0.30 netmask 0.0.0.0 dev eth0
    5. Test for your externalIP again, it should be different now
    6. root@kodi1:/lib# curl ifconfig.me
      151.164.43.34
    7. Also test if you can ping to www.google.com from RPi2
    8. Now all Internet traffic on RPi2 (192.168.0.40) will be routed through RPi1 (192.168.0.30)
    9. root@kodi1:~# route -n
      Kernel IP routing table
      Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
      0.0.0.0         192.168.0.30    0.0.0.0         UG    0      0        0 eth0
      192.168.0.0     0.0.0.0         255.255.255.255 UH    0      0        0 eth0
      192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

Wednesday, March 6, 2019

Connect your OSMC Kodi to Wifi with IEEE8021x authentication

I was struggling a bit to connect my Kodi to Wifi which has IEEE8021x authentication. But after some googling, the following worked out fine.


  1. Scan for Wifi networks and find the one you want to connect to
  2. connmanctl scan wifi
    connmanctl services
    Output:
    root@kodi1:~# connmanctl scan wifi
    Scan completed for wifi
    root@kodi1:~# connmanctl services
    *AR Wired                ethernet_b827ebf9x_cable
                             wifi_b827ebacx_hidden_managed_none
        SSID1                wifi_b827ebacx_7a657573_managed_psk
        SSID2 .              wifi_b827ebacx_7a6575735f696f74_managed_psk
                             wifi_b827ebacx_hidden_managed_psk
        SSID3                wifi_b827ebacx_54656c656e6574576946726565_managed_ieee8021x
        ...
  3. Get the detailed information needed to connect to the Wifi network
  4. connmanctl services wifi_xxx_managed_ieee8021x
  5. Enter the following in your shell
  6. cat > /var/lib/connman/peap-mschapv2.config <<EOF
    [global]
    Name = <ssid>
    Description = <description of network>
    [service_peap]
    Type = wifi
    Name = <ssid>
    EAP = peap
    Phase2 = MSCHAPV2
    Identity = <login>
    Passphrase = <passphrase>
    EOF

  7. Now connman should pick this up automatically. Please be aware that the file needs to end with .config and be placed in /var/lib/connman!
  8. Run the services command again, and notice that there will be an asterix next to the wifi network, if it has been found and the config file has been processed correctly. (as said: Immutable, AutoConnect and Favorite are set to true)
  9. Output:
    root@kodi1:~# connmanctl services
    *AO SSID3                wifi_b827ebacx_54656c656e6574576946726565_managed_ieee8021x
    *AR Wired                ethernet_b827ebf9x_cable
                             wifi_b827ebacx_hidden_managed_none
        SSID1                wifi_b827ebacx_7a657573_managed_psk
        SSID2 .              wifi_b827ebacx_7a6575735f696f74_managed_psk
                             wifi_b827ebacx_hidden_managed_psk
        ...

  10. Connect to the Wifi network

  11. connmanctl connect wifi_xxx_managed_ieee8021x