Arduino Nano: Data Logging and Ethernet shields

Ứng dụng Arduino trong điều khiển thiết bị điện, điện tử.
Xêm thêm tại:
Post Reply
User avatar
Posts: 270
Joined: 05 Jan 2017, 09:34

Arduino Nano: Data Logging and Ethernet shields

Post by support » 03 Oct 2017, 10:57

Hướng dẫn sử dụng: Data logger shield for Nano Xem thêm tại ... d-for-nano

Code: Select all

/* Derived from Ed Mallon's starter logging code.
C. Fastie modified to run without sleeping anything.
added BME280 sensor, added seconds to time readout
added unixtime
#include <SdFat.h>
#include  <SPI.h>
#include <Wire.h>
#include <RTClib.h>               
#include <Adafruit_BME280.h>

Adafruit_BME280 bme;                  // The BME280 will be an I2C device
float hgInches = 29.95;               // Enter the sealevel barometric pressure here (xx.xx inches Hg)
#define SEALEVELPRESSURE_HPA (hgInches/0.02952998751)      // hPa=(inches Hg)/0.02952998751
SdFat SD;
#define MOSIpin 11
#define MISOpin 12
#define DS1307_I2C_ADDRESS 0x68
char TmeStrng[] = "0000/00/00,00:00:00";     //19 ascii characters 

void setup() {  
  Serial.begin(9600);                  // Open serial communications and wait for port to open:
  Wire.begin();                        // start the i2c interface
  RTC.begin();                         // start the RTC 
  RTC.adjust(DateTime((__DATE__), (__TIME__)));    //sets the RTC to the computer time when the sketch is loaded
  Serial.print("Find SD card: ");
  if (!SD.begin(10)) {
    Serial.println("Card failed");
  Serial.println(" SD card OK");   
  //————-print a header to the data file———-
  File dataFile ="datalog.txt", FILE_WRITE);
  if (dataFile) {                     // if the file is available, write to it:
    dataFile.println("Nano Logger");
  else {
    Serial.println("file error");     // if the file isn’t open, pop up an error:
    bool status;      
    status = bme.begin(0x76);         // 0x76 is the I2C address of the clone sensors I have
    if (!status) {
        Serial.println("No BME280");
        while (1);
}                                     // end of setup
void loop() {
    float BMEt = (bme.readTemperature());
    float BMEh = (bme.readHumidity());
    float BMEp = (bme.readPressure() / 100.0F);
    float BMEa = (bme.readAltitude(SEALEVELPRESSURE_HPA));
    DateTime now =;              //this reads the time from the RTC
    sprintf(TmeStrng, "%04d/%02d/%02d,%02d:%02d:%02d", now.year(), now.month(),, now.hour(), now.minute(), now.second()); // [added seconds]

    Serial.print("RTC utc Time: ");
    Serial.print("RTC time: ");
    Serial.print("BME280 temp: ");
    Serial.println(" C");
    Serial.print("BME280 Humidity: ");
    Serial.println(" %");
    Serial.print("Pressure: ");
    Serial.println(" hPa");
    Serial.print("Elevation: ");
    Serial.println(" m");
                //Construct a data string to write to µSD card
    String dataString = "";                  //this erases the previous string
    dataString += TmeStrng;
    dataString += ",";    
    dataString += String(now.unixtime());
    dataString += ",";      
    dataString += String(BMEt);
    dataString += ",";   
    dataString += String(BMEh);
    dataString += ",";    
    dataString += String(BMEp);
    dataString += ",";    
    dataString += String(BMEa);
//——– write the data to the SD card ——–
File dataFile ="datalog.txt", FILE_WRITE);  // if the file is available, write to it:
if (dataFile) {
else {
  Serial.println("file error");        // if the file isn’t open, pop up an error:
 delay(3000);                          // write data every 3 seconds
}             // END of the MAIN LOOP
The Ethernet Controller (ENC28J60) is a so called SPI device and uses the SPI pins (10, 11, 12, 13) of your Arduino.
  • SS stands for Slave Select, used to enable or disable the slave device (the Ethernet module in this case).
  • MOSI stands for Master Output Slave Input, or in other words: Arduino OUTPUT (data from Arduino to Ethernet Controller).
  • MISO stands for the opposite, Master Input Slave Output, or: Arduino INPUT (data from Ethernet Controller to Arduino).
  • SCK is the clock used for SPI timing.
Trao đổi về có bao nhiêu thiết bị cùng dùng SPI:
Để kết nối mạng LAN thì nên dùng thư viện sau đây để chạy (dã test OK):
Arduino-Nan0-Data-Logging-and-Ethernet-Shields.jpg (418.65 KiB) Viewed 167 times
Các ứng dụng chính:
  • Ghi dữ liệu nhận được vào thẻ nhớ.
  • Ghi dữ liệu nhận được vào
Giao thức giao tiếp Web - REST: ... o-ban.html
Vấn đề nảy sinh

Post Reply