Nodemcu+DHT11+MicroSD溫溼度記錄儀(一)

一、效果

1、實物連接圖

在這裏插入圖片描述

2、生成的溫溼度記錄文件

在這裏插入圖片描述

二、電路圖

	電路比較簡單,可以直接看代碼裏的引腳定義

三、代碼

#include "time.h"
#include <ESP8266WiFi.h>
//#include <WiFi.h>
#include "DHT.h"
#include <SPI.h>
#include <SD.h>


#define DHTPIN D1 
#define DHTTYPE DHT11
#define LED_BUILTIN 2

#define MOSI D7
#define MISO D6
#define CLK D5
#define CS D8

DHT dht(DHTPIN, DHTTYPE);
File myFile;

const char* ssid     = "**************";                    // your network SSID (name)
const char* password = "************";                    // your network password


const char* ntpServer = "ntp3.aliyun.com";
const long  gmtOffset_sec = 7*3600;
const int   daylightOffset_sec = 3600;

/*
  %a Abbreviated weekday name 
  %A Full weekday name 
  %b Abbreviated month name 
  %B Full month name 
  %c Date and time representation for your locale 
  %d Day of month as a decimal number (01-31) 
  %H Hour in 24-hour format (00-23) 
  %I Hour in 12-hour format (01-12) 
  %j Day of year as decimal number (001-366) 
  %m Month as decimal number (01-12) 
  %M Minute as decimal number (00-59) 
  %p Current locale's A.M./P.M. indicator for 12-hour clock 
  %S Second as decimal number (00-59) 
  %U Week of year as decimal number,  Sunday as first day of week (00-51) 
  %w Weekday as decimal number (0-6; Sunday is 0) 
  %W Week of year as decimal number, Monday as first day of week (00-51) 
  %x Date representation for current locale 
  %X Time representation for current locale 
  %y Year without century, as decimal number (00-99) 
  %Y Year with century, as decimal number 
  %z %Z Time-zone name or abbreviation, (no characters if time zone is unknown) 
  %% Percent sign 
  You can include text literals (such as spaces and colons) to make a neater display or for padding between adjoining columns. 
  You can suppress the display of leading zeroes  by using the "#" character  (%#d, %#H, %#I, %#j, %#m, %#M, %#S, %#U, %#w, %#W, %#y, %#Y) 
*/
char buffer[80];


void printLocalTime()
{
  time_t rawtime;
  struct tm * timeinfo;

  time (&rawtime);
  timeinfo = localtime (&rawtime);

  strftime (buffer,80,"%Y/%m/%d %H:%M:%S ",timeinfo);
  //struct tm timeinfo;
  //time_t now = time(nullptr);
  Serial.print(buffer);
  //Serial.print(" ");
  //Serial.print(ctime(&now));
  //Serial.print(&timeinfo, " %d %B %Y %H:%M:%S ");
}


void setup() 
{
  Serial.begin(115200);
  delay(10);
  pinMode(LED_BUILTIN, OUTPUT);
  dht.begin();
  // We start by connecting to a WiFi network
  Serial.print("\n\nConnecting to ");
  Serial.println(ssid);
  
  /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
    would try to act as both a client and an access-point and could cause
  network-issues with your other WiFi-devices on your WiFi-network. */
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }
  
  Serial.println();
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  
  Serial.println("\nWaiting for time");
  unsigned timeout = 5000;
  unsigned start = millis();
  while (!time(nullptr)) 
  {
    Serial.print(".");
    delay(1000);
  }
  delay(1000);
  
  Serial.println("Time...");
  
  Serial.print("Initializing SD card...");
  
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  
}

void loop() 
{
  delay(2000);
  digitalWrite(LED_BUILTIN, LOW);

  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }
  //time_t now = time(nullptr);
  //Serial.print(ctime(&now));
  printLocalTime();
  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F(" % Temperature: "));
  Serial.print(t);
  Serial.println(F(" °C "));

  myFile = SD.open("test.txt", FILE_WRITE);
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.print(buffer);
    //myFile.print(" ");
    myFile.print(F("Humidity: "));
    myFile.print(h);
    myFile.print(F(" % Temperature: "));
    myFile.print(t);
    myFile.println(F(" °C "));
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
  digitalWrite(LED_BUILTIN, HIGH);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章