Hi everyone,
How can I convert a Char * value to an integer or float?
All codes:
#include <SPI.h>
#include <Ethernet.h>
String str;
char* veri[10];
int lamba;
float nem;
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 0, 1);
// initialize the library instance:
EthernetClient client;
char server[] = "www.gurkanozturk.com.tr"; // also change the Host line in httpRequest()
//IPAddress server(64,131,82,241);
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10*1000; // delay between updates, in milliseconds
void setup() {
pinMode(2, OUTPUT);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip, myDns);
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
} else {
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}
// give the Ethernet shield a second to initialize:
delay(1000);
}
void loop() {
while (client.connected() || client.available()) {
char c = client.read(); //gets byte from ethernet buffer
str = str + c;
}
// if ten seconds have passed since your last connection,
// then connect again and send data:
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
}
}
// this method makes a HTTP connection to the server:
void httpRequest() {
Serial.println(str);
int i=1;
char* vout;
int str_len = str.length() + 1;
char char_array[str_len];
str.toCharArray(char_array, str_len);
vout = strtok(char_array,"#");
while(vout != NULL){
veri[i] = vout;
vout = strtok(NULL, "#");
i++;
}
Serial.println(veri[1]);
Serial.println(veri[2]);
Serial.println(veri[3]);
lamba = atoi(veri[1]);
Serial.println(lamba);
nem = atof(veri[2]);
Serial.println(nem);
if(lamba == 1) { digitalWrite(2, HIGH); }
client.stop();
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println(" ");
Serial.println("connecting...");
// send the HTTP GET request:
client.println("GET /kulucka/arduino.php HTTP/1.1");
client.println("Host: www.gurkanozturk.com.tr");
client.println("User-Agent: arduino-ethernet");
client.println("Connection: close");
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
} else {
// if you couldn't make a connection:
Serial.println(" ");
Serial.println(" ");
Serial.println("connection failed");
}
}
Incorrect section:
Serial.println(veri[1]);
Serial.println(veri[2]);
Serial.println(veri[3]);
lamba = atoi(veri[1]);
Serial.println(lamba);
nem = atof(veri[2]);
Serial.println(nem);
if(lamba == 1) { digitalWrite(2, HIGH); }
What do you see when you print str in httpRequest() ?
Php 1#0#0 from the results before breaking apart.
result:
1
0
0
0 (to int)
But dials a value of 1 to an integer 0.
"char *" is a pointer to a character variable, which could be a single byte or an array of bytes.
Which variable is to be converted, and what individual characters are in that variable?
The values that veri[1] variable can take; 1 or 0.
The results are coming from the database to Arduino. But he doesn't see it as an integer so I can't process it.
veri[1] = 1;
int lamba = atoi(veri[1]);
Serial.print(lamba);
result:
0
Are you certain there are no non-printing characters before the initial number?
Insert this immediately after the line that prints out veri[1], and see if you get anything in addition to a 31 (ascii for '1').
byte x=0;
while (veri[1][x] != NULL) {
Serial.print(veri[1][x], HEX);
Serial.print(" ");
x++;
}
Serial.println(" ");
gurkanozturk:
The values that veri[1] variable can take; 1 or 0.
The results are coming from the database to Arduino. But he doesn't see it as an integer so I can't process it.
veri[1] = 1;
int lamba = atoi(veri[1]);
Serial.print(lamba);
result:
0
veri[1] is a character pointer, you can't assign a value to it like that. You would need to change the ascii characters in the character array that it points to.
david_2018:
Are you certain there are no non-printing characters before the initial number?
Insert this immediately after the line that prints out veri[1], and see if you get anything in addition to a 31 (ascii for '1').
byte x=0;
while (veri[1][x] != NULL) {
Serial.print(veri[1][x], HEX);
Serial.print(" ");
x++;
}
Serial.println(" ");
my code :
Serial.println(veri[1]);
Serial.println(veri[2]);
Serial.println(veri[3]);
byte x=0;
while (veri[1][x] != NULL) {
Serial.print(veri[1][x], HEX);
Serial.print(" ");
x++;
}
Serial.println(" ");
result :
1
0
0
6E 6E 65 63 74 69 6F 6E 3A 20 63 6C 6F 73 65 D A D A FFFFFFEF FFFFFFBB FFFFFFBF FFFFFFEF FFFFFFBB FFFFFFBF 31
Very odd, I have no idea unless something is corrupting the string str.
That text shows that veri[1] is pointing to:
"nnection: close" carriage-return linefeed carriage-return linefeed 0xEF 0xBB 0xEF 0xEF 0xBB 0xBF "1"
Ah, my guess is your problem is with this section of the code:
while (client.connected() || client.available()) {
char c = client.read(); //gets byte from ethernet buffer
str = str + c;
}
I'm not familiar with working with Strings, but I'm sure you can't simply add a character to a string like that.
Hopefully someone else can show you how to do that properly.
david_2018:
Ah, my guess is your problem is with this section of the code:
I'm not familiar with working with strings, but I'm sure you can't simply add a character to a string like that.
Hopefully someone else can show you how to do that properly.
Check Wstrings.h it has an overload of 'operator +' that allows it:
friend StringSumHelper & operator + (const StringSumHelper &lhs, char c);
The following doesn't work, because the decimal number 1 is not the same as the ASCII digit '1', which is what the atoi() function expects. "atoi" stands for ASCII to integer.
veri[1] = 1;
int lamba = atoi(veri[1]);
Furthermore, the atoi() function expects a zero-terminated ASCII string of digits.
This will work:
char veri[] = "1"; //a zero terminated character array: ASCII digit 1, followed by zero byte
int lambda = atoi(veri);
Something else I notice, str is not cleared after each use, so it will continue to grow in length over time.
jremington:
The following doesn't work, because the decimal number 1 is not the same as the ASCII digit '1', which is what the atoi() function expects. "atoi" stands for ASCII to integer.
veri[1] = 1;
int lamba = atoi(veri[1]);
Furthermore, the atoi() function expects a zero-terminated ASCII string of digits.
This will work:
char veri[] = "1"; //a zero terminated character array: ASCII digit 1, followed by zero byte
int lambda = atoi(veri);
The veri array is an array of character pointers, used to store the token pointers returned by strtok().
If you want to manually set it to some specific text for testing, this is one way to do it:
char test[] = "1";
veri[1] = test;
The data from the database appears in ASCII format. That's why I can't do a numerical operation.
How can I convert ASCII values within a Veri variable to numeric data?
The veri array is an array of character pointers, used to store the token pointers returned by strtok().
I believe a better, and more standard approach is to copy each piece of the string from the pointer location into a null terminated character array which can then be converted with atoi/atof. strcpy can be used.
Trying to make your code generic with up to 10 parsed pieces of indeterminate length creates difficulty. It will make things much easier if you have a known response which you want to parse.
Php 1#0#0 from the results before breaking apart.
Code to parse this message into three pieces is very simple.
String str = "Php 1#0#0"; //test received values
//size veri for one char plus null terminator
char veri[3][2] = {" "," "," "};
int lamba;
float nem;
void setup() {
Serial.begin(9600);
}
void loop() {
httpRequest();
delay(1000);
}
// this method makes a HTTP connection to the server:
void httpRequest() {
Serial.println(str);
int i = 0;
char* vout;
int str_len = str.length() + 1;
char char_array[str_len];
str.toCharArray(char_array, str_len);
vout = strtok(char_array," ");//find space after php
while (vout != NULL) {
vout = strtok(NULL, "#");
strcpy(veri[i],vout);
i++;
}
Serial.println(veri[0]);
Serial.println(veri[1]);
Serial.println(veri[2]);
Serial.println();
lamba = atoi(veri[1]);
Serial.println(lamba);
nem = atof(veri[2]);
Serial.println(nem);
Serial.println();
}