本帖最后由 serialworld 于 2025-2-3 07:42 编辑
使用Arduino ESP32 S2和DTH11制作的温湿度计
#define LED_PIN 15
#include <U8x8lib.h>
#include <Ticker.h>
#include <Adafruit_NeoPixel.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTTYPE DHT11 // DHT 11
#define DHTPIN 33
DHT_Unified dht(DHTPIN, DHTTYPE);
#define PIN_WS2812B 18 // The ESP32 pin GPIO16 connected to WS2812B
#define NUM_PIXELS 8 // The number of LEDs (pixels) on WS2812B LED strip
#define PIN_DHT11 33
Adafruit_NeoPixel ws2812b(NUM_PIXELS, PIN_WS2812B, NEO_GRB + NEO_KHZ800);
U8X8_SH1106_128X64_NONAME_SW_I2C u8x8(11, 12, /* reset=*/U8X8_PIN_NONE);
Ticker ticker1;
Ticker ticker2;
char temp2[20];
char hum2[20];
void led_show() {
ws2812b.clear();
for (int pixel = 0; pixel < NUM_PIXELS; pixel++) { // for each pixel
ws2812b.setPixelColor(pixel, ws2812b.Color(random(1, 15), random(1, 15), random(1, 15))); // it only takes effect if pixels.show() is called
delay(50);
ws2812b.show();
}
// delay(10);
// ws2812b.clear(); // set all pixel colors to 'off'. It only takes effect if pixels.show() is called
// ws2812b.show(); // update to the WS2812B Led Strip
// delay(10); // 1 second off time
}
void led_show2() {
ws2812b.clear();
for (int pixel = 0; pixel < NUM_PIXELS; pixel++) { // for each pixel
ws2812b.setPixelColor(pixel, ws2812b.Color(random(1, 5), random(1, 5), random(1, 5))); // it only takes effect if pixels.show() is called
}
ws2812b.show(); // update to the WS2812B Led Strip
delay(100); // 1 second off time
}
void led_show3() {
ws2812b.clear();
static int count = 0;
int index;
int position;
for (position = 0; position < NUM_PIXELS; position++) {
ws2812b.clear();
for (int pixel = 0; pixel < NUM_PIXELS; pixel++) { // for each pixel
// if (position == pixel or (position+count)%NUM_PIXELS == pixel)
if (position == pixel or (position + count) % NUM_PIXELS == pixel)
ws2812b.setPixelColor(pixel, ws2812b.Color(random(1, 20), random(1, 20), random(1, 20)));
else
ws2812b.setPixelColor(pixel, ws2812b.Color(0, 0, 0));
}
ws2812b.show();
delay(100);
}
count++;
}
void callback1() {
digitalWrite(LED_PIN, HIGH);
led_show3();
}
void callback2() {
digitalWrite(LED_PIN, LOW);
led_show3();
}
void setup() {
// put your setup code here, to run once:
sensor_t sensor;
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
u8x8.begin();
u8x8.setPowerSave(0);
ticker1.attach(5, callback1);
ticker2.attach(3, callback2);
dht.begin();
ws2812b.begin(); // initialize WS2812B strip object (REQUIRED)
}
void loop() {
// put your main code here, to run repeatedly:
// digitalWrite(LED_PIN, HIGH);
// delay(1000);
// digitalWrite(LED_PIN, LOW);
// delay(1000);
sensors_event_t event;
dht.temperature().getEvent(&event);
Serial.print(F("Temperature: "));
float temp1 = event.temperature;
Serial.print(temp1);
Serial.println(F("°C"));
dht.humidity().getEvent(&event);
Serial.print(F("Humidity: "));
float hum1 = event.relative_humidity;
Serial.print(hum1);
Serial.println(F("%"));
u8x8.refreshDisplay();
// delay(100);
// u8x8.clear();
// u8x8.setFont(u8x8_font_px437wyse700b_2x2_r);
u8x8.setFont(u8x8_font_courB18_2x3_r);
// u8x8.setFont(u8x8_font_chroma48medium8_r);
// u8x8.refreshDisplay(); // only required for SSD1606/7
dtostrf(temp1, 2, 1, temp2);
dtostrf(hum1, 2, 1, hum2);
// u8x8.setFont(u8x8_font_chroma48medium8_r);
u8x8.drawString(0, 1, "Tem:");
u8x8.drawString(8, 1, temp2);
u8x8.setInverseFont(0);
u8x8.drawString(0, 5, "Hum:");
u8x8.drawString(8, 5, hum2);
u8x8.setInverseFont(0);
//u8x8.drawString(0,8,"Line 8");
//u8x8.drawString(0,9,"Line 9");
u8x8.refreshDisplay(); // only required for SSD1606/7
// Serial.print("Hello world!\n");
// Serial.println(esp_random());
// Serial.println(random(1, 15));
Serial.print(touchRead(T1));
Serial.print("\t");
Serial.print(touchRead(T2));
Serial.print("\t");
Serial.print(touchRead(T3));
Serial.print("\t");
Serial.print(touchRead(T4));
Serial.print("\t");
Serial.println(touchRead(T5));
}
th2
th3