久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1065|回復: 0
收起左側

Arduino Nano和NodeMCU ESP8266讀取DHT11環境溫濕度數據及OLED顯示

[復制鏈接]
ID:1110079 發表于 2024-8-13 18:17 | 顯示全部樓層 |閱讀模式
Arduino Nano 開發板引腳定義

實物展示
IMG_20240813.jpg


代碼
  1. /*
  2. https://breakrow.com/miliohm/tem ... ke-oled-termometer/
  3. 10 - DHT11 pin
  4. OLED:
  5. SDA - SDA  
  6. SCL - SCL
  7. */
  8. #include <SPI.h>
  9. #include <Wire.h>
  10. #include <Adafruit_GFX.h>
  11. #include <Adafruit_SSD1306.h>
  12. #include <Fonts/FreeMonoBold18pt7b.h>

  13. #include "DHT.h"
  14. #define DHTPIN 10 // data connection pin of DHT11
  15. #define DHTTYPE DHT11   // DHT 11
  16. DHT dht(DHTPIN, DHTTYPE);

  17. int h;
  18. int t;

  19. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  20. #define SCREEN_HEIGHT 64 // OLED display height, in pixels

  21. // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
  22. #define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
  23. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

  24. #define bitmap_height   128
  25. #define bitmap_width    64
  26. static const unsigned char PROGMEM logo_bmp[] =
  27. { 0x00, 0x00, 0x00, 0x00, 0xFF, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  28.   0x00, 0x00, 0x00, 0x00, 0x3F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29.   0x00, 0x00, 0x00, 0x00, 0x1F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  30.   0x00, 0x00, 0x00, 0x0F, 0x8F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31.   0x00, 0x00, 0x00, 0x08, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32.   0x00, 0x00, 0x00, 0x08, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  33.   0x00, 0x00, 0x00, 0x08, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  34.   0x00, 0x00, 0x00, 0x78, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  35.   0x00, 0x00, 0x00, 0x08, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  36.   0x00, 0x00, 0x80, 0x08, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  37.   0x00, 0x01, 0x80, 0x78, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  38.   0x00, 0x01, 0x80, 0x08, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  39.   0x00, 0x01, 0xC0, 0x08, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  40.   0x00, 0x01, 0xC0, 0x78, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  41.   0x00, 0x01, 0xC0, 0x78, 0x07, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  42.   0x00, 0x03, 0xC0, 0x08, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43.   0x00, 0x03, 0xE0, 0x08, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44.   0x00, 0x07, 0xE0, 0x78, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  45.   0x00, 0x07, 0xF0, 0x08, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46.   0x00, 0x0F, 0xF0, 0x08, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47.   0x00, 0x0F, 0xF8, 0x78, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48.   0x00, 0x1F, 0xF8, 0x08, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49.   0x00, 0x1F, 0xF8, 0x08, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50.   0x00, 0x3F, 0xFC, 0x78, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51.   0x00, 0x3F, 0xFE, 0x08, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52.   0x00, 0x7F, 0xFE, 0x0F, 0x8F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  53.   0x00, 0x7F, 0xFF, 0x7F, 0x83, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54.   0x00, 0xFF, 0xFF, 0x0F, 0x83, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  55.   0x00, 0xFF, 0xFF, 0x0F, 0x83, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  56.   0x01, 0xFF, 0xFF, 0x7F, 0x80, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  57.   0x03, 0xFF, 0xFF, 0x0F, 0x8F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  58.   0x03, 0xFF, 0xFF, 0x0F, 0x87, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59.   0x03, 0xFF, 0xFF, 0x1F, 0x8F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60.   0x07, 0xFF, 0xFF, 0x7F, 0x83, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61.   0x0F, 0xFF, 0xFF, 0x0F, 0x83, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62.   0x0F, 0xFF, 0xFF, 0x0F, 0x8F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  63.   0x1F, 0xFF, 0xFF, 0x7F, 0x83, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  64.   0x1F, 0xFF, 0xFF, 0x0F, 0x8F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  65.   0x1F, 0x0F, 0xF3, 0xEF, 0x83, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66.   0x3E, 0x03, 0xE7, 0xCF, 0xC0, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  67.   0x3C, 0x61, 0xC7, 0x9F, 0xE0, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  68.   0x7C, 0x71, 0xCF, 0xBF, 0xE3, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  69.   0x7C, 0x71, 0xCF, 0xBF, 0xF3, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  70.   0x7C, 0x71, 0x9F, 0xBF, 0xE3, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  71.   0xFC, 0x71, 0x3F, 0xBF, 0xE3, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  72.   0xFC, 0x71, 0x38, 0x1F, 0xE7, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  73.   0xFC, 0x02, 0x20, 0x0F, 0x87, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  74.   0xFE, 0x06, 0x46, 0x03, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  75.   0xFF, 0x8C, 0xC7, 0x18, 0x1F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  76.   0xFF, 0xFC, 0xC7, 0x1C, 0x7F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  77.   0xFF, 0xF9, 0xC7, 0x1C, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  78.   0xFF, 0xF9, 0xC7, 0x1E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  79.   0x7F, 0xF3, 0xC6, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  80.   0x7F, 0xE3, 0xC0, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  81.   0x7F, 0xE7, 0xE0, 0x7E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  82.   0x3F, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  83.   0x1F, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  84.   0x1F, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  85.   0x0F, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  86.   0x07, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  87.   0x03, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  88.   0x00, 0x7F, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  89.   0x00, 0x1F, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  90.   0x00, 0x03, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  91. };

  92. void setup() {
  93.   Serial.begin(9600);
  94.   dht.begin();

  95.   // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  96.   if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
  97.     Serial.println(F("SSD1306 allocation failed"));
  98.     for (;;); // Don't proceed, loop forever
  99.   }
  100.   // Clear the buffer
  101.   display.clearDisplay();
  102.   printText();
  103.   delay(1500);
  104. }

  105. void loop() {
  106.   h = dht.readHumidity();
  107.   t = dht.readTemperature();
  108.   if (isnan(h) || isnan(t)) {
  109.     Serial.println("Failed to read from DHT sensor!");
  110.     return;
  111.   }
  112.   showBitmap();
  113.   printText();
  114.   display.display();
  115.   delay(500);
  116.   display.clearDisplay();
  117. }

  118. void printText() {
  119.   display.setFont(&FreeMonoBold18pt7b);
  120.   display.setTextColor(WHITE);        // Draw white text
  121.   display.setCursor(45, 28);            // Start at top-left corner
  122.   display.print(t);
  123.   display.drawCircle(92, 8, 3, WHITE);
  124.   display.setCursor(100, 27);
  125.   display.print("C");
  126.   display.setCursor(45, 62);
  127.   display.print(h);
  128.   display.print("%");

  129. }

  130. void showBitmap(void) {
  131.   display.drawBitmap(0, 0, logo_bmp, bitmap_height, bitmap_width, WHITE);
  132.   //display.display();
  133. }
復制代碼




視頻效果​​​BV1pNYCeNErk



NodeMCU ESP8266 開發板引腳定義

實物展示

IMG_20240813.jpg

代碼
  1. /*
  2. https://cloud.tencent.com/developer/article/1688146
  3.   GPIO0 (D3) - DHT11 pin
  4.   GPIO4 (D2) - SDA
  5.   GPIO5 (D1) - SCL
  6. */

  7. #include <Wire.h>
  8. #include <Adafruit_GFX.h>
  9. #include <Adafruit_SSD1306.h>
  10. #include <Adafruit_Sensor.h>
  11. #include <DHT.h>

  12. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  13. #define SCREEN_HEIGHT 64 // OLED display height, in pixels

  14. // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
  15. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

  16. #define DHTPIN 0     // Digital pin connected to the DHT sensor : GPIO0 - D3

  17. // Uncomment the type of sensor in use:
  18. #define DHTTYPE    DHT11     // DHT 11
  19. //#define DHTTYPE    DHT22     // DHT 22 (AM2302)
  20. //#define DHTTYPE    DHT21     // DHT 21 (AM2301)

  21. DHT dht(DHTPIN, DHTTYPE);

  22. void setup() {
  23.   Serial.begin(9600);

  24.   dht.begin();

  25.   if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
  26.     Serial.println(F("SSD1306 allocation failed"));
  27.     for(;;);
  28.   }
  29.   delay(2000);
  30.   display.clearDisplay();
  31.   display.setTextColor(WHITE);
  32. }

  33. void loop() {
  34.   delay(2000);

  35.   //read temperature and humidity
  36.   float t = dht.readTemperature();
  37.   float h = dht.readHumidity();
  38.   if (isnan(h) || isnan(t)) {
  39.     Serial.println("Failed to read from DHT sensor!");
  40.   }
  41.   // clear display
  42.   display.clearDisplay();
  43.   
  44.   // display temperature
  45.   display.setTextSize(1);
  46.   display.setCursor(0,0);
  47.   display.print("Temperature: ");
  48.   display.setTextSize(2);
  49.   display.setCursor(0,17);
  50.   display.print(t);
  51.   display.print(" ");
  52.   display.setTextSize(1);
  53.   display.cp437(true);
  54.   display.write(167);
  55.   display.setTextSize(2);
  56.   display.print("C");
  57.   
  58.   // display humidity
  59.   display.setTextSize(1);
  60.   display.setCursor(0, 35);
  61.   display.print("Humidity: ");
  62.   display.setTextSize(2);
  63.   display.setCursor(0, 45);
  64.   display.print(h);
  65.   display.print(" %");
  66.   
  67.   display.display();
  68. }
復制代碼



視頻效果

​​​BV1pNYCeNErb




評分

參與人數 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 国产在视频一区二区三区吞精 | 99视频| av片在线观看 | 欧美视频二区 | av黄色在线观看 | 亚洲综合区 | 日韩综合在线 | 中文字幕一区二区三区在线观看 | 天天干亚洲 | 国产精品毛片一区二区在线看 | 99热最新网址 | 一区二区福利视频 | 欧美精品一区二区三区四区五区 | 一级黄色裸片 | 高清不卡毛片 | 亚洲精品aⅴ| 一级片在线观看 | 精品无码久久久久国产 | 国产精品一区二区电影 | 黄色一级大片在线观看 | 欧美成人高清视频 | 三级黄色网址 | 国产视频一区二区 | 91久久精品 | 日韩av看片 | 欧美日韩国产在线 | 九色在线观看 | 又爽又黄axxx片免费观看 | 在线视频日韩 | 99精品网 | 精品美女视频在免费观看 | 久久999| 羞羞视频网站在线观看 | 国产在线www | 毛片在线免费 | 大香在线伊779 | 国产精品久久久久久久久久久久久 | 一级毛片视频 | 宅女噜噜66国产精品观看免费 | 国产伦精品一区二区三区视频金莲 | 精品国产aⅴ |