166 lines
3.7 KiB
C++
166 lines
3.7 KiB
C++
#include <Wire.h>
|
|
#include <RTClib.h>
|
|
|
|
#include <SoftwareSerial.h>
|
|
#include <ModbusMaster.h>
|
|
#include <SD.h>
|
|
#include <SPI.h>
|
|
|
|
#define SD_CS_PIN 10 // Chip Select for SD Card
|
|
//RS485 pins
|
|
#define DE_RE_PIN 4
|
|
#define RX_PIN 3 // SoftwareSerial RX pin
|
|
#define TX_PIN 2 // SoftwareSerial TX pin
|
|
#define SLAVE_ID 101
|
|
|
|
RTC_DS3231 rtc; // Create an RTC object
|
|
File dataFile;
|
|
SoftwareSerial modbusSerial(RX_PIN, TX_PIN); // Create a software serial instance
|
|
ModbusMaster node;
|
|
|
|
void setup()
|
|
{
|
|
Serial.begin(9600); // For debugging
|
|
Serial.println("Startup \n");
|
|
|
|
// Initialize RTC
|
|
if (!rtc.begin())
|
|
{
|
|
Serial.println("Couldn't find RTC\n");
|
|
}
|
|
|
|
if (rtc.lostPower())
|
|
{
|
|
Serial.println("RTC lost power, let's set the time!\n");
|
|
// Comment out the following line once the time is set to avoid resetting on every start
|
|
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
|
|
}
|
|
|
|
Serial.print("Time: ");
|
|
Serial.print(rtc.now().timestamp());
|
|
Serial.println("\n");
|
|
|
|
// Initialize SD card
|
|
Serial.println("SD card initializing...");
|
|
pinMode(SD_CS_PIN, OUTPUT);
|
|
|
|
if (!SD.begin(SPI_HALF_SPEED, SD_CS_PIN ))
|
|
{
|
|
Serial.println("SD card initialization failed!\n");
|
|
return;
|
|
}
|
|
Serial.println("SD card initialized.\n");
|
|
|
|
Serial.println("Initialize RS485 module / Modbus \n");
|
|
|
|
|
|
pinMode(DE_RE_PIN, OUTPUT);
|
|
digitalWrite(DE_RE_PIN, LOW); // Set to LOW for receiving mode initially
|
|
|
|
modbusSerial.begin(9600 );
|
|
//
|
|
// while (!modbusSerial) {
|
|
// }
|
|
node.begin(SLAVE_ID, modbusSerial);
|
|
node.preTransmission(preTransmission);
|
|
node.postTransmission(postTransmission);
|
|
}
|
|
|
|
void preTransmission()
|
|
{
|
|
digitalWrite(DE_RE_PIN, HIGH); // Enable RS485 transmit
|
|
}
|
|
|
|
void postTransmission()
|
|
{
|
|
digitalWrite(DE_RE_PIN, LOW); // Disable RS485 transmit
|
|
}
|
|
|
|
|
|
void writeFile(char *str)
|
|
{
|
|
if (!dataFile)
|
|
{
|
|
Serial.println("Error opening file");
|
|
return;
|
|
}
|
|
DateTime now = rtc.now();
|
|
// Log the current date and time
|
|
dataFile.print(now.year(), DEC);
|
|
dataFile.print('-');
|
|
dataFile.print(now.month(), DEC);
|
|
dataFile.print('-');
|
|
dataFile.print(now.day(), DEC);
|
|
dataFile.print(" ");
|
|
dataFile.print(now.hour(), DEC);
|
|
dataFile.print(':');
|
|
dataFile.print(now.minute(), DEC);
|
|
dataFile.print(':');
|
|
dataFile.print(now.second(), DEC);
|
|
dataFile.print(",");
|
|
dataFile.print(str);
|
|
|
|
dataFile.println();
|
|
}
|
|
|
|
String getFilename()
|
|
{
|
|
DateTime now = rtc.now();
|
|
String mb = "";
|
|
mb += now.year();
|
|
mb += now.month();
|
|
mb += now.day();
|
|
mb += ".csv";
|
|
|
|
return mb;
|
|
}
|
|
static unsigned long lastRefreshTime = 0;
|
|
void loop()
|
|
{
|
|
uint8_t result = result = node.readHoldingRegisters(2705 , 2);
|
|
|
|
// Check if the read was successful
|
|
if (result == node.ku8MBSuccess)
|
|
{
|
|
Serial.print("Read successful: ");
|
|
|
|
} else {
|
|
Serial.print("Read error: ");
|
|
Serial.println(result, HEX);
|
|
}
|
|
|
|
|
|
|
|
|
|
if(millis() - lastRefreshTime >= 1000)
|
|
{
|
|
lastRefreshTime += 1000;
|
|
|
|
Serial.print("Time: ");
|
|
Serial.print(rtc.now().timestamp());
|
|
// Serial.print("\nHeep:");
|
|
// Serial.print(ESP.getFreeHeap());
|
|
Serial.print("\n");
|
|
|
|
// Open file for writing
|
|
String filename = getFilename();
|
|
Serial.print("Open Card ");
|
|
Serial.print(filename.c_str());
|
|
Serial.print("\n");
|
|
|
|
dataFile = SD.open(filename.c_str(), FILE_WRITE);
|
|
|
|
|
|
if (dataFile)
|
|
{
|
|
dataFile.close(); // Close the file
|
|
Serial.print("Data written to SD card: ");
|
|
Serial.print(filename.c_str());
|
|
Serial.print("\n");
|
|
}
|
|
|
|
Serial.print("\n\n");
|
|
|
|
}
|
|
}
|