程式碼:
https://gist.github.com/clive520/02c258f6eee607e7dbc9d6f1461c3ff5
//////////////////////////////////////////////
// RemoteXY include library //
//////////////////////////////////////////////
// RemoteXY select connection mode and include library
#define REMOTEXY_MODE__ESP32CORE_WIFI_POINT
#include <WiFi.h>
#include <RemoteXY.h>
// RemoteXY connection settings
#define REMOTEXY_WIFI_SSID "RemoteXY_ESP32"
#define REMOTEXY_WIFI_PASSWORD "12345678"
#define REMOTEXY_SERVER_PORT 6377
// RemoteXY configurate
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
{ 255,0,0,24,0,85,0,10,201,1,
129,0,1,8,18,9,17,230,186,171,
229,186,166,0,129,0,44,8,9,9,
17,229,186,166,0,129,0,2,57,18,
9,17,230,191,149,229,186,166,0,129,
0,42,57,9,9,17,37,0,67,6,
21,9,20,8,2,26,11,67,6,22,
57,19,8,2,26,11,66,130,14,22,
35,27,2,26,66,130,15,68,35,27,
2,26 };
// this structure defines all the variables and events of your control interface
struct {
// output variables
char text_1[11]; // string UTF8 end zero
char text_2[11]; // string UTF8 end zero
int8_t level_1; // =0..100 level position
int8_t level_2; // =0..100 level position
// other variable
uint8_t connect_flag; // =1 if wire connected, else =0
} RemoteXY;
#pragma pack(pop)
/////////////////////////////////////////////
// END RemoteXY include //
/////////////////////////////////////////////
#include "Freenove_WS2812_Lib_for_ESP32.h"
#include "DHT.h"
#define DHTPIN 15 // what digital pin we're connected to
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// to 3.3V instead of 5V!
DHT dht(DHTPIN, DHTTYPE);
#define LEDS_COUNT 6
#define LEDS_PIN 13
#define CHANNEL 0
float t;
float h;
int NN = 0;
Freenove_ESP32_WS2812 strip = Freenove_ESP32_WS2812(LEDS_COUNT, LEDS_PIN, CHANNEL, TYPE_GRB);
int delayval = 100;
void setup() {
RemoteXY_Init ();
Serial.begin(9600);
Serial.println("DHT11 test!");
dht.begin(); //啟動DHT
strip.begin();
strip.setBrightness(100); //亮度
}
void loop() {
RemoteXY_Handler ();
if (NN >= 100) {
DHT_11();//副程式讀取濕度溫度
LIGHT();//副程式亮燈
NN = 0;
}
dtostrf(t, 0, 2, RemoteXY.text_1);
dtostrf(h, 0, 2, RemoteXY.text_2);
RemoteXY.level_1 = (int)(t *2);
RemoteXY.level_2 = (int)(h);
delay(100);
NN = NN + 1;
}
//副程式讀取濕度溫度
void DHT_11() {
h = dht.readHumidity();//讀取濕度
t = dht.readTemperature();//讀取攝氏溫度
float f = dht.readTemperature(true);//讀取華氏溫度
if (isnan(h) || isnan(t) || isnan(f)) { //判斷是否有讀到數值
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
}
//副程式亮燈
void LIGHT() {
if(t < 10 ){strip.setLedColorData(0, 255, 0, 0);strip.setLedColorData(1, 0, 0, 0);strip.setLedColorData(2, 0, 0, 0);} //(G,R,B)小於10度亮一顆綠燈
else if(t < 20){strip.setLedColorData(0, 255, 0, 0);strip.setLedColorData(1, 255, 0, 0);strip.setLedColorData(2, 0, 0, 0);}//(G,R,B)小於20度亮2顆綠燈
else if(t < 25){strip.setLedColorData(0, 255, 0, 0);strip.setLedColorData(1, 255, 0, 0);strip.setLedColorData(2, 255, 0, 0);}//(G,R,B)小於25度亮3顆綠燈
else if(t < 30){strip.setLedColorData(0, 255, 0, 0);strip.setLedColorData(1, 255, 0, 0);strip.setLedColorData(2, 255, 0, 0);}//(G,R,B)小於30度亮3顆綠燈
else if(t < 35){strip.setLedColorData(0, 0, 255, 0);strip.setLedColorData(1, 255, 0, 0);strip.setLedColorData(2, 255, 0, 0);}//(G,R,B)小於35度亮1顆紅燈2顆綠燈
else if(t < 40){strip.setLedColorData(0, 0, 255, 0);strip.setLedColorData(1, 0, 255, 0);strip.setLedColorData(2, 255, 0, 0);}//(G,R,B)小於40度亮2顆紅燈1顆綠燈
else if(t >= 40){strip.setLedColorData(0, 0, 255, 0);strip.setLedColorData(1, 0, 255, 0);strip.setLedColorData(2, 0, 255, 0);}//(G,R,B)大於40度亮3顆紅燈
if(h < 40 ){strip.setLedColorData(5, 0, 0, 255);strip.setLedColorData(4, 0, 0, 0);strip.setLedColorData(3, 0, 0, 0);} //(G,R,B)濕度小於40亮一顆藍燈
else if(h < 60){strip.setLedColorData(5, 0, 0, 255);strip.setLedColorData(4, 0, 0, 255);strip.setLedColorData(3, 0, 0, 0);}//(G,R,B)濕度小於60亮2顆藍燈
else if(h < 80){strip.setLedColorData(5, 0, 0, 255);strip.setLedColorData(4, 0, 0, 255);strip.setLedColorData(3, 0, 0, 255);}//(G,R,B)濕度小於80亮3顆藍燈
else if(h < 100){strip.setLedColorData(5, 0, 255, 255);strip.setLedColorData(4, 0, 0, 255);strip.setLedColorData(3, 0, 0, 255);}//(G,R,B)濕度小於100亮1顆紫燈亮2顆藍燈
//strip.setLedColorData(0, 255, 0, 0);strip.setLedColorData(1, 0, 255, 0);strip.setLedColorData(2, 0, 0, 255);
strip.show();
}
沒有留言:
張貼留言