---
# System prepended metadata

title: Body temperature and face detect

---

Body temperature and face detect
===

本篇主要紀錄使用Raspberry pi製作非接觸式體溫量測裝置。

---

### 環境:

- 開發版: Raspberry pi 4b+
- 建置發行版工具: yocto poky
- 非接觸式體溫計: MLX90614 [spec](https://www.melexis.com/en/product/mlx90614/digital-plug-play-infrared-thermometer-to-can)
- 非接觸式體溫計(已放棄): amg8833 [spec](https://industry.panasonic.eu/components/sensors/industrial-sensors/grid-eye/amg88xx-high-performance-type/amg8833-amg8833)
- 7 inch Raspberry pi 觸控式螢幕 [參考](https://www.raspberrypi.com.tw/8826/470/)
- Raspberry pi camera v2 [參考](https://www.raspberrypi.com.tw/12085/716/)
- 記憶體32GB(含以上)
- UI介面採用瀏覽器(Chromium)當作UI介面，後臺採用django


![](https://i.imgur.com/9oIQcmA.png)


---

### 建置發行版:

基礎YOCTO建置請參考[連結](https://hackmd.io/WoDfPXS5QOKBoPY-qjjUcQ)


- 設定檔:

* local.conf
```text=
#
# This file is your local configuration file and is where all local user settings
# are placed. The comments in this file give some guide to the options a new user
# to the system might want to change but pretty much any configuration option can
# be set in this file. More adventurous users can look at local.conf.extended
# which contains other examples of configuration which can be placed in this file
# but new users likely won't need any of them initially.
#
# Lines starting with the '#' character are commented out and in some cases the
# default values are provided as comments to show people example syntax. Enabling
# the option is a question of removing the # character and making any change to the
# variable as required.

#
# Machine Selection
#
# You need to select a specific machine to target the build with. There are a selection
# of emulated machines available which can boot and run in the QEMU emulator:
#
#MACHINE ?= "qemuarm"
#MACHINE ?= "qemuarm64"
#MACHINE ?= "qemumips"
#MACHINE ?= "qemumips64"
#MACHINE ?= "qemuppc"
#MACHINE ?= "qemux86"
#MACHINE ?= "qemux86-64"
#
# There are also the following hardware board target machines included for 
# demonstration purposes:
#
#MACHINE ?= "beaglebone-yocto"
#MACHINE ?= "genericx86"
#MACHINE ?= "genericx86-64"
#MACHINE ?= "edgerouter"
#
MACHINE ??= "raspberrypi4"
KERNEL_IMAGETYPE = "Image"
# RPI_USE_U_BOOT = "1"
ENABLE_UART = "1"
ENABLE_I2C = "1"
# ENABLE_SPI_BUS = "1"
# KERNEL_MODULE_AUTOLOAD_rpi += "i2c-dev i2c-bcm2708"
RASPBERRYPI_HD_CAMERA = "1"
VIDEO_CAMERA = "1"
PARALLEL_MAKE = "-j 4"
GPU_MEM_256="192"
DISABLE_OVERSCAN = "1"
DISABLE_SPLASH = "1"
DISABLE_RPI_BOOT_LOGO = "1"
MACHINE_FEATURES_append=" vc4graphics xf86-video-fbdev"
DISTRO_FEATURES_append = " opengl x11 fbdev"
MACHINE_FEATURES_remove=" wayland"
VC4GRAPHICS = "1"

# DISTRO_FEATURES_append = " eglfs opengl ${DISTRO_FEATURES_LIBC}"
# This sets the default machine to be qemux86-64 if no other machine is selected:
# MACHINE ??= "qemux86-64"

#
# Where to place downloads
#
# During a first build the system will download many different source code tarballs
# from various upstream projects. This can take a while, particularly if your network
# connection is slow. These are all stored in DL_DIR. When wiping and rebuilding you
# can preserve this directory to speed up this part of subsequent builds. This directory
# is safe to share between multiple builds on the same machine too.
#
# The default is a downloads directory under TOPDIR which is the build directory.
#
DL_DIR ?= "${TOPDIR}/downloads"

#
# Where to place shared-state files
#
# BitBake has the capability to accelerate builds based on previously built output.
# This is done using "shared state" files which can be thought of as cache objects
# and this option determines where those files are placed.
#
# You can wipe out TMPDIR leaving this directory intact and the build would regenerate
# from these files if no changes were made to the configuration. If changes were made
# to the configuration, only shared state files where the state was still valid would
# be used (done using checksums).
#
# The default is a sstate-cache directory under TOPDIR.
#
SSTATE_DIR ?= "${TOPDIR}/sstate-cache"

#
# Where to place the build output
#
# This option specifies where the bulk of the building work should be done and
# where BitBake should place its temporary files and output. Keep in mind that
# this includes the extraction and compilation of many applications and the toolchain
# which can use Gigabytes of hard disk space.
#
# The default is a tmp directory under TOPDIR.
#
TMPDIR = "${TOPDIR}/tmp"

#
# Default policy config
#
# The distribution setting controls which policy settings are used as defaults.
# The default value is fine for general Yocto project use, at least initially.
# Ultimately when creating custom policy, people will likely end up subclassing 
# these defaults.
#
DISTRO ?= "poky"
# As an example of a subclass there is a "bleeding" edge policy configuration
# where many versions are set to the absolute latest code from the upstream 
# source control systems. This is just mentioned here as an example, its not
# useful to most new users.
# DISTRO ?= "poky-bleeding"

#
# Package Management configuration
#
# This variable lists which packaging formats to enable. Multiple package backends
# can be enabled at once and the first item listed in the variable will be used
# to generate the root filesystems.
# Options are:
#  - 'package_deb' for debian style deb files
#  - 'package_ipk' for ipk files are used by opkg (a debian style embedded package manager)
#  - 'package_rpm' for rpm style packages
# E.g.: PACKAGE_CLASSES ?= "package_rpm package_deb package_ipk"
# We default to rpm:
PACKAGE_CLASSES ?= "package_ipk"

#
# SDK target architecture
#
# This variable specifies the architecture to build SDK items for and means
# you can build the SDK packages for architectures other than the machine you are
# running the build on (i.e. building i686 packages on an x86_64 host).
# Supported values are i686, x86_64, aarch64
#SDKMACHINE ?= "i686"

#
# Extra image configuration defaults
#
# The EXTRA_IMAGE_FEATURES variable allows extra packages to be added to the generated
# images. Some of these options are added to certain image types automatically. The
# variable can contain the following options:
#  "dbg-pkgs"       - add -dbg packages for all installed packages
#                     (adds symbol information for debugging/profiling)
#  "src-pkgs"       - add -src packages for all installed packages
#                     (adds source code for debugging)
#  "dev-pkgs"       - add -dev packages for all installed packages
#                     (useful if you want to develop against libs in the image)
#  "ptest-pkgs"     - add -ptest packages for all ptest-enabled packages
#                     (useful if you want to run the package test suites)
#  "tools-sdk"      - add development tools (gcc, make, pkgconfig etc.)
#  "tools-debug"    - add debugging tools (gdb, strace)
#  "eclipse-debug"  - add Eclipse remote debugging support
#  "tools-profile"  - add profiling tools (oprofile, lttng, valgrind)
#  "tools-testapps" - add useful testing tools (ts_print, aplay, arecord etc.)
#  "debug-tweaks"   - make an image suitable for development
#                     e.g. ssh root access has a blank password
# There are other application targets that can be used here too, see
# meta/classes/image.bbclass and meta/classes/core-image.bbclass for more details.
# We default to enabling the debugging tweaks.
EXTRA_IMAGE_FEATURES ?= "debug-tweaks"

#
# Additional image features
#
# The following is a list of additional classes to use when building images which
# enable extra features. Some available options which can be included in this variable
# are:
#   - 'buildstats' collect build statistics
#   - 'image-mklibs' to reduce shared library files size for an image
#   - 'image-prelink' in order to prelink the filesystem image
# NOTE: if listing mklibs & prelink both, then make sure mklibs is before prelink
# NOTE: mklibs also needs to be explicitly enabled for a given image, see local.conf.extended
USER_CLASSES ?= "buildstats image-mklibs image-prelink"

#
# Runtime testing of images
#
# The build system can test booting virtual machine images under qemu (an emulator)
# after any root filesystems are created and run tests against those images. It can also
# run tests against any SDK that are built. To enable this uncomment these lines.
# See classes/test{image,sdk}.bbclass for further details.
#IMAGE_CLASSES += "testimage testsdk"
#TESTIMAGE_AUTO_qemuall = "1"

#
# Interactive shell configuration
#
# Under certain circumstances the system may need input from you and to do this it
# can launch an interactive shell. It needs to do this since the build is
# multithreaded and needs to be able to handle the case where more than one parallel
# process may require the user's attention. The default is iterate over the available
# terminal types to find one that works.
#
# Examples of the occasions this may happen are when resolving patches which cannot
# be applied, to use the devshell or the kernel menuconfig
#
# Supported values are auto, gnome, xfce, rxvt, screen, konsole (KDE 3.x only), none
# Note: currently, Konsole support only works for KDE 3.x due to the way
# newer Konsole versions behave
#OE_TERMINAL = "auto"
# By default disable interactive patch resolution (tasks will just fail instead):
PATCHRESOLVE = "noop"

#
# Disk Space Monitoring during the build
#
# Monitor the disk space during the build. If there is less that 1GB of space or less
# than 100K inodes in any key build location (TMPDIR, DL_DIR, SSTATE_DIR), gracefully
# shutdown the build. If there is less that 100MB or 1K inodes, perform a hard abort
# of the build. The reason for this is that running completely out of space can corrupt
# files and damages the build in ways which may not be easily recoverable.
# It's necessary to monitor /tmp, if there is no space left the build will fail
# with very exotic errors.
BB_DISKMON_DIRS ??= "\
    STOPTASKS,${TMPDIR},1G,100K \
    STOPTASKS,${DL_DIR},1G,100K \
    STOPTASKS,${SSTATE_DIR},1G,100K \
    STOPTASKS,/tmp,100M,100K \
    ABORT,${TMPDIR},100M,1K \
    ABORT,${DL_DIR},100M,1K \
    ABORT,${SSTATE_DIR},100M,1K \
    ABORT,/tmp,10M,1K"

#
# Shared-state files from other locations
#
# As mentioned above, shared state files are prebuilt cache data objects which can be
# used to accelerate build time. This variable can be used to configure the system
# to search other mirror locations for these objects before it builds the data itself.
#
# This can be a filesystem directory, or a remote url such as http or ftp. These
# would contain the sstate-cache results from previous builds (possibly from other
# machines). This variable works like fetcher MIRRORS/PREMIRRORS and points to the
# cache locations to check for the shared objects.
# NOTE: if the mirror uses the same structure as SSTATE_DIR, you need to add PATH
# at the end as shown in the examples below. This will be substituted with the
# correct path within the directory structure.
#SSTATE_MIRRORS ?= "\
#file://.* http://someserver.tld/share/sstate/PATH;downloadfilename=PATH \n \
#file://.* file:///some/local/dir/sstate/PATH"

#
# Yocto Project SState Mirror
#
# The Yocto Project has prebuilt artefacts available for its releases, you can enable
# use of these by uncommenting the following line. This will mean the build uses
# the network to check for artefacts at the start of builds, which does slow it down
# equally, it will also speed up the builds by not having to build things if they are
# present in the cache. It assumes you can download something faster than you can build it
# which will depend on your network.
#
#SSTATE_MIRRORS ?= "file://.* http://sstate.yoctoproject.org/2.5/PATH;downloadfilename=PATH"

#
# Qemu configuration
#
# By default native qemu will build with a builtin VNC server where graphical output can be
# seen. The line below enables the SDL UI frontend too.
PACKAGECONFIG_append_pn-qemu-system-native = " sdl"
# By default libsdl2-native will be built, if you want to use your host's libSDL instead of 
# the minimal libsdl built by libsdl2waylandtend, which takes somewhat longer to build, but adds
# a handy set of menus for controlling the emulator.
#PACKAGECONFIG_append_pn-qemu-system-native = " gtk+"

#
# Hash Equivalence
#
# Enable support for automatically running a local hash equivalence server and
# instruct bitbake to use a hash equivalence aware signature generator. Hash
# equivalence improves reuse of sstate by detecting when a given sstate
# artifact can be reused as equivalent, even if the current task hash doesn't
# match the one that generated the artifact.
#
# A shared hash equivalent server can be set with "<HOSTNAME>:<PORT>" format
#
#BB_HASHSERVE = "auto"
#BB_SIGNATURE_HANDLER = "OEEquivHash"

#
# Memory Resident Bitbake
#
# Bitbake's server component can stay in memory after the UI for the current command
# has completed. This means subsequent commands can run faster since there is no need
# for bitbake to reload cache files and so on. Number is in seconds, after which the
# server will shut down.
#
#BB_SERVER_TIMEOUT = "60"

# CONF_VERSION is increased each time build/conf/ changes incompatibly and is used to
# track the version of this file when it was generated. This can safely be ignored if
# this doesn't mean anything to you.
CONF_VERSION = "1"
IMAGE_ROOTFS_EXTRA_SPACE = "20971520"

IMAGE_INSTALL_append = " python3"
IMAGE_INSTALL_append  = " python3-pip"
IMAGE_INSTALL_append  = " python3-wheel"
IMAGE_INSTALL_append = " ntp"
IMAGE_INSTALL_append = " v4l-utils"
IMAGE_INSTALL_append = " opkg"
IMAGE_INSTALL_append = " opencv"
IMAGE_INSTALL_append = " userland"
IMAGE_INSTALL_append = " libgpiod"
IMAGE_INSTALL_append = " vim"
IMAGE_INSTALL_append = " rsync"
IMAGE_INSTALL_append = " tslib"
IMAGE_INSTALL_append = " openssh"
IMAGE_INSTALL_append = " python3-pyqt5"
IMAGE_INSTALL_append = " chromium-x11"
IMAGE_INSTALL_append = " packagegroup-core-buildessential"
IMAGE_INSTALL_append = " python3-pillow"
IMAGE_INSTALL_append = " cmake"
IMAGE_INSTALL_append = " meta-dlib"
IMAGE_INSTALL_append = " clang"
IMAGE_INSTALL_append = " openssl"
IMAGE_INSTALL_append = " openssl-dev"
IMAGE_INSTALL_append = " openssl-src"
IMAGE_INSTALL_append = " libnss-mdns"
IMAGE_INSTALL_append = " libnss-mdns-dev"
IMAGE_INSTALL_append = " libnfc"
IMAGE_INSTALL_append = " qrencode"
IMAGE_INSTALL_append = " amg8833"
IMAGE_INSTALL_append = " mlx90614"
IMAGE_INSTALL_append = " git"

TOOLCHAIN_TARGET_TASK_append = " opencv"

INHERIT += "extrausers"
EXTRA_USERS_PARAMS = "usermod -P root root; "

# MACHINE_FEATURES += "pitft pitft22 pitft28r pitft28c pitft35r"

LICENSE_FLAGS_WHITELIST="commercial GPL"
```



* bblayers.conf
```text=
# POKY_BBLAYERS_CONF_VERSION is increased each time build/conf/bblayers.conf
# changes incompatibly
POKY_BBLAYERS_CONF_VERSION = "2"

BBPATH = "${TOPDIR}"
BBFILES ?= ""

BBLAYERS ?= " \
  $PATH/poky/meta \
  $PATH/poky/meta-poky \
  $PATH/poky/meta-yocto-bsp \
  $PATH/poky/meta-raspberrypi \
  $PATH/poky/meta-openembedded/meta-oe \
  $PATH/poky/meta-openembedded/meta-multimedia \
  $PATH/poky/meta-openembedded/meta-networking \
  $PATH/poky/meta-openembedded/meta-python \
  $PATH/poky/meta-openembedded/meta-filesystems \
  $PATH/poky/meta-qt5 \
  $PATH/poky/meta-python2 \
  $PATH/poky/meta-browser/meta-chromium \
  $PATH/poky/meta-clang \
  $PATH/poky/meta-layer-gui
  "

```

:::info
$PATH 請代入自己的實際路徑
:::

:::info

meta-layer-gui的來源路徑為:
https://github.com/sss22213/meta-layer-gui

其他的都可以在:
https://git.yoctoproject.org/
找到
:::

- 編譯:
```bash=
bitbake core-image-x11
```

- 燒錄:
編譯過後，映像檔將產生於 以下資料夾:
```bash=
$YOCTO_WORKSPACE/tmp/deploy/images/raspberrypi4
```

檔名為:
```bash=
core-image-x11-raspberrypi4.wic.bz2
```

- 使用以下指令，即可燒錄映像檔到記憶卡之中:
```bash=
sudo bmaptool copy core-image-x11-raspberrypi4.wic.bz2 <SDCORD所在位置>
```

:::info
$YOCTO_WORKSPACE 為自己產生yocto的資料夾
:::

:::info
記憶卡請保持32GB(含以上)
::::


:::info
如果不想編譯的，底下連結有已經編譯好的映像檔載點(20210616):
https://drive.google.com/file/d/1ag3rO1OuaNS320cc1pKieCkfOb4XbQva/view?usp=sharing
:::

---

### 初次設定:

* 如何使用?
系統操作預設可透過Console(UART1)或是影像輸出(HDMI, DSI螢幕)兩種方式。


:::info
Console 接腳
![](https://i.imgur.com/INzLu4a.png)
:::

* 設定SSH:

系統已經預載OpenSSH，但受未設定。請依照以下方式設定SSH:

[SSH設定](https://hackmd.io/L6y3Q_yWQeeo_5vpB89liA)


* 設定無線網路:

系統可透過WPA_CLI設定無線網路。請依照以下方式設定無線網路:

[無線網路](https://hackmd.io/qUmavI9QQuK7IQ9a7tvzqw)

無線網路卡開機時不會自動掛載，為了開機會自動連上無線網路，請進入以下檔案:
```bash=
/etc/rc0.d/K80networking
```

並且依照以下內容新增:
```bash=
case "$1" in
start)
	echo -n "Configuring network interfaces... "
	sysctl -e -p /etc/sysctl.conf >/dev/null 2>&1
	ifup -a
+	ifup wlan0
	echo "done."
	;;

```

最後重開機就可以正常使用SSH登入系統了~~

---

### 新增i2c驅動到開機自動載入模組
```bash=
touch /etc/modules
echo "i2c-dev" >> /etc/modules
```

---

### 設定後台

1. 安裝django和PiHardwareInfo
```bash=
pip3 install django PiHardwareInfo
```

2. 下載後台程式
```bash=
git clone https://github.com/sss22213/UI-by-browser
```

3. 開啟後台
```bash=
python3 manager runserver 0.0.0.0:8000
```

4. 開啟前台
```bash=
export DISPLAY:=0;chromium --kiosk --no-default-browser-check --start-maximized --incognito  --no-sandbox --enable-webgl  127.0.0.1:8000 &
```

:::info
因為我是透過SSH登入系統，但顯示是在另一片七吋螢幕，因此透過
```bash=
export DISPLAY:=0
```
輸出到另一片螢幕

如果想要預設將UI程式預設輸出在別的螢幕，可將此行加入/etc/profile。
這樣開機就會自行設定了
:::

