Overwrite File on Esp8266 using SdCard Module

Code fine When is use O_TRUNC on Arduino Uno, but when i upload it on Wemos D1 R1. The error always say that O_TRUNC / O_READ are not declared.

ReadWrite_v2.ino (639 Bytes)

It looks like you will have to delete the file if it exists and open it again to write to it

I need that file for backup data, when the device shut down then it turn on again, it wont start from 0 again.

I need that file for backup data, when the device shut down then it turn on again, it wont start from 0 again.

Doesn't O_TRUNC truncate the file to zero bytes in length, effectively deleting its contents ?

yes when i use O_TRUNC for overwrite data using arduino, it works fine. but when i use O_TRUNC on esp8266 always error.

esp8266\2.6.3\libraries\ESP8266SdFat\src/FatLib/FatApiConstants.h:81:15: note: 'sdfat::O_TRUNC'

const uint8_t O_TRUNC = 0X10;

shiemi1998:
yes when i use O_TRUNC for overwrite data using arduino, it works fine. but when i use O_TRUNC on esp8266 always error.

But you said

I need that file for backup data, when the device shut down then it turn on again, it wont start from 0 again.

So if you could us O_TRUNC it would delete the file

I am not clear when and why you want to us O_TRUNC. Perhaps if you posted a full program it would help

#include <SPI.h>
#include <SD.h>

#define CS_PIN D8

File myFile;

String a;

int b;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while(!SD.begin(CS_PIN)){
Serial.println(“Error Finding SdCard”);
if (SD.begin(CS_PIN)){
break;
}
}
Serial.println(“SdCard Found”);
myFile = SD.open(“test.txt”, FILE_READ | O_READ);
if (!myFile.available()){
Serial.println(“File doesnt exists”);
delay(1000);
} else {
Serial.println(“Reading File”);
a = myFile.read();
Serial.println(a);
delay(1000);
}
}

void loop() {
// put your main code here, to run repeatedly:
myFile = SD.open("test.txt", FILE_WRITE | O_TRUNC);
if (b != 2) {
b = b + 1;
myFile.print(b);
Serial.println(b);
} else {
b = 0;
myFile.print(b);
Serial.println(b);
}
}

 myFile = SD.open("test.txt", FILE_WRITE | O_TRUNC)

Now I am completely confused. Why would you open the file each time through loop() and trancate its length to zero when you do ?

Please explain exactly what you want to do

if i didnt trancate it, it will append. like on node-red when want write to file there is 2 options. 1. append, 2. overwrite.

i want write data on a file, when i write data again on that file.

i already try to delete the file, then write again. but when i restart wemos and read that file. there is no file on sd card.

shiemi1998:
i already try to delete the file, then write again. but when i restart wemos and read that file. there is no file on sd card.

Post the code that does that

#include <SD.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <SPI.h>
#include <ModbusTCPSlave.h>
#include <ESP8266WiFi.h>

#define CS_PIN D8

File myFile;

const char* ssid = “test”;
const char* password = “pepsodent”;

ModbusTCPSlave modbus;

int data;
long int y, z, b;

String x, c;

float a;

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void setup() {
Serial.begin(115200);
modbus.begin(ssid, password);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print(“Please Wait”);

Serial.println("");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(“WiFi : “);
Serial.print(ssid);
Serial.println(” are not available”);
delay(1000);
if (WiFi.status() == WL_CONNECTED) {
break;
}
}
Serial.print("Your IP Address is : ");
Serial.println(WiFi.localIP());
delay(1000);

while (!SD.begin(CS_PIN)) {
Serial.println(“SDCard initialization failed!”);
delay(1000);
if (SD.begin(CS_PIN)) {
break;
}
}

Serial.println(“SDCard Initialization Done”);
delay(1000);

myFile = SD.open(“BACKUP.TXT”, FILE_READ);
if (!myFile.available()) {
Serial.println(“There is no Backup File”);
delay(1000);
} else {
while (myFile.available()) {
if (x == “”) {
Serial.print(“Checking Backup File”);
delay(500);
Serial.write(".");
delay(500);
Serial.write(".");
delay(500);
Serial.write(".");
delay(500);
Serial.println("");
String READ = myFile.readStringUntil(’\n’);
x = READ;
myFile.close();
Serial.println(“Your Last Energy Usage : " + x + " kWh”);
} else {
break;
}
}
Serial.println(“Backup File Done”);
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“All Intialize Done”);
lcd.clear();
}
}

void loop() {
modbus.Run();
if (Serial.available() > 0)
{
if (x != “”) {

myFile = SD.open(“BACKUP.TXT”, FILE_READ);
String READ = myFile.readStringUntil(’\n’);
x = READ;
myFile.close();

y = x.toInt();
data = Serial.read();
y = y + data;
b = y;
Serial.println(b);
//SD.remove(“BACKUP.TXT”);
} else {
data = Serial.read();
y = y + data;
b = y;
Serial.println(b);
}

c = String(b);
Serial.println("?");
myFile = SD.open(“test.txt”, FILE_WRITE | O_TRUNC);
myFile.print(c);
myFile.close();
modbus.MBHoldingRegister[0] = 1;
modbus.MBHoldingRegister[1] = b;

if (Secs == 1) {
myFile = SD.open(“BACKUP.TXT”, FILE_READ);
String READ = myFile.readStringUntil(’\n’);
x = READ;
y = x.toInt();
myFile.close();
a = y / 60;
Serial.println(a);
modbus.MBHoldingRegister[2] = a;
lcd.setCursor(0, 0);
lcd.print(a);
}

lcd.clear();
b = 0;
c = “”;
//return;
}
}

how?

It would help considerably if you posted your code as advised in Read this before posting a programming question

it doenst help anything with O_TRUNC problem on esp8266

shiemi1998:
it doenst help anything with O_TRUNC problem on esp8266

No, but it will help others to download and examine your code but feel free to ignore the advice.

I am afraid that I can offer no more help

this the error