/*温湿度时钟案例 - 优化版 新增:串口输入学号、上电点阵显示学号、定时串口上报学号+时间+温湿度 */
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <DHT.h>
#include <SPI.h>
#include <Wire.h>
#include "Font7Seg.h"
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 4
#define CLK_PIN 52
#define DATA_PIN 51
#define CS_PIN 10
#define SPEED_TIME 75
#define PAUSE_TIME 0
#define MAX_MESG 20
#define DS1307_ADDRESS 0x68
#define DHTPIN 2
#define DHTTYPE DHT22
#define TIMEDHT 1000
// ========== 新增配置 ==========
#define REPORT_INTERVAL 5000 // 串口上报间隔(ms)
#define SHOW_STUDENT_ID_TIME 3000 // 上电学号显示时长
#define RTC_INTERVAL 1000 // RTC读取限流,1秒读一次,避免I2C风暴
char studentId[20] = "";
bool studentIdReady = false;
uint32_t reportTimer = 0;
uint32_t rtcTimer = 0;
// ==============================
uint8_t wday, mday, month, year;
uint8_t hours, minutes, seconds;
char szTime[9];
char szMesg[MAX_MESG + 1] = "";
float humidity, celsius, fahrenheit;
uint8_t degC[] = { 6, 3, 3, 56, 68, 68, 68 };
uint8_t degF[] = { 6, 3, 3, 124, 20, 20, 4 };
uint8_t clear = 0x00;
uint32_t timerDHT = TIMEDHT;
DHT dht(DHTPIN, DHTTYPE);
MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
uint8_t decToBcd(uint8_t value) {
return ((value / 10 * 16) + (value % 10));
}
uint8_t bcdToDec(uint8_t value) {
return ((value / 16 * 10) + (value % 16));
}
void beginDS1307() {
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(clear);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 0x07);
seconds = bcdToDec(Wire.read());
minutes = bcdToDec(Wire.read());
hours = bcdToDec(Wire.read() & 0x3f);
wday = bcdToDec(Wire.read());
mday = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
}
void getTime(char *psz, bool f = true) {
sprintf(psz, "%02d%c%02d", hours, (f ? ':' : ' '), minutes);
}
void getDate(char *psz) {
char szBuf[10];
sprintf(psz, "%d %s %04d", mday , mon2str(month, szBuf, sizeof(szBuf) - 1), (year + 2000));
}
void getTemperature() {
if ((millis() - timerDHT) > TIMEDHT) {
timerDHT = millis();
humidity = dht.readHumidity();
celsius = dht.readTemperature();
fahrenheit = dht.readTemperature(true);
if (isnan(humidity) || isnan(celsius) || isnan(fahrenheit)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
}
}
char *mon2str(uint8_t mon, char *psz, uint8_t len) {
static const __FlashStringHelper* str[] =
{
F("Jan"), F("Feb"), F("Mar"), F("Apr"),
F("May"), F("Jun"), F("Jul"), F("Aug"),
F("Sep"), F("Oct"), F("Nov"), F("Dec")
};
strncpy_P(psz, (const char PROGMEM *)str[mon - 1], len);
psz[len] = '\0';
return (psz);
}
char *dow2str(uint8_t code, char *psz, uint8_t len) {
static const __FlashStringHelper* str[] =
{
F("Sunday"), F("Monday"), F("Tuesday"),
F("Wednesday"), F("Thursday"), F("Friday"),
F("Saturday")
};
strncpy_P(psz, (const char PROGMEM *)str[code - 1], len);
psz[len] = '\0';
return (psz);
}
// ========== 新增:串口读取学号 ==========
void readStudentIdFromSerial() {
Serial.println(F("====================================="));
Serial.println(F("请在串口输入你的学号,输入后回车:"));
Serial.println(F("====================================="));
while (!Serial.available()) {
delay(100);
}
Serial.readBytesUntil('\n', studentId, sizeof(studentId)-1);
for(int i=0; i<strlen(studentId); i++){
if(studentId[i] == '\r' || studentId[i] == '\n') studentId[i] = '\0';
}
Serial.print(F("已接收学号:"));
Serial.println(studentId);
Serial.println(F("开始运行温湿度时钟..."));
}
// ========================================
void setup(void) {
Serial.begin(9600);
Wire.begin();
P.begin(2);
P.setInvert(false);
P.setZone(0, MAX_DEVICES - 4, MAX_DEVICES - 1);
P.setZone(1, MAX_DEVICES - 4, MAX_DEVICES - 1);
P.displayZoneText(1, szTime, PA_CENTER, SPEED_TIME, PAUSE_TIME, PA_PRINT, PA_NO_EFFECT);
P.displayZoneText(0, szMesg, PA_CENTER, SPEED_TIME, 0, PA_PRINT , PA_NO_EFFECT);
P.addChar('$', degC);
P.addChar('&', degF);
dht.begin();
readStudentIdFromSerial();
strcpy(szMesg, studentId);
P.displayReset(0);
rtcTimer = millis();
studentIdReady = false;
}
void loop(void) {
static uint32_t lastTime = 0;
static uint8_t display = 0;
static bool flasher = false;
static bool secJumpFlag = false;
P.displayAnimate();
// 学号显示阶段(非阻塞)
if(!studentIdReady)
{
if(millis() - rtcTimer >= SHOW_STUDENT_ID_TIME)
{
studentIdReady = true;
rtcTimer = millis();
}
return;
}
// RTC限流读取
if(millis() - rtcTimer > RTC_INTERVAL)
{
rtcTimer = millis();
beginDS1307();
}
getTemperature();
// 串口上报
if(millis() - reportTimer > REPORT_INTERVAL)
{
reportTimer = millis();
Serial.print(F("学号:"));
Serial.print(studentId);
Serial.print(F(" | 时间:"));
Serial.print(year + 2000);
Serial.print("-");
Serial.print(month);
Serial.print("-");
Serial.print(mday);
Serial.print(" ");
Serial.print(hours);
Serial.print(":");
Serial.print(minutes);
Serial.print(":");
Serial.print(seconds);
Serial.print(F(" | 温度:"));
Serial.print(celsius,1);
Serial.print(F(" ℃ | 湿度:"));
Serial.print(humidity,0);
Serial.println(F(" %"));
}
if (P.getZoneStatus(0))
{
switch (display)
{
case 0: // 摄氏温度
P.setPause(0, 1000);
P.setTextEffect(0, PA_SCROLL_LEFT, PA_SCROLL_UP);
dtostrf(celsius, 3, 1, szMesg);
strcat(szMesg, "$");
display++;
break;
case 1: // 华氏温度
P.setTextEffect(0, PA_SCROLL_UP, PA_SCROLL_DOWN);
dtostrf(fahrenheit, 3, 1, szMesg);
strcat(szMesg, "&");
display++;
break;
case 2: // 湿度(保留你原来的 %UR)
P.setTextEffect(0, PA_SCROLL_DOWN, PA_SCROLL_LEFT);
dtostrf(humidity, 3, 0, szMesg);
strcat(szMesg, "%UR");
display++;
break;
case 3: // 时钟
P.setFont(0, numeric7Seg);
P.setTextEffect(0, PA_PRINT, PA_NO_EFFECT);
P.setPause(0, 0);
if ((millis() - lastTime) >= 1000)
{
lastTime = millis();
getTime(szMesg, flasher);
flasher = !flasher;
}
if (seconds == 0 && !secJumpFlag) {
secJumpFlag = true;
display++;
P.setTextEffect(0, PA_PRINT, PA_WIPE_CURSOR);
}
if(seconds != 0){
secJumpFlag = false;
}
break;
case 4: // 星期
P.setFont(0, nullptr);
P.setTextEffect(0, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
dow2str(wday, szMesg, MAX_MESG);
display++;
break;
default: // 日期
P.setTextEffect(0, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
getDate(szMesg);
display = 0;
break;
}
P.displayReset(0);
}
}