Erreur de conversion :invalid conversion from 'int' to 'const char*

Bonjour à tout le monde.
une petite question toujours dans mon projet de capteur de cadence ble.
j arrive enfin à trouver la cadence cette formule

int cadence;  
{
  crankEven=pData[4]*256+pData[3];
  presentCrankEven= cumulcrankRevolutions*256 + crankRevolutions;
  deltaCrank=crankEven - presentCrankEven;
  NumDeltaCrank=deltaCrank/1024;
  deltaTime=pData[1]-lastCrankEventTime;
  cadence=deltaTime*60/NumDeltaCrank;
  if (cadence > 200) {cadence=0;}
  lastCrankEventTime =  pData[1];
  crankRevolutions = pData[3];
  cumulcrankRevolutions = pData[4];

}

je peux afficher 'cadence' dans le moniteur serie ou l écrire sur un fichier .csv sur une carte SD
par contre quand je veux l écrire dans la memoire SPIFFS de ma carte j ai cette erreur

error: invalid conversion from 'int' to 'const char*' [-fpermissive]

j ouvre bien ma memoire, crée le fichier mais après je bute

void setup() {
  Serial.begin(115200);
  if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){
       Serial.println("SPIFFS Mount Failed");
       return;
    } 
    listDir(SPIFFS, "/", 0);
    writeFile(SPIFFS, "/data.csv", "Cadence\n");
    

} // End of setup.


// This is the Arduino main loop function.
void loop() {
  appendFile(SPIFFS, "/data.csv",'cadence');

je suis sur que vous avez des idées pour résoudre mon problème.

Bonjour,

Comme on ne connait ni le type des variable ni la ligne concernée, comment veux tu qu'on te réponde?

en effet c est pas facile
c est la variable cadence que je voudrai inscrire dans la memoire
je la declare par int cadence
je la calcule et la valide par la fonction.
elle est comprise entre 0 et 150
merci

je viens de rajouter cela

  char cadenceC[cadence];
  appendFile(SPIFFS, "/data.csv",cadenceC);

je n ai plus d erreur de compilation, j écris bien dans la memoire
par contre je sorts ca en lecture

Reading file: /data.csv
- read from file:
Cadence
 N N⸮⸮⸮0⸮⸮?DP⸮?⸮⸮⸮0⸮⸮?DP⸮? N⸮⸮
⸮⸮⸮⸮?⸮$⸮?XS⸮? N N N N N N N N N N N N N N N N N

une erreur de conversion mais laquelle ?

kamill:
Bonjour,

Comme on ne connait ni le type des variable ni la ligne concernée, comment veux tu qu'on te réponde?

  • Il faut montrer le code en entier, notamment les noms des bibliothèques utilisées.
  • Tu parles de SPIFFS sans préciser le modèle de ta carte : ESP32, ESP8266, autre ?

Donne toutes les précisions plutôt que jouer aux devinettes, on gagnera tous du temps !

Selon toi, qu'est-ce que cette ligne est sensée faire?

char cadenceC[cadence];

transformer int en char non ?

char cadenceC[cadence];

cadence est un int (0-150)donc normal qui peut imprimer des caractères non imprimable.

Mauvaise réponse.
Essaie encore....

char cadenceC[cadence];

Cette ligne déclare un tableau de cadence éléments(??) et il est non initialisé.

Si tu répondais aux question de lesept on connaitrait les librairies que tu utilises et on pourrait peut-être de donner des solutions pertinentes

je viens de voir que le code entier n avait pas été publié
le voici

#include "FS.h"
#include "SPIFFS.h"
#define FORMAT_SPIFFS_IF_FAILED true

#include "BLEDevice.h"

static BLEUUID serviceUUID("00001816-0000-1000-8000-00805f9b34fb");
static BLEUUID    charUUID("00002A5B-0000-1000-8000-00805f9b34fb");

int lastCrankEventTime;
int crankRevolutions;
int cumulcrankRevolutions;
float timeDifference;
float crankEven;
float presentCrankEven;
float deltaCrank;
float NumDeltaCrank;
float deltaTime;
int cadence;  
static boolean doConnect = false;
static boolean connected = false;
static boolean doScan = false;
static BLERemoteCharacteristic* pRemoteCharacteristic;
static BLEAdvertisedDevice* myDevice;

static void notifyCallback(
  BLERemoteCharacteristic* pBLERemoteCharacteristic,
  uint8_t* pData,
  size_t length,
  bool isNotify) {
    Serial.print("Notify callback for characteristic ");
    Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
    Serial.print(" of data length ");
    Serial.println(length);
    Serial.print("cadence : ");
    Serial.println(cadence);
    {
  crankEven=pData[4]*256+pData[3];
  presentCrankEven= cumulcrankRevolutions*256 + crankRevolutions;
  deltaCrank=crankEven - presentCrankEven;
  NumDeltaCrank=deltaCrank/1024;
  deltaTime=pData[1]-lastCrankEventTime;
  cadence=deltaTime*60/NumDeltaCrank;
  if (cadence > 200) {cadence=0;}
  lastCrankEventTime =  pData[1];
  crankRevolutions = pData[3];
  cumulcrankRevolutions = pData[4];

}  
}

class MyClientCallback : public BLEClientCallbacks {
  void onConnect(BLEClient* pclient) {
  }

  void onDisconnect(BLEClient* pclient) {
    connected = false;
    Serial.println("onDisconnect");
  }
};

bool connectToServer() {
    Serial.print("Forming a connection to ");
    Serial.println(myDevice->getAddress().toString().c_str());
    
    BLEClient*  pClient  = BLEDevice::createClient();
    Serial.println(" - Created client");

    pClient->setClientCallbacks(new MyClientCallback());

    // Connect to the remove BLE Server.
    pClient->connect(myDevice);  // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private)
    Serial.println(" - Connected to server");

    // Obtain a reference to the service we are after in the remote BLE server.
    BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
    if (pRemoteService == nullptr) {
      Serial.print("Failed to find our service UUID: ");
      Serial.println(serviceUUID.toString().c_str());
      pClient->disconnect();
      return false;
    }
    Serial.println(" - Found our service");


    // Obtain a reference to the characteristic in the service of the remote BLE server.
    pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
    if (pRemoteCharacteristic == nullptr) {
      Serial.print("Failed to find our characteristic UUID: ");
      Serial.println(charUUID.toString().c_str());
      pClient->disconnect();
      return false;
    }
    Serial.println(" - Found our characteristic");

    // Read the value of the characteristic.
    if(pRemoteCharacteristic->canRead()) {
      std::string value = pRemoteCharacteristic->readValue();
      Serial.print("The characteristic value was: ");
      Serial.println(value.c_str());
    }

    if(pRemoteCharacteristic->canNotify())
      pRemoteCharacteristic->registerForNotify(notifyCallback);

    connected = true;
}
/**
 * Scan for BLE servers and find the first one that advertises the service we are looking for.
 */
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
 /**
   * Called for each advertising BLE server.
   */
  void onResult(BLEAdvertisedDevice advertisedDevice) {
    Serial.print("BLE Advertised Device found: ");
    Serial.println(advertisedDevice.toString().c_str());

    // We have found a device, let us now see if it contains the service we are looking for.
    if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID)) {

      BLEDevice::getScan()->stop();
      myDevice = new BLEAdvertisedDevice(advertisedDevice);
      doConnect = true;
      doScan = true;

    } // Found our server
  } // onResult
}; // MyAdvertisedDeviceCallbacks
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
    Serial.printf("Listing directory: %s\r\n", dirname);

    File root = fs.open(dirname);
    if(!root){
        Serial.println("- failed to open directory");
        return;
    }
    if(!root.isDirectory()){
        Serial.println(" - not a directory");
        return;
    }

    File file = root.openNextFile();
    while(file){
        if(file.isDirectory()){
            Serial.print("  DIR : ");
            Serial.println(file.name());
            if(levels){
                listDir(fs, file.name(), levels -1);
            }
        } else {
            Serial.print("  FILE: ");
            Serial.print(file.name());
            Serial.print("\tSIZE: ");
            Serial.println(file.size());
        }
        file = root.openNextFile();
    }
}

void readFile(fs::FS &fs, const char * path){
    Serial.printf("Reading file: %s\r\n", path);

    File file = fs.open(path);
    if(!file || file.isDirectory()){
        Serial.println("- failed to open file for reading");
        return;
    }

    Serial.println("- read from file:");
    while(file.available()){
        Serial.write(file.read());
    }
}

void writeFile(fs::FS &fs, const char * path, const char * message){
    Serial.printf("Writing file: %s\r\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("- frite failed");
    }
}

void appendFile(fs::FS &fs, const char * path, const char * message){
    Serial.printf("Appending to file: %s\r\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");
    }
}

void renameFile(fs::FS &fs, const char * path1, const char * path2){
    Serial.printf("Renaming file %s to %s\r\n", path1, path2);
    if (fs.rename(path1, path2)) {
        Serial.println("- file renamed");
    } else {
        Serial.println("- rename failed");
    }
}

void deleteFile(fs::FS &fs, const char * path){
    Serial.printf("Deleting file: %s\r\n", path);
    if(fs.remove(path)){
        Serial.println("- file deleted");
    } else {
        Serial.println("- delete failed");
    }
}

void testFileIO(fs::FS &fs, const char * path){
    Serial.printf("Testing file I/O with %s\r\n", path);

    static uint8_t buf[512];
    size_t len = 0;
    File file = fs.open(path, FILE_WRITE);
    if(!file){
        Serial.println("- failed to open file for writing");
        return;
    }

    size_t i;
    Serial.print("- writing" );
    uint32_t start = millis();
    for(i=0; i<2048; i++){
        if ((i & 0x001F) == 0x001F){
          Serial.print(".");
        }
        file.write(buf, 512);
    }
    Serial.println("");
    uint32_t end = millis() - start;
    Serial.printf(" - %u bytes written in %u ms\r\n", 2048 * 512, end);
    file.close();

    file = fs.open(path);
    start = millis();
    end = start;
    i = 0;
    if(file && !file.isDirectory()){
        len = file.size();
        size_t flen = len;
        start = millis();
        Serial.print("- reading" );
        while(len){
            size_t toRead = len;
            if(toRead > 512){
                toRead = 512;
            }
            file.read(buf, toRead);
            if ((i++ & 0x001F) == 0x001F){
              Serial.print(".");
            }
            len -= toRead;
        }
        Serial.println("");
        end = millis() - start;
        Serial.printf("- %u bytes read in %u ms\r\n", flen, end);
        file.close();
    } else {
        Serial.println("- failed to open file for reading");
    }
}

part1

void setup() {
  Serial.begin(115200);
  if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){
       Serial.println("SPIFFS Mount Failed");
       return;
    } 
    listDir(SPIFFS, "/", 0);
    writeFile(SPIFFS, "/data.csv", "Cadence\n");
    
  Serial.println("Starting Arduino BLE Client application...");
  BLEDevice::init("");

  // Retrieve a Scanner and set the callback we want to use to be informed when we
  // have detected a new device.  Specify that we want active scanning and start the
  // scan to run for 5 seconds.
  BLEScan* pBLEScan = BLEDevice::getScan();
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setInterval(1349);
  pBLEScan->setWindow(449);
  pBLEScan->setActiveScan(true);
  pBLEScan->start(5, false);

} // End of setup.


// This is the Arduino main loop function.
void loop() {
  char cadenceC[cadence];
  appendFile(SPIFFS, "/data.csv",cadenceC);
  appendFile(SPIFFS, "/data.csv","\n");

  // If the flag "doConnect" is true then we have scanned for and found the desired
  // BLE Server with which we wish to connect.  Now we connect to it.  Once we are 
  // connected we set the connected flag to be true.
  if (doConnect == true) {
    if (connectToServer()) {
      Serial.println("We are now connected to the BLE Server.");
    } else {
      Serial.println("We have failed to connect to the server; there is nothin more we will do.");
    }
    doConnect = false;
  }

  // If we are connected to a peer BLE Server, update the characteristic each time we are reached
  // with the current time since boot.
  if (connected) {
    String newValue = "Time since boot: " + String(millis()/1000);
    Serial.println("Setting new characteristic value to \"" + newValue + "\"");
    
    // Set the characteristic's value to be the array of bytes that is actually a string.
    pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());
  }else if(doScan){
    BLEDevice::getScan()->start(0);  // this is just eample to start scan after disconnect, most likely there is better way to do it in arduino
  }
  
  delay(1000); // Delay a second between loops.
} // End of loop

part2

void loop() {
  char cadenceC[cadence];
  appendFile(SPIFFS, "/data.csv",cadenceC);
  appendFile(SPIFFS, "/data.csv","\n");

Comme veux-tu que cela fonctionne ?
Tu déclares un tableau cadenceC qui a pour taille la valeur d'une variable cadence, sans l'initialiser.
Ce tableau contient n'importe quoi car c'est une variable locale sur la pile.

  char cadenceC[6];

  sprintf(cadenceC, "%d\n", cadence);
  appendFile(SPIFFS, "/data.csv",cadenceC);
//  appendFile(SPIFFS, "/data.csv","\n");

Mais tu pourrais simplifier en écrivant directement cadence :

void appendFile(fs::FS &fs, const char * path, int cadence)
{
// ...
    if(file.println(cadence)){
// ...
}
// et dans la loop :
  appendFile(SPIFFS, "/data.csv",cadence);
// et tu te passes de la variable cadenceC

merci de votre aide. c est vrai que dans la declaration j y comprends un beignet.
ca marche.
je continue le dev
s