Search code examples
arduinoesp32

the code compiles on arduino nano but not on esp32 :(


void loop() {
  // put your main code here, to run repeatedly:
  // send packet
  p = (bme.readPressure() / 100.0F );
  h = (bme.readHumidity());
  t = (bme.readTemperature());
  alt = bme.readAltitude(SEALEVELPRESSURE_HPA);
  
 String data = String(h) + "-" + String(t)+ "-" + String(p)+ "-" + String(alt);
  
  int dataLength = data.length(); dataLength++;
  uint8_t total[dataLength]; //variable for data to send
  data.toCharArray(total, dataLength); //change type data from string ke uint8_t
  Serial.println(data);
  rf95.send(total, dataLength); //send data
  rf95.waitPacketSent();
  delay(2000);
  
 
}

im using the radiohead library and it works really well on arduino to send the data and then receives on a esp32, but if i make the esp32 the sender with this code it throws me this error

error: invalid conversion from 'uint8_t*' {aka 'unsigned char*'} to 'char*' [-fpermissive]

Solution

  • refer to toCharArray() function in arduino reference as they stated that :

    myString.toCharArray(buf, len)

    buf: the buffer to copy the characters into. Allowed data types: array of char.

    so the function expects a parameter of type char* not uint8_t*

    so either change :

    uint8_t total[dataLength]; -> char total[dataLength];

    or

    data.toCharArray(total, dataLength); -> data.toCharArray((char*)total, dataLength);

    either of them should solve your problem.