#include <LiquidCrystal.h>
#include <SD.h>
#include "RTClib.h"
#include "WiFiEsp.h"
// Emulate Serial1 on pins 2/3 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(3, 2); // RX, TX
#endif
#define sdCard_CS_PIN 10
//#define sdCard_DO_PIN 12
//#define sdCard_DI_PIN 10
File root;
LiquidCrystal lcd(4,9,8,7,6,5);
RTC_DS1307 rtc;
File raceFile;
String theDay = "theday";
char theTime[9];
char ssid[] = "Wokwi-GUEST"; // your network SSID (name)
char pass[] = ""; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
lcd.begin(16, 2); //columns and rows
// you can now interact with the LCD, e.g.:
lcd.print("Powered Up");
lcd.setCursor(0,2);
lcd.print("Getting Ready");
delay(2000);
//Serial.println("Starting Up!");
//Serial.println(sdCard_CS_PIN);
Serial.println("Initializing SD card... ");
if (!SD.begin(sdCard_CS_PIN)) {
Serial.println("Card initialization failed!");
lcd.clear();
lcd.print("SD Card Failed");
while (true);
}
Serial.println("initialization done.");
// LCD Screen Feedback on SD Card
lcd.clear();
lcd.print("SD Card");
lcd.setCursor(0,2);
lcd.print("Initialized");
//check for clock
//delay(2000);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
lcd.clear();
lcd.print("Clock Failed");
Serial.flush();
abort();
} else {
DateTime now = rtc.now();
theDay = String(now.year()) + "/" + now.month() + "/" + now.day();
snprintf(theTime, 9, "%02d:%02d:%02d", now.hour(), now.minute(), now.second());
Serial.print("Date: ");
Serial.println(theDay);
Serial.print("Time: ");
Serial.println(theTime);
delay(2000);
lcd.clear();
lcd.print(theDay);
lcd.setCursor(0,2);
lcd.print(theTime);
}
// write to file
/*
if (SD.exists("stage1-start.txt")) {
Serial.println("stage1-start.txt exists.");
} else {
Serial.println("stage1-start.txt doesn't exist.");
}
// open a new file and immediately close it:
Serial.println("Creating stage1-start.txt...");
raceFile = SD.open("stage1-start.txt", FILE_WRITE);
raceFile.close();
// Check to see if the file exists:
if (SD.exists("stage1-start.txt")) {
Serial.println("stage1-start.txt exists.");
} else {
Serial.println("stage1-start.txt doesn't exist.");
}
*/
// initialize serial for ESP module
Serial1.begin(11522);
// initialize ESP module
WiFi.init(&Serial1);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}
// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
Serial.println("You're connected to the network");
} // end void setup
void loop() {
// put your main code here, to run repeatedly:
DateTime now = rtc.now();
theDay = String(now.year()) + "/" + now.month() + "/" + now.day();
snprintf(theTime, 9, "%02d:%02d:%02d", now.hour(), now.minute(), now.second());
lcd.clear();
lcd.print(theDay);
lcd.setCursor(0,2);
lcd.print(theTime);
delay(1000);
}