294|2

117

帖子

0

TA的资源

一粒金砂(中级)

ESP32 S2和DTH11制作的温湿度计 [复制链接]

本帖最后由 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));
}
clock3.jpg

th2

th2

th3

th3
th1.png

 

最新回复

这个是非常好的设计呀,背板是万能板吗?如果配了电池,可以跑多长时间?   详情 回复 发表于 2025-2-3 07:52

回复
举报

7113

帖子

11

TA的资源

版主

这个是非常好的设计呀,背板是万能板吗?如果配了电池,可以跑多长时间?

点评

背板不是万用板,是打样的pcb板,本来是其他用途的,这次拿来用了。 估计配电池是不行的,电流测量了一下大概60mA,太大了。还不知道如何降低电流。  详情 回复 发表于 2025-2-4 10:56

回复

117

帖子

0

TA的资源

一粒金砂(中级)

lugl4313820 发表于 2025-2-3 07:52 这个是非常好的设计呀,背板是万能板吗?如果配了电池,可以跑多长时间?

背板不是万用板,是打样的pcb板,本来是其他用途的,这次拿来用了。

估计配电池是不行的,电流测量了一下大概60mA,太大了。还不知道如何降低电流。


回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表