HTTP GET Not working with Char Array?

Hi Guys,

I have a small issue trying to get some data through to an HTTP GET Request with the following method.
(Code is summed up)

** Ports opening and connection done before this **

 client.print("GET /scripts/urltosendto.php?datastring=");
 client.print(receivedChars);
 client.print(" HTTP/1.1\r\n");
 client.print("Host: ");
 client.print(host);
 client.print("\r\n");
 client.print("Connection: close\r\n\r\n");

My goal is to get serial data from another Arduino, and insert that serial data into a variable to allow it to be sent off through the GET Request. This request works perfectly fine with a test String in place of the "receivedChars"(I Know, I know, Don't Use String). But when I read the serial data it obviously returns bytes and so I will need to store it to a char array, however, client.print(char array name) always causes the GET Request to return with Error 400 Bad Request.

To elaborate a little more about the test String. If I do this:

String testReceivedChars = "1,4,5,1,691,591,592";

And use it in the GET request it works, but if I then go:

testReceivedChars = recievedChars;

It breaks as it would if I just use recievedChars char array.

Can someone tell me how I can get around this or let me know what I am doing wrong here?
Thanks so much!

Below is the method I use to Read the Serial Data

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

boolean newData = false;



void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
    
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.println(receivedChars);
        delay(200);
        newData = false;
    }
}

void loop() 
{
  recvWithEndMarker();
  showNewData();

You can't do this! testReceivedChars = recievedChars;

testReceivedChars is a String which is not the same as a character array.

See if reply #11 by zoomkat is useful How to convert byte array to String? - Programming Questions - Arduino Forum

Okay on another look,

it turns out my server that I am sending to only accepts strings and I am trying to send it an array of bytes.

That makes a lot of sense, now its the most commonly asked question on these forums.

how to convert char array to string without using String class.

Im just going to go ahead and assume my answer is going to be in one of the string.h functions?

how to convert char array to string without using String class.

A string is a NULL terminated char array. Your char array gets NULL terminated, so it IS a string.

Post ALL of your code, so we can see where it might be failing.

Add more debug Serial.print() statements, so YOU can see where it IS failing.

Hi Paul,

That is what I also thought but like I said, I found out that my server that I am sending to only accepts strings instead of bytes, which is why its throwing a 400 bad request.

Here is my code if you can point me in the right that direction that would be great!. I think I just need to copy the char array to a string? but like you said if its null terminated, then it IS a string? so it is confusing me a bit.

// Library
#include <ESP8266WiFi.h>

// WiFi settings
const char* ssid = "Fire";
const char* password = "yessir";



// Time to sleep (in seconds):
const int sleepTimeS = 10;

// Host
const char* host = "hostname.co.za";


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


boolean newData = false;
void setup() 
{
  // Serial
  Serial.begin(9600);
  
  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  Serial.println("");
  Serial.println("WiFi connected");
  
  // Print the IP address
  Serial.println(WiFi.localIP());

}

void sendProbeData(){

   // Logging data to cloud
  Serial.print("Connecting to ");
  Serial.println(host);
  
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
  
 
 client.print("GET /scripts/urltosendto.php?datastring=");
 client.print(receivedChars);
 client.print(" HTTP/1.1\r\n");
 client.print("Host: ");
 client.print(host);
 client.print("\r\n");
 client.print("Connection: close\r\n\r\n");
               
  delay(10);
  
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  
  Serial.println();
  Serial.println("closing connection");

  // Sleep
  //Serial.println("ESP8266 in sleep mode");
  //ESP.deepSleep(sleepTimeS * 1000000);
  
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
    
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.println(receivedChars);
        delay(200);
        sendProbeData();
        newData = false;
    }
}

void loop() 
{
  recvWithEndMarker();
  showNewData();

}

so it is confusing me a bit.

Well, your hand-waving is confusing me, so, we're even.

        Serial.println(receivedChars);

Anonymous printing sucks.

        Serial.print("receivedChars: [");
        Serial.print(receivedChars);
        Serial.println("]");

conveys an order of magnitude more data and takes very little longer to type, compile and execute.

Sharing the output WILL be necessary.

BrenMaarten:
That is what I also thought but like I said, I found out that my server that I am sending to only accepts strings instead of bytes, which is why its throwing a 400 bad request.

Why are you unwilling to read reply #11 by zoomkat? ? ?

ieee488:
Why are you unwilling to read reply #11 by zoomkat? ? ?

I did see his reply, I don't fully understand the strcpy function so currently going over it now and now trying to adapt it to my situation.

PaulS:
Sharing the output WILL be necessary.

The Ouput of receivedChars is simply [001172,1,1,76,0,1,19,18,18,17,17]

The Ouput of receivedChars is simply [001172,1,1,76,0,1,19,18,18,17,17]

I want to see what is ACTUALLY printed, NOT your interpretation of what is printed.

If you see
receivedChars: [001172,1,1,76,0,1,19,18,18,17,17]
then receivedChars does NOT contain "[001172,1,1,76,0,1,19,18,18,17,17]".

It contains "001172,1,1,76,0,1,19,18,18,17,17".

But, that data does NOT make sense in a GET request like so:
GET /scripts/urltosendto.php?datastring=001172,1,1,76,0,1,19,18,18,17,17 HTTP1.1

It might make sense in a GET request like:
GET /scripts/urltosendto.php?datastring="001172,1,1,76,0,1,19,18,18,17,17" HTTP1.1

The Output prints:

receivedChars: [001172,1,1,76,0,1,19,18,18,17,17]

But I found out where the problem was.

If receivedChars is allocated anything more than it actually uses by const byte numChars = 37;
Then the GET Request throws the Bad Request error with the detail Accept-Ranges:Bytes.

So basically, the array always needs to be the exact size, which is obviously why using a String worked since its automatically allocated its size.

This does pose a problem though because that array size can fluctuate depending on the data I receive from the Serial.

Do you know how I can get around this?

Do you know how I can get around this?

If you have put the NULL in the right place, and output the data correctly, the size of the array does NOT matter. You can have an array that can hold 80 characters, with a NULL in position 3, and only three characters will be sent.

Serial.print() the same stuff you are client.print()ing - but between brackets, so you KNOW what the data REALLY looks like. The problem may become obvious.