#include <SD.h>
#include <SPI.h>
// Pin connected to the SD card select pin
const int chipSelect = 4;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Initialization failed!");
return;
}
Serial.println("Initialization done.");
// Open the file
File file = SD.open("IP.txt");
if (file) {
// Read the IP address from the file
String ip_address = file.readStringUntil('\n');
file.close();
// Split the IP address into its components
int parts[4];
int partIndex = 0;
int startPos = 0;
for (int i = 0; i < ip_address.length(); i++) {
if (ip_address.charAt(i) == ',') {
parts[partIndex++] = ip_address.substring(startPos, i).toInt();
startPos = i + 1;
}
}
parts[partIndex] = ip_address.substring(startPos).toInt();
// Print the IP address as integers
for (int i = 0; i < 4; i++) {
Serial.print("Part ");
Serial.print(i);
Serial.print(": ");
Serial.println(parts[i]);
}
} else {
Serial.println("Error opening ip_address.txt");
}
}
void loop() {
// Nothing to do here
}
//code for replacing the ip address
/*#include <SD.h>
#include <SPI.h>
const int chipSelect = 10;
const int bufferSize = 32;
char buffer[bufferSize];
void setup() {
Serial.begin(9600);
if (!SD.begin(chipSelect)) {
Serial.println("Initialization failed!");
return;
}
Serial.println("Initialization done.");
}
void loop() {
if (Serial.available()) {
Serial.println("Enter the new IP address (format: x.x.x.x): ");
while (Serial.available() < bufferSize) {} // Wait for user input
Serial.readBytesUntil('\n', buffer, bufferSize);
updateIPAddress();
}
}
void updateIPAddress() {
File file = SD.open("ip_address.txt", FILE_WRITE);
if (file) {
file.seek(0);
file.write(buffer, strlen(buffer));
file.close();
Serial.println("IP address updated successfully.");
} else {
Serial.println("Error opening ip_address.txt");
}
}
*/