ESP8266桌面时钟总结

前言:今天用Arduino+ESP8266开发板+WS2812灯珠做了一个桌面时钟,一路做下来还是挺坎坷的。因此在此总结一下。

制作思路

整个制作过程大体上分为三部分:

  • 用串联的灯珠表示数字
  • 使用WiFi联网获取时间
  • 将时间与灯珠关联起来
  1. 第一步是花费时间最多的一步:
    • 首先是FastLED颜色设置,使用的是FastLED库,这里面有不少颜色的设置方法,毕竟用了WS2812不用点花里胡哨的颜色有些浪费。

    • 将数字在灯珠上表示出来,我的思路是把一个数字写为一个函数,只需要传入一个起始位置即可在一块区域表示一个数字,因为灯珠是串联而且是S形分布,刚开始的时候没有注意到,导致表示数字的函数不能复用,所以这里吃了亏,做了一半又更换思路使函数能复用。

    • 在这里插入图片描述

    • 这里还有一个耽误时间的点:我使用的是FastLED这个库,不知道管脚的对应情况,后来查了好久才从官网找到对应的管脚设置,虽然英文比较难看,但是该看的还是得看,看得多了就能看懂了。

  2. 第二步获取时间相对比较简单,使用的是NPTClient库,本想找个API获取时间,顺便学习一下使用ESP8266访问互联网,但是找了一圈发现大部分API都失效了,所以就用了这个NTPClient库。
  3. 第三步是将时间与灯珠关联起来,这一步思路很简单,使用四个switch语句分别匹配小时和分钟即可。

总结:

做到这里只是简单的表示了一下时间,还有很多地方可以优化,比如可以修改灯珠的颜色,根据时间调整亮度,提供一个接口用于修改这些参数。后续也会慢慢优化,添加一些功能。

源码

写的很烂,欢迎指点

#include <FastLED.h>
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

const char *ssid = "Xiaomi_947C";
const char *password = "20020806";
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "ntp1.aliyun.com", 60*60*8, 30*60*1000);
#define PST_1 24
#define PST_2 72
#define PST_3 152
#define PST_4 200
#define FASTLED_ESP8266_NODEMCU_PIN_ORDER 3
#define NUM_LEDS    256    //256颗LED
CRGB leds[NUM_LEDS];
void setup() {
  Serial.begin(9600);
  WiFi.begin(ssid, password);

  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    Serial.print ( "." );
  }
  Serial.print("\n");
  Serial.print("Connected to ");
  Serial.println(WiFi.SSID());  // 通过串口监视器输出连接的WiFi名称
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());
  timeClient.begin();
  LEDS.addLeds<WS2812, FASTLED_ESP8266_NODEMCU_PIN_ORDER, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(1);  //可以设置全局亮度,调低亮度不刺眼
}
void loop(){
  timeClient.update();
  int hours = timeClient.getHours();
  int minu =  timeClient.getMinutes();
  for(int i=0;i<256;i++){
    leds[i]=CRGB::Black;
  }
  pst_choose(hours,minu);
  point_blink();
}

void pst_choose(int hour,int minu){
  switch(hour/10)
  {
    case 0:num_0(PST_1);
      break;
    case 1:num_1(PST_1);
      break;
    case 2:num_2(PST_1);
      break;
    case 3:num_3(PST_1);
      break;
    case 4:num_4(PST_1);
      break;
    case 5:num_5(PST_1);
      break;
    case 6:num_6(PST_1);
      break;
    case 7:num_7(PST_1);
      break;
    case 8:num_8(PST_1);
      break;
    case 9:num_9(PST_1);
      break;
  }
  switch(hour%10)
  {
    case 0:num_0(PST_2);
      break;
    case 1:num_1(PST_2);
      break;
    case 2:num_2(PST_2);
      break;
    case 3:num_3(PST_2);
      break;
    case 4:num_4(PST_2);
      break;
    case 5:num_5(PST_2);
      break;
    case 6:num_6(PST_2);
      break;
    case 7:num_7(PST_2);
      break;
    case 8:num_8(PST_2);
      break;
    case 9:num_9(PST_2);
      break;
  }
  switch(minu/10)
  {
    case 0:num_0(PST_3);
      break;
    case 1:num_1(PST_3);
      break;
    case 2:num_2(PST_3);
      break;
    case 3:num_3(PST_3);
      break;
    case 4:num_4(PST_3);
      break;
    case 5:num_5(PST_3);
      break;
    case 6:num_6(PST_3);
      break;
    case 7:num_7(PST_3);
      break;
    case 8:num_8(PST_3);
      break;
    case 9:num_9(PST_3);
      break;
  }
  switch(minu%10)
  {
    case 0:num_0(PST_4);
      break;
    case 1:num_1(PST_4);
      break;
    case 2:num_2(PST_4);
      break;
    case 3:num_3(PST_4);
      break;
    case 4:num_4(PST_4);
      break;
    case 5:num_5(PST_4);
      break;
    case 6:num_6(PST_4);
      break;
    case 7:num_7(PST_4);
      break;
    case 8:num_8(PST_4);
      break;
    case 9:num_9(PST_4);
      break;
  }
}

void point_blink(){
  leds[121]=CRGB::HotPink;
  leds[122]=CRGB::HotPink;
  leds[124]=CRGB::HotPink;
  leds[125]=CRGB::HotPink;
  leds[130]=CRGB::HotPink;
  leds[131]=CRGB::HotPink;
  leds[133]=CRGB::HotPink;
  leds[134]=CRGB::HotPink;
  FastLED.show();
  delay(1000);
  leds[121]=CRGB::Black;
  leds[122]=CRGB::Black;
  leds[124]=CRGB::Black;
  leds[125]=CRGB::Black;
  leds[130]=CRGB::Black;
  leds[131]=CRGB::Black;
  leds[133]=CRGB::Black;
  leds[134]=CRGB::Black;
  FastLED.show();
  delay(1000);
}
void num_0(int NUM_PST){
  for(int i=NUM_PST+9;i<NUM_PST+16;i=i+6){
    leds[i]=CRGB::HotPink;
  }
  for(int i=NUM_PST+16;i<NUM_PST+23;i=i+6){
    leds[i]=CRGB::HotPink;
  }
  for(int i=NUM_PST+25;i<NUM_PST+32;i++){
    leds[i]=CRGB::HotPink;
  }
  for(int i=NUM_PST;i<NUM_PST+7;i++){
    leds[i]=CRGB::HotPink;
  }
  FastLED.show();

}

void num_1(int NUM_PST){
  for(int i=NUM_PST+15;i<NUM_PST+23;i++){
    leds[i]=CRGB::HotPink;
  }
  leds[NUM_PST+4]=CRGB::HotPink;
  leds[NUM_PST+10]=CRGB::HotPink;
  leds[NUM_PST]=CRGB::HotPink;
  leds[NUM_PST+31]=CRGB::HotPink;
  FastLED.show();
}
void num_2(int NUM_PST){
  for(int i=NUM_PST;i<NUM_PST+4;i++){
    leds[i]=CRGB::HotPink;
  }
  leds[NUM_PST+6]=CRGB::HotPink;
  leds[NUM_PST+9]=CRGB::HotPink;
  leds[NUM_PST+12]=CRGB::HotPink;
  leds[NUM_PST+15]=CRGB::HotPink;
  leds[NUM_PST+16]=CRGB::HotPink;
  leds[NUM_PST+19]=CRGB::HotPink;
  leds[NUM_PST+22]=CRGB::HotPink;
  for(int i=NUM_PST+25;i<NUM_PST+29;i++){
    leds[i]=CRGB::HotPink;
  }
  leds[NUM_PST+31]=CRGB::HotPink;
  FastLED.show();
}
void num_3(int NUM_PST){
  for(int i=NUM_PST;i<NUM_PST+8;i=i+3){
    leds[i]=CRGB::HotPink;
  }
  for(int i=NUM_PST+9;i<NUM_PST+16;i=i+3){
    leds[i]=CRGB::HotPink;
  }
  for(int i=NUM_PST+16;i<NUM_PST+23;i=i+3){
    leds[i]=CRGB::HotPink;
  }
  for(int i=NUM_PST+25;i<NUM_PST+32;i++){
    leds[i]=CRGB::HotPink;
  }
  FastLED.show();
  }

void num_4(int NUM_PST){
  for(int i=NUM_PST+3;i<NUM_PST+7;i++){
    leds[i]=CRGB::HotPink;
  }
  for(int i=NUM_PST+25;i<NUM_PST+32;i++){
    leds[i]=CRGB::HotPink;
  }
  leds[NUM_PST+12]=CRGB::HotPink;
  leds[NUM_PST+19]=CRGB::HotPink;
  FastLED.show();
  }
void num_5(int NUM_PST)
{
  leds[NUM_PST]=CRGB::HotPink;
  for(int i=NUM_PST+3;i<NUM_PST+7;i++){
    leds[i]=CRGB::HotPink;
  }
  for(int i=NUM_PST+9;i<NUM_PST+16;i=i+3){
    leds[i]=CRGB::HotPink;
  }
  for(int i=NUM_PST+16;i<NUM_PST+23;i=i+3){
    leds[i]=CRGB::HotPink;
  }
  leds[NUM_PST+25]=CRGB::HotPink;
  for(int i=NUM_PST+28;i<NUM_PST+32;i++){
    leds[i]=CRGB::HotPink;
  }
  FastLED.show();
}
void num_6(int NUM_PST){
  for(int i=NUM_PST;i<NUM_PST+7;i++){
    leds[i]=CRGB::HotPink;
  }
  for(int i=NUM_PST+9;i<NUM_PST+16;i=i+3){
    leds[i]=CRGB::HotPink;
  }
  for(int i=NUM_PST+16;i<NUM_PST+23;i=i+3){
    leds[i]=CRGB::HotPink;
  }
  for(int i=NUM_PST+28;i<NUM_PST+32;i++){
    leds[i]=CRGB::HotPink;
  }
  leds[NUM_PST+25]=CRGB::HotPink;
  FastLED.show();
}
void num_7(int NUM_PST){
  leds[NUM_PST+6]=CRGB::HotPink;
  leds[NUM_PST+9]=CRGB::HotPink;
  leds[NUM_PST+22]=CRGB::HotPink;
  for(int i=NUM_PST+25;i<NUM_PST+32;i++){
    leds[i]=CRGB::HotPink;
  }
  FastLED.show();
}
void num_8(int NUM_PST){
  for(int i=NUM_PST+9;i<NUM_PST+16;i=i+3){
    leds[i]=CRGB::HotPink;
  }
  for(int i=NUM_PST+16;i<NUM_PST+23;i=i+3){
    leds[i]=CRGB::HotPink;
  }
  for(int i=NUM_PST+25;i<NUM_PST+32;i++){
    leds[i]=CRGB::HotPink;
  }
  for(int i=NUM_PST;i<NUM_PST+7;i++){
    leds[i]=CRGB::HotPink;
  }
  FastLED.show();
}
void num_9(int NUM_PST){
  for(int i=NUM_PST+3;i<NUM_PST+7;i++){
    leds[i]=CRGB::HotPink;
  }
  for(int i=NUM_PST+9;i<NUM_PST+13;i=i+3){
    leds[i]=CRGB::HotPink;
  }
  for(int i=NUM_PST+19;i<NUM_PST+23;i=i+3){
    leds[i]=CRGB::HotPink;
  }
  for(int i=NUM_PST+25;i<NUM_PST+32;i++){
    leds[i]=CRGB::HotPink;
  }

  FastLED.show();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

指针到处飞

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

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

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

打赏作者

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

抵扣说明:

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

余额充值