目前我正在做一个需要监控一些传感器的项目。我使用的传感器之一是 TCS34725 RGBC 光传感器。使用 Blynk,我将数据记录到数据库并将其显示在浏览器仪表板和 Blynk 应用程序上。让它运行 2 个月(大约)后,它停止读取该值。
////---------- Blynk Setup
#define BLYNK_PRINT Serial // This prints to Serial Monitor
#include <ESP8266WiFi.h> // for ESP8266
#include <BlynkSimpleEsp8266.h> // for ESP8266
////----------
////---------- OTA Setup
#include <ESP8266mDNS.h> // For OTA w/ ESP8266
#include <WiFiUdp.h> // For OTA
#include <ArduinoOTA.h> // For OTA
char OTAName[] = "Node1";
////----------
////---------- Projects parameter
#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include "Adafruit_SHT31.h"
//#define TCS34725_INTEGRATION_TIME TCS34725_INTEGRATIONTIME_700MS
#define TCS34725_INTEGRATION_TIME TCS34725_INTEGRATIONTIME_24MS
//#define TCS34725_GAIN TCS34725_GAIN_1X
#define TCS34725_GAIN TCS34725_GAIN_60X
const float ATIME_ms = 24.0;
const float AGAINx = 60.0;
#define TCS34725_R_Coef 0.136
#define TCS34725_G_Coef 1.000
#define TCS34725_B_Coef -0.444
#define TCS34725_GA 1.0
#define TCS34725_DF 310.0
//https://www.apogeeinstruments.com/conversion-ppfd-to-lux/
#define LUX2PPFD 0.0135 //sunlight = 0.0185, Cool white CFL = 0.0135
#define PIN_PPFD V3
#define PIN_DLI V4
#define PIN_LUX V2
#define PIN_E_TEMP V0
#define PIN_E_RH V1
uint16_t r, g, b, c, LUX;
uint16_t ir;
uint16_t r_comp, g_comp, b_comp, c_comp;
float PPFD, T, H , CPL;
BlynkTimer timer;
Adafruit_SHT31 sht31 = Adafruit_SHT31();
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATION_TIME, TCS34725_GAIN);
////----------
///-----------Credentials
char auth[] = "YourAuth";
char ssid[] = "YourSSID";
const char* pass = "YourPassword";
char server[] = "192.168.141.230"; // IP for your Local Server
int port = 8080;
////----------
void myTimerEvent()
{
//modify this if you modify the TCS34725_INTEGRATION_TIME and TCS34725_GAIN
tcs.getRawData(&r, &g, &b, &c);
//LUX = tcs.calculateLux(r,g,b);
ir = (r + g + b > c) ? (r + g + b - c) / 2 : 0;
r_comp = r - ir;
g_comp = g - ir;
b_comp = b - ir;
c_comp = c - ir;
LUX = (TCS34725_R_Coef * float(r_comp) + TCS34725_G_Coef * float(g_comp) + TCS34725_B_Coef * float(b_comp)) / CPL;
PPFD = LUX * LUX2PPFD;
T = sht31.readTemperature();
H = sht31.readHumidity();
/*
Serial.print("[");Serial.print(millis());Serial.print("]");
Serial.print(F("Lux:"));Serial.print(LUX);Serial.println();;
Serial.print("[");Serial.print(millis());Serial.print("]");
Serial.print(F("PPFD:"));Serial.println(PPFD);
Serial.print("[");Serial.print(millis());Serial.print("]");
Serial.print(F("Temperature:"));Serial.print(T);Serial.print(F("C "));Serial.print(F("Humidity:"));Serial.print(H);Serial.println(F("%"));
Serial.println();
*/
Blynk.virtualWrite(PIN_LUX, LUX);
Blynk.virtualWrite(PIN_PPFD, PPFD);
Blynk.virtualWrite(PIN_E_TEMP, T);
Blynk.virtualWrite(PIN_E_RH, H);
}
void setup() {
Serial.begin(9600); // BLYNK_PRINT data
WiFi.begin(ssid, pass);
Blynk.config(auth, server, port);
Blynk.connect();
ArduinoOTA.setHostname("Node1"); // For OTA - Use your own device identifying name
ArduinoOTA.begin(); // For OTA
//timer.setInterval(5 * 60 * 1000L, myTimerEvent); // every 5 minutes
timer.setInterval(60 * 1000L, myTimerEvent); // every 1 minute
sht31.begin(0x44);
tcs.begin();
CPL = (ATIME_ms * AGAINx) /(TCS34725_GA * TCS34725_DF);
myTimerEvent();
}
void loop() {
ArduinoOTA.handle(); // For OTA
if(!Blynk.connected()){
Serial.println("Blynk has been disconnected");
Serial.print("Connecting");
while(!Blynk.connected()){ // reconnect if Blynk is disconnected
Serial.print(".");
boolean connection = Blynk.connect();
if(connection){
Serial.println();
break;
}
}
}
if(Blynk.connected()){
Blynk.run();
timer.run(); // Initiates BlynkTimer
ArduinoOTA.handle(); // For OTA
}
}
此代码工作 2 个月。除了 TCS34725,我在连接到 WeMos D1 mini 的电路中还有 SHT31-D、7805 5V 稳压器和 3030 5V 风扇。唯一发生的事故是盒子(我放置所有电路的地方)下降了 10 - 20 厘米,并且仍然工作了几个星期。
我能得到的唯一值是当我重置我的 Wemos D1 mini 并且它会在发送 0 之前发送 1 个读数。