树莓派做一个获取GPS时间的NTP服务器

由于单位用的是内部网络,机器时间无法与internet同步,导致内网的所有设备各自为政,对工作和管理带来的麻烦说不清,为解决这个问题,决定用手里的树莓派做通过GPS获取时间,然后提供NTP服务。在某宝逛了一圈,找到一个GPS模块L76X GPS Module,91包邮,就下手拿来做测试吧!。
在这里插入图片描述

第一步:烧录系统

下载烧录树莓派系统,到官网下载了一个Raspberry Pi Imager,烧录的最新的系统;本文用的是包含桌面版的的镜像(Raspberry Pi OS),烧录好以后拔出读卡器,再插回去,修改一下无线鼠标卡顿的问题。
打开 /boot/cmdline.txt,在最后面加上空格在输入 usbhid.mousepoll=0;然后把TF卡插入树莓派开启做初始化设置。

第二步:接线

将模块上的接口依次连接到树莓派上GPIO:

树莓派GPS模块
GPIO18PPS
GPIO14(UART_TXD)RX
GPIO15(UART_RXD)TX
GNDGND
5VVCC

如下图接法:
在这里插入图片描述
这是我接好的照片:
在这里插入图片描述

第三步:配置树莓派串口

1、关闭串口登录功能
关闭树莓派的串口登陆功能,使得树莓派可以使用GPIO 14和GPIO15作为串口通信。
进入系统后,进行配置:

sudo raspi-config

选择【Interface Options】找到【Serial Port】回车进入,第一个问使用串口登录(关闭),第二个问是否开启串口(开启)

2、关闭蓝牙功能
需要注意的是Raspberry-Pi 3相比于1和2在使用串口的时候会有问题

原因是树莓派CPU内部有两个串口,一个是硬件串口(官方称为PL011
UART),一个是迷你串口(官方成为mini-uart)。在树莓派2B/B+这些老版树莓派上,官方设计时都是将“硬件串口”分配给GPIO中的UART(GPIO14&GPIO15),因此可以独立调整串口的速率和模式。而树莓派3的设计上,官方在设计时将硬件串口分配给了新增的蓝牙模块上,而将一个没有时钟源,必须由内核提供时钟参考源的“迷你串口”分配给了GPIO的串口,这样以来由于内核的频率本身是变化的,就会导致“迷你串口”的速率不稳定,这样就出现了无法正常使用的情况。

A、修改/boot/config.txt

sudo nano /boot/config.txt

把 enable_uart=0 改为 enable_uart=1;
在最后面添加

dtoverlay=pi3-miniuart-bt
force_turbo=1

B、修改/boot/cmdline.txt

sudo nano /boot/cmdline.txt

删除所有的console=xxx的语句,例如将

console=tty1 root=PARTUUID=06850cc7-02 rootfstype=ext4 fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles usbhid.mousepoll=0

改为

root=PARTUUID=06850cc7-02 rootfstype=ext4 fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles usbhid.mousepoll=0

C、修改蓝牙服务 /lib/systemd/system/hciuart.service

sudo nano /lib/systemd/system/hciuart.service

修改[Unit]中的After字段,由

After=dev-serial1.device

改至

After=dev-ttyS0.device

修改[Service]中的ExecStart字段,由

ExecStart=/usr/bin/btuart

改成

ExecStart=/usr/lib/hciattach /dev/ttyS0 bcm43xx 460800 noflow -

注:其中的bcm43xx真的是xx哦

保存退出后,直接reboot重启派,串口接下来可以用作通信了

D、测试串口

cat /dev/ttyAMA0

在这里插入图片描述
如果显示上面的数据,那就证明以上的工作正常了!

第四步:安装PPS-tools

A、PPS-tools

sudo apt-get install pps-tools
sudo apt-get install libcap-dev

B、修改/boot/config.txt

sudo nano /boot/config.txt

在文件的最后加入如下内容保存退出

dtoverlay=pps-gpio,gpiopin=18

C、修改/etc/modules

sudo nano /etc/modules

在文件的最后加入如下内容保存退出

pps-gpio

到此PPS-tools的安装配置完成,reboot重启派测试一下PPS是否正常

D、测试PPS是否正常

使用 lsmod | grep pps 验证模块是否正常加载
如输出下面信息,说明工作正常

在这里插入图片描述

使用 dmesg | grep pps 命令检查 PPS GPIO 是否被配置

若输出如下信息系正常
在这里插入图片描述
使用 sudo ppstest /dev/pps0 将会看到类似下面的输出
在这里插入图片描述
正常情况下每一秒输出一条,如果发现输出间隔不是一秒,可能是连线错误或者 GPS 模块未设置导致的,如果出现time_pps_fetch() error -1 (Connection timed out) 说明信号不好,试试放到室外。

第五步:安装GPSD

sudo apt install gpsd gpsd-clients python3-gps

安装后修改 /etc/default/gpsd 文件

sudo nano /etc/default/gpsd

修改如下:

DEVICES="/dev/ttyAMA0"
GPSD_OPTIONS="-n -G"

配置开机启动服务,修改/lib/systemd/system/gpsd.service文件

sudo nano /lib/systemd/system/gpsd.service

修改[Unit]中的After=chrony.serviceBefore=chrony.service

保存退出后,运行以下命令,设为开机启动

sudo systemctl enable gpsd

再reboot重启以下树莓派

接下来通过运行

cgps -s

来查看gpsd的数据,你将会看到类似下面的输出
在这里插入图片描述

第六步:安装 chrony

sudo apt install chrony -y

安装后修改配置文件 /etc/chrony/chrony.conf

sudo nano /etc/chrony/chrony.conf

在最后加上:

refclock PPS /dev/pps0 lock GPSD prefer refid PPS
refclock SHM 0 offset 0.0 delay 0.2 refid GPSD
allow

并检查一下配置文件是否包含一下内容,如果没有,就添加进去

leapsectz right/UTC
makestep 1.0 -1
rtcsync

配置完成后重启服务

sudo service chrony restart

可以通过下面两个命令来查看连接状态

watch -n1 chronyc sources -v
watch -n1 chronyc sourcestats -v

好了,现在其他设备可以直接把时间同步服务器设为树莓派的IP即可同步了!!写了一个小时,做个记录,以后忘记了可以看看!!

参考资源:
https://www.txisfine.cn/archives/b3317436
https://blog.csdn.net/xiaohu50/article/details/78731534

  • 5
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
For an Arduino-based GPS module with a compass, you can consider using the NEO-6M GPS module along with a digital compass module like HMC5883L. Here are the steps to connect and use them together: 1. Connect the NEO-6M GPS module to your Arduino: - VCC pin to 5V - GND pin to GND - RX pin to any digital pin (e.g., Arduino's pin 2) - TX pin to any digital pin (e.g., Arduino's pin 3) 2. Connect the HMC5883L compass module: - VCC pin to 5V - GND pin to GND - SDA pin to Arduino's A4 (or SDA) pin - SCL pin to Arduino's A5 (or SCL) pin 3. Install the necessary libraries: - For GPS: Install the "TinyGPS++" library by Mikal Hart. - For compass: Install the "Adafruit_Sensor" and "Adafruit_HMC5883_U" libraries by Adafruit. 4. Write the code to read GPS and compass data: - Include the required libraries. - Initialize the GPS and compass objects. - In the loop function, read GPS data using the TinyGPS++ library. - Use the compass library to read compass data. Here's an example code snippet to get you started: ```arduino #include <TinyGPS++.h> #include <Wire.h> #include <Adafruit_Sensor.h> #include <Adafruit_HMC5883_U.h> TinyGPSPlus gps; // GPS object Adafruit_HMC5883_Unified compass; // Compass object void setup() { Serial.begin(9600); Wire.begin(); // Start I2C communication compass.begin(); // Begin compass } void loop() { while (Serial.available() > 0) { if (gps.encode(Serial.read())) { // Read GPS data if (gps.location.isValid()) { Serial.print("Latitude: "); Serial.println(gps.location.lat(), 6); Serial.print("Longitude: "); Serial.println(gps.location.lng(), 6); } } } sensors_event_t event; compass.getEvent(&event); // Read compass data float heading = atan2(event.magnetic.y, event.magnetic.x); if (heading < 0) heading += 2 * PI; if (heading > 2 * PI) heading -= 2 * PI; float headingDegrees = heading * 180 / PI; Serial.print("Heading: "); Serial.println(headingDegrees); delay(1000); } ``` Make sure to adjust the pin numbers and modify the code according to your setup. This code reads GPS latitude and longitude data and compass heading data, which you can then use for navigation or other purposes.
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

୧⍢⃝୨ LonelyCoder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值