【搬运】用StickC做一个简易的定时开关装置-Arduino中文社区 - Powered by Discuz! Archiver

vany5921 发表于 2019-12-17 17:08

【搬运】用StickC做一个简易的定时开关装置

      依然是日本用户的一个作品,日本的加班文化比较盛行,经常喜欢在下班后开各种散漫无意义的会议,早回家的男人被视为是没出息的,因此各种应酬也接踵而至,作者做了一个简单的领带灯,来呼吁此类会议应该早点结束。


主角只有一个M5StickC和一个LED灯,通过NTP获取网络时间来校准本地RTC,然后使用内置的的RTC来计时,通过比较预设的时间点来判断是否开启LED,逻辑还是比较简单的。

#include <M5StickC.h>
#include <WiFi.h>

//RTC Define
RTC_TimeTypeDef RTC_TimeStruct;
RTC_DateTypeDef RTC_DateStruct;

//Wi-Fi Define
const char* ssid       = "SSID";
const char* password   = "PASSWORD";
const char* ntpServer ="cn.pool.ntp.org";

//LCD Display Define
TFT_eSprite *Spr;//用精灵显示更流畅
uint32_t front_col = TFT_YELLOW;
uint32_t back_col = TFT_BLACK;

//Fuinction Define
void wifi_connect();
String ZeroPad(int num);
uint16_t getColor(uint8_t red, uint8_t green, uint8_t blue); //RGB888转RGB565

//Set Target_time
int Target_Hours = 15;//定时(注:如果当前时间超过了设定时间则当日设定无效)
int Target_Minutes = 30;

void setup() {
M5.begin();
M5.Lcd.setRotation(1);
pinMode(32, OUTPUT);
pinMode(M5_BUTTON_HOME, INPUT_PULLUP);
pinMode(M5_BUTTON_RST, INPUT_PULLUP);
M5.Axp.ScreenBreath(10);

//Wi-Fi Connect
wifi_connect();

//NTP TimeScale
configTime(8 * 3600, 0, ntpServer); //东八区

//GET NOW TIME
struct tm timeInfo;
if (getLocalTime(&timeInfo)) {
    Serial.println("ntpServer:");
    Serial.println(ntpServer);

    // Initialize RTC time
    RTC_TimeTypeDef TimeStruct;
    TimeStruct.Hours   = timeInfo.tm_hour;
    TimeStruct.Minutes = timeInfo.tm_min;
    TimeStruct.Seconds = timeInfo.tm_sec;
    M5.Rtc.SetTime(&TimeStruct);

    // Initialize RTC Date
    RTC_DateTypeDef DateStruct;
    DateStruct.WeekDay = timeInfo.tm_wday;
    DateStruct.Month = timeInfo.tm_mon + 1;
    DateStruct.Date = timeInfo.tm_mday;
    DateStruct.Year = timeInfo.tm_year + 1900;
    M5.Rtc.SetData(&DateStruct);
}

//LCD Splite Initialize
Spr = new TFT_eSprite(&M5.Lcd);
Spr->setColorDepth(8);
Spr->createSprite(320, 240);
Spr->setBitmapColor(front_col, back_col);

}

void loop() {
M5.update();

//Get RTC
M5.Rtc.GetTime(&RTC_TimeStruct);
M5.Rtc.GetData(&RTC_DateStruct);

int Target_Seconds = 30;
bool state = 0;

if ((Target_Hours == RTC_TimeStruct.Hours) && (Target_Minutes == RTC_TimeStruct.Minutes) && (Target_Seconds > RTC_TimeStruct.Seconds)) {
    state = 1;
}
if (state){
    Spr->fillSprite(getColor(255, 0, 0));
    Spr->setTextColor(getColor(0, 0, 0), getColor(255, 0, 0));
}
else {
    Spr->fillSprite(getColor(0, 0, 0));
    Spr->setTextColor(getColor(255, 255, 0), getColor(0, 0, 0));
}

//Now Time Display
Spr->setTextSize(1);
String date_str = String("") + String(ZeroPad(RTC_DateStruct.Year)) + String("/") + String(ZeroPad(RTC_DateStruct.Month)) + String("/") + String(ZeroPad(RTC_DateStruct.Date));
Spr->drawString(date_str, 10, 15, 4);
String time_str = String("") + String(ZeroPad(RTC_TimeStruct.Hours)) + String(":") + String(ZeroPad(RTC_TimeStruct.Minutes)) + String(":") + String(ZeroPad(RTC_TimeStruct.Seconds));
Spr->drawString(time_str, 10, 45, 4);
Spr->pushSprite(0, 0);

//LED Flashes regularly
static unsigned long old_time = millis();
unsigned long time = millis();
unsigned long interval = 1000;
if (state) {

    //LED Light Flash By Grove Port
    unsigned long dtime = time - old_time;
    if (RTC_TimeStruct.Seconds > 15) {
      interval = 500;
    }
    if (RTC_TimeStruct.Seconds > 20) {
      interval = 200;
    }
    if (RTC_TimeStruct.Seconds > 25) {
      interval = 100;
    }
    if (dtime < interval / 2) {
      digitalWrite(32, HIGH);
    }
    else if (dtime >= interval / 2) {
      digitalWrite(32, LOW);
    }
    if (dtime > interval) {
      old_time = time;
    }

}
else {
    //LED Light OFF By Grove Port
    digitalWrite(32, LOW);
}

}

//Get 24Bit Color -> 16Bit Color
uint16_t getColor(uint8_t red, uint8_t green, uint8_t blue) {
return ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3);
}

//Wi-Fi Connect
void wifi_connect() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)delay(500);
Serial.println(WiFi.localIP());
}

//String ZeroPad
String ZeroPad(int num) {
char buf;
sprintf(buf, "%02d", num);
String bud_str = buf;
return bud_str;
}
页: [1]
查看完整版本: 【搬运】用StickC做一个简易的定时开关装置