From bf5270f337c5b134372e571b6705449144746fba Mon Sep 17 00:00:00 2001 From: chenxin Date: Tue, 13 May 2025 16:55:49 +0800 Subject: [PATCH] ok --- .vscode/settings.json | 3 + src/DS18B20.cpp | 6 +- src/DS3502.cpp | 65 ++++++++++++++ src/DS3502.h | 30 +++++++ src/DigitalPot.cpp | 24 ------ src/DigitalPot.h | 19 ----- src/LightControl.cpp | 35 ++++++++ src/LightControl.h | 31 +++++++ src/MCP45HVX1.cpp | 193 ------------------------------------------ src/MCP45HVX1.h | 94 -------------------- src/TJC_Show.cpp | 26 ++++-- src/TJC_Show.h | 6 +- src/WiFiControl.cpp | 73 +++++++++++++--- src/WiFiControl.h | 48 +++++++++-- src/main.cpp | 91 +++++++++++++++----- src/webpages.h | 103 +++++++++++++++++++++- 16 files changed, 463 insertions(+), 384 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 src/DS3502.cpp create mode 100644 src/DS3502.h delete mode 100644 src/DigitalPot.cpp delete mode 100644 src/DigitalPot.h create mode 100644 src/LightControl.cpp create mode 100644 src/LightControl.h delete mode 100644 src/MCP45HVX1.cpp delete mode 100644 src/MCP45HVX1.h diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..286704c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "window.zoomLevel": 0 +} \ No newline at end of file diff --git a/src/DS18B20.cpp b/src/DS18B20.cpp index df7fea8..2a983e9 100644 --- a/src/DS18B20.cpp +++ b/src/DS18B20.cpp @@ -224,9 +224,9 @@ void DS18B20::printTemperature() { requestTemperatures(); // 发送请求以开始温度转换 //float temperature = getTempC(); // 获取温度(摄氏度) - //Serial.print("Temp: "); - Serial.print(getTempC()); - Serial.println("℃ "); + Serial.printf("Temp: %.2f\n", getTempC()); + //Serial.print(getTempC()); + //Serial.println("℃ "); } diff --git a/src/DS3502.cpp b/src/DS3502.cpp new file mode 100644 index 0000000..e0a028e --- /dev/null +++ b/src/DS3502.cpp @@ -0,0 +1,65 @@ +#include "DS3502.h" + + +DS3502::DS3502(uint8_t address) { + _address = address; + _wire = &Wire; // 使用默认的 Wire 对象 +} + +/** + * 初始化 I2C,设置 SDA 和 SCL 引脚。 + */ +bool DS3502::begin(uint8_t sda_pin, uint8_t scl_pin) { + _wire->begin(sda_pin, scl_pin); // 初始化 I2C,总线设置 + return true; +} + +/** + * 获取电位器当前的 Wiper 值 + */ +uint8_t DS3502::getWiper() { + _wire->beginTransmission(_address); + _wire->write(DS3502_WIPER); // 请求 Wiper 寄存器 + _wire->endTransmission(false); // 持续连接 + _wire->requestFrom(_address, (uint8_t)1); // 读取一个字节 + return _wire->read(); +} + +/** + * 设置新的 Wiper 值,范围为 0 到 127 + */ +void DS3502::setWiper(uint8_t new_wiper_value) { + //if (new_wiper_value > 127) return; // 超出范围则不进行操作 + + _wire->beginTransmission(_address); + _wire->write(DS3502_WIPER); // 指定写入 Wiper 寄存器 + _wire->write(new_wiper_value); // 写入新的值 + _wire->endTransmission(); +} + +/** + * 设置电位器的默认值(IVR),这会在上电时设置 Wiper 默认值 + */ +void DS3502::setWiperDefault(uint8_t new_wiper_default) { + if (new_wiper_default > 127) return; // 超出范围则不进行操作 + + // 切换模式到默认模式 + _wire->beginTransmission(_address); + _wire->write(DS3502_MODE); // 设置寄存器为 Mode Select 寄存器 + _wire->write(0x00); // 设置为写入默认值 + _wire->endTransmission(); + + // 设置新的默认 Wiper 值 + _wire->beginTransmission(_address); + _wire->write(DS3502_WIPER); // 设置 Wiper 寄存器 + _wire->write(new_wiper_default); // 写入新的默认值 + _wire->endTransmission(); + + delay(100); // 等待 EEPROM 写入完成 + + // 恢复模式 + _wire->beginTransmission(_address); + _wire->write(DS3502_MODE); + _wire->write(0x80); // 恢复到正常操作模式 + _wire->endTransmission(); +} diff --git a/src/DS3502.h b/src/DS3502.h new file mode 100644 index 0000000..6d44e6a --- /dev/null +++ b/src/DS3502.h @@ -0,0 +1,30 @@ +#ifndef DS3502_H +#define DS3502_H + +#include + +#define DS3502_ADDRESS 0x28 // 默认 I2C 地址 +#define DS3502_MODE 0x00 // Mode Selector Register +#define DS3502_WIPER 0x00 // Wiper Register + +class DS3502 { +public: + DS3502(uint8_t address = DS3502_ADDRESS); // 构造函数声明 + + bool begin(uint8_t sda_pin, uint8_t scl_pin); + + // 获取电位器的当前值 + uint8_t getWiper(); + + // 设置电位器的值 + void setWiper(uint8_t new_wiper_value); + + // 设置默认电位器值(IVR) + void setWiperDefault(uint8_t new_wiper_default); + +private: + uint8_t _address; + TwoWire* _wire; +}; + +#endif diff --git a/src/DigitalPot.cpp b/src/DigitalPot.cpp deleted file mode 100644 index df0bcfa..0000000 --- a/src/DigitalPot.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "DigitalPot.h" - -TPL0401A::TPL0401A(uint8_t i2cAddress) : _i2cAddress(i2cAddress) -{ -} - -uint16_t TPL0401A::writeWiper(uint16_t wiperValue) { - Wire1.beginTransmission(0x2E); // device address TPL04001-A #46 (0x2E), TPL04001-B #46 (0x2E) - Wire1.write(byte(0x00)); // sends instruction. 0x00 = Write - Wire1.write(wiperValue); // sends value - Wire1.endTransmission(); // end transmission - - _wiperValue = wiperValue; // 更新成员变量 - - // Print the current pot value - Serial.print("pot: "); - Serial.println(wiperValue); - - return wiperValue; -} - -uint16_t TPL0401A::readWiperValue() const { - return _wiperValue; -} \ No newline at end of file diff --git a/src/DigitalPot.h b/src/DigitalPot.h deleted file mode 100644 index cfcaa2d..0000000 --- a/src/DigitalPot.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef DIGITALPOT_H -#define DIGITALPOT_H - -#include "Wire.h" -#include - -class TPL0401A { - public: - TPL0401A(uint8_t i2cAddress); - uint16_t writeWiper(uint16_t wiperValue); - uint16_t readWiperValue() const; - private: - uint8_t _i2cAddress; - uint16_t _wiperValue; - }; - - extern TPL0401A digiPot; - -#endif diff --git a/src/LightControl.cpp b/src/LightControl.cpp new file mode 100644 index 0000000..9f3f470 --- /dev/null +++ b/src/LightControl.cpp @@ -0,0 +1,35 @@ +// LightControl.cpp + +#include "LightControl.h" +#include "Arduino.h" + +LightControl::LightControl(BH1750& sensor, DS3502& pot) + : lightSensor(sensor), digitalPot(pot) {} + +void LightControl::setTargetLight(float target) { + targetLightLevel = target; +} + +float LightControl::getCurrentLight() { + return lightSensor.readLightLevel(); +} + +void LightControl::adjustWiper() { + float currentLight = getCurrentLight(); + float error = targetLightLevel - currentLight; + + // 计算输出变化量(比例控制) + int delta = (int)(abs(error) * Kp); + if (delta < 1) delta = 1; // 最小调节步长 + + if (error > 0) { + // 需要增加亮度 -> 减小电位器阻值 + lastWiperValue = max(0, lastWiperValue - delta); + } else if (error < 0) { + // 需要减少亮度 -> 增大电位器阻值 + lastWiperValue = min(127, lastWiperValue + delta); + } + + digitalPot.setWiper(lastWiperValue); + Serial.printf("Current Light: %.0f lux | Wiper Value: %d\n", currentLight, lastWiperValue); +} \ No newline at end of file diff --git a/src/LightControl.h b/src/LightControl.h new file mode 100644 index 0000000..f76ca07 --- /dev/null +++ b/src/LightControl.h @@ -0,0 +1,31 @@ +// LightControl.h + +#ifndef LIGHTCONTROL_H +#define LIGHTCONTROL_H + +#include +#include "DS3502.h" + +class LightControl { +public: + // 构造函数 + LightControl(BH1750& sensor, DS3502& pot); + + // 设置目标照度 + void setTargetLight(float target); + + // 获取当前照度 + float getCurrentLight(); + + // 执行一次调节 + void adjustWiper(); + +private: + BH1750& lightSensor; // 引用 BH1750 传感器对象 + DS3502& digitalPot; // 引用 DS3502 数字电位器对象 + float targetLightLevel; // 目标照度值 + int lastWiperValue = 0; // 上一次 Wiper 值 + float Kp = 0.5; // 比例增益(可根据实际情况调整) +}; + +#endif \ No newline at end of file diff --git a/src/MCP45HVX1.cpp b/src/MCP45HVX1.cpp deleted file mode 100644 index 920d015..0000000 --- a/src/MCP45HVX1.cpp +++ /dev/null @@ -1,193 +0,0 @@ -/****************************************************** - Arduino library for MCP45HVX1 digital potentiometers - - Author: Jonathan Dempsey JDWifWaf@gmail.com - - Version: 1.0.1 - License: Apache 2.0 - *******************************************************/ - -#include "MCP45HVX1.h" - -#define DEBUG 0 - -/* 7 Bit I2C Operation Components ......................................... */ - -/* TCON configuration.................. */ -#define TCON_R0HW (0x08) // Shutdown Resistor Force -#define TCON_R0A (0x04) // Terminal A Connection -#define TCON_R0W (0x02) // Wiper Connection -#define TCON_R0B (0x01) // Terminal B Connection - -#define GCALL_TCON (0x60) -#define GCALL_WIPER (0x40) -#define GCALL_WIPERUP (0x42) -#define GCALL_WIPERDWN (0x44) -#define GCALL_COM_WRITE (0x02) -#define GCALL_COM_RWRITE (0x03) -#define GCALL_COM_WIPERINC (0x42) -#define GCALL_COM_WIPERDEC (0x44) - -#define MEM_WIPER (0x00) -#define MEM_TCON (0x40) - -#define COM_WRITE (0x00) -#define COM_READ (0x0C) -#define COM_WIPERINC (0x04) -#define COM_WIPERDEC (0x08) - -/* Setup ............................................................... */ -MCP45HVX1::MCP45HVX1(uint8_t address) : _address(address) {}; - -void MCP45HVX1::begin(int sda, int scl, TwoWire &inWire) { - MCPWire = &inWire; // 使用传递的 I2C 对象实例 - if (sda != -1 && scl != -1) { - MCPWire->begin(sda, scl); // 自定义 SDA 和 SCL 引脚 - } else { - MCPWire->begin(); // 默认 I2C 引脚初始化 - } - MCPWire->setClock(MCPCSPEED); // 设置 I2C 时钟速度 -} - - -/* Wiper Register..........................................................*/ -void MCP45HVX1::writeWiper(uint8_t wiperValue) -{ - this->MCPWire->beginTransmission(_address); - this->MCPWire->write(MEM_WIPER | COM_WRITE); - this->MCPWire->write(wiperValue); - this->MCPWire->endTransmission(); -} - -uint8_t MCP45HVX1::readWiper() -{ - uint8_t buff = 0; - - this->MCPWire->beginTransmission(_address); - this->MCPWire->write(MEM_WIPER | COM_READ); - this->MCPWire->endTransmission(false); - - this->MCPWire->requestFrom(_address, (uint8_t)2); - if(this->MCPWire->available()) - { - buff = this->MCPWire->read(); // First byte is 0x00 - #if DEBUG - Serial.print("\nRead Wiper MSB: "); - Serial.println(buff); - #endif - buff = this->MCPWire->read(); // Second byte contains the wiper value - #if DEBUG - Serial.print("Read Wiper LSB: "); - Serial.println(buff); - #endif - } - - return buff; -} - -void MCP45HVX1::incrementWiper(uint8_t incriments) -{ - this->MCPWire->beginTransmission(_address); - for(uint8_t x = 0; x < incriments; x++) - { - this->MCPWire->write(MEM_WIPER | COM_WIPERINC); - } - this->MCPWire->endTransmission(); -} - -void MCP45HVX1::decrementWiper(uint8_t decriments) -{ - this->MCPWire->beginTransmission(_address); - for(uint8_t x = 0; x < decriments; x++) - { - this->MCPWire->write(MEM_WIPER | COM_WIPERDEC); - } - this->MCPWire->endTransmission(); -} - -/* TCON Register...........................................................*/ -uint8_t MCP45HVX1::readTCON() -{ - uint8_t buff = 0; - - this->MCPWire->beginTransmission(_address); - this->MCPWire->write(MEM_TCON | COM_READ); - this->MCPWire->endTransmission(false); - - this->MCPWire->requestFrom(_address, (uint8_t)2); - if(this->MCPWire->available()) - { - buff = this->MCPWire->read(); // First byte is always 0x00 - #if DEBUG - Serial.print("\nRead TCON MSB: "); - Serial.println(buff); - #endif - buff = this->MCPWire->read(); // Second byte contains the wiper value (for compatability) - #if DEBUG - Serial.print("Read TCON LSB: "); - Serial.println(buff); - #endif - } - - return buff; -} - -void MCP45HVX1::defaultTCON() -{ - this->TCON_lib_reg.R0HW = true; - this->TCON_lib_reg.R0A = true; - this->TCON_lib_reg.R0B = true; - this->TCON_lib_reg.R0W = true; - - write_TCON_Register(); -} - -void MCP45HVX1::writeTCON(TCON_Register *inReg) -{ - memcpy(&this->TCON_lib_reg, inReg, sizeof(this->TCON_lib_reg)); - write_TCON_Register(); -} - -void MCP45HVX1::write_TCON_R0HW(bool isOn) -{ - this->TCON_lib_reg.R0HW = isOn; - write_TCON_Register(); -} - -void MCP45HVX1::write_TCON_R0A(bool isOn) -{ - this->TCON_lib_reg.R0A = isOn; - write_TCON_Register(); -} - -void MCP45HVX1::write_TCON_R0W(bool isOn) -{ - this->TCON_lib_reg.R0W = isOn; - write_TCON_Register(); -} - -void MCP45HVX1::write_TCON_R0B(bool isOn) -{ - this->TCON_lib_reg.R0B = isOn; - write_TCON_Register(); -} - -void MCP45HVX1::write_TCON_Register() -{ - uint8_t buff = 0xFF; - - this->TCON_lib_reg.R0HW ? (buff |= TCON_R0HW) : (buff ^= TCON_R0HW); - this->TCON_lib_reg.R0A ? (buff |= TCON_R0A) : (buff ^= TCON_R0A); - this->TCON_lib_reg.R0B ? (buff |= TCON_R0B) : (buff ^= TCON_R0B); - this->TCON_lib_reg.R0W ? (buff |= TCON_R0W) : (buff ^= TCON_R0W); - - #if DEBUG - Serial.print("Writing TCON: "); Serial.println(buff); - #endif - - this->MCPWire->beginTransmission(_address); - this->MCPWire->write(MEM_TCON | COM_WRITE); - this->MCPWire->write(buff); - this->MCPWire->endTransmission(); -} - diff --git a/src/MCP45HVX1.h b/src/MCP45HVX1.h deleted file mode 100644 index 1137717..0000000 --- a/src/MCP45HVX1.h +++ /dev/null @@ -1,94 +0,0 @@ -/****************************************************** - Arduino library for MCP45HVX1 digital potentiometers - - Author: Jonathan Dempsey JDWifWaf@gmail.com - - Version: 1.0.1 - License: Apache 2.0 - *******************************************************/ - -#ifndef _MCP45HVX1_H -#define _MCP45HVX1_H - -#include -#include - -#define MCPCSPEED 100000 // I2C clock speed - -class MCP45HVX1 -{ - public: - - /* TCON Register Resistor Configuration ............................... */ - typedef struct - { - bool R0HW = true; - bool R0A = true; - bool R0B = true; - bool R0W = true; - }TCON_Register; - - /* Setup ............................................................... */ - MCP45HVX1(uint8_t address = 0x3e); - - void begin(int sda = -1, int scl = -1, TwoWire &inWire = Wire); - - /* Wiper Register ...................................................... */ - uint8_t readWiper(); - void writeWiper(uint8_t wiperValue); - void incrementWiper(uint8_t incriments = 1); - void decrementWiper(uint8_t decriments = 1); - - /* TCON Register ....................................................... */ - uint8_t readTCON(); - void writeTCON(TCON_Register *inReg); - void defaultTCON(); - - void inline shutdown() - { - write_TCON_R0HW(false); - }; - void inline startup() - { - write_TCON_R0HW(true); - }; - void inline connectTerminalA() - { - write_TCON_R0A(true); - }; - void inline disconnectTerminalA() - { - write_TCON_R0A(false); - }; - void inline connectTerminalB() - { - write_TCON_R0B(true); - }; - void inline disconnectTerminalB() - { - write_TCON_R0B(false); - }; - void inline connectWiper() - { - write_TCON_R0W(true); - }; - void inline disconnectWiper() - { - write_TCON_R0W(false); - }; - - protected: - private: - uint8_t _address; - - TwoWire* MCPWire; - TCON_Register TCON_lib_reg; - - void write_TCON_Register(); - void write_TCON_R0HW(bool state); - void write_TCON_R0A(bool state); - void write_TCON_R0W(bool state); - void write_TCON_R0B(bool state); -}; - -#endif \ No newline at end of file diff --git a/src/TJC_Show.cpp b/src/TJC_Show.cpp index badf38c..7490e65 100644 --- a/src/TJC_Show.cpp +++ b/src/TJC_Show.cpp @@ -1,12 +1,13 @@ #include "TJC_Show.h" +#include "WiFiControl.h" extern RunTime runtime; -extern TPL0401A digiPot; +extern DS3502 ds3502; extern uint16_t wiperValue; -TJC_Show::TJC_Show(DS18B20& ds18b20, BH1750& bh1750_a, BH1750& bh1750_b, TPL0401A& digiPot, INA226& INA) - : _serial(TJC_SERIAL), _ds18b20(ds18b20), _bh1750_a(bh1750_a), _bh1750_b(bh1750_b), _digiPot(digiPot), _INA(INA) {} +TJC_Show::TJC_Show(DS18B20& ds18b20, BH1750& bh1750_a, BH1750& bh1750_b, DS3502& ds3502, INA226& INA) + : _serial(TJC_SERIAL), _ds18b20(ds18b20), _bh1750_a(bh1750_a), _bh1750_b(bh1750_b), _ds3502(ds3502), _INA(INA) {} void TJC_Show::init() { _serial.begin(115200, SERIAL_8N1, TJC_RX_PIN, TJC_TX_PIN); @@ -31,6 +32,15 @@ void TJC_Show::showQR() { void TJC_Show::showInfo() { char str[128]; + if (SELECTED_WIFI_MODE == WIFI_MODE_STA){ + snprintf(str, sizeof(str), "t15.txt=\"MODE_STA:%s\"\xff\xff\xff", WiFi.localIP()); + sendCommand(str); + } + else if(SELECTED_WIFI_MODE == WIFI_MODE_AP){ + snprintf(str, sizeof(str), "t15.txt=\"MODE_AP:%s\"\xff\xff\xff", WiFi.softAPIP()); + sendCommand(str); + } + snprintf(str, sizeof(str), "t0.txt=\"%.2f\"\xff\xff\xff", _ds18b20.getTempC()); sendCommand(str); @@ -42,16 +52,16 @@ void TJC_Show::showInfo() { snprintf(str, sizeof(str), "t14.txt=\"%s\"\xff\xff\xff", runtime.formatDuration(runtime.getActiveDuration())); sendCommand(str); - snprintf(str, sizeof(str), "t8.txt=\"%d\"\xff\xff\xff", _digiPot.readWiperValue()); + snprintf(str, sizeof(str), "t8.txt=\"%d\"\xff\xff\xff", _ds3502.getWiper()); sendCommand(str); - snprintf(str, sizeof(str), "t9.txt=\"%.3f\"\xff\xff\xff", _INA.getBusVoltage()); + snprintf(str, sizeof(str), "t9.txt=\"%.2f\"\xff\xff\xff", _INA.getBusVoltage()); sendCommand(str); - snprintf(str, sizeof(str), "t10.txt=\"%.3f\"\xff\xff\xff", _INA.getShuntVoltage_mV()); + snprintf(str, sizeof(str), "t10.txt=\"%.2f\"\xff\xff\xff", _INA.getShuntVoltage_mV()); sendCommand(str); - snprintf(str, sizeof(str), "t11.txt=\"%.3f\"\xff\xff\xff", _INA.getCurrent_mA()); + snprintf(str, sizeof(str), "t11.txt=\"%.2f\"\xff\xff\xff", _INA.getCurrent()); sendCommand(str); - snprintf(str, sizeof(str), "t12.txt=\"%.3f\"\xff\xff\xff", _INA.getPower_mW()); + snprintf(str, sizeof(str), "t12.txt=\"%.2f\"\xff\xff\xff", _INA.getPower()); sendCommand(str); } diff --git a/src/TJC_Show.h b/src/TJC_Show.h index fc61e4a..c151d55 100644 --- a/src/TJC_Show.h +++ b/src/TJC_Show.h @@ -4,7 +4,7 @@ #include #include "BH1750.h" #include "DS18B20.h" -#include "DigitalPot.h" +#include "DS3502.h" #include "INA226.h" #include #include "RunTime.h" @@ -16,7 +16,7 @@ class TJC_Show { public: - TJC_Show(DS18B20& ds18b20, BH1750& bh1750_a, BH1750& bh1750_b, TPL0401A& digiPot, INA226& INA); + TJC_Show(DS18B20& ds18b20, BH1750& bh1750_a, BH1750& bh1750_b, DS3502& DS3502, INA226& INA); void init(); void showQR(); void showInfo(); @@ -26,7 +26,7 @@ private: DS18B20& _ds18b20; BH1750& _bh1750_a; BH1750& _bh1750_b; - TPL0401A& _digiPot; + DS3502& _ds3502; INA226& _INA; void clearSerialBuffer(); void sendCommand(const char* command); diff --git a/src/WiFiControl.cpp b/src/WiFiControl.cpp index 5bd4e1a..243d54d 100644 --- a/src/WiFiControl.cpp +++ b/src/WiFiControl.cpp @@ -1,17 +1,70 @@ #include "WiFiControl.h" + +const char* STA_SSIDS[STA_WIFI_COUNT] = {"SERVIRST-CT", "IRIS"}; +const char* STA_PASSWORDS[STA_WIFI_COUNT] = {"servirst8888", "irishk*******"}; + + void WiFi_Init() { #if SELECTED_WIFI_MODE == WIFI_MODE_STA - WiFi.begin(STA_SSID, STA_PASSWORD); // WiFi - while (WiFi.status() != WL_CONNECTED) { - delay(1000); - Serial.println("Connecting to WiFi..."); + if (!connectToKnownNetworks()) { + switchToAPMode(); } - Serial.print("Address: "); - Serial.println(WiFi.localIP()); - #elif SELECTED_WIFI_MODE == WIFI_MODE_AP - WiFi.softAP(AP_SSID, AP_PASSWORD); - Serial.print("AP IP address: "); - Serial.println(WiFi.softAPIP()); + #else + switchToAPMode(); #endif +} + +bool connectToKnownNetworks() +{ + Serial.println("Scanning nearby networks..."); + + int numNetworks = WiFi.scanNetworks(); + if (numNetworks == 0) { + Serial.println("No networks found."); + return false; + } + + Serial.print("Found "); + Serial.print(numNetworks); + Serial.println(" networks."); + + for (int i = 0; i < numNetworks; ++i) { + String scannedSSID = WiFi.SSID(i); + + for (int j = 0; j < STA_WIFI_COUNT; ++j) { + if (scannedSSID == STA_SSIDS[j]) { + Serial.print("Found known network: "); + Serial.println(scannedSSID); + + WiFi.begin(STA_SSIDS[j], STA_PASSWORDS[j]); + unsigned long startTime = millis(); + + while (WiFi.status() != WL_CONNECTED && millis() - startTime < 10000) { + delay(500); + Serial.print("."); + } + + if (WiFi.status() == WL_CONNECTED) { + Serial.println("\nConnected to WiFi"); + Serial.print("IP Address: "); + Serial.println(WiFi.localIP()); + return true; + } else { + Serial.println("Failed to connect."); + } + } + } + } + + Serial.println("Could not connect to any known network."); + return false; +} + +void switchToAPMode() +{ + Serial.println("Switching to AP mode."); + WiFi.softAP(AP_SSID, AP_PASSWORD); + Serial.print("AP IP address: "); + Serial.println(WiFi.softAPIP()); } \ No newline at end of file diff --git a/src/WiFiControl.h b/src/WiFiControl.h index cef4935..d03e567 100644 --- a/src/WiFiControl.h +++ b/src/WiFiControl.h @@ -1,19 +1,51 @@ +// #include + +// #define WIFI_MODE_STA 1 +// #define WIFI_MODE_AP 2 + +// // 选择WiFi模式,取消注释需要的模式 +// #define SELECTED_WIFI_MODE WIFI_MODE_STA +// //#define SELECTED_WIFI_MODE WIFI_MODE_AP + + +// // STA模式下的SSID和密码 +// // #define STA_SSID "SERVIRST-CT" +// // #define STA_PASSWORD "servirst8888" +// #define STA_SSID "IRIS" +// #define STA_PASSWORD "irishk*******" + +// // AP模式下的SSID和密码 +// #define AP_SSID "APSSID" +// #define AP_PASSWORD "APPassword" + +// void WiFi_Init(void); + +// WiFiControl.h +#ifndef WIFICONTROL_H +#define WIFICONTROL_H + #include -#define WIFI_MODE_STA 1 -#define WIFI_MODE_AP 2 +#define WIFI_MODE_STA 0 +#define WIFI_MODE_AP 1 -// 选择WiFi模式,取消注释需要的模式 +// 默认选择WiFi模式 #define SELECTED_WIFI_MODE WIFI_MODE_STA //#define SELECTED_WIFI_MODE WIFI_MODE_AP - -// STA模式下的SSID和密码 -#define STA_SSID "SERVIRST-CT" -#define STA_PASSWORD "servirst8888" +// 设置多个 Wi-Fi 账号密码 +#define STA_WIFI_COUNT 2 +extern const char* STA_SSIDS[STA_WIFI_COUNT]; +extern const char* STA_PASSWORDS[STA_WIFI_COUNT]; // AP模式下的SSID和密码 #define AP_SSID "APSSID" #define AP_PASSWORD "APPassword" -void WiFi_Init(void); \ No newline at end of file +void WiFi_Init(void); + +bool connectToKnownNetworks(void); +void switchToAPMode(void); + + +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 6029808..4a6bb13 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,7 +4,7 @@ #include #include #include -#include "MCP45HVX1.h" + #include "INA226.h" #include #include // 用于存储非易失性数据 @@ -12,17 +12,15 @@ #include "RunTime.h" #include "WebServer.h" #include "WiFiControl.h" -#include "DigitalPot.h" +#include "DS3502.h" +#include "LightControl.h" // Ticker 用于定时检查电流状态 Ticker currentCheckTicker; -// MCP45HVX1 数字电位器 -MCP45HVX1 mcp45hvx1(0x3C); - -//TPL0401A 数字电位器 -TPL0401A digiPot(0x2E); +// DS3502 数字电位器 +DS3502 ds3502(0x28); // INA226 电流监控器 INA226 INA(0x40); @@ -38,6 +36,9 @@ DS18B20 ds18b20(&oneWire); BH1750 bh1750_a; BH1750 bh1750_b; +// 按键引脚 +#define INCREASE_BUTTON_PIN 3 +#define DECREASE_BUTTON_PIN 46 // 风扇控制引脚 #define FAN_PIN1 15 @@ -50,6 +51,12 @@ extern RunTime runtime; Preferences prefs; +TJC_Show tjcShow(ds18b20, bh1750_a, bh1750_b, ds3502, INA); + +// 比例调节,bh1750_a +LightControl lightCtrl(bh1750_a, ds3502); +bool autoAdjustEnabled = false; // 是否启用自动调节,默认关闭 +float targetLightValue = 2000; // 默认目标照度 void WebServer_Init(void) { // 根路径的页面 @@ -77,7 +84,7 @@ void WebServer_Init(void) { // 配置Wiper接口 server.on("/wiper", HTTP_GET, [](AsyncWebServerRequest *request) { - String wiper = String(digiPot.readWiperValue()); + String wiper = String(ds3502.getWiper()); request->send(200, "text/plain", wiper); }); @@ -116,7 +123,7 @@ void WebServer_Init(void) { server.on("/setWiper", HTTP_POST, [](AsyncWebServerRequest *request) { if (request->hasParam("value", true)) { int wiperValue = request->getParam("value", true)->value().toInt(); - digiPot.writeWiper(wiperValue); + ds3502.setWiper(wiperValue); request->send(200, "text/plain", "Wiper value set"); } else { request->send(400, "text/plain", "Missing value parameter"); @@ -129,7 +136,7 @@ void WebServer_Init(void) { // 风扇控制 void FansControl(void) { - if (ds18b20.getTempC() > 25) { + if (ds18b20.getTempC() > 30) { digitalWrite(FAN_PIN1, HIGH); digitalWrite(FAN_PIN2, HIGH); } else { @@ -140,7 +147,7 @@ void FansControl(void) { // 蜂鸣器 void Buzzer(void) { - if (ds18b20.getTempC() > 20) { + if (ds18b20.getTempC() > 45) { digitalWrite(BUZZER_PIN, HIGH); //tone(BUZZER_PIN, 1000, 1000); } else { @@ -149,8 +156,33 @@ void Buzzer(void) { } } +//按键控制 +void ButtonControl(void) { + // 按键检测 + static int lastWiperValue = ds3502.getWiper(); + static const int step = 5; // 步进值 + + if (digitalRead(INCREASE_BUTTON_PIN) == LOW) { + int currentWiperValue = ds3502.getWiper(); + if (currentWiperValue < 127) { + ds3502.setWiper(currentWiperValue + step); + lastWiperValue = currentWiperValue + step; + Serial.printf("Increased Wiper Value: %d\n", lastWiperValue); + } + delay(200); // 防抖动 + } + + if (digitalRead(DECREASE_BUTTON_PIN) == LOW) { + int currentWiperValue = ds3502.getWiper(); + if (currentWiperValue > 0) { + ds3502.setWiper(currentWiperValue - step); + lastWiperValue = currentWiperValue - step; + Serial.printf("Decreased Wiper Value: %d\n", lastWiperValue); + } + delay(200); // 防抖动 + } +} -TJC_Show tjcShow(ds18b20, bh1750_a, bh1750_b, digiPot, INA); void setup(void) @@ -160,7 +192,7 @@ void setup(void) // 初始化 I2C 总线 Wire.begin(5, 4); // SDA SCL - mcp45hvx1.begin(5, 4); + ds3502.begin(5, 4); // INA226 初始化 INA.setMaxCurrentShunt(10.0, 0.005); // 设置最大电流和分流电阻 @@ -192,7 +224,18 @@ void setup(void) tjcShow.init(); tjcShow.showQR(); - digiPot.writeWiper(30); + ds3502.setWiper(2); + delay(300); + Serial.printf("pot: %d\n", ds3502.getWiper()); + + pinMode(FAN_PIN1, OUTPUT); + pinMode(FAN_PIN2, OUTPUT); + pinMode(BUZZER_PIN, OUTPUT); + + // 初始化按键引脚 + pinMode(INCREASE_BUTTON_PIN, INPUT_PULLUP); + pinMode(DECREASE_BUTTON_PIN, INPUT_PULLUP); + } @@ -205,11 +248,11 @@ void loop(void) bh1750_a.readLightLevel(), bh1750_b.readLightLevel()); - // MCP45HVX1 操作 - //digiPot.writeWiper(127); // 随机设置 Wiper 值,random(0, 256) - mcp45hvx1.writeWiper(random(0, 127)); - Serial.print("Current Wiper Value: "); - Serial.println(mcp45hvx1.readWiper()); +// // MCP45HVX1 操作 +// //digiPot.writeWiper(127); // 随机设置 Wiper 值,random(0, 256) +// ds3502.setWiper(random(0, 127)); +// Serial.print("Current Wiper Value: "); +// Serial.println(ds3502.getWiper()); // INA226 数据读取 @@ -218,9 +261,9 @@ void loop(void) Serial.print("\t"); Serial.print(INA.getShuntVoltage_mV(), 3); //分流电压 Serial.print("\t"); - Serial.print(INA.getCurrent_mA(), 3); //通过分流器的电流 + Serial.print(INA.getCurrent(), 3); //通过分流器的电流 Serial.print("\t"); - Serial.print(INA.getPower_mW(), 3); //Current x BusVoltage + Serial.print(INA.getPower(), 3); //Current x BusVoltage Serial.println(); @@ -229,6 +272,12 @@ void loop(void) FansControl(); Buzzer(); + ButtonControl(); + + if (autoAdjustEnabled) { + lightCtrl.adjustWiper(); // 比例调节 + } + delay(1000); } diff --git a/src/webpages.h b/src/webpages.h index d75827f..6d379b6 100644 --- a/src/webpages.h +++ b/src/webpages.h @@ -1,3 +1,104 @@ +// #ifndef WEBPAGES_H +// #define WEBPAGES_H + +// #include + +// const char index_html[] PROGMEM = R"rawliteral( +// +// +// +// ESP32 Data Display +// +// +// +// +//

实时数据显示

+//

累计使用时长: 加载中...

+// +//

温度: 加载中... °C

+//

光照强度(A): 加载中... lx

+//

光照强度(B): 加载中... lx

+//

电位器Wiper值: 加载中...

+//

总线电压: 加载中... V

+//

分流电压: 加载中... mV

+//

电流: 加载中... mA

+//

功率: 加载中... mW

+//

设置Wiper值

+// +// +// +// +// )rawliteral"; // 添加分号 + +// #endif // WEBPAGES_H + + #ifndef WEBPAGES_H #define WEBPAGES_H @@ -90,7 +191,7 @@ const char index_html[] PROGMEM = R"rawliteral(

电流: 加载中... mA

功率: 加载中... mW

设置Wiper值

- +