หน้าเว็บ

วันอาทิตย์ที่ 23 กุมภาพันธ์ พ.ศ. 2563

Home Assistant : HA Install On Pi Run Play MP3

Home Assistant : HA Install On Pi Run Play MP3
1. Download PI และ Restore ใส่ SD Card นำไป Boot
https://www.raspberrypi.org/downloads/raspbian/

2. ตั้งค่า wifi
https://raspberrypihq.com/how-to-connect-your-raspberry-pi-to-wifi/
ถ้ามี Error แสดง
Wi-Fi is currently blocked by rfkill

ใช้คำสั่ง
  1. sudo rfkill unblock 0
  2. ifconfig wlan0 up

https://www.raspberrypi.org/forums/viewtopic.php?t=206223

3. Set Wifi Connect
  1. iwlist wlan0 scan
  2. nano /etc/wpa_supplicant/wpa_supplicant.conf

เพิ่ม
network={
ssid="SCI_EMP"
psk="XXXXXX"
}
แล้ว reboot เครื่อง Run คำสั่ง
  1. ifconfig wlan0

จะได้ IP

4. Config rapberry pi
เลือก Time zone
  1. open ssh timezone

  1. raspi-config

- เปลี่ยนชื่อ Host ตามที่ต้องการ เช่น pi-moning
- เปิด ssh
และอื่น ๆ
Run set locale
  1. locale-gen en_US en_US.UTF-8 th_TH th_TH.UTF-8

ติดตั้ง ntp และ config ntp

5. ติดตั้ง
https://www.home-assistant.io/docs/installation/raspberry-pi/
Run Command
  1. sudo apt-get update
  2. sudo apt-get upgrade -y
  3. sudo apt-get install python3 python3-dev python3-venv python3-pip libffi-dev libssl-dev
  4. sudo useradd -rm homeassistant -G dialout,gpio,i2c
  5. cd /srv
  6. sudo mkdir homeassistant
  7. sudo chown homeassistant:homeassistant homeassistant
  8. sudo -u homeassistant -H -s
  9. cd /srv/homeassistant
  10. python3 -m venv .
  11. source bin/activate
  12. python3 -m pip install wheel
  13. pip3 install homeassistant


hass ใช้เวลาสักพัก ประมาณ 20-30 นาที
ถ้าขึ้น INFO (MainThread) [homeassistant.core] Timer:starting คือเสร็จแล้ว
6. เข้าหน้า Web ตั้งรหัสผ่าน user
http://ipaddress:8123

7. ทำ hass auto service
ทำต่อจากข้อ 8
https://intranet.sci.com/blog.php?u=281&b=1603
เพิ่มเติม
https://www.home-assistant.io/docs/autostart/systemd/
https://community.home-assistant.io/t/unit-home-assistant-homeassistant-service-not-found/28507

8. Confing configuration.yaml
all config file
  1. cd /home/homeassistant/.homeassistant

คำสั่ง restart service
  1. systemctl restart home-assistant@homeassistant

AIQ
สร้างไฟล aqi.sh
#!/bin/sh
#New Key 20200206 348 Day
result=$(curl -s --location --request GET "api.airvisual.com/v2/city?city=Samut%20Sakhon&state=Samut%20Sakhon&country=Thailand&key=XXXXXXXXXXXXXX")
echo $result | jq -r .data.current.pollution.aqius


สมัครฟรี 10000 ครั้ง/เดือน หมดอายุ 348 วัน XXXX คือคีย์ที่ได้จาก Web
https://www.airvisual.com/thailand/bangkok

9. ติดต้ง jq
  1. sudo apt install jq

10. Code sh ยัดค่าใส่ sensor
  1. nano configuration.yaml

เพิ่ม
#AIQ From aiq.sh
sensor:
- platform: command_line
name: AQI
scan_interval: 3600
command: "/home/homeassistant/.homeassistant/aqi.sh"


11. เล่นเพลง MP3
copy mp3 ไปไว้ตามที่ต้องการ
ติดตั้งโปรแกรม
  1. apt-get install omxplayer

ทดลองเล่นด้วยคำสั่ง
  1. omxplayer /home/pi/MP3/Auto_7.55.mp3

ถ้ามี Error
Error: Unable to open font

ให้ติดตั้ง fonts เพิ่ม
  1. apt-get install libpcre3 fonts-freefont-ttf

https://raspberrypi.stackexchange.com/questions/32755/install-omxplayer-on-the-commandline

จากข้อ 11. user อื่น ๆ เล่นได้แต่พอไป Test run จาก หน้า Web Home Assistant จะไม่ได้ ไม่เล่นเพลง
ต้อง Add user homeassistant เข้า Group ด้วย ไม่อย่างนั้น จะ Error เมื่อเรียกจาก homeassistant
  1. usermod -a -G audio homeassistant
  2. usermod -G video homeassistant

แล้ว reboot เครื่อง

ลอง Run ที่ User homeassistant
  1. su - homeassistant
  2. /usr/bin/omxplayer /home/pi/MP3/1.mp3


12. Fix IP Wifi
https://electrondust.com/2017/11/25/setting-raspberry-pi-wifi-static-ip-raspbian-stretch-lite/
  1. nono /etc/dhcpcd.conf

เพิ่มไว้ล่างสุด
interface wlan0
static ip_address=192.168.0.175
static routers=192.168.0.2
static domain_name_servers=8.8.8.8

แล้ว Reboot เครื่อง

13. configuration.yaml
  1. nano configuration.yaml

shell_command:
auto_normal: /usr/bin/omxplayer /home/homeassistant/.homeassistant/MP3/Auto_7.55.mp3
song_national: /usr/bin/omxplayer /home/homeassistant/.homeassistant/MP3/Song_National.mp3
no_act: /usr/bin/omxplayer /home/homeassistant/.homeassistant/MP3/No_Act.mp3
song_enough: /usr/bin/omxplayer /home/homeassistant/.homeassistant/MP3/Song_Enough.mp3
stop_mp3: bash /home/homeassistant/.homeassistant/stop_mp3.sh


14. scrip.yaml
  1. nano scrip.yaml

'1582256882671':
alias: 1. ^{ ^a ^d ^h ^` ^b ^i ^a ^v Manual
sequence:
- service: shell_command.no_act
- service: system_log.write
'1582256923595':
alias: 2. ^` ^~ ^g ^h ^h ^g ^~ ^` ^~ ^g Manual
sequence:
- service: shell_command.song_enough
'1582256950346':
alias: 3. ^` ^~ ^g ^j ^u Manual
sequence:
- service: shell_command.song_national
'1582259558847':
alias: Stop Sound
sequence:
- data: {}
service: shell_command.stop_mp3


15. automations.yaml
  1. nano automations.yaml

- id: '1582084332514'
alias: 1. ^` ^b ^i ^a ^v ^{ ^a ^u ^{ ^a ^w ^a ^y 7:55
description: ^` ^b ^i ^a ^v ^{ ^a ^u ^` ^{ ^t ^` ^~ ^g ^h ^h ^g ^~ ^` ^~ ^g + ^` ^~ ^g ^j ^u
trigger:
- at: '7:54:57'
platform: time
condition: []
action:
- service: shell_command.auto_normal
initial_state: true
- id: '1582106072377'
alias: 2. ^g ^t ^a ^h ^a 7:30, 7:50 ^a ^` ^{ ^t ^` ^~ ^g ^j ^u 8:00
description: ^{ ^a ^g ^t ^a ^h ^a ^a ^` ^{ ^t ^` ^~ ^g ^j ^u 8:00
trigger:
- at: '7:30:00'
platform: time
condition: []
action:
- data: {}
service: shell_command.no_act
- delay: 00:19:45
- service: shell_command.no_act
- delay: 00:09:45
- service: shell_command.song_national
initial_state: false


***** เพิ่มเติม IP Wifi มีปัญหา ****
พบปัญหา Wifi ที่ Config จาก Service dhcpcd มีปัญหา
เครื่องเปิดปกติทำงานปกติ แต่ Wifi ใช้ไม่ได้เข้าไม่ได้

เปลี่ยนวิธี Config ip wifi ใหม่ ใช้
https://raspberrypi.stackexchange.com/questions/37920/how-do-i-set-up-networking-wifi-static-ip-address

  1. nano /etc/network/interfaces

เพิ่ม
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.0.175
netmask 255.255.255.0
gateway 192.168.0.2
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf


ของ Lan ถ้าต้องการใช้ก็ Fix จากไฟล์นี้เหมือนกัน เช่น
auto eth0
iface eth0 inet static
address 10.1.1.30
netmask 255.255.255.0
gateway 10.1.1.1


แล้วปิด Service dhcpcd ไปให้ไปใช้ network/interfaces แทน
  1. sudo systemctl disable dhcpcd
  2. sudo systemctl enable networking


แล้ว Reboot เครื่อง ก็จะสามารถใช้ IP ที่ Fix ได้

วันอาทิตย์ที่ 2 กุมภาพันธ์ พ.ศ. 2563

Raspberry Pi Headless Setup With WiFi and SSH Enabled

Raspberry Pi Headless Setup With WiFi and SSH Enabled
https://www.gngrninja.com/code/2019/3/10/raspberry-pi-headless-setup-with-wifi-and-ssh-enabled

1. Download
https://www.raspberrypi.org/downloads/raspbian/
2. แตกไฟล์ Burn ใส่ SD ด้วยการ Restore ทับทั้งหมด
3. นำ SD ไปใส่เครื่องให้ Boot ก่อนสักพัก เพื่อให้ทำ Config และ Resize Partition (ถ้าไม่ทำมีการสร้างไฟล์เพิ่มใหม่ใน Boot จะหาย เมื่อนำไป Boot )
4. ปิดเครื่องเอา SD ไปต่อ Linux เปิดใน Disk Boot
5. สร้าง ไฟล์ wpa_supplicant.conf ในไปไว้ใน Boot
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
ap_scan=1
fast_reauth=1
country=JP

network={
ssid="Your network's SSID"
psk="Your network's password/psk"
id_str="0"
priority=100
}


6. สร้างไฟล์ ssh ด้วย text editor ไม่ต้องใส่นามสกุล ไว้ที่ Dive Boot
7. เปลี่ยนสิทธิ์ เป็น 777 ทั้ง 2 ไฟล์
8. นำไป Boot จะต่อ WIFI และ ได้ IP สามารถ SSH ไปได้