/* #include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>*/
#include "EmonLib.h" // Include Emon Library
#include "RTClib.h"
#include <Wire.h> // i2C Conection Library
#include "SPI.h"
#include "SD.h"
#include "FS.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,2,4);
File file;
/************************Wifi_connection*******************************/
/*
const char DEVICE_LOGIN_NAME[] = "265f2aab-3138-4278-b7a6-64cf287eed5e";
const char SSID[] = "Amer Al-khorasani"; // Network SSID (name)
const char PASS[] = "Amer@254177@sami@mansour@7734242"; // Network password (use for WPA, or use as key for WEP)
const char DEVICE_KEY[] = "VEZYAPVOW1WK4XW5JJYR"; // Secret device password
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);*/
/****************************PIN_Difine********************************/
#define VOLT_CAL 160
#define CURRENT_CAL 107
#define CHART_VALUES 3
#define RTC_SDA_Pin 21
#define RTC_SCL_Pin 22
#define SD_CS 5 // SDcard module
#define SD_MOSI 23 // SDcard module
#define SD_MISO 19 // SDcard module
#define SD_SCK 18 // SDcard module
EnergyMonitor emon1; // Create an instance
RTC_DS3231 rtc; // SDA,SCL;
DateTime t;
const char * Values;
/****************************Varibles*********************************/
char *fileName = "";
unsigned long int milli_time; //variable to hold the time
float voltage; //variable to hold the voltage form A0
float currentDraw ;
float supplyVoltage ;
float wattDraw ;
String str;
int counter = 0;
int entryId = 0;
int hh = 0, mm = 0, ss = 0, dd = 0, bb = 0, set_day;
int yy = 0;
String Day = " ";
// Eeprom Store Variable
uint8_t HH;
uint8_t MM;
String fileName2 = "";
unsigned long previousTime = 0;
int SPS = 5;
char serialData;
unsigned long lastTime = 0;
float sensors[CHART_VALUES] = {0, 0, 0};
const String valuesLabels[CHART_VALUES] = {"voltage", "current","watt"};
long sampleTime = 20;
byte port = 0b000000101; // Bluetooth,OTG USB,Arduino plotter
//Timers for datalogging
unsigned long lastTimer = 0;
unsigned long timerDelay = 18000;
/*********************Void of Function *******************************/
/*
void arduino_plotter(String *chartLabels, int *values, uint8_t elements)
{
Serial.print("voltage");
Serial.print(":");
Serial.print(sensors[0]);
Serial.print(",");
Serial.print("current");
Serial.print(":");
Serial.print(sensors[1]);
Serial.print(",");
Serial.print("watt");
Serial.print(":");
Serial.print(sensors[2]);
Serial.println();
}*/
// Write to the SD card
void writeFile(fs::FS &fs, const String& path, const char * message) {
Serial.printf("Writing file: %s\n", path);
File file = fs.open(path, FILE_WRITE);
if(!file) {
Serial.println("Failed to open file for writing");
return;
}
if(file.print(message)) {
Serial.println("File written");
} else {
Serial.println("Write failed");
}
file.close();
}
// Append data to the SD card
void appendFile(fs::FS &fs, const char * path, const char * message) {
Serial.printf("Appending to file: %s\n", path);
File file = fs.open(path, FILE_APPEND);
if(!file) {
Serial.println("Failed to open file for appending");
return;
}
if(file.print(message)) {
Serial.println("Message appended");
} else {
Serial.println("Append failed");
}
file.close();
}
/*****************************SetUp******************************/
void setup() {
Serial.begin(115200); // Initialize serial and wait for port to open:;
lcd.init();
rtc.begin(); // memulai koneksi i2c dengan RTC
emon1.voltage(34, VOLT_CAL, 1.7); // Voltage: (input pin, calibration, phase_shift)
emon1.current(0, CURRENT_CAL); // Current: (input pin, calibration)
Wire.begin();
// delay(2000);
Serial.println("Start:");
pinMode(SD_CS ,OUTPUT);
digitalWrite(SD_CS ,HIGH);
/***********from SDcardinit setup **********/
if(!SD.begin(5)){
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE){
Serial.println("No SD card attached");
return;
}
Serial.print("SD Card Type: ");
if(cardType == CARD_MMC){
Serial.println("MMC");
} else if(cardType == CARD_SD){
Serial.println("SDSC");
} else if(cardType == CARD_SDHC){
Serial.println("SDHC");
} else {
Serial.println("UNKNOWN");
}
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.printf("SD Card Size: %lluMB\n", cardSize);
while (!SD.begin(SD_CS))
{
Serial.println("Initialization failed!");
delay(2000);
}
t = rtc.now();
hh = t.hour(), DEC;
mm = t.minute(), DEC;
ss = t.second(), DEC;
dd = t.day(), DEC;
bb = t.month(), DEC;
yy = t.year(),DEC;
delay(1000);
// dataFile.close();
/*
fileName2 += String(dd);
fileName2 += "_";
fileName2 += String(hh);
fileName2 += "_";
fileName2 += String(mm);
fileName2 += ".txt";
SD.begin();
*/
File file = SD.open("/data");
if(!file) {
Serial.println("File doesn't exist");
Serial.println("Creating file...");
writeFile(SD, "/data" , "voltage, current,watt \r\n");
}
else {
Serial.println("File already exists");
}
file.close();
}
void loop() {
// ArduinoCloud.update();
emon1.calcVI(20,1000); // Calculate all. No. of half wavelengths (crossings), time-out
currentDraw = emon1.Irms; //extract Irms into Variable
supplyVoltage = emon1.Vrms; //extract Vrms into Variable
wattDraw = currentDraw *supplyVoltage;
String dataString = "";
t = rtc.now();
hh = t.hour(), DEC;
mm = t.minute(), DEC;
ss = t.second(), DEC;
dd = t.day(), DEC;
bb = t.month(), DEC;
yy = t.year(),DEC;
String date = String(yy) +"/"+ String(bb) +"/"+ String(dd)+ "," ;
String timeval = String(hh) + ":" + String(mm) + ":" +String(ss) + ",";
String Values = String(supplyVoltage) + "," + String(currentDraw) + "," + String(wattDraw);
// Log data every timerDelay seconds
if ((millis() - lastTimer) > timerDelay) {
dataString = String(date) + String(timeval)+ String(Values) + "\r\n";
Serial.print(dataString);
//Append the data to file
appendFile(SD, "/data", dataString.c_str());
lastTimer = millis();
}
/*
if (Serial.available())
{
serialData = Serial.read();
if (serialData == 'S')
{
SPS = Serial.parseInt();
}
if (serialData == 'm')
{
port = Serial.parseInt();
}
}
/**********************LCD_Code Don't Edit this code *******************************/
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Voltage:");
lcd.setCursor(8,0);
lcd.print(supplyVoltage);
lcd.setCursor(0,1);
lcd.print("Current:");
lcd.setCursor(9,1);
lcd.print(currentDraw);
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Power:");
lcd.setCursor(6,0);
lcd.print(wattDraw);
lcd.setCursor(0,1);
// lcd.print("i:");
// lcd.setCursor(2,1);
lcd.print(String(hh) + ":" + String(mm) + ":" + String(ss));
delay(2000);
}