การใช้ Raspberry Pi เบื้องต้น

Somsak Lima
24 min readSep 20, 2021

ตอนนี้บันทึกไว้เพื่อใช้เอง ยังไม่ได้ Clean และยังมีบางข้อเป็น Experiment ทำงานจริงยังไม่ได้ เช่น พวก Grafana SWARM

1. SDcard ที่เคยลง Raspberry Pi มาแล้ว ถ้าจะเขียน Image ทับด้วยโปรแกรม Win32 Disk Imager ให้ลบ Partition เดิมสอง Partition ออก สร้าง Partition ใหม่เต็มขนาด SDcard แล้ว Format เป็น FAT32 และตั้ง Active ซึ่งจะเหมือน SDcard ใหม่จาก โรงงาน สามารถทำได้ด้วยโปรแกรม

Mini Tool Partition Wizard เป็นต้น

หรือใช้ Pi Imger เลือก Erase

2. Pi Image เปิด Raspberry PI Imager เลือก Raspberry PI OS Lite (32bit) เขียนลง micro SD Card ดึงแล้วเสียบใหม่

ถ้าต้องการเลือกลง Raspbian OS 64 bit ให้อ่านข้อ 56

สร้าง ssh ไม่มี Dot หรือ
สร้าง ssh.txt ก่อน File/Change Type (หรือ rename) เอา txt ออก

3. ssh login เช่น ssh pi@pi_ip_address

Username : pi 
Password : raspberry

เห็นข้อความ Wi-Fi is currently blocked by rfkill.
Use raspi-config to set the country before use. (sudo rc_gui)

IP ได้จากการ SCAN หา เช่น

MAC อาจจะเริ่มจาก 02-E0 ถ้าอยู่บน Wifi Repeter จะไม่แสดง Adapter Company

กรณีใช้ Wireless Network Watcher แล้วไม่แสดงชื่อ Raspberry PI ให้ใช้โปรแกรม Netscan โดยเปิด Option SCAN Port 22

4. ปรับปรุงโปรแกรมที่ติดตั้งให้ทันสมัย

sudo apt update
sudo apt upgrade
sudo apt full-upgrade -y
sudo apt upgrade -y
sudo apt upgrade — fix-missing
sudo apt autoremove

สั่งรวดเดียวโดยใช้

 sudo apt update -y && sudo apt upgrade -y

5. เช็ครายการ package ที่มีใหม่ให้ upgrade

apt list — upgradable

6. แสดงข้อความตอนบูท

dmesg

7. ดูเวอร์ชั่น OS

uname -a
Linux raspberrypi 5.15.61-v8+ #1579 SMP PREEMPT Fri Aug 26 11:16:44 BST 2022 aarch64 GNU/Linux

8. เปิด Service ssh และ SPI

sudo raspi-config 

เลือก 3 Interfacing option
P1 SSH
P4 SPI

9. ติดตั้งโปรแกรมที่มีประโยชน์และตั้งค่า Config ใหม่

sudo apt install xrdp locate lrzsz mc wget git finger scrot nmap -y
sudo updatedb
locate filename

ตั้งค่า Config ใหม่

sudo -AE piwiz

Run คำสั่งเพื่อสร้างฐานข้อมูลสำหรับช่วยในการหาตำแหน่งไฟล์

sudo updatedb
locate filename

10. ตั้งค่าตั้งค่า Config ใหม่ สำหรับ Port Ethernet

sudo nano /etc/dhcpcd.conf
interface eth0
static ip_address=192.168.1.100/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1

11. reboot

sudo reboot
เช็ค ssh pi@192.168.1.100 pi/raspberry
rdp 192.168.1.100 pi/raspberry

12. ติดตั้ง DOCKER (ไม่ต้องใช้ sudo) ใช้ User Pi ห้ามติดตั้งด้วย User ธรรมดา

curl -sSL https://get.docker.com | sh
#sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker
docker ps

ข้ามไป 21. สำหรับ Docker
คำสั่งต่อไปนี้ไม่จำเป็นแล้ว

$ sudo systemctl enable docker.service 
$ sudo systemctl enable containerd.service

ถ้าจะ update

sudo apt upgrade docker docker-compose

13. ติดตั้งโปรแกรม Cap หน้าจอ

sudo apt install simplescreenrecorder

14. เปลี่ยน User จาก pi เป็น Root

sudo su

15. แสดง Version OS ที่ติดตั้งอยู่

$ lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description: Raspbian GNU/Linux 10 (buster)
Release: 10
Codename: busterpi

หรือ เช่น

pi@raspberrypi:~ $ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 11 (bullseye)
Release: 11
Codename: bullseye

16. ดูรายละเอียด OS

$ cat /etc/os-release
PRETTY_NAME=”Raspbian GNU/Linux 9 (stretch)”
NAME=”Raspbian GNU/Linux”
VERSION_ID=”9"
VERSION=”9 (stretch)”
VERSION_CODENAME=stretch
ID=raspbian
ID_LIKE=debian
HOME_URL=”http://www.raspbian.org/"
SUPPORT_URL=”http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL=”http://www.raspbian.org/RaspbianBugs"

17. เพิ่ม USER กำหนดให้เป็น SUDOER หรือใช้คำสั่ง sudo ได้

sudo adduser slvp

เพิ่มเข้าไป Group sudoer

sudo usermod -aG sudo slvp 

แล้ว Reboot ต้องทำไม่นั้นจะใช้ Remote Desptop เพื่อ SDcopy ด้วย User ธรรมดาไม่ได้

หรือใช้วิธียากกว่าโดยแก้ไขไฟล์ด้วยคำสั่ง

sudo visudo

วิธีลบ user

sudo userdel -r -f slvp
sudo deluser -r slvp

18. ตั้ง User/Password WIFI

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

19. ดู Version GPIO

$ gpio -v
gpio version: 2.50
Copyright © 2012–2018 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty
Raspberry Pi Details:
Type: Pi 3, Revision: 02, Memory: 1024MB, Maker: Embest
* Device tree is enabled.
* → Raspberry Pi 3 Model B Rev 1.2
* This Raspberry Pi supports user-level GPIO access

20. ดูขา GPIO และชื่อเรียกขา wPi

gpio readall

21.ติดตั้ง DOCKER COMPOSER ถ้า Raspbian ใหม่ Bookworm ให้ดูอีกบทความ

sudo pip3 install docker-compose

เช็ค Version DOCKER COMPOSER

pi@raspberrypi:~ $ docker-compose version
docker-compose version 1.29.2, build unknown
docker-py version: 5.0.3
CPython version: 3.7.3
OpenSSL version: OpenSSL 1.1.1d 10 Sep 2019

22.ติดตั้ง Portainer:9000

ถ้าใช้ Raspbian 32bit

sudo docker pull portainer/portainer-ce:linux-arm
sudo docker run -d --restart always -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:linux-arm

เรียกใช้โดยใช้ Port 9000

http://[RASPBERRY_PI_IP_ADDRESS]:9000

ถ้าใช้ Raspbian 64bit

docker pull portainer/portainer-ce:linux-arm64
sudo docker run -d --restart always -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:linux-arm64

ลองเพิ่มระบุ name

sudo docker run -d --restart always -p 9000:9000 --name portainer  -v /var/run/docker.sock:/var/run/docker.sock  -v  portainer_data:/data   portainer/portainer-ce:linux-arm64  

เรียกใช้โดยใช้ Port 9000

http://[RASPBERRY_PI_IP_ADDRESS]:9000

ตั้ง User Name/Password admin/ยาว 8/12 ตัวอักษร
เช่น admin/m2mlorawan หรือ m2mlorawan123

คลิก ICON รูปเรือ

ค่าเดิมๆ

Setting App Templates

https://raw.githubusercontent.com/portainer/templates/master/templates-2.0.json

อาจจะลองเปลี่ยนไปเป็น

https://raw.githubusercontent.com/RAKWireless/portainer-templates/master/portainer_app_template.json
หรือ
https://raw.githubusercontent.com/technorabilia/portainer-templates/main/lsio/templates/templates-2.0.json
*** https://raw.githubusercontent.com/Qballjos/portainer_templates/master/Template/template.json
https://raw.githubusercontent.com/dnburgess/dbtechtemplate/master/Template/v2/templates.json

400-1
https://raw.githubusercontent.com/Lissy93/portainer-templates/main/templates.json

จะเห็น App สำหรับ IoT มีเพิ่มมากขึ้น

Upgrade Portainer

docker stop portainer
docker rm portainer

แล้วลงใหม่

23. เช็ค Version App ที่ติดตั้งอยู่

dpkg -l | grep libseccomp
ii libseccomp2:armhf 2.3.3-4 armhf high level interface to Linux seccomp filter

24.ติดตั้ง Chirpstack V.4X บน Docker ติดตั้ง

ใช้ user root ติดตั้งโดย

sudo su
cd /home/pi
git clone https://github.com/brocaar/chirpstack-docker.git
cd chirpstack-docker

แก้ความถี่จาก eu868 เป็น as923

nano /home/pi/chirpstack-docker/docker-compose.yml
environment:
- INTEGRATION__MQTT__EVENT_TOPIC_TEMPLATE=eu868/gateway/{{ .GatewayID }}/event/{{ .EventType }}
- INTEGRATION__MQTT__STATE_TOPIC_TEMPLATE=eu868/gateway/{{ .GatewayID }}/state/{{ .StateType }}
- INTEGRATION__MQTT__COMMAND_TOPIC_TEMPLATE=eu868/gateway/{{ .GatewayID }}/command/#
environment:
- INTEGRATION__MQTT__EVENT_TOPIC_TEMPLATE=as923/gateway/{{ .GatewayID }}/event/{{ .EventType }}
- INTEGRATION__MQTT__STATE_TOPIC_TEMPLATE=as923/gateway/{{ .GatewayID }}/state/{{ .StateType }}
- INTEGRATION__MQTT__COMMAND_TOPIC_TEMPLATE=as923/gateway/{{ .GatewayID }}/command/#

และแก้เพิ่มความถี่ให้ครบ 8 ช่องความถี่

nano /home/pi/chirpstack-docker/configuration/chirpstack/region_as923.toml

เพิ่มตรงด้านล่างสุดของไฟล์

[[regions.network.extra_channels]]
frequency=923600000
min_dr=0
max_dr=5
[[regions.network.extra_channels]]
frequency=923800000
min_dr=0
max_dr=5
[[regions.network.extra_channels]]
frequency=924000000
min_dr=0
max_dr=5
[[regions.network.extra_channels]]
frequency=924200000
min_dr=0
max_dr=5
[[regions.network.extra_channels]]
frequency=924400000
min_dr=0
max_dr=5
[[regions.network.extra_channels]]
frequency=924600000
min_dr=0
max_dr=5

แก้ mqtt Server ถ้าจะใช้ mqtt

nano /home/pi/chirpstack-docker/configuration/chirpstack-gateway-bridge/chirpstack-gateway-bridge.toml
# See https://www.chirpstack.io/gateway-bridge/install/config/ for a full
# configuration example and documentation.
[integration.mqtt.auth.generic]
servers=["tcp://mosquitto:1883"]
username=""
password=""

เสร็จแล้วสั่งเริ่มสร้าง Container

cd /home/pi/chirpstack-docker/
sudo docker-compose up -d

เรียกใช้ Chirpstack ที่ Port:8080

เช็ค Version ของ Chirpstack

pi@raspberrypi:~ $ sudo docker exec -u root  -it chirpstack-docker-chirpstack-1 /usr/bin/chirpstack --version
chirpstack 4.5.1

หรือ

pi@raspberrypi:~ $ sudo docker exec -u root  -it chirpstack-docker-chirpstack-1 /bin/sh
/ # chirpstack --version
chirpstack 4.5.1

หรือ คลิก Container ชื่อ chirpstack-docker-chirpstack-1

คลิกที่ Console

เลือก Command /bin/sh แล้วกด Connect

จะเข้าไปที่ sh ใน Container สามารถใช้คำสั่งเช็ค version ได้ตามภาพ

เรียกใช้ Chirp ที่ Port:8080
ใช้ User/Password admin/admin

ลองดู Regions ว่ามีความถี่ครบ 8 ช่องที่ as923

หลัง Reboot Server ตอนเข้าใช้งาน ถ้าเจอ Error “context deadline exceeded (code: 2) error” ตอน Add NS ให้ดู IP ของ NS ชื่อ chirpstack/chirpstack-network-server:3 บน Docker Portainer ว่าเป็น IP อะไร ใช้ IP นั้นแทน

เรียกใช้งาน ChirpSatck REST API V.4.4.0

เรียกใช้ http://IP:8090/

24.1 การใช้งาน Chirpstack อย่างย่อ ใช้กับ Multichannel Gateway LoRaWAN ยี่ห้อ Dragino LPS8N-AS923-TH กับ Temp & Humid Sensor ยี่ห้อ Rejeee

Rejeee LoRaWAN Temp & Humid Sensor

24.1.1 เข้าเมนูด้านซ้าย Device profiles คลิก Add device profile

ถ้าเลือก Authentication แบบ OTAA ให้เลือก เมนู Join OTAA

ใส่ Javascript ใต้เมนู Codec

ให้ Copy code จากด้านล่างไปใส่

function decodeUplink(input) {
var obj = {};
var warnings = [];
obj.battery = (input.bytes[1]&0x1F);
obj.vol = (input.bytes[2]);
obj.temperature = (((input.bytes[4] & 0x80 ? input.bytes[4] - 0x100 : input.bytes[4]) << 8)
+
input.bytes[5]) / 10;
obj.humidity = input.bytes[7];
if (obj.temperature < -10) {
warnings.push("it's cold");
}
return {
data: obj,
warnings: warnings
};
}

24.1.2 Gateway

Add gateway หากใช้ LoRaWAN Gateway Dragino ทำโดย เปิดเวป http://IP gateway:8000

login ด้วย

User admin 
Password dragino

Copy จากหน้า Web Admin ของ Dragino ตรงGateway EUI มาใส่หน้า ChirpStack ตรงช่อง Gateway ID (EUI64)

ย้ายหมุดบนแผนที่ไปให้ตรงตำแหน่งติดตั้งจริง อาจจะต้องกดเครื่องหมาย ลบหลายๆ ครั้งเพื่อย่อขนาดแผนที่ลงก่อน

24.1.3 Application

Add application

24.1.4 Add device ใต้เมนู Application

เลือก Disable frame-counter validation

ป้อน OTAA Keys

หากมีข้อมูลเข้าจะเห็นในเมนู LoRaWAN frames

ตรงเมนู Event หากคลิกตรงบรรทัดที่สนใจจะมีข้อมูลรายละเอียดแสดงที่หน้าต่างด้านขวา และจะเห็นข้อมูล Payload ที่ถอดระหัสแล้ว เช่น จะเห็นค่า Temperature และ Humidity

24.1.5 Integration

เพิ่ม INTEGRATIONS ใน Application

คลิก + ADD ตรงใต้รูป Influxdb

InfluxDB version * เลือก InfuxDB 2.x

API endpoint (write) * http://192.168.1.100:8086/api/v2/write
Organization * Influxdata
Bucket * tablerejeee

Token * ให้ Copy จาก Influx ซึ่งจะพูดถึงในหัวข้อต่อไป เรื่อง “วิธีใช้งานและการเตรียม Influxdb สำหรับเก็บข้อมูล Sensor” โดยนำค่า Token จาก Influxdb มาป้อนตรงนี้

25. ติดตั้ง httpd บน Docker

sudo docker run -dit — name tecmint-web -p 8080:80 -v /home/user/website/:/usr/local/apache2/htdocs/ httpd:2.4
sudo docker ps

26. Port ที่ใช้

เรียกใช้

Portainer ที่ Port:9000 admin/m2mlorawan123
Chirpstack ที่ Port:8080 admin/admin
Chirpstack API ที่ Port:8090
Grafana ที่ Port:3000 admin/grafana
InfluxDB ที่ Port:8086 admin/influx123
NodeRed ที่ Port:1880
Dragino Gateway ที่ Port:8000 admin/dragino
Dragino 10.130.1.1 ssh port 2222, web admin port

27. ดูแอป Docker และวิธีลงแอปบน Docker

https://hub.docker.com/
https://hub.docker.com/_/influxdb?tab=tags

28. ติดตั้ง Influxdb 2.7.3 บน Raspbian 64

ใช้ User Pi ติดตั้ง

sudo docker pull influxdb:2.7.3
sudo docker run -d --restart always -p 8086:8086 --name=influxdb   -v influxdb:/var/lib/influxdb  influxdb:2.7.3

ทดสอบ influx โดย

sudo docker exec -it influxdb influx

เช็ค Version

root@c1a64b3dc751:/# influxd version
InfluxDB v2.7.3 (git: ed645d9216) build_date: 2023-10-17T15:39:46Z

หรือ

root@c1a64b3dc751:/# curl -sL -I localhost:8086/ping
HTTP/1.1 204 No Content
Vary: Accept-Encoding
X-Influxdb-Build: OSS
X-Influxdb-Version: v2.7.3
Date: Thu, 16 Nov 2023 14:05:12 GMT

ทดสอบ Influx ผ่าน API

pi@raspberrypi:~ $ curl localhost:8086/health
{“name”:”influxdb”, “message”:”ready for queries and writes”, “status”:”pass”, “checks”:[], “version”: “v2.2.0”, “commit”: “a2f8538837”}

ถ้าต้องการให้แสดงออกหน้าจอรูปแบบ Json ให้ส่งต่อไปที่คำสั่ง json_pp

curl localhost:8086/health|python -m json.tool หรือ
curl localhost:8086/health|json_pp

ถ้าจะให้แสดง JSON แบบสีให้ติดตั้ง jq

sudo apt install jq

แล้วเรียกคำสั่งเช่น

curl localhost:8086/health|jq

คำสั่ง json_reformat

ติดตั้งโดยคำสั่ง

sudo apt install yajl-tools

แสดงชื่อ Bucket ออกมาทั้งหมด

curl http://localhost:8086/api/v2/buckets — header “Authorization: Token your_api_token”

ดูเพิ่มเติมที่ https://www.influxdata.com/blog/getting-started-influxdb-api/

ดูเพิ่มเติม https://jet.dev/blog/secure-influxdb-setup-with-docker/

29.วิธีใช้งานและการเตรียม Influxdb 2.X สำหรับเก็บข้อมูล Sensor

https://www.influxdata.com/blog/getting-started-with-influxdb-2-0-ppscraping-metrics-running-telegraf-querying-data-and-writing-data/

เปิด http://ip:8086

คลิก Get Started

user :admin
password :influx123
Organization :Influxdata
Initial Bucket Name :system

จะเจอหน้า

คลิก Quick Start

29.1 สร้าง API Token

เรียกเมนู API Token ใต้ ICON เครื่องหมายลูกศรชี้ขึ้น

ด้านขวามี Drop Down เลือก Token ที่ต้องการ

มี Read/Write API Token กับ All Access API Token ลองเลือกใช้

Read/Write API Token Influxdb แต่ละ Version มีวิธีใช้แตกต่างกันเล็กน้อย

ป้อน tablerejeee คลิก Save

เลือกอันบน Read/Write API Token

ป้อนชื่อ Description ด้านซ้ายให้เลือกสิทธิ Read ว่าอ่าน BUCKETS อะไรได้ และด้านขวาให้เลือกสิทธิ Write ว่าสามารถเขียน BUCKETS อะไรได้ เลือกเสร็จให้กด Save

เมื่อสร้างแล้วจะสามารถเข้าไปดู Token ที่สร้างไว้ โดยคลิกบรรทัดที่เราสร้างไว้ เช่น

จะเห็นระหัส Token ให้ Copy to Clipboard แล้วเอาไปใส่ใน Integration ของ Influxdb V.2.X

และให้ Save เก็บ Token ไว้ใช้กับ Grafana ด้วย ซึ่งต้องใช้ตอนสร้าง Data Source

29.2 วิธีลบ Bucket

Data/Buckets สีส้มบนขวาตรงชื่อ มี Delete Bucket

ล้างข้อมูล Structure ใน Bucket?

กรณีสร้าง bucket ใหม่ ต้อง Copy Token API ไปแก้ใน integration API Key ใน Integration จาก Chirpstack ด้วย

รวมทั้งแก้ Token ใน Data Source ของ Grafana ด้วย

29.3 Visualization บน Influxdb 2.X

ใช้ ICON Explorer

คลิก Explorer ลอง View Graph ดู เมื่อดูแล้ว OK สามารถ Save เข้า Dashboard ที่ต้องการได้

ใช้ ICON Dashboard

คลิก ICON Dashboard ด้านซ้าย

ด้านขวาเลือก Create Dashboard เลือก New Dashboard

คลิก Add Cell

Create Busket ก่อนชื่อ tablerejeee

คลิกที่ + Create Bucket เมื่อสร้างแล้วรอให้ Device ส่งข้อมูลเข้ามาจะเห็น _measurement ขึ้น และสามารถสร้างกราฟได้

เลือก From Bucket
Filter _measurement
Filter dev_eui ที่ต้องการ
Filter _field เลือก Value

เสร็จแล้วลองคลิก Submit เมื่อได้ที่ต้องการแล้วให้คลิกเครื่องหมายถูกที่มุมขวาบน

อ่านเพิ่ม Influxdb 2.0 Document
https://docs.influxdata.com/influxdb/v2.0/
https://medium.com/geekculture/deploying-influxdb-2-0-using-docker-6334ced65b6c
https://www.influxdata.com/blog/getting-started-with-influxdb-2-0-scraping-metrics-running-telegraf-querying-data-and-writing-data/
https://www.lionbloggertech.com/how-to-setup-influxdb-telegraf-on-docker/
https://www.blackvoid.club/grafana-8-influxdb-2-telegraf-2021-monitoring-stack/

29.4 วิธีใช้ Flux Language

InfluxDB v2 : Flux language, quick reference guide and cheat sheet คลิก

หรือ คลิก

30. ติดตั้ง Grafana บน docker ใช้ User Pi

sudo docker volume create grafana-storage
sudo docker run -d --restart always -p 3000:3000 --name=grafana -v grafana-storage:/var/lib/grafana grafana/grafana

เรียกใช้งาน http://IP address:3000

Password เดิม admin/admin
ตั้งใหม่เป็น admin/grafana

ติดตั้ง Plugin ก่อนอื่นเข้า shell ของ Container Grafana ก่อน ดูชื่อ Container จากเวป portainer คอลัมภ์แรก

docker exec -it <container_name> /bin/bash
docker exec -it blissful_lumiere /bin/bash

https://grafana.com/grafana/plugins/?type=panel

เช่น

https://grafana.com/grafana/plugins/pr0ps-trackmap-panel/?tab=installation

grafana-cli plugins install pr0ps-trackmap-panel
bash-5.1$ grafana-cli plugins install pr0ps-trackmap-panel
? Downloaded pr0ps-trackmap-panel v2.1.2 zip successfully
Please restart Grafana after installing plugins. Refer to Grafana documentation for instructions if necessary.

เพิ่ม Datasource อ่าน Influxdb 2.X

เลือก InfluxDB ถ้าเปลี่ยน IP จาก 192.168.1.100 เป็น IP อื่นให้แก้ Datasource ใน Grafana ด้วย

Basic Authen Detail
User: admin
Password: influx123

Influxdb Detial
Oraganization: Influxdata
Token: Copy มาจาก Influxdb API token
Default bucket: อะไรสัก bucket หนึ่ง ตอนเรียกใช้กำหนดได้ใหม่ เช่น tablerejeee

คลิก Save & Test ถ้า OK จะขึ้นสีเขียว 1 bucket found

ดูเพิ่ม https://www.youtube.com/watch?v=NGv7LeCaPtc

31. Grafana อ่าน Influxdb
เข้า Configuration/Data source

หนึ่ง Database คือ หนึ่ง Data source

URL http://192.168.1.150:8086 ใช้ IP No. ที่ Fix ไว้

Create your first Dashboard

Add a new panel

Copy Flux Data จาก InfluxDB

31.1 เปลี่ยน Legend ให้สั้นลง

1- Select Add overwrite on the right side at the bottom
2- Selection option “Fields with name”
3- Select the field e.g. in your case select Value 1
4- Then click on “Add overwrite property”
5- Select Standard options → Display name
6- Give your custom name here e-g- CPU 1
7- Click Save and Apply on the top right corner

เลือก Tab All เลือก เลื่อนหน้าจอไปล่างสุด คลิกที่ + Add field override

32. ติดตั้ง NodeRed บน Docker

ref:https://nodered.org/docs/getting-started/docker

docker run -d --restart always -it -p 1880:1880 -v node_red_data:/data --name nodered nodered/node-red

เรียกใช้ :1880 ใช้ Username/Password คือ admin/admin

33.คำสั่งใช้กับ docker

docker ps
docker image ls
docker volume ls
docker network ls
docker logs influxdb
docker-compose stop
docker-compose rm
docker-compose logs

34.แสดงหมายเลข IP ของ Raspberry PI

ip address

35. สร้าง DOCKER SWARM

Rrf:https://carlpaton.github.io/2020/09/raspberry-pi-cluster-swarm/sudo docker swarm leavesudo docker swarm init — advertise-addr 192.168.1.150To add a worker to this swarm, run the following command:docker swarm join — token SWMTKN-1–2c7zeu5wdd7fosaqlgizqqf9el6pgzgfy24vn5yjb7p6ktgc1v-5gp7p9pmy65mowuipui6iv9 192.168.1.150:2377To add a manager to this swarm, run ‘docker swarm join-token manager’ and follow the instructions.#docker swarm join — token AA 192.168.1.150:2377
Node1 sudo docker swarm join — token SWMTKN-1–14im48dmagw5gykzt1uy1vbxa5icmk4xu5jyuoesfpeuwvwvwu-5vqbnxq0e350otrlvuvu441 192.168.1.150:2377
Node2 sudo docker swarm join — token SWMTKN-1–613dqki6q1ht7wb93aa2564ysxiv9x6y9nsomi8vlxuje1csvr-3a2rh9xv0k005f5f955pkvx 192.168.1.150:2377
Node3
sudo docker node ls

36.เผลอลบ ingress network แก้โดย

Error response from daemon: rpc error: code = 9 desc = service needs ingress network, but no ingress network is present

แก้โดย

docker network create — ingress — driver overlay ingress

37.ติดตั้ง SWARM Service visualizer
Ref: https://github.com/dockersamples/docker-swarm-visualizer
Ref: https://carlpaton.github.io/2020/09/raspberry-pi-cluster-swarm/

Copy ตรง First Service

sudo docker service create — name viz — publish 5000:8080/tcp — constraint node.role==manager — mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock alexellis2/visualizer-arm:latestเปลี่ยน 8080 เป็น 5000 เพราะซ้ำ Chirpstracksudo docker service create \
— name viz \
— publish 5000:8080/tcp \
— constraint node.role==manager \
— mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \
alexellis2/visualizer-arm:latest

http://192.168.1.150:8081/
sudo docker service ls

38.ติดตั้งแบบ SWARM nginx

sudo docker service create —eplicas name web — replicas 1 -p 80:80 nginx

39. ขยาย run ใน Node SWARM อื่นๆ

docker service scale web=5

40. ลบ Node ออก

$ eval $(docker-machine env worker03)
$ sudo docker swarm leave
Node left the swarm.
$docker node rm worker03
worker03
demote Node จาก Manager เป็น workerdocker node demote node3

41. ดู Perfornance ของ PI 4 cores

htop

42. Docker compose เข้า SWARM

pi@master:~/chirpstack-docker $ sudo docker-compose up -d
WARNING: The Docker Engine you’re using is running in swarm mode.
Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.To deploy your application across the swarm, use `docker stack deploy`.docker stack deploy --compose-file docker-compose.yml stackdemoหมายเหตุ [บางครั้ง IP ยังไม่ FIX อาจเรียกใช้ไม่ได้ ให้แก้ .yml ให้ FIX IP

43. แก้ .yml ให้ restart container ทุกครั้ง ก่อน docker-compose up

kodi:
build: ./kodi-rpi
image: kodi-rpi
restart: always

44. สร้าง Service nginx

docker service create -d \
--replicas=4 \
--name devtest-service \
--mount source=myvol2,target=/app \
nginx:latest

45. https://wmartins.github.io/2017/10/creating-a-monitoring-stack-with-docker-swarm-grafana-influxdb-and-telegraf/

docker node ls

docker node update --label-add grafana=true <NODE-ID>
pi@master:~/somsak2 $ cat docker-compose.yml
version: “3.3”
services:
grafana:
image: grafana/grafana:4.5.2
ports:
— 3000:3000
volumes:
— /data/grafana:/var/lib/grafana
deploy:
placement:
constraints:
— node.labels.grafana == true
docker stack deploy -c docker-compose.yml grafana

docker service scale grafana_grafana=4

46. ตัวอย่าง Fix IP ของ docker-compose

https://alejandrocelaya.blog/2017/04/21/set-specific-ip-addresses-to-docker-containers-created-with-docker-compose/

version: '3'services:
test_1:
container_name: test_1
image: some:image
networks:
testing_net:
ipv4_address: 172.28.1.1
test_2:
container_name: test_2
image: some:image
networks:
testing_net:
ipv4_address: 172.28.1.2
test_3:
container_name: test_3
image: some:image
networks:
testing_net:
ipv4_address: 172.28.1.3
networks:
testing_net:
ipam:
driver: default
config:
- subnet: 172.28.0.0/16

47. Capture หน้าจอ Remote Desktop ที่ใช้งาน PI

เปิด irfanview เข้าเมนู Options/ Capture/Screenshort คลิก Start
เปิดหน้าจอที่ต้องการจับภาพ แล้ว กด Ctrl-F11 พร้อมกัน

48. Dhcpcd stop เพราะ Docker
dhcpcd.service: Main process exited, code=killed, status=11/SEGV

dhcpcd.service: Failed with result ‘signal’.

ref: https://forums.raspberrypi.com/viewtopic.php?t=305640
เพราะว่า This is what’s happening.
The behavior of dhcpcd is to try to IP-config every interface that is live in the system. Bridges are an exception somehow; older versions of dhcpcd used to jump in when a bridge was created, but now you need to declare the interface in dhcpcd.conf for it to get configured.

Nevertheless Docker usually creates bridges, but also the veth pairs that are used to connect containers to said bridges.
Dhcpcd doesn’t see the veths that are within containers, but it sees the other halves, plugged into the bridges in the host. And it tries to configure them.

Of course the bridge(s) created by docker are not connected to a regular DHCP network, so dhcpcd fails at DHCP config, and falls back to zeroconf. This takes time and perhaps Docker fights back. Meanwhile, bridge members must not have an IP config, and dhcpcd doesn’t care if the interface has a master or not, it just tries again and again.

So until you exclude veth from being processed by dhcpcd, it is super busy disrupting networking in the host, and possibly for the containers as well.

แก้โดยใส่บรรทัด
denyinterfaces veth*

ในไฟล์ /etc/dhcpcd.conf

ก่อน Interface eth0

49. Docker SWARM

50. SDcard Copy

51. ทำ Server ต่างๆ ดูจาก Debian https://www.server-world.info/en/note?os=Debian_11&p=desktop&f=7

52. Neofetch
ติดตั้งด้วยคำสั่ง
sudo apt install neofetch

เรียกใช้ neofetch

53.ปรับ Resolution หน้าจอ Desktop

54. Test Speed SDcard

sudo apt update
sudo apt install agnostics

55. ติดตั้ง FTP Server

จริงๆ มี ftp ผ่าน ssh อยู่แล้ว เช่น ใช้ผ่าน filezilla

Host ใส่ sftp:// นำ

ดูของ Ubuntu ประกอบ

56. Raspberry Pi 64 bit

ติก choose os เลือกเป็น Raspberry Pi OS(64-bit)

คลิกที่รูป Icon ฟันเฟืองจะมี Advance Option ให้เลือก

เลือก for this session only, ติกเลือก Enable SSH, Use password authentication

ติก Set username and password ให้ป้อน password ที่ต้องการ

ติก Set locale settings เลือก Time zone เป็น Asia/Bangkok และ Keyboard layout เป็น US

Persistent settings ก็ไว้ตามเดิม แล้วคลิก SAVE เริมคลิก WRITE เพื่อให้โปรแกรมเขียน Image ลงบน SSD จนเสร็จ แล้วก็สามารถนำไปใช้ได้

57 การใช้ Chirpstack กับ Influxdb 2.X

ตั้งค่า integration ของ Chirpstack ตามรูป

Token ได้จากเข้า webadmin influxdb http://192.168.1.100:8086/

สร้าง Bucket ขึ้นมาก่อน เช่นสร้างชื่อ bucket

ไปที่ API Token

กำหนดสิทธิ์ของ token bucket

คลิกชื่อ bucket และ Copy token ไปใส่ใน Chirpstack

58. ติดตั้ง Firefox

sudo apt update

sudo apt install firefox-esr

59. SD Card Copier

Ghost SSD ที่ run อยู่ได้แบบ Real time

Copy จากความจุน้อยไปความจุมาก ขยายขนาดให้อัตโนมัติ

ถ้า Ghost จาก External SSD ไป External SSD สำหรับ Raspberry PI 4 อยู่ในกล่อง Argon ONE M2 Port USB 3.0 อาจจะไม่พอหรือเกยกันตอนเสียบ แนะนำให้ใช้ PI3 ในกล่องพลาสติกใส

60. Docker run hello-world

pi@raspberrypi:~ $ sudo docker run hello-world
Unable to find image ‘hello-world:latest’ locally
latest: Pulling from library/hello-world
7050e35b49f5: Pull complete
Digest: sha256:bfea6278a0a267fad2634554f4f0c6f31981eea41c553fdf5a83e95a41d40c38
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the “hello-world” image from the Docker Hub.
(arm64v8)
3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/get-started/

61. sudo docker run -it ubuntu bash
Unable to find image ‘ubuntu:latest’ locally
latest: Pulling from library/ubuntu
185e8a4c1005: Pull complete
Digest: sha256:9101220a875cee98b016668342c489ff0674f247f6ca20dfc91b91c0f28581ae
Status: Downloaded newer image for ubuntu:latest

จะเข้า bash #

62. No-IP ไม่ใช้ Docker

mkdir /home/pi/noip
cd /home/pi/noip
wget https://www.noip.com/client/linux/noip-duc-linux.tar.gz
tar vzxf noip-duc-linux.tar.gz
cd noip-2.1.9–1
sudo make
sudo make install
sudo /usr/local/bin/noip2
To confirm that the service is working properly you can run the following command.
sudo noip2 ­-S (Capital “S”)

ตั้งให้ทำงานตอน Start คลิก

sudo nano /etc/rc.local

ใส่ /usr/local/bin/noip2 ก่อนบรรทัดสุดท้าย EXIT

63. หลังลง raspbian เสร็จ Pi จะเรียกโปรแกรมเพื่อ Config เครื่อง เช่น Password Pi, Locale, Keyboard การ SCAN หา Wifi ตั้งค่า Connect wifi และปรับขนาดหน้าจอ Desktop เพื่อให้พอดีขอบ โปรแกรม Config ตัวนี้ สามารถเรียกตรงให้ทำงานได้โดย

sudo piwiz

64.git clone

มีสองทางเลือกคือใช้ git ให้ Copy url .git แล้วใช้คำสั่ง git clone

git clone https://github.com/kaciker/iiOT-All-in-ONE.git

อีกทางคือ Download zip ด้วยคำสั่ง wget ให้ชี้ไปที่ Download ZIP แล้วคลิกขวา Copy URL .zip แล้วใช้คำสั่ง


wget https://github.com/kaciker/iiOT-All-in-ONE/archive/refs/heads/main.zip

แล้ว unzip main.zip

65.สั่งทำงาน docker-compose.yml

sudo docker-compose up -d

66. วิธี Clone SSD Raspbian

ใช้ PI 3 B+ มี Port USB หลายอัน และ boot จาก SDCard

แล้วใช้ SD Card Copier บน Raspbian Copy

67.หน้าจอ Desktop ล้นจอด้านบนและซ้ายจอทีวี

ให้ On Underscan ใน Display

68.ต่อ Wifi ไม่ได้ให้ใช้
sudo piwiz แล้วเชื่อมแทน SSID แทน

69. SSD บูทผ่าน Port USB 3.0 ไม่ได้ แต่บูทผ่าน Port USB 2.0 ได้

อ่าน https://www.pragmaticlinux.com/2021/03/fix-for-getting-your-ssd-working-via-usb-3-on-your-raspberry-pi/ หัวข้อ Disable the USB Attached SCSI Protocol (UASP) on your Raspberry PI 4

สั่ง lsusb

ได้ 152d:0578 ก่อน Micron Technology Corp

แก้ไฟล์ sudo nano /boot/cmdline.txt

ใส่ usb-storage.quirks=152d:0578:u

ไว้ด้านหน้าบรรทัด แล้ว Save

หรือหา USB to SATA 3 ที่รองรับ UASP https://jamesachambers.com/raspberry-pi-4-usb-boot-config-guide-for-ssd-flash-drives/

70. ติดตั้ง Telegraf ด้วย IoT stack

https://sensorsiot.github.io/

curl -fsSL https://raw.githubusercontent.com/SensorsIot/IOTstack/master/

./menu.sh
docker-compose up -d

pico docker-compose.yml

version: ‘3.6’
services:
telegraf:
container_name: telegraf
build: ./.templates/telegraf/.
restart: unless-stopped
environment:
— TZ=Etc/GMT+7
ports:
— “8092:8092/udp”
— “8094:8094/tcp”
— “8125:8125/udp”
volumes:
— ./volumes/telegraf:/etc/telegraf
— /var/run/docker.sock:/var/run/docker.sock:ro

71. mosquitto Client

ติดตั้งด้วยคำสั่ง

sudo apt install -y mosquitto-clients

เฝ้ารับข้อมูลด้วยคำสั่ง

mosquitto_sub -h localhost -t “/sensor/tempurature”

ส่งข้อมูลด้วยคำสั่ง

mosquitto_pub -h localhost -t “/sensor/tempurature” -m hello

หรือใช้คำสั่งใน Container Mosquitto ของ Chirpstack ชื่อ containner คือ chirpstack-docker_mosquitto_1

docker container exec chirpstack-docker_mosquitto_1 mosquitto_pub -t “/sensor/tempurature” -m hello

รอรับข้อมูลด้วยคำสั่ง

mosquitto_sub -h localhost -t “/sensor/tempurature” หรือdocker container exec chirpstack-docker_mosquitto_1 mosquitto_sub -h localhost -t “/sensor/tempurature”

ส่งข้อมูลแบบ JSON

docker container exec chirpstack-docker_mosquitto_1 mosquitto_pub -t “/sensor/tempurature” -m “{\”temp\”: 25, \”humid\”: 52}”

ใช้ mqtt server ฟรี broker.hivemq.com

mosquitto_sub -h broker.hivemq.com -t “/sensor/tempurature”
mosquitto_pub -h broker.hivemq.com -t “/sensor/tempurature” -m hello

ใช้ mqtt server ฟรี broker.emqx.io

mosquitto_sub -h broker.emqx.io -t “/sensor/tempurature”
mosquitto_pub -h broker.emqx.io -t “/sensor/tempurature” -m hello

72. Config Telegraf

73. telegraf

docker container exec telegraf telegraf — debug

docker container exec telegraf telegraf — debug -test

docker container exec telegraf cat /etc/telegraf/telegraf.conf

docker logs telegraf

docker container logs -f --since 10m telegraf

แก้ Config

73. ติดตั้งและกำหนดค่า Telegraph

สร้างไฟล์ telegraf.conf

[agent]
interval = “10s”
round_interval = true
metric_batch_size = 1000
metric_buffer_limit = 10000
collection_jitter = “0s”
flush_interval = “10s”
flush_jitter = “0s”
precision = “”
hostname = “”
omit_hostname = false
[[inputs.docker]]
endpoint = “unix:///var/run/docker.sock”
gather_services = false
container_names = []
source_tag = false
container_name_include = []
container_name_exclude = []
timeout = “5s”
perdevice = true
total = false
docker_label_include = []
docker_label_exclude = []
tag_env = [“JAVA_HOME”, “HEAP_SIZE”]
[[outputs.influxdb_v2]]
urls = [“http://192.168.1.100:8086"]
token = “GVyjoMQJdjP3C6sNMO8Q7QxcJApXjWn4HksmRaTz6ypjEaobP841qSFDvuJ2lLosCTfdtIX5nBrpLv1YID3HPg==”
organization = “influxdata”
bucket = “tablerejeee”

แก้ใน [[outputs.influxdb_v2]] ให้ตรงกับ influx

run คำสั่งต่อไปนี้ใน Dir ที่มีไฟล์ telegraf.conf

docker run -d \
--name=telegraf \
--hostname=telegraf \
--user telegraf:$(stat -c '%g' /var/run/docker.sock) \
-p 8125:8125/udp \
-p 8092:8092/udp \
-p 8094:8094 \
-v $PWD/telegraf.conf:/etc/telegraf/telegraf.conf:ro \
-v /var/run/docker.sock:/var/run/docker.sock \
-e TZ=Etc/GMT+7 \
--restart unless-stopped \
telegraf

อ้างอิง https://fictionbecomesfact.com/telegraf-container-monitor-docker

ดูผลการ run จาก Log
docker logs telegraf

72. Run คำสั่งใน Container

docker exec -u root -it telegraf /bin/sh -c “chmod 666 /var/run/docker.sock”

sudo docker exec -it InfluxDB influx

InfluxDB ตัว I,DB ใหญ่

73. ดู Oraganization Influxdb

sudo docker exec -it InfluxDB influx org ls

ยัง Error ไม่มี Authorize

74.Telegraf with TTN

Save raw
cd ..sudo wget https://raw.githubusercontent.com/InfluxCommunity/plant_buddy/master/microcontroller/thing_network/telegraf
-mqtt-thing-network.conf

แก้ Input TTN
User Name /Password

ให้ Copy จาก MQTT

Public Address :eu1.cloud.thethings.network:1883
Public TLS Address :eu1.cloud.thethings.network:1883
User Name:myrejeee@ttn
Password: XXXXXXX

75. SCAN Network

sudo apt install arp-scan
sudo arp-scan -xq 192.168.1.0/24

sudo nmap -sn 192.168.1.0/24 | grep “Nmap scan report for\|MAC”

76. RUN NO IP ใต้ docker
ดูจาก Link https://hub.docker.com/r/hypriot/rpi-noip

ใช้คำสั่งเดียว

docker run -ti -v "noip:/usr/local/etc/" --restart=always  hypriot/rpi-noip noip2 -C

[sudo] password for slvp:
Using default tag: latest
latest: Pulling from hypriot/rpi-noip
Image docker.io/hypriot/rpi-noip:latest uses outdated schema1 manifest format. Please upgrade to a schema2 image for better future compatibility. More information at https://docs.docker.com/registry/spec/deprecated-schema-v1/
38070c4d0c33: Pull complete
a3ed95caeb02: Pull complete
7901c4c17622: Pull complete
Digest: sha256:32c42dc8a4ac8dae240471f62a767bc6fefcc14296f5ae240262c8b1431a2761
Status: Downloaded newer image for hypriot/rpi-noip:latest
docker.io/hypriot/rpi-noip:latest

WARNING: The requested image’s platform (linux/arm) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested

Auto configuration for Linux client of no-ip.com.

Please enter the login/email string for no-ip.com m2m@gmail.com
Please enter the password for user ‘m2m@gmail.com’ ********

Only one host [m2mlorawan.ddns.net] is registered to this account.

It will be used.
Please enter an update interval:[30]
Do you wish to run something at successful update?[N] (y/N) ^M

New configuration file ‘/usr/local/etc/no-ip2.conf’ created.

77. แสดงหมายเลข IP No. ที่ได้ของ Router internet

เปิดเวป https://portchecker.co/

ping m2mlorawan.ddns.net

78. เพิ่ม Keyboard ภาษาไทยบน Desktop

อ่าน Link https://playelek.com/rpi-th-keyboard/
เลือกปุ่มสลับภาษาเป็น Alt+shift

79. ดู CPU ด้วยคำสั่ง lscpu

80.ดู Software ล่าสุดสำหรับ Docker
เช่น https://hub.docker.com/_/influxdb
https://hub.docker.com/r/grafana/grafana

81.ThinksBoard

Browse to http://{host-ip}:9090 to access the web interface.

  • System Administrator: sysadmin@thingsboard.org / sysadmin
  • Tenant Administrator: tenant@thingsboard.org / tenant
  • Customer User: customer@thingsboard.org / customer

82. ใช้ NAS จาก Raspbian

คลิก GO, Network

83. Exec คำสั่ง Container บน Docker

sudo docker exec -it grafana uname -a
Linux 304d23403aba 6.1.0-rpi6-rpi-v8 #1 SMP PREEMPT Debian 1:6.1.58-1+rpt2 (2023-10-27) aarch64 Linux
sudo docker exec -it influxdb uname -a
Linux c1a64b3dc751 6.1.0-rpi6-rpi-v8 #1 SMP PREEMPT Debian 1:6.1.58-1+rpt2 (2023-10-27) aarch64 GNU/Linux
sudo docker exec -u root  -it influxdb /bin/sh
# ls
LICENSE README.md bin boot dev docker-entrypoint-initdb.d entrypoint.sh etc home influx influxdb2-2.7.3 lib media mnt opt proc root run sbin srv sys tmp usr var
# whoami
root
# influx
NAME:
influx - Influx Client

USAGE:
influx [command]

HINT: If you are looking for the InfluxQL shell from 1.x, run "influx v1 shell"

COMMANDS:
version Print the influx CLI version
write Write points to InfluxDB
bucket Bucket management commands
completion Generates completion scripts
query Execute a Flux query
config Config management commands
org, organization Organization management commands
delete Delete points from InfluxDB
user User management commands
task Task management commands
telegrafs List Telegraf configuration(s). Subcommands manage Telegraf configurations.
dashboards List Dashboard(s).
export Export existing resources as a template
secret Secret management commands
v1 InfluxDB v1 management commands
auth, authorization Authorization management commands
apply Apply a template to manage resources
stacks List stack(s) and associated templates. Subcommands manage stacks.
template Summarize the provided template
bucket-schema Bucket schema management commands
scripts Scripts management commands
ping Check the InfluxDB /health endpoint
setup Setup instance with initial user, org, bucket
backup Backup database
restore Restores a backup directory to InfluxDB
remote Remote connection management commands
replication Replication stream management commands
server-config Display server config
help, h Shows a list of commands or help for one command
pi@raspberrypi:~ $ sudo docker exec -u root  -it chirpstack-docker-chirpstack-1 /bin/sh
/ # ls
bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var

84.Install Raspberry Pi OS Desktop on a Virtual Machine (VirtualBox)

https://roboticsbackend.com/install-raspbian-desktop-on-a-virtual-machine-virtualbox/#Download_Raspberry_Pi_OS_Desktop_ISO

Test.

  1. ตัวเดิมๆ
    git clone https://github.com/brocaar/chirpstack-docker.git
    cd chirpstack-docker
    git clone https://github.com/kaciker/iiOT-All-in-ONE.git
  2. แนะนำให้ติดตั้งจากที่ แก้ alway restart, internal fix ip และ AS923 แล้ว
    git clone https://github.com/m2mlorawan/chirpstack-docker.git
    cd chirpstack-docker
    sudo docker-compose up -d
  3. ตัด Chirpstack ออก
    git clone https://github.com/m2mlorawan/iiOT-All-in-ONE.git
    cd iiOT-All-in-ONE
    sudo docker-compose up -d

http://192.168.1.100:8080/#/network-servers/1

--

--

Somsak Lima

สนับสนุนและส่งเสริมให้ผู้สนใจสามารถใช้งานเทคโนโลยี LoRa และ LoRaWAN ได้ โดยนำความรู้ที่ได้ไปต่อยอดเพื่อใช้งาน