### 制作自己的树莓派镜像
采用win32diskimager-1.0.0-install.exe进行镜像制作工作
```
sudo dd if=/dev/sdb of=/your/path/to/customOS.img
wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh
chmod +x pishrink.sh
sudo mv pishrink.sh /usr/local/bin
sudo pishrink.sh /your/path/to/CustomOS.img /your/path/to/CustomOS-shrink.img
```
### loar调试
- https://www.cnblogs.com/arthaslcm/p/15624781.html
- LoRaWAN服务器(chirpstack) https://github.com/brocaar/chirpstack-docker.git 修改docker-compose.yml version 中的3改成2
- LoRaWAN网关(https://github.com/Lora-net/lora_gateway https://github.com/Lora-net/packet_forwarder
- LoRaWAN终端选型(https://www.ebyte.com/product-class.html?cid=1&pid=184)
- 从零搭建lora网关
https://www.cnblogs.com/answerinthewind/p/13154904.html
- lora网关对接nodered
https://shumeipai.nxez.com/2023/01/16/gw1302s-lora-module-and-raspberry-pi-implement-lora-gateway.html
- lorawan对接thingsboard
https://www.cnblogs.com/arthaslcm/p/15624781.html
## tb+tb gateway+emqx+mqttx 搭建
### thingsboard环境搭建
- http://www.ithingsboard.com/docs/user-guide/install/docker/
- http://www.ithingsboard.com/docs/user-guide/install/docker/?ubuntuThingsboardQueue=inmemory
-
- 搭建网址1:https://blog.youkuaiyun.com/qq_18235445/article/details/127357268
- 搭建网址2:https://www.cnblogs.com/thingskit/p/13065330.html
```
本次演示环境Windows 10,TB版本为Thingsboard 3.0,Thingsboard-gateway版本为2.3.1.1,采用EMQ X作为MQTT broker,用MQTT box作为演示设备。
### 环境准备:
- thingsboard3.0源码安装
下载地址:https://github.com/thingsboard/thingsboard/releases
教程:https://www.iotschool.com/wiki/tbinstall 和 https://www.iotschool.com/topics/732
- 安装thingsboard-gateway2.3.1.1,特别说明:本地运行TB-gateway需要安装Python3.5以上的环境才能运行
下载地址:https://github.com/thingsboard/thingsboard-gateway/releases
教程:https://thingsboard.io/docs/iot-gateway/install/source-installation/
- 安装EMQ X
下载地址:https://github.com/emqx/emqx/releases
下载地址2:https://www.emqx.com/zh/downloads/broker/
教程:https://docs.emqx.io/broker/latest/cn/getting-started/install.html##zip
- 安装MQTT Box
下载地址:https://www.iotschool.com/topics/553
### 一切准备就绪,现在开始
#### 1、由于TB默认占用了1883端口,所以需要更改EMQ X的默认端口
配置文件位置:

listener.tcp.external =
找到后,将本行修改为:
listener.tcp.external = 127.0.0.1:1884
```
### thingsboard 安装
prepare.sh
```
#!/bin/bash
mkdir -p .mytb-data && sudo chown -R 799:799 .mytb-data
mkdir -p .mytb-logs && sudo chown -R 799:799 .mytb-logs
```
docker-compose.yml
```
version: '2.2'
services:
mytb:
restart: always
image: "thingsboard/tb-postgres"
ports:
- "8080:9090"
- "1884:1883"
- "5683:5683/udp"
environment:
TB_QUEUE_TYPE: in-memory
volumes:
- ~/docker-work/thingsboard/.mytb-data:/data
- ~/docker-work/thingsboard/.mytb-logs:/var/log/thingsboard
```
- 注意这里修改了1883端口改成了对应的1884,由于该端口会跟对应的emqx复用
docker-compose up -d
127.0.0.1:8080
```
### thingsboard gateway 安装
https://github.com/thingsboard/thingsboard-gateway
tb-gateway install
```
https://thingsboard.io/docs/iot-gateway/installation/
wget https://github.com/thingsboard/thingsboard-gateway/releases/latest/download/python3-thingsboard-gateway.deb
sudo apt install ./python3-thingsboard-gateway.deb -y
```
修改/etc/thingsboard-gateway/config/tb_gateway.yaml
```
thingsboard:
host:xxx.xxx.xxx.xxx #修改为tb的地址
port:1884 #从1883修改为对应的1884,由于对应的tb的端口改成了1884
```
修改/etc/thingsboard-gateway/config/mqtt.json
```
"host": "xxx.xxx.xxx.xxx" #修改为对应的emqx的地址
```
### emqx 安装
https://www.emqx.io/docs/zh/v5.0/deploy/install-ubuntu.html
127.0.0.1:18083
### 树莓派
https://www.raspberrypi.com/
#### 代码
https://github.com/orgs/raspberrypi/
- 密码问题:
- https://lingshunlab.com/book/raspberry-pi/new-raspberry-pi-os-initialize-account-and-password
- 创建userconf.txt 里面内内容如下:
- pi:$6$4ilokQRQxmURT.py$aJWBQ5yniJJPwV3CKawYJcnSK5JZGhrVZYF3K4omRUFv6KL0MysEH7F4NZRMNMcYF.U3xsQvWrx7ZL2GKxuv.1
- echo 'mypassword' | openssl passwd -6 -stdin
- ssh默认开启问题
- 创建ssh文件
#### 更换仓库源
deb https://mirror.nju.edu.cn/debian/ bullseye main contrib non-free
deb https://mirror.nju.edu.cn/debian-security/ bullseye-security main contrib non-free
deb https://mirror.nju.edu.cn/debian bullseye-updates main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian bullseye main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian bullseye-updates main contrib non-
#### ubuntu安装
step1:安装ubuntu20.04
step2:安装docker
```
sudo apt-get install ca-certificates curl gnupg lsb-release
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
```
step3:卡住,切换阿里源
```
sudo apt-get remove docker docker-engine docker-ce docker.io
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin docker-compose
```
step4:docker的代理
/etc/docker/daemon.json
```
{
"registry-mirrors": [
"http://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn",
"https://registry.docker-cn.com"
]
}
```
step5:增加普通用户到docker用户组中
```
sudo gpasswd -a ${USER} docker
sudo systemctl restart docker
```
### windows下启动lorawan(管理员用户运行)
```
https://www.erlang.org/ (otp_win64_22.3.exe)
用户变量path:C:\Program Files\erl10.7\bin
https://github.com/gotthardp/lorawan-server/releases/download/v0.6.7/lorawan-server-0.6
.7.tar.gz
lorawan-server.bat
127.0.0.1:8080
```
### raspberry下安装lorawan
- 在树莓派32位系统上需要参考如下安装erlang包,如果是在64位系统上直接安装即可
/etc/apt/source.list
```
deb http://packages.erlang-solutions.com/debian bullseye contrib
```
```
wget https://packages.erlang-solutions.com/ubuntu/erlang_solutions.asc
sudo apt-key add erlang_solutions.asc
sudo apt-get update
sudo apt-get install erlang
wget https://github.com/gotthardp/lorawan-server/releases/download/v0.6.7/lorawan-server_0.6.7_all.deb
sudo dpkg -i lorawan-server_0.6.7_all.deb
sudo systemctl enable lorawan-server
sudo systemctl start lorawan-server
```
### windows下安装 mqttbrocker(管理员用户运行)
```
JAVA安装配置
系统变量JAVA_HOME:C:\Program Files\Java\jdk1.8.0_91
用户变量path:%JAVA_HOME%\bin
镜像下载地址:http://archive.apache.org/dist/activemq/activemq-apollo/1.7.1
cd C:\Program Files\apache-apollo-1.7.1\bin
apollo.cmd create mybroker
cd C:\Program Files\apache-apollo-1.7.1\bin\mybroker\bin
apollo-broker.cmd run
C:\Program Files\apache-apollo-1.7.1\bin\mybroker\etc\users.properties
https://127.0.0.1:61681 (admin password)
http://127.0.0.1:61680 (admin password)
```
### mqttx client安装
https://pan.baidu.com/s/1S_l4uDDklUOWXpdVwIuqnA
### 设备端节点设置
AT+ADDR=ABC12345
AT+NWKSKEY=FEDCBA9876543210FEDCBA9876543210
AT+APPSKEY=FEDCBA9876543210FEDCBA9876543210
AT+CHANRNG=0,7
AT+CLASS=A
AT+JOIN
### 野火系列
```
1、野火资料全部开源,可免费下载
野火所有产品资料链接:
https://doc.embedfire.com
第一个链接点进去,找到对应产品再点进去就有下载链接的了
2、教学视频可直接B站在线观看,无需下载:https://space.bilibili.com/356820657
3、野火WX公众号:野火电子,欢迎关注,用于通知资料的更新。
4、野火论坛:www.firebbs.cn
5、学习交流群,
(1)STM32:410127420,(2)Linux:959456691,(3)FPGA:759373727,(4)瑞萨单片机:574831257,企鹅群欢迎加入讨论学习。
野火所有产品资料链接:
http://doc.embedfire.com/products/link/zh/latest/index.html
教学视频可直接B站在线观看,无需下载: https://space.bilibili.com/356820657
为更加方便的下载资料,请安装“野火大学堂”客户端,下载资料速度远超百度云,还可以在线看视频
https://www.firebbs.cn/forum.php?mod=viewthread&tid=29500 (复制到浏览器打开)
```
#### 1.适配rtl8821cu usb网卡
https://blog.youkuaiyun.com/whyliu_/article/details/127157144
step1:文件追加/lib/udev/rules.d/40-usb_modeswitch.rules
```
# Realtek rtl8821cu
ATTR{idVendor}=="0bda", ATTR{idProduct}=="1a2b", RUN+="usb_modeswitch '/%k'"
```
step2:创建文件/etc/usb_modeswitch.d/0bda:1a2b
```
# Realtek rtl8211cu
TargetVendor=0x0bda
TargetProduct=0xc820
StandardEject=1
```
#### 2.修改背景,串口登录打印
cp /etc/alternatives/desktop-theme/grub/grub-16x9.png /etc/alternatives/lubancat-wallpaper.png (背景)
rm /etc/alternatives/lubancat-logo.png (greeter登录头像)
logo_kernel.bmp (内核启动logo)
vi /etc/issue (串口登录显示)
vi /etc/update-motd.d/00-header (串口登录显示)
- 用户名@主机名
vi /etc/hosts
vi /etc/hostname
#### 3.调整232/485
vi /boot/uEnv/uEnv.txt
```
dtoverlay=/dtb/overlay/rk356x-lubancat-uart3-m1-overlay.dtbo
dtoverlay=/dtb/overlay/rk356x-lubancat-uart4-m1-overlay.dtbo
dtoverlay=/dtb/overlay/rk356x-lubancat-uart7-m1-overlay.dtbo
dtoverlay=/dtb/overlay/rk356x-lubancat-uart9-m1-overlay.dtbo
```
vi /boot/extlinux/extlinux.conf
```
console=tty1 consoleblank=0 loglevel=7 改成 quiet splash
```
#### 4.用户密码修改
sed -i 's/autologin-user=cat/autologin-user=debian/g' /etc/lightdm/lightdm.conf
userdel -r cat
adduser debian
```
uid=1000(cat) gid=1000(cat) groups=1000(cat),4(adm),20(dialout),24(cdrom),27(sudo),29(audio),30(dip),44(video),46(plugdev),50(staff),101(systemd-journal),109(bluetooth),110(netdev),116(pulse-access)
usermod -G adm,dialout,cdrom,sudo,audio,dip,video,plugdev,staff,systemd-journal,bluetooth,netdev,pulse-access debian debian需要增加到哪些用户组和用户名
```
#### 5.4g模块配置
```
https://doc.embedfire.com/linux/rk356x/quick_start/zh/latest/quick_start/wireless/4g/4g.html
------------------------------------------------------------------------------------------
##检查sim卡是否正常工作
#在后台显示/dev/ttyUSB2返回的信息(如果想退出后台显示,使用后面的命令:pkill cat)
cat /dev/ttyUSB2 &
#检查sim卡的状态
echo -e "AT+CPIN?\r\n" > /dev/ttyUSB2
#如果返回值为如下即为获取sim卡成功
+CPIN: READY
OK
#如果返回值为如下即为获取sim卡失败
+CME ERROR: 10
+CME ERROR: 13
------------------------------------------------------------------------------------------
## 获取当前网卡模式
#查询当前模式
#在后台显示/dev/ttyUSB2返回的信息(如果想退出后台显示,使用后面的命令:pkill cat)
cat /dev/ttyUSB2 &
#查询当前网卡的模式
echo -e "AT+QCFG=\"usbnet\"\r\n" > /dev/ttyUSB2
#例子:
root@lubancat:~# cat /dev/ttyUSB2 &
[1] 1155
root@lubancat:~# echo -e "AT+QCFG=\"usbnet\"\r\n" > /dev/ttyUSB2
root@lubancat:~#
+QCFG: "usbnet",3
#解析
可以看到返回了3,这个是网卡在RNDIS模式
------------------------------------------------------------------------------------------
##配置模块网卡模式为ECM
#在后台显示/dev/ttyUSB2返回的信息(如果想退出后台显示,使用后面的命令:pkill cat)
cat /dev/ttyUSB2 &
#配置为ECM模式(返回:OK 代表配置成功)
echo -e "AT+QCFG=\"usbnet\",1\r\n" > /dev/ttyUSB2
#重启模块(重启模块才能生效)
echo -e "AT+CFUN=1,1\r\n" > /dev/ttyUSB2
#重启后可以查看配置是否成功生成
------------------------------------------------------------------------------------------
## 配置模块网卡模式为RNDIS
#在后台显示/dev/ttyUSB2返回的信息(如果想退出后台显示,使用后面的命令:pkill cat)
cat /dev/ttyUSB2 &
#配置为ECM模式(返回:OK 代表配置成功)
echo -e "AT+QCFG=\"usbnet\",3\r\n" > /dev/ttyUSB2
#重启模块(重启模块才能生效)
echo -e "AT+CFUN=1,1\r\n" > /dev/ttyUSB2
#重启后可以查看配置是否成功生成
------------------------------------------------------------------------------------------
设置成ecm模式后,然后重新刷固件即可。
```
### 编译环境搭建
```
step1:
解压对应的虚拟机"鲁班猫\8-SDK源码压缩包\开发环境虚拟机镜像"
或者
拷贝"鲁班猫\8-SDK源码压缩包、LubanCat_Linux_SDK_20230419.7z"到虚拟机的/home/lubancat/LubanCat_SDK/目录下,执行"7z x LubanCat_Linux_SDK_20230419.7z" --> ".repo/repo/repo sync -l"
step2: 拷贝文件系统补丁目录ubuntu_rootfs_patch到/home/lubancat/LubanCat_SDK/ubuntu,同时在该目录下执行git am ubuntu_rootfs_patch/*.patch
step3: 拷贝内核补丁目录lubancat_kernel_patch到/home/lubancat/LubanCat_SDK/kernel,同时在该目录下执行git am lubancat_kernel_patch/*.patch
step4: ./build.sh launch (选中13)
step5: ./build.sh ubuntu && ./build.sh
step6: 拷贝rockdev/update.img进行烧录即可。
```
#### 1.环境搭建
```
apt update
apt install python3-pip sudo rpm dos2unix git
pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
python3 -m pip install --no-cache-dir --upgrade pip && python3 -m pip install --no-cache-dir --upgrade setuptools && python3 -m pip install --no-cache-dir importlib_metadata && python3 -m pip install --no-cache-dir -r requirements.txt && python3 -m pip install --no-cache-dir pymmh3 && python3 -m pip install --no-cache-dir mmh3 && python3 -m pip install --no-cache-dir tb-mqtt-client
python3 setup.py install
source generate_deb_package.sh
dpkg -i python3-thingsboard-gateway.deb
apt install -f
```
pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
echo sqlalchemy >> requirements.txt
echo construct >> requirements.txt
pip3 install -r requirements.txt
pip3 install pymodbus==3.2.2
pip3 install protobuf==4.22.3
python3 setup.py
python3 thingsboard_gateway/tb_gateway.py
#### 2.emqx 安装ubuntu20.04
wget https://www.emqx.com/zh/downloads/broker/v5.0.26/emqx-5.0.26-ubuntu20.04-arm64.deb
dpkg -i emqx-5.0.26-ubuntu20.04-arm64.deb
### 编译deb包(未验证)
sudo apt install rpm dos2unix
dos2unix make_packages.sh && source make_packages.sh
dos2unix generate_deb_package.sh && source generate_deb_package.sh
sudo chown $USER:$USER -R .
### ubuntu的定制
#### lightdm自动登录
/etc/lightdm/lightdm.conf
```
[Seat:*]
autologin-guest=false
autologin-user=admin
autologin-user-timeout=0
```
##### 问题1
```
docker里面安装tb gateway失败原因
echo $USER是空的,可以在~/.bashrc最后一行加上export USER=root
```
#### 问题2
- 网络优先级设置
- ip route show
- 10-globally-managed-devices.conf
```
[keyfile]
unmanaged-devices=*,except:type:wifi,except:type:wwan,except:type:ethernet,except:type:gsm
[device]
interface-name=usb0
autoconnect-priority=602
[device]
interface-name=usb1
autoconnect-priority=603
[device]
interface-name=eth0
autoconnect-priority=101
[device]
interface-name=eth1
autoconnect-priority=100
[device]
interface-name=p2p0
autoconnect-priority=600
[device]
interface-name=wlan0
autoconnect-priority=601
```