esp8266 OLED World Clock(marcopython)

esp8266串口WiFi模块CH340 NodeMCU

白显 OLED 1.3寸 四针脚 IIC接口 128*64

软件用的thonny

# OLED World Clock
# By Shubham Santosh

from machine import Pin,I2C
from time import sleep
import sh1106# OLED World Clock
import utime,gfx,math
import ntptime
import network

i2c=I2C(scl=Pin(5),sda=Pin(4))
oled=sh1106.SH1106_I2C(128,64,i2c)
graphics = gfx.GFX(128,64,oled.pixel)
# enabling pull-up resistor
next=Pin(14,Pin.IN,Pin.PULL_UP)   #pin D5 of NodeMCU
back=Pin(12,Pin.IN,Pin.PULL_UP)   #pin D6 of NodeMCU

week=["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]

ssid="你的"
password="密码"

ssid="另一个你的"
password="密码"

oled.text("OLED",0,0)
oled.text("WORLD",0,10)
oled.text("CLOCK",0,20)
oled.show()

sleep(1)
oled.fill(0)
wifi=network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(ssid,password)
while(wifi.isconnected()==False):
  oled.text("CONNECTING",15,0)
  oled.text("...to...",23,15)
  oled.text("Most wanted Open",0,30)
  oled.show()
oled.text("Connected",15,45)
oled.show()

xcenter=95     # X and Y center coordinates of clock circle
ycenter=32
radius=30     # radius of cloock circle
count=0      # to move next/previous 

ntptime.settime()

#UTC offset time of state/country in hrs, mins stored in dictionary
time_zones={       
0:["北京",8,0],
1:["LOS ANGELES",-8,0],
2:["MEXICO CITY",-6,0],
3:["MIAMI",-5,0],
4:["SANTIAGO",-3,0],
5:["NEW YORK",-5,0],
6:["RIO",-3,0],
7:["NUUK",-3,0],
8:["ACCRA",0,0],
9:["LISBON",0,0],
10:["LONDON",0,0],
11:["PARIS",1,0],
12:["MOSCOW",3,0],
13:["DUBAI",4,0],
14:["KARACHI",5,0],
15:["NEW DELHI",5,30],
16:["COLOMBO",5,30],
17:["DHAKA",6,0],
18:["BANGKOK",7,0],
19:["BEIJING",8,0],
20:["SINGAPORE",8,0],
21:["HONGKONG",8,0],
22:["TOKYO",9,0],
23:["SYDNEY",11,0],
24:["AUCKLAND",13,0],
}


def display_clock(hrs,mins):
  
  oled.text('12',88,4)     # Clock hour positions
  oled.text('3',117,30)
  oled.text('6',92,52)
  oled.text('9',67,30)
  graphics.circle(xcenter,ycenter,radius,1)
  
  Sangle=curr_time[5]*6       #Second angle
  Mangle=mins*6                #Minute angle
  if hrs>=0 and hrs<=12:
    Hangle=30*hrs          #Hour angle
  elif hrs>12:             # Hour format is 24
    Hangle=(hrs-12)*30
  # Obtaining the (x,y) coordinates of the Second hand
  shift_sec_x=0.8*radius*math.sin(math.radians(Sangle)) # 0.8 is the ratio of the length
  shift_sec_y=0.8*radius*math.cos(math.radians(Sangle))
  #plotting the second hand
  graphics.line(xcenter,ycenter,round(xcenter+shift_sec_x),round(ycenter-shift_sec_y),1)
  # Obtaining the (x,y) coordinates of the Minute hand
  shift_min_x=radius*math.sin(math.radians(Mangle))
  shift_min_y=radius*math.cos(math.radians(Mangle))
  #plotting the Minute hand
  graphics.line(xcenter,ycenter,round(xcenter+shift_min_x),round(ycenter-shift_min_y),1)
  #Obtaining the (x,y) coordinated of hour hand
  shift_hour_x=0.6*radius*math.sin(math.radians(Hangle))
  shift_hour_y=0.6*radius*math.cos(math.radians(Hangle))
  #plotting the hour hand
  graphics.line(xcenter,ycenter,round(xcenter+shift_hour_x),round(ycenter-shift_hour_y),1)
  
 

while True:
  oled.fill(0)        # Clear screen
  if next.value()==0:   # move to next city/state
   count+=1
  elif back.value()==0:  #move to previous city/state
   count-=1
  if (count<=0):     # circular rotation
   count=25+count
  count=count%25 
  
  offset_sec=time_zones[count][1]*60*60+ time_zones[count][2]*60  # convert UTC offset to seconds
  curr_time=utime.localtime(offset_sec+utime.time())     # provides day,date and time from epoch time
  oled.text(time_zones[count][0],0,0)         # display the city/state
  (Hr,Min)=(curr_time[3],curr_time[4])      # obtain current hour,min of the city/state
  time_str=str(Hr)+":"+str(Min)+":"+str(curr_time[5])   
  oled.text(time_str,0,20)   # display time of city/state
  display_clock(Hr,Min)      # display analog clock
  oled.text(week[curr_time[6]],0,35)       # display day of the week
  curr_date=str(curr_time[0])+"/"+str(curr_time[1])+"/"+str(curr_time[2])
  oled.text(curr_date,0,50)        # display current date of city/state
  oled.show()
  sleep(0.9)       

代码自网络,开头有标记了作者,部分修改以适用设备。

目前问题:

  1. 不准,没有设置多长时间同步一次。有会的帮忙补一下吧。
  2. 无法显示中文。暂时没有找到类似中文的库文件,可能需要那个点阵转换的软件做。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值