#include <Wire.h>
#include <Keypad.h>
#include <RTClib.h>
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {19, 18, 5, 17};
byte colPins[COLS] = {16, 4, 2, 15};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
RTC_DS1307 rtc;
#define EEPROM_ADDRESS 0x50
char inputBuffer[100]; // Increased buffer size to accommodate date and time
int inputIndex = 0;
float volume = 0;
float price = 90.0;
float rate;
float vol = 0;
long code = 0;
#define EEPROM_SIZE 512
void setup() {
Serial.begin(115200);
Wire.begin();
rtc.begin();
}
void loop() {
char key = keypad.getKey();
if (key >= '0' && key <= '9') {
inputBuffer[inputIndex++] = key;
code = code * 10 + (key - '0');
rate = 1000.0 / price;
vol = (code * rate);
volume = round(vol * 100.0) / 100000.0;
delay(100);
}
if (key == 'D') {
inputBuffer[inputIndex] = '\0'; // Null-terminate the string
saveToEEPROM(inputBuffer);
inputIndex = 0; // Reset the input index
code = 0;
}
if (Serial.available() > 0) {
String command = Serial.readStringUntil('\n');
if (command == "show") {
viewSavedData();
}
}
}
void saveToEEPROM(char* inputBuffer) {
if (inputBuffer) {
DateTime now = rtc.now();
String dateTimeStr = String(now.year()) + "-" + String(now.month()) + "-" + String(now.day()) +
" " + String(now.hour()) + ":" + String(now.minute()) + ":" + String(now.second());
String dataToStore = "Rs." + String(inputBuffer) + " - " + volume +"litre" + "-" + dateTimeStr + "\n";
int eepromAddress = 0;
// Find the first available EEPROM address
while (eepromAddress < EEPROM_SIZE) {
Wire.beginTransmission(EEPROM_ADDRESS);
Wire.write(eepromAddress >> 8);
Wire.write(eepromAddress & 0xFF);
Wire.endTransmission(false);
Wire.requestFrom(EEPROM_ADDRESS, 1);
if (Wire.available()) {
char data = Wire.read();
if (data == '\0') {
break; // Found an empty slot
}
} else {
break;
}
eepromAddress++;
}
if (eepromAddress < EEPROM_SIZE) {
// Write each byte of the string to EEPROM
for (int i = 0; i < dataToStore.length(); i++) {
Wire.beginTransmission(EEPROM_ADDRESS);
Wire.write(eepromAddress >> 8);
Wire.write(eepromAddress & 0xFF);
Wire.write(dataToStore[i]);
Wire.endTransmission();
delay(5);
eepromAddress++;
if (eepromAddress >= EEPROM_SIZE) {
Serial.println("Error: EEPROM memory full");
delay(1000000000);
break;
}
}
Serial.println("Data saved to EEPROM: " + dataToStore);
delay(1000);
} else {
Serial.println("Error: EEPROM memory full");
}
}
}
void viewSavedData() {
int eepromAddress = 0;
char buffer[EEPROM_SIZE];
int bytesRead = 0;
Serial.print("Data read from EEPROM: \n");
while (true) {
Wire.beginTransmission(EEPROM_ADDRESS);
Wire.write(eepromAddress >> 8);
Wire.write(eepromAddress & 0xFF);
Wire.endTransmission(false);
Wire.requestFrom(EEPROM_ADDRESS, 1);
if (Wire.available()) {
char data = Wire.read();
buffer[bytesRead] = data;
bytesRead++;
if (data == '\0' || bytesRead >= EEPROM_SIZE) {
// Print the current entry and move to the next line
buffer[bytesRead] = '\0';
Serial.println(buffer);
if (data == '\0') {
break; // Reached the end of EEPROM
}
bytesRead = 0; // Reset for the next entry
}
} else {
break;
}
eepromAddress++;
}
delay(1000);
}
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
chip1:A0
chip1:A1
chip1:A2
chip1:GND
chip1:VCC
chip1:WP
chip1:SCL
chip1:SDA
keypad1:R1
keypad1:R2
keypad1:R3
keypad1:R4
keypad1:C1
keypad1:C2
keypad1:C3
keypad1:C4
rtc1:GND
rtc1:5V
rtc1:SDA
rtc1:SCL
rtc1:SQW