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: http://kythuatvatlieu.org/magazine/arduino.html
Post Reply
User avatar
support
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 https://publiclab.org/notes/cfastie/04- ... 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
RTC_DS1307 RTC;
#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");
    return;
  }
  
  Serial.println(" SD card OK");   
  //————-print a header to the data file———-
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
  if (dataFile) {                     // if the file is available, write to it:
    dataFile.println("Nano Logger");
    dataFile.close();
  }
  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 = RTC.now();              //this reads the time from the RTC
    sprintf(TmeStrng, "%04d/%02d/%02d,%02d:%02d:%02d", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second()); // [added seconds]

    Serial.print("RTC utc Time: ");
    Serial.print(now.unixtime());
    Serial.println();
    Serial.print("RTC time: ");
    Serial.println(TmeStrng);
    Serial.print("BME280 temp: ");
    Serial.print(BMEt);
    Serial.println(" C");
    Serial.print("BME280 Humidity: ");
    Serial.print(BMEh);
    Serial.println(" %");
    Serial.print("Pressure: ");
    Serial.print(BMEp);
    Serial.println(" hPa");
    Serial.print("Elevation: ");
    Serial.print(BMEa);
    Serial.println(" m");
    Serial.println();
                //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 = SD.open("datalog.txt", FILE_WRITE);  // if the file is available, write to it:
if (dataFile) {
  dataFile.println(dataString);
  dataFile.close();
}
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: http://forum.arduino.cc/index.php?topic=27986.0
Để 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
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 api.thingspeak.com.
Giao thức giao tiếp Web - REST: https://techtalk.vn/web-service-rest-se ... o-ban.html
====================================================
Vấn đề nảy sinh


Post Reply