一、效果
1、實物圖
2、串口信息
3、生成的文件
4、功能說明
相比之前的版本,這個版本增加了光敏電阻電路,增加了一條光照度信息,其他電路基本不變。代碼方面,可以將採集的信息自動以日期爲文件名進行分類,方便信息的整理。注:截圖少掉的日期文件,是模塊沒有通電,MicroSD模塊據說只能支持小於2G的內存卡,雖然容量看起來比較小,但經過我幾天的連續測試,如果2秒儲存一次數據,數據長度爲61字節,能夠保存1年以上的數據。如果阿里的授時服務器,溫溼度傳感器,SD卡模塊,網絡有問題,整個代碼都不能順利的跑起來。目前這個方案還沒有出現上述問題,比較穩定可靠。如果大家感興趣的話,建議獨立測試每個模塊,功能都正常了以後,再一起裝起來。
5、光敏電阻電路圖
二、代碼
在這裏插入代碼片#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
#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);
}