Nodemcu+DHT11+MicroSD温湿度记录仪(二)

一、效果

1、实物图

Nodemcu+DHT11+MicroSD温湿度记录仪(二)_第1张图片

2、串口信息

Nodemcu+DHT11+MicroSD温湿度记录仪(二)_第2张图片

3、生成的文件

Nodemcu+DHT11+MicroSD温湿度记录仪(二)_第3张图片

4、功能说明

相比之前的版本,这个版本增加了光敏电阻电路,增加了一条光照度信息,其他电路基本不变。代码方面,可以将采集的信息自动以日期为文件名进行分类,方便信息的整理。注:截图少掉的日期文件,是模块没有通电,MicroSD模块据说只能支持小于2G的内存卡,虽然容量看起来比较小,但经过我几天的连续测试,如果2秒储存一次数据,数据长度为61字节,能够保存1年以上的数据。如果阿里的授时服务器,温湿度传感器,SD卡模块,网络有问题,整个代码都不能顺利的跑起来。目前这个方案还没有出现上述问题,比较稳定可靠。如果大家感兴趣的话,建议独立测试每个模块,功能都正常了以后,再一起装起来。

5、光敏电阻电路图

Nodemcu+DHT11+MicroSD温湿度记录仪(二)_第4张图片

二、代码

在这里插入代码片#include "time.h"
#include 
//#include 
#include "DHT.h"
#include 
#include 

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

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

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];
char label[13];
int sensorValue = 0;

String inString = "";




void charToString()
{
  int i;
  for(i=0;i<13;i++){
    inString += label[i];
    }
  }
void clearChar()
{
  int i;
  for(i=0;i<13;i++){
    label[i] += '\0';
    }
  }
void printLocalTime()
{
  time_t rawtime;
  struct tm * timeinfo;

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

  strftime (buffer,80,"%Y/%m/%d %H:%M:%S ",timeinfo);
  strftime (label,9,"%Y%m%d%H",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() 
{
  inString = "";
  delay(2000);
  
  digitalWrite(LED_BUILTIN, LOW);
  sensorValue = analogRead(sensorPin);
  
  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.print(F(" °C"));
  Serial.print(F(" Light: "));
  Serial.println(sensorValue);

  //Serial.println(label);
  strcat(label ,".txt");
  Serial.print("Files name : ");
  Serial.println(label);
  charToString();
  Serial.println("data lenth : 69");
  

  myFile = SD.open(inString, FILE_WRITE);
  
  if (myFile) {
    Serial.print("Writing data ......");
    myFile.print(buffer);
    Serial.println(buffer);
    //myFile.print(" ");
    myFile.print(F("Humidity: "));
    myFile.print(h);
    myFile.print(F(" % Temperature: "));
    myFile.print(t);
    myFile.print(F(" °C"));
    myFile.print(F(" Light: "));
    myFile.println(sensorValue);
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening File name");
    
  }
  
  /*
if (!SD.begin(4)) {
  Serial.println("micro sdcard normal");
   
}
else {
  Serial.println("micro sdcard failed");
  
  }
  */
  
  clearChar();
  inString = "";
  digitalWrite(LED_BUILTIN, HIGH);
  
}

你可能感兴趣的:(DIY)