树莓派日期时间不准的修正方法

树莓派日期时间不准的修正方法

在树莓派上,打date命令可以看到系统的日期时间:

311415114852764
(后面的CST表示中国标准时间)

树莓派没有电池,断电后无法保存时间。树莓派默认安装了NTP(Network Time Protocol)服务来获取互联网上ntp服务器提供的时间。如果这个时间不准,可以用这个命令校准一下。

1
sudo ntpd -s -d

如果还是不准,就用这个命令强制设置

1
sudo date  --s= "2014-08-31 14:42:00"

最后,添加一些国内可用的ntp服务器,如果你的时间正常,可以不做这些设置。

打开ntp服务的配置文件

1
sudo nano  /etc/ntp .conf

找到这两行

1
2
# You do need to talk to an NTP server or two (or three).
# server ntp.your-provider.example

在下面添加以下内容,是一些亲测可用的ntp服务器。第一行最后的perfer表示优先使用此服务器,也就是复旦大学的ntp服务器。添加之后按Ctrl+X保存退出。

1
2
3
4
5
server ntp.fudan.edu.cn iburst perfer
server time.asia.apple.com iburst
server asia.pool.ntp.org iburst
server ntp.nict.jp iburst
server time.nist.gov iburst

重启ntp服务

1
sudo /etc/init .d /ntp restart

via

本文来自: 树莓派实验室
链接地址: http://shumeipai.nxez.com/2015/06/28/raspberry-pi-date-and-time-correction-method-allowed.html
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现树莓OLED显示时间日期和天气,你可以采取以下步骤: 1. 首先,你需要连接树莓和OLED屏幕。你可以使用I2C接口连接OLED屏幕和树莓。确保你的树莓已经安装了I2C驱动程序。 2. 接下来你需要安装Python的Adafruit_SSD1306库。这个库提供了与OLED屏幕通信的功能。 3. 然后,你需要编写Python代码来从网络获取天气信息。你可以使用OpenWeatherMap API来获取天气信息。你需要在该网站上注册并获取一个API密钥。 4. 编写Python代码来显示时间日期和天气信息。你可以使用Python的datetime库来获取当前时间日期。然后,将这些信息写入OLED屏幕。你需要将天气信息和图标从OpenWeatherMap API获取并将它们显示在OLED屏幕上。 下面是一个示例Python代码,可用于在树莓上显示时间日期和天气: ```python import time import datetime import requests from PIL import Image from PIL import ImageDraw from PIL import ImageFont import Adafruit_SSD1306 # 初始化OLED屏幕 disp = Adafruit_SSD1306.SSD1306_128_64(rst=None) disp.begin() disp.clear() disp.display() # 创建图像对象 width = disp.width height = disp.height image = Image.new('1', (width, height)) draw = ImageDraw.Draw(image) # 设置字体 font = ImageFont.truetype('FreeSans.ttf', 14) font_small = ImageFont.truetype('FreeSans.ttf', 12) # 获取天气信息 api_key = 'your_api_key' # 替换为你的OpenWeatherMap API密钥 city_name = 'Beijing' # 替换为你所在的城市名称 url = 'http://api.openweathermap.org/data/2.5/weather?q={0}&appid={1}&units=metric'.format(city_name, api_key) response = requests.get(url) data = response.json() weather = data['weather'][0]['main'] icon_url = 'http://openweathermap.org/img/w/{0}.png'.format(data['weather'][0]['icon']) icon_response = requests.get(icon_url) icon = Image.open(BytesIO(icon_response.content)) # 显示时间日期和天气信息 while True: # 清空屏幕 draw.rectangle((0, 0, width, height), outline=0, fill=0) # 获取当前时间日期 now = datetime.datetime.now() time_str = now.strftime('%H:%M:%S') date_str = now.strftime('%Y-%m-%d') # 显示时间日期 draw.text((0, 0), time_str, font=font, fill=255) draw.text((0, 16), date_str, font=font_small, fill=255) # 显示天气信息 draw.text((0, 32), weather, font=font_small, fill=255) image.paste(icon, (50, 32)) # 显示图像 disp.image(image) disp.display() # 等待1秒 time.sleep(1) ``` 这个示例代码应该可以让你有一个很好的开始点,你可以根据自己的需求和喜好进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值