Invalid conversion from char to const char* - c++

Sorry in advance for what I feel is going to be a really simple problem, but I've been stuck on it for hours and I haven't been able to work out how to fix it based on the stuff I've found here or on google.
I've got an arduino hooked up to a GPS and a radio and am trying to broadcast the GPS signal to my radio. What I am trying to do now is get the NMEA sentence from the GPS into the variable 'text', but am getting confused by this error, which I think is due to arrays.
My error is occurring on this line:
sprintf(text, char(c));
I've tried a few different things but this is where I'm stuck at the moment. Any help would be really appreciated.
#define RADIOPIN 13
#include <string.h>
#include <util/crc16.h>
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(3, 2);
#define GPSECHO true
Adafruit_GPS GPS(&mySerial);
char datastring[80];
char text[80];
void setup() {
Serial.begin(115200);
GPS.begin(9600);
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_5HZ);
delay(3000);
pinMode(RADIOPIN,OUTPUT);
}
void loop(){
gpscheck();
}
void gpscheck(){
char c = GPS.read();
if (c) {
// Serial.print(c);
sprintf(text, char*(c));
Serial.print(text);
}
}

You might want to read a reference of sprintf. Then you will see that the second argument is a string.
So the following would be fine:
sprintf(text, "%c", c);
To be on the safe side, you might want to use snprintf instead, to lessesn the risk of buffer overflows.
Of course, for a single character, you might as well do e.g.
text[0] = c;
text[1] = '\0'; /* Terminate string */

sprintf(text, char*(c));
If you want to print character
simply use printf("%c",c);
if you want to copy that into text
sprintf(text,"%c", c);

Related

Float variable to hex C++ arduino

I encountered an issue while having fun with Arduino. I am currently using the first version of Arduino IDE (v 1.18.49.0).
Problem is that I can't figure out how to convert float value to hex. I am trying to use various solutions that occurred earlier on StackOverflow, but none of them seemed to work for me. My best attempt is:
char data[9];
float a = 3.141592654;
sprintf(data, "%x", *(unsigned int*)&a);
Serial.println(data);
Expected Result: 40490fdb
Actual Result: fdb
What's surprising using the same code, yet slightly modified, compiled by g++ gives the expected result.
#include <stdio.h>
int main(int argc, char** argv)
{
char data[9];
float a = 3.141592654;
sprintf(data, "%x", *(unsigned int*)&a);
printf(data);
return 0;
}
Why do the results differ? Maybe there is a better way to do that?
Thanks in advance!
If you are sure you want to see the bit pattern of a 4 byte variable, you should prefer a conversion like this:
sprintf(data, "%08lx", *(uint32_t*)&a);
(Here it's irrelevant, but in general, leading zeros help readability)
[edited to include #timemage's hint]

C++ / Arduino IDE. Publish float to MQTT - is conversion to string -> array necessary?

I am trying to publish a float to an MQTT channel in C++, within the Arduino IDE.
The following code does appear to work, but it seems to be a bit long-winded. I cobbled it together from stuff I found online. Is all this really necessary (the conversion to an array via a string), or is there a better way?
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <SparkFunBME280.h>
BME280 atmosSensor;
String tStr;
String pStr;
String hStr;
char tArr[4];
char pArr[4];
char hArr[4];
void setup() {
Setup wifi, mqtt, etc.
}
void loop() {
float tempReading = atmosSensor.readTempC();
float pressureReading = atmosSensor.readFloatPressure();
float humidityReading = atmosSensor.readFloatHumidity();
tStr = String(tempReading);
pStr = String(pressureReading);
hStr = String(humidityReading);
tStr.toCharArray(tArr, tStr.length()+1);
pStr.toCharArray(pArr, pStr.length()+1);
hStr.toCharArray(hArr, hStr.length()+1);
client.publish("atmos1/temperature", tArr);
client.publish("atmos1/pressure", pArr);
client.publish("atmos1/humidity", hArr);
}
N.B. I have pruned this code down considerably, to just the relevant bit. I'm really just asking whether the conversion to a String, and then to an array etc, is necessary.
You can use dtostrf to do the conversion in fewer steps.
char* dtostrf(double __val, signed char __width, unsigned char __prec, char * __s )
It would look something like this:
void loop()
{
float humidityReading = atmosSensor.readFloatHumidity();
constexpr size_t BUFFER_SIZE = 7; //1 char for the sign, 1 char for the decimal dot, 4 chars for the value & 1 char for null termination
char buffer[BUFFER_SIZE];
dtostrf(humidityReading, BUFFER_SIZE - 1 /*width, including the decimal dot and minus sign*/, 2 /*precision*/, buffer);
client.publish("atmos1/humidity", buffer, BUFFER_SIZE); //notice we're using the overload where you specify the length of the buffer, as we know it and it saves a call to strlen
}
A cursory glance at the MQTT source code shows that the data is stored in an internal buffer after a call to publish(), so you should be safe in reusing the same buffer for multiple calls to publish(). But be sure to check more thoroughly than I did ;)

I want to receive multiple data from arduino to raspberry pi using I2C

Thank you to whoever is kind enough to look into this question.
I want to receive multiple data from arduino to raspberry pi using I2C.
I can obtain 1 data from arduino, but once I move to more than one data, it fails to do so.
I have tried multiple methods so far, and I found this method to work the best to obtain data from Arduino.
My previous attempt in obtaining data from arduino is as follows:
I want to read from Arduino using I2C using Raspberry Pi
Raspberry Pi's terminal response has weird font that cannot be recognized
Which are all solved by now.
Got Massive Help from link below
https://area-51.blog/2014/02/15/connecting-an-arduino-to-a-raspberry-pi-using-i2c/
Arduino Code
#include <Wire.h>
#define echoPin 7
#define trigPin 8
int number=0;
long duration;
long distance;
void setup()
{
//Join I2C bus as slave with address 8
Wire.begin(8);
//Call SendData & Receive Data
Wire.onRequest(SendData);
//Setup pins as output and input to operate ultrasonic sensor
Serial.begin(9600);
pinMode(echoPin,INPUT);
pinMode(trigPin,OUTPUT);
}
void loop ()
{
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(2);
digitalWrite(trigPin,LOW);
duration=pulseIn(echoPin,HIGH);
distance=duration/58.2;
Serial.print(distance);
Serial.println(" cm");
}
void SendData()
{
Wire.write(distance);
Wire.write("Why No Work?");
Wire.write(distance);
}
C++ Code
//Declare and Include the necessary header files
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <fcntl.h>
//Define Address of Slave Address
#define ADDRESS 0x08
//Eliminate the Used of std in the future
using namespace std;
static const char *devName="/dev/i2c-1";
int main(int argc, char **argv)
{
//Check to see if C++ works
cout<<"Hello, World!\n";
cout<<"I2C: Connecting"<<endl;
int file;
if ((file = open(devName, O_RDWR))<0)
{
fprintf(stderr, "I2C: Failed to access");
exit(1);
}
if (ioctl(file, I2C_SLAVE, ADDRESS)<0)
{
cout<<"Failed to Access"<<endl;
}
char buf[0];
char dd;
for (int i=0; i<100;i++)
{
read(file,buf, 3);
float distance= (int) buf[0];
dd= buf[1];
float dist=(int) buf[2];
cout<<distance<<endl;
usleep(10000);
cout<<"doh"<<endl;
cout<<dd<<endl;
cout<<dist<<endl;
}
return 0;
}
What I would expect from the c++ code would be as follows
15
doh
Why No Work?
15
But I get
15
doh
weird font can't be recognized
255
Wire.write(distance);
wants to write a long onto the I2C bus. For an Arduino This is 32 bits, 4 bytes, of data. I'm not sure exactly what wire.write does because the documentation I can find is substandard to the point of being garbage, but the documentation looks like it's going to send exactly 1 of the 4 bytes you wanted to send. In order to send more than one byte, it looks like you need to use the array version: Wire.write(&distance, sizeof (distance));, but even this may not be sufficient. I'll get back into that later.
Wire.write("Why No Work?");
writes a null-terminated string (specifically a const char[13]) onto the I2C bus. I don't know arduino well enough to know if this also sends the terminating null.
so
Wire.write(distance);
Wire.write("Why No Work?");
Wire.write(distance);
needed to write at least 4 + 12 + 4 bytes onto the I2C bus. and probably only wrote 1 + 12 + 1.
On the Pi side,
read(file,buf, 3);
read out 3 bytes. This isn't enough to get the whole of distance, let alone the array of characters and second write of distance. You need to read all of the data you wrote, at least 20 bytes.
In addition,
char buf[0];
defines an array of 0 length. There isn't much you can do with it as there is no space to store anything here. It cannot hold 3 characters, let alone the 20 or 21 necessary. read of 3 bytes wrote into invalid memory and the program can no longer be counted on for sane results.
This means that at best
float distance= (int) buf[0];
dd= buf[1];
float dist=(int) buf[2];
got only one byte of the four bytes of distance and it's dumb luck that the result was the same as expected. dd got exactly one character, not the whole string, and this is turning out to be nonsense because of one of the preceding mistakes. dist is similarly garbage.
To successfully move data from one machine to another, you need to establish a communication protocol. You can't just write a long onto a wire. long doesn't have the same size on all platforms, nor does it always have the same encoding. You have to make absolutely certain that both sides agree on how the long is to be written (size and byte order) and read.
Exactly how you are going to do this is up to you, but here are some pointers and a search term, serialization, to assist you in further research.

Writing STRINGS to serial port in C++ linux

I know this question is scattered all over the internet, but still, nothing is getting me completely there yet. I want to write data to a serial port in C++ (linux) for a a Propeller board. Program works fine when taking input from the console, but when I write strings to it always return: ERROR - Invalid command from the device. I tried creating array of char with Hex values then it worked. here's a working code, below. But how will i be able to just provide a string variable of command and send it to the serial port? perhaps, how do you I convert it to hex values if it's the only way? Thanks everyone
note: the loop is to use user input from console. What i need is a way to send a string variable to the serial port.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
int main(int argc,char** argv){
struct termios tio;
struct termios stdio;
int tty_fd;
fd_set rdset;
unsigned char c='D';
printf("Please start with %s /dev/ttyS1 (for example)\n",argv[0]);
memset(&stdio,0,sizeof(stdio));
stdio.c_iflag=0;
stdio.c_oflag=0;
stdio.c_cflag=0;
stdio.c_lflag=0;
stdio.c_cc[VMIN]=1;
stdio.c_cc[VTIME]=0;
tcsetattr(STDOUT_FILENO,TCSANOW,&stdio);
tcsetattr(STDOUT_FILENO,TCSAFLUSH,&stdio);
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); // make the reads non-blocking
memset(&tio,0,sizeof(tio));
tio.c_iflag=0;
tio.c_oflag=0;
tio.c_cflag=CS8|CREAD|CLOCAL; // 8n1, see termios.h for more information
tio.c_lflag=0;
tio.c_cc[VMIN]=1;
tio.c_cc[VTIME]=5;
tty_fd=open(argv[1], O_RDWR | O_NONBLOCK);
cfsetospeed(&tio,B115200); // 115200 baud
cfsetispeed(&tio,B115200); // 115200 baud
tcsetattr(tty_fd,TCSANOW,&tio);
//char str[] = {'V','E','R','\r'};
//the above str[] doesn't work although it's exactly the same as the following
char str[] = {0x56, 0x45, 0x52, 0x0D};
write(tty_fd,str,strlen(str));
if (read(tty_fd,&c,1)>0)
write(STDOUT_FILENO,&c,1);
while (c!='q')
{
if (read(tty_fd,&c,1)>0) write(STDOUT_FILENO,&c,1); // if new data is available on the serial port, print it out
if (read(STDIN_FILENO,&c,1)>0)
if(c!='q')
write(tty_fd,&c,1); // if new data is available on the console, send it to the serial port
}
close(tty_fd);
}
I'm happy to solve my own solution but yet disappointed to not have seen the trivial matter much sooner. char by default are signed in c++, which makes it holding the range -128 to 127. However, we are expecting the ASCII values which are 0 to 255. Hence it's as simple as declaring it to be unsigned char str[] and everything else should work. Silly me, Silly me.
Still, Thank you everyone for helping me!!!
Are you sure you should end with '\r'? When entering text from console the return key will result in a '\n' character (on Linux) and not '\r'
Also error checking is missing on most functions (open(), fcntl(), etc.). Maybe one of these functions fail. To find out how to check for errors read the man page (for example man 2 open for the open() command. In case of open() the man page explains it returns -1 when it could not open the file/port.
After your edit you wrote:
char str[] = {0x56, 0x45, 0x52, 0x0D};
write(tty_fd,str,strlen(str));
which is wrong. strlen expects a '\0' terminated string which str is obviously not so now it sends your data and whatever there is in memory until it sees a '\0'. You need to add 0x00 to your str array.

Const Char array being modified to all 0s for AVR micro written in C

I am helping a friend get a graphics LCD working on his AVR, a few months ago all was working without issue, it has been untouched since then. The chip has now been swapped out from an ATMega32 to an ATMega164P. Essentially the same chip with more flash, since this change a lot of the code has stopped working.
We have narrowed where the error is occuring, but are unable to rectify it. It is where we pass in a pointer to a const char string, and attempt to print that string, however for some reason the stack(heap, something else?) gets corrupted and the pointer contains all zeros. Does anyone have any ideas how this could occur? We have -O1 level optimisations enabled which are required for correct timing, we have switched to winAVR compiler as well with no changes. We also do not have access to a debugger, and only have limited 'print' style debugging.
Here is the section of code that is causing issues:
//in the header file that is included
int SGCTEXTStringF(int column, int row, int font, int colour, const char* text);
//Call the function
SGCTEXTStringF(0, 0, 0x10, SGCColour(255,255,255), "text");
//Function
int SGCTEXTStringF(int column, int row, int font, int colour, const char* text){
unsigned char bytes[23]={0};
//...Code to communicate with LCD and set up a 'print string'
if(text[0] == 0) {
bytes[6] = 'a';
bytes[7] = 't';
bytes[8] = 'e';
bytes[9] = 's';
bytes[10] = 't';
}
//..more code to finish sending the array
}
Now the display prints 'atest' when the code is run, this is showing that the const char array is being some how zero'd? I have also tried the following lines which also all print 'atest'
if(text[1] == 0) //prints 'atest'
if(*text== 0) //prints 'atest'
if(text != 0) //prints 'atest'
This shows that it gets a valid pointer, but it appears to point to all zeros.
We also tried changing the call to the method to:
const char * string = "test";
SGCTEXTStringF(0, 0, 0x10, SGCColour(255,255,255), string);
This code was known to be working a few months ago, we even have a video of it running, now every single function in the program exhibits the same issue, char arrays being passed (on the stack?) don't seem to work and get cleared to 0s.
I can arrange to host a copy of the complete source code if anyone is interested. Any help or pointers at all are appreciated!
The ATMega164P is not the replacement for the ATMega32 but for the ATMega16. For the ATMega32 you would need the ATMega324P.
If you are using the memory map for the ATMega32 (which has double EEPROM, SRAM and flash), your string may have landed in memory which simply does not exist.
For porting issues you may look at the porting guide from Atmel.