invalid conversion from 'char*' to 'int'

Hey anybody kinda need help for my error.

void loop() {
float distance = getDistance();
RtcDateTime currentTime = rtcObject.GetDateTime();
char str[6];

sprintf(str, "%d%d%d",currentTime.Hour(),currentTime.Minute(),currentTime.Second());

Serial.println(str);
delay(100);
Serial.print("distance(cm) = ");
Serial.print(distance);
Serial.print("\n");

myFile = SD.open("distance.txt", FILE_WRITE);

if(myFile) {
Serial.println("Writing to file...");
myFile.println(str);
myFile.println(distance);
myFile.println("\n");
sendData(str, distance);
myFile.close();
Serial.println("Done!");
} else {
Serial.println("Error opening the file.");
}
//delay(100);
}

void sendData(int times,int dis1)
{
Serial.print("Connecting to : ");
Serial.println(host);
if (!client.connect(host, httpsPort)) {
Serial.println("Connection failed");
return;
}

if (client.verify(fingerprint, host)) {
Serial.println("Certificate matched.");
} else {
Serial.println("Certificate is not matched.");
}
String string_time = String(times, DEC);
String string_distance = String(dis1, DEC);
//String string_distance2 = String(dis2, DEC);
String url = "/macros/s/" + GAS_ID + "/exec?time=" + string_time + "&distance1=" + string_distance;
Serial.print("requesting URL: ");
Serial.println(url);

client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n\r\n");
}

Senddata() takes two ints. You are passing a charachter array as the first argument.

Probably best to look (and post) what line the compiler says the error is on.

Here is my full code.

#include <Wire.h>
#include <RtcDS3231.h>
#include <SPI.h>
#include <SD.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>

const int pinCS = 15;
const int trigPin = 2;
const int echoPin = 0;
float soundVelocity = 0.034;
File myFile;
RtcDS3231 rtcObject(Wire);

const char* ssid = “iPhone”;
const char* password = “12345asd”;
const char* host = “script.google.com”;
const int httpsPort = 443;
// Use WiFiClientSecure class to create TLS connection
WiFiClientSecure client;
// SHA1 fingerprint of the certificate, don’t care with your GAS service
const char* fingerprint = “46 B2 C3 44 9C 59 09 8B 01 B6 F8 BD 4C FB 00 74 91 2F EF F6”;
String GAS_ID = “AKfycbwLP–JZLPHfbW0qBrQX6gANFpPNnsIBHLcUZwUl9Yk8EdyeIxR”;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(pinCS, OUTPUT);
digitalWrite(pinCS, HIGH);
Serial.begin(115200); //Serial
rtcObject.Begin();
Serial.println();

if(SD.begin()) {
Serial.print(“SD card is ready to use.”);
Serial.print("\n");
} else {
Serial.println(“SD card initialization failed.”);
return;
}

Serial.print(“connecting to “);
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(”.”);
}
Serial.println("");
Serial.println(“WiFi connected”);
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void loop() {
float distance = getDistance();
RtcDateTime currentTime = rtcObject.GetDateTime();
char str[10];

sprintf(str, “%d%d%d”,currentTime.Hour(),currentTime.Minute(),currentTime.Second());

Serial.println(str);
delay(100);
Serial.print(“distance(cm) = “);
Serial.print(distance);
Serial.print(”\n”);

myFile = SD.open(“distance.txt”, FILE_WRITE);

if(myFile) {
Serial.println(“Writing to file…”);
myFile.println(str);
myFile.println(distance);
myFile.println("\n");
sendData(str, distance);
myFile.close();
Serial.println(“Done!”);
} else {
Serial.println(“Error opening the file.”);
}
//delay(100);
}

void sendData(char times,int dis1)
{
Serial.print("Connecting to : ");
Serial.println(host);
if (!client.connect(host, httpsPort)) {
Serial.println(“Connection failed”);
return;
}

if (client.verify(fingerprint, host)) {
Serial.println(“Certificate matched.”);
} else {
Serial.println(“Certificate is not matched.”);
}
String string_time = String(times, DEC);
String string_distance = String(dis1, DEC);
//String string_distance2 = String(dis2, DEC);
String url = “/macros/s/” + GAS_ID + “/exec?time=” + string_time + “&distance1=” + string_distance;
Serial.print("requesting URL: ");
Serial.println(url);

client.print(String("GET “) + url + " HTTP/1.1\r\n” +
"Host: " + host + “\r\n” +
“User-Agent: BuildFailureDetectorESP8266\r\n” +
“Connection: close\r\n\r\n”);
}

float getDistance()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long highleveltime = pulseIn(echoPin, HIGH);
float distance = (float (highleveltime) * soundVelocity) / 2.0;
return distance;
delay(500);
}

I’m actually trying to save the timestamp inside a memory and its form is like this for example 20:11:33 . Yet, I don’t know how to define its type data inside that function. Much appreciated if you could help me out.

fahmishahrin:
I’m actually trying to save the timestamp inside a memory and its form is like this for example 20:11:33 . Yet, I don’t know how to define its type data inside that function. Much appreciated if you could help me out.

So you have fixed the error and are moving on? If not please tell us the line number the compiler is complaining about.

Here is my error :

C:\Users\Intel I5 Kabylake\Desktop\Arduino\test1\test1.ino: In function ‘void loop()’:

test1:77:27: error: invalid conversion from ‘char*’ to ‘int’ [-fpermissive]

sendData(str, distance);

^

test1:86:6: error: initializing argument 1 of ‘void sendData(int, int)’ [-fpermissive]

void sendData(int times,int dis1)

^

exit status 1
invalid conversion from ‘char*’ to ‘int’ [-fpermissive]

adwsystems:
So you have fixed the error and are moving on? If not please tell us the line number the compiler is complaining about.

I have fixed it until now, yet its not like what I desired.

This is what I have changed.

void sendData(char* times,int dis1)
{
Serial.print("Connecting to : ");
Serial.println(host);
if (!client.connect(host, httpsPort)) {
Serial.println("Connection failed");
return;
}

if (client.verify(fingerprint, host)) {
Serial.println("Certificate matched.");
} else {
Serial.println("Certificate is not matched.");
}
char string_time = atoi(times);
String string_distance = String(dis1, DEC);
//String string_distance2 = String(dis2, DEC);
String url = "/macros/s/" + GAS_ID + "/exec?time=" + string_time + "&distance1=" + string_distance;
Serial.print("requesting URL: ");
Serial.println(url);

client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n\r\n");
}

 char string_time =  atoi(times);

WTF? Do you have a clue what atoi() returns?

Also, suppose that it is 2 minutes and 9 seconds after 8AM. What will the string that you produce look like?

Suppose that it is 15 minutes and 9 seconds after 11PM. What will that string look like? Does converting that to an int pass the sniff test?

Please edit your posts to add code tags. See "How to use this forum".

PaulS:
Also, suppose that it is 2 minutes and 9 seconds after 8AM. What will the string that you produce look like?

Suppose that it is 15 minutes and 9 seconds after 11PM. What will that string look like? Does converting that to an int pass the sniff test?

Honestly I dont really understand the usage of atoi. I simply put that it solved the error while compiling hahaha. My desired string will be like this = 23:15:33 , hour:min:sec for example. I hope you got my point.

My desired string will be like this = 23:15:33

The way that you are constructing the string, it will be 231533. The atoi() function will happily convert that to an int, except that 231533 is not a valid int value.

fahmishahrin:
Honestly I dont really understand the usage of atoi.

atoi() Function
The function prototype of atoi() function as taken from stdlib.h Library is:

int  _CType atoi(const char *__s);

Assume that we have a character array (named dArray) containing the ASCII codes (0x33, 0x34, 0x35, 0x36) for the digits 3, 4, 5, and 6; the function will parse this array and will transform the ASCII codes into an integrated decimal number 1345 which will be returned to the user via an int type variable named x (_CType in the prototype). The argument of the function takes a pointer variable which holds the base address of a character array (say, dArray[]).

char dArray[] = {0x31, 0x33,0x34, 0x35}; // or "1345" or {‘1’, ‘3’, ‘4’, ‘5’}; null-byte is auto inserted 
char *ptr1;
int x2 = &dArray;       //X2 holds the base address of the array named dArray[]
ptr1 = x2; //base address is passed to the pointer variable, ptr1
int x1 = atoi(ptr1); //or int x1 = atoi(dArray);
Serial.println(x1, DEC); //shows: 1345

The function will fail if the data array (dArray) begins with a digit other than 0 to 9. For example char dArray = “A123”;. The *int x1 = atoi(ptr1);*can also be written as int x1 = atoi(dArray);.