Invalid Conversion From 'char' to 'const char*

Hi all,

I have this section in my program:

int packetSize = LoRa.parsePacket();
if (packetSize) {
while (LoRa.available()) {
Serial.println ("Display LoRa Read Value "); //debug;
char rx_byte = LoRa.read();
Serial.println (rx_byte);

if (strncmp(rx_byte, "lat", 3) == 0){
{
strcpy (lat1,rx_byte);
}

if (strncmp(rx_byte, "lng", 3) == 0){
{
strcpy (lng1,rx_byte);
rxtime1 = millis(); //time since arduino powered up (for setting how long ago the message was received)
}
}
}
}
}

And I have initialized the variables like this:

char lat1[] = "3333333333333333333333333";
char lng1[] = "4444444444444444444444444";
int rxtime1 = 0; //previously ago
char rx_byte[] = "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000";

When I compile the code it gives these errors:

/home/pc/_ZebsData/GoBuddy-Project/GoBuddy_Work_in_Progress/GoBuddy_Work_in_Progress.ino: In function 'void loop()':
/home/pc/_ZebsData/GoBuddy-Project/GoBuddy_Work_in_Progress/GoBuddy_Work_in_Progress.ino:86:32: warning: invalid conversion from 'char' to 'const char*' [-fpermissive]
if (strncmp(rx_byte, "lat", 3) == 0){
^
In file included from /opt/arduino-1.8.9/hardware/arduino/avr/cores/arduino/Arduino.h:25:0,
from sketch/GoBuddy_Work_in_Progress.ino.cpp:1:
/opt/arduino-1.8.9/hardware/tools/avr/avr/include/string.h:434:12: note: initializing argument 1 of 'int strncmp(const char*, const char*, size_t)'
extern int strncmp(const char , const char , size_t) ATTR_PURE;
^
/home/pc/_ZebsData/GoBuddy-Project/GoBuddy_Work_in_Progress/GoBuddy_Work_in_Progress.ino:88:23: warning: invalid conversion from 'char' to 'const char
' [-fpermissive]
strcpy (lat1,rx_byte);
^
In file included from /opt/arduino-1.8.9/hardware/arduino/avr/cores/arduino/Arduino.h:25:0,
from sketch/GoBuddy_Work_in_Progress.ino.cpp:1:
/opt/arduino-1.8.9/hardware/tools/avr/avr/include/string.h:305:14: note: initializing argument 2 of 'char
strcpy(char*, const char*)'
extern char strcpy(char , const char );
^
/home/pc/_ZebsData/GoBuddy-Project/GoBuddy_Work_in_Progress/GoBuddy_Work_in_Progress.ino:91:32: warning: invalid conversion from 'char' to 'const char
' [-fpermissive]
if (strncmp(rx_byte, "lng", 3) == 0){
^
In file included from /opt/arduino-1.8.9/hardware/arduino/avr/cores/arduino/Arduino.h:25:0,
from sketch/GoBuddy_Work_in_Progress.ino.cpp:1:
/opt/arduino-1.8.9/hardware/tools/avr/avr/include/string.h:434:12: note: initializing argument 1 of 'int strncmp(const char
, const char
, size_t)'
extern int strncmp(const char , const char , size_t) ATTR_PURE;
^
/home/pc/_ZebsData/GoBuddy-Project/GoBuddy_Work_in_Progress/GoBuddy_Work_in_Progress.ino:93:23: warning: invalid conversion from 'char' to 'const char
' [-fpermissive]
strcpy (lng1,rx_byte);
^
In file included from /opt/arduino-1.8.9/hardware/arduino/avr/cores/arduino/Arduino.h:25:0,
from sketch/GoBuddy_Work_in_Progress.ino.cpp:1:
/opt/arduino-1.8.9/hardware/tools/avr/avr/include/string.h:305:14: note: initializing argument 2 of 'char
strcpy(char*, const char*)'
extern char *strcpy(char *, const char *);

Do you know how I might be able to fix this?

Any help would be amazing!

Zeb

Use code tags please, not arrays cell and don’t post snippets

What the “000000....000” doing there ??
What is the type of rxByte? By its name I’d say it’s 1 byte, not a cString...

char rx_byte = LoRa.read();

A byte can never contain more than one character. So it can't be a c-string.

Hi sterretje,

rx_byte is a variable to store the received LoRa data, the reason it is called byte is because it adds 1 number/letter at a time to the variable.

Thanks very much

Zeb

Ah - yes dis not even see that, read only the second blurb.

So here you go - can’t work this way because it does not accumulate into a string what you receive (don’t confuse with what you print, it’s byte by byte)

May be you want to read Serial Input Basics to understand how to build a message from something coming through a serial type of interface and use it for comparisons

Hi J-M-L,

The 00000.......00 is the value that rx_byte is initialized as at first so it has room for storing lat and long later in the program.

Is there a better way to do this?

Can I just initialize it as char rx_byte;?

Thanks very much,

Zeb

P.S. How do I use code tags?

Thanks for the link J-M-L!

Sorry I was sending my reply to you just after you replied to me!

Zeb

ZebH:
Hi J-M-L,

The 00000.......00 is the value that rx_byte is initialized as at first so it has room for storing lat and long later in the program.

Hum... you modified your first post to correct what was there before.... How can anyone make sense of this thread now.

read the tutorial I pointed to, you'll see how to do what you want

Okay thanks J-M-L!

I edited the post because I had missed out assigning rxtime to 0.

.....And just in case anyone wants to know what all of my code is:

#include <SPI.h>
#include <LoRa.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>

//Setting up pin 8 as 'ledPin'
#define ledPin 8

char lat1;
char lng1;
int rxtime1 = 0; 
int ago1 = 0;
char rx_byte;
int seconds = 0;

// set pins for GPS serial rx and tx
static const int RXPin = 6, TXPin = 7;
// set GPS Baud rate constant
static const uint32_t GPSBaud = 9600;

// set initial Bluetooth input string
char BT_input=' ';

// The TinyGPS++ object
TinyGPSPlus gps;

// Define the serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);

//Assign a unique ID to the accelerometer
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);

void setup() {

pinMode(ledPin,OUTPUT); //set pin 8 as an output

//start arduino serial comms
Serial.begin(9600);

//start gps serial comms
ss.begin(GPSBaud);
while (!Serial); // should this be there at all? try commenting out and see if any difference

//start the LoRa serial comms
//Serial.println("Starting LoRa Sender and Receiver");
if (!LoRa.begin(915E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}

{
#ifndef ESP8266 //LoRa, What exactly does it do?
while (!Serial); // for Leonardo/Micro/Zero
#endif

//Initialise the accelerometer sensor
if(!accel.begin()) {
 /* There was a problem detecting the ADXL345 ... check your connections */
Serial.println("Ooops, no ADXL345 detected ... Check your wiring!");
while (1);
}

}
Serial.println ("Check if rx_byte init correctly "); //debug
Serial.println (rx_byte); //debug
}

void loop() {

Check:{
//try to parse a LoRa packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
 // received a packet
 //Store 1 character at a time into the serial buffer while characters are available
while (LoRa.available()) {
Serial.println ("Display LoRa Read Value "); //debug;
rx_byte = LoRa.read();
Serial.println (rx_byte);
//read the serial buffer and store to a variable
//print that variable to the serial output

if (strncmp(rx_byte, "lat", 3) == 0){
{
strcpy (lat1,rx_byte);
}

if (strncmp(rx_byte, "lng", 3) == 0){
{
strcpy (lng1,rx_byte);
rxtime1 = millis(); //time since arduino powered up (for setting how long ago the message was received)
}
}
}
}
}

//check for phone app refresh button press packet received via bluetooth
{
if (Serial.available())
{
BT_input = Serial.read();
// if the refresh button is pressed on the phone app
if ( BT_input== 48)       //ascii code for 0 is dec 48
{
goto Resend; 
}
}
}

/* Check the accelerometer for an accident event */ 
sensors_event_t event; 
accel.getEvent(&event);

/* Display the results (acceleration is measured in m/s^2) */
if (event.acceleration.y + event.acceleration.x > 15 || event.acceleration.y + event.acceleration.x < -15) {
seconds = 0;
goto Countdown;
}
delay (10);

goto Check;
}



Countdown:
//Serial.print (event.acceleration.y + event.acceleration.x || event.acceleration.y + event.acceleration.x );
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
delay (1000);
seconds++;
if (seconds < 5) goto Countdown;
//if (seconds = 6) goto LastChance; //don't need this line of the next line is LastChance anyway, it will go there itself

LastChance:
if (seconds > 4) seconds = 0;
digitalWrite(ledPin,HIGH);
delay(500);
digitalWrite(ledPin,LOW);
delay (500);
digitalWrite(ledPin,HIGH);
delay(500);
digitalWrite(ledPin,LOW);
delay (500);
seconds++;
if (seconds < 2) goto LastChance;
//else goto Help;

Help:{
while (ss.available() > 0)
if (gps.encode(ss.read()))
goto Lat;


Lat:{
//Transmit latitude over LoRa
LoRa.beginPacket();
if (gps.location.isValid())
{
LoRa.print ("lat");
LoRa.print (gps.location.lat(), 6); // add lat prefix to the data sent
delay (1000);
}
else
{
//LoRa.println(F("INVALID"));
goto Help;
}
LoRa.endPacket();
 delay (20);
// flash the LED to show that a packet has been sent
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
delay (1000);
goto Lng;
}


//Send the longitude coordinate via LoRa
Lng:{  
LoRa.beginPacket();
if (gps.location.isValid())
{
LoRa.print ("lng");
LoRa.print (gps.location.lng(), 6);
delay(1000);
}
else
{
//LoRa.println(F("INVALID"));
goto Help;
}
LoRa.endPacket();
delay (20);
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
delay (1000);
goto Check;
}
}

Resend:
{ 
ago1 = (millis() - rxtime1) / 1000 / 60;
// agomins = ago1 / 1000 / 60;  
Serial.print (lat1);
delay (20);
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
delay (1000);

Serial.print (lng1);
delay(20);
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
delay (1000);

Serial.print (ago1);
delay(20);
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
delay (1000);
goto Check;
}
}

Thanks very much!!!

Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)


do you have a BASIC programing background? using GOTO is not well regarded in C or C++ (except in very rare cases)

Thanks so much J-M-L!

I am having trouble with this section of code:

 Serial.println ("Display LoRa Read Value ");
 rx_byte = LoRa.read();
 Serial.println (rx_byte);

Each time rx_byte gets updated it gets overwritten instead of adding on to the end of the last reading. Does anyone know what might cause this? (Look at post above for all the code).

Thanks very much

Zeb

Does anyone know what might cause this?

I do and if you would read the, previously mentioned, serial input basics tutorial , so would you.

Hi groundFungus,

I am trying to understand the serial input basics tutorial and am having a little bit of trouble as I am still a beginner in programming and am working on a school project!

Is my problem that I need to initialize the max number of characters the variable will hold like "char myChar[20];"?

Thanks very much for all your help and patience!

Zeb

Look at the example #2 of the serial input basics. Here is the process.

First we need a place to put the data with enough space to hold the longest string that we will receive. The receivedChars array must have room for the number of bytes in the data set + 1 for the terminator that will later be added. Note that receivedChars is a character array, not yet a string).

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

Then we need a variable to keep track of the position in the array (array index) as we receive and store data.

static byte ndx = 0;

And specify the character that ends the data set. It can be any character, but line feed ('\n') is convenient.

char endMarker = '\n';

A byte to temporarily store the byte read.

char rc;

If a byte is available read it in. This reads one byte.

while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

Check to see if it is the terminator ('\n').

if (rc != endMarker) {

If not add it to the receiving array in the position (array index) .

receivedChars[ndx] = rc;

Increment the array position (index) for the next character.

ndx++;

And make sure that it stays in the bounds of the array.

if (ndx >= numChars) {
                ndx = numChars - 1;
            }

If the received character is the terminator, add a null to the end of the array to make it into a string.

receivedChars[ndx] = '\0'; // terminate the string

Set the index back to zero to be ready for the next data and set the flag to let the rest of the program know that there is new data to process.

ndx = 0;
            newData = true;

See also

The string reference.
Arrays.

Hi ground Fungus,

Thanks so much! Just checking that I replace the Serial.available and Serial.read with LoRa.available and LoRa.read?

Thanks very much

Zeb

Yes. And you need to make sure that the data sent ends with the terminator character or the process will not work.

Hi groundFungus,

Sorry about this stupid question. What is the Terminator Character?

Thanks very much for all you patience!

Zeb

Whatever you choose that will not be a part of a message

In the sample code that I provided the terminating character is a line feed ('/n'). The last character sent by the Lora needs to be the terminator as it marks the end of the data string. When the terminator is received the program knows that all of the data string is received and the program can move on to operating on the data string.

And as J-M-L says, you can make the terminator any character that you want as long as that character will never be part of the data.

Hi all,

Yay! I can now successfully store and display the received LoRa data! Thanks very much!

I am now struggling with a part of code that I am writing that checks if the variable receivedChars (received LoRa data stored in a variable) includes the word "lat" (if the latitude has been received) or "long" (the longitude has been received).

Would it be possible to use this code?

if (newData == true) {
 Serial.print("Received Data ");
 Serial.println(receivedChars);

  if (strncmp(receivedChars, "lat", 3) == 0){
  {
  strcpy (lat1,receivedChars);
  Serial.println ("lat1");
  Serial.println (lat1);
  }
  }
  
  if (strncmp(receivedChars, "lng", 3) == 0){
  {
  strcpy (lng1,receivedChars);
  Serial.println ("lng1");
  Serial.println (lng1);
  rxtime1 = millis(); //time since arduino powered up (for setting how long ago the message was received)
  }
  }
  
  newData = false;
  }

Thanks again

Zeb