用户
 找回密码
 立即注册

QQ登录

只需一步,快速开始

点击进入授权页面

只需一步,快速开始

  • QQ空间
  • 回复
  • 收藏

该用户从未签到

本帖最后由 杰来杰去 于 2018-1-8 14:01 编辑

主要用到以下几个模块(加一块UNO)
SD模块oleddht11ds1307
模块的具体参数、资料大家可以网上找。

好了,直接发代码
[Bash shell] 纯文本查看 复制代码
/*
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 10
 * oled and DS1307 attached to SPI bus as follows
 **SCL - pin A5
 **SDA - pin A4 
 * DHT11 attached to arduino as follows
 **OUT - pin 2
 
 created  by Tom Igoe and powenko
 update 8 jan 2018
 by jie
  */
#include <SD.h>
#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 RTC;

// 128x64 I2C OLED
#include <dht11.h> 
// DHT11数字传感器库
#include "U8glib.h" 
// OLED库
File myFile;
const int chipSelect = 10;
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);    // I2C
dht11 DHT11;
#define DHT11PIN 2 
// Data链接数字针脚2

void draw(void) {
  u8g.setFont(u8g_font_8x13); //使用8x13大小的字符
  u8g.setPrintPos(0, 10); // 对应 x,y 轴值
  u8g.print("HUMI(%) :");
  u8g.setPrintPos(73,10); 
  u8g.print((float)DHT11.humidity);
  u8g.setPrintPos(0, 23); 
  u8g.print("TEMP(oC):");
  u8g.setPrintPos(73, 23);
  u8g.print((float)DHT11.temperature);
  DateTime now = RTC.now();
  u8g.setPrintPos(0, 40); 
  u8g.print(now.year(), DEC);
  u8g.setPrintPos(32, 40); 
  u8g.print('/');
  u8g.setPrintPos(40, 40); 
  u8g.print(now.month(), DEC);
  u8g.setPrintPos(56, 40); 
  u8g.print('/');
  u8g.setPrintPos(64, 40); 
  u8g.print(now.day(), DEC);
  
  u8g.setPrintPos(0, 53); 
  u8g.print(now.hour(), DEC);
  u8g.setPrintPos(16, 53); 
  u8g.print(':');
  u8g.setPrintPos(24, 53); 
  u8g.print(now.minute(), DEC);
  u8g.setPrintPos(40, 53); 
  u8g.print(':');
  u8g.setPrintPos(48, 53); 
  u8g.print(now.second(), DEC);
  
}

void setup()
{
  Serial.begin(9600);
  Wire.begin();
  RTC.begin();
  RTC.adjust(DateTime(__DATE__, __TIME__));
  Serial.println("DHT11 TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT11LIB_VERSION);
  Serial.println();
  Serial.print("Initializing SD card...");
  pinMode(10, OUTPUT);
   
if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
}

void loop()
{
  Serial.println("\n");
  int chk = DHT11.read(DHT11PIN);
  Serial.print("Read sensor: ");
  switch (chk)
  {
    case DHTLIB_OK: 
                Serial.println("OK"); 
                break;
    case DHTLIB_ERROR_CHECKSUM: 
                Serial.println("Checksum error"); 
                break;
    case DHTLIB_ERROR_TIMEOUT: 
                Serial.println("Time out error"); 
                break;
    default: 
                Serial.println("Unknown error"); 
                break;
  }

  Serial.print("Humidity (%): ");
  Serial.println((float)DHT11.humidity, 2);

  Serial.print("Temperature (oC): ");
  Serial.println((float)DHT11.temperature, 2);
  DateTime now = RTC.now();
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();
  delay(200);
  u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );

  delay(200);
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);
  
  // if the file opened okay, write to it:
  if (myFile) {
    myFile.println("\n");
    myFile.print("Humidity (%): ");
    myFile.println((float)DHT11.humidity, 2);
    myFile.print("Temperature (oC): ");
    myFile.println((float)DHT11.temperature, 2);
     DateTime now = RTC.now();
    myFile.print(now.year(), DEC);
    myFile.print('/');
    myFile.print(now.month(), DEC);
    myFile.print('/');
    myFile.print(now.day(), DEC);
    myFile.print(' ');
    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.print(now.minute(), DEC);
    myFile.print(':');
    myFile.print(now.second(), DEC);
    myFile.println();
    delay(200);
  // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }      
}


具体效果如图:
1】oled上的显示 7.JPG
2】串口监视器的显示
2.JPG
3】SD卡内数据
1.JPG
顺便附上用到的几个库:
(oled的库是大家就自行网上找啦。)


DS1307.zip

71.34 KB, 下载次数: 10

rtclib.zip

44.45 KB, 下载次数: 9

Dht11.zip

3 KB, 下载次数: 16

新人第一次发帖,多多指教!
考虑下定时新建文件存储
sanhuasr 发表于 2018-1-8 19:13
考虑下定时新建文件存储

嗯嗯,好建议。
发新帖
发表评论
高级模式  
您需要登录后才可以回帖 登录 | 立即注册  
关闭

推荐主题 上一条 /2 下一条