I am reaching frustration and tried many options to solve this issue with no success.
Using Wemos D1 clone Atmega 2560 with on board WiFi.
Attempting to read data from a web server (index.php?request=filename&case=data...)
It is a file on the server that I have created to pull information from database and spit it out upon request raw text with no headers or HTML tags.
The index.php file will strictly output text or numbers.
The control board code is the problem.
I have tried client.read() and http.writeToStream() with no success.
String content;
client.connect(address, 80);
Serial.println("\nMaintaining connection.");
while (client.available()) {
//tested with delay(10); and no success
char c = client.read();
content = content + c;
}
if(content){
Serial.println("\nReading from client success: " + content);
}
I have tried another method:
int httpCode = http.GET();
if (httpCode > 0) {
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
if (httpCode == HTTP_CODE_OK) {
http.writeToStream(&Serial);
}//successful in streaming the data to serial monitor but not into return string from function.
}
And one more option but more complicated as this method uses a byte as an output and limit my understanding and ability to convert this into a proper string.
Again, this output proper results to serial monitor but not into a return string.
while (http.connected() && (len > 0 || len == -1)) {
// get available data size
size_t size = stream->available();
if (size) {
// read up to 128 byte
int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
// write it to Serial
Serial.write(buff, c);
if (len > 0) {
len -= c;
}
}
}
All results are with successful server connection.
Again, this output proper results to serial monitor but not into a return string.
Because you make NO effort whatsoever to store the data that you print to the serial port in an array. A string is simply a NULL terminated char array.
ANY C book or website devoted to C will have extensive coverage of char arrays as strings.
You toss around code and claim, with not a shred of evidence that "this works" or "that fails". Show us what the code does, and how that differs from what you want. Any one of those snippets could be made to work.
First method of client.read(); did not produce any results.
I have prompted Serial.println("Processing client.read"); and it indicated no errors.
However, char c = client.read(); produced no reading since Serial.print(c); shows nothing.
Second method
http.writeToStream(&Serial); is working but how do I capture the information to a char or string?
Third method
Serial.write(buff, c); is also working but how to capture the information to a char or string?
I am also suspecting something may be wrong with the control board since it takes a few cycles of WiFi reads to actually print out proper client read results.
String WEB_processing(String address){
String content;
char eot = char(4); //It is determined by ASCII code 4 as End Of Transmission so I made sure this is the output from the web client after every transmission.
char c;
WiFiClient client;
if(WiFi.status() == WL_CONNECTED){
http.begin(client, address);
http.addHeader("Content-Type", "text/plain");
int httpCode = http.GET();
if (httpCode == 200) {
Serial.print("\nStream results: ");
while((client.available() > 0) && (c != eot)) {// client.available predicts the size still available to read
c = client.read(); //reading one character at a time
content += c; //collecting all characters into one string
}
}else{
Serial.print("\nStream fail.");
}
Serial.print("\nContent output: " + content);
http.end();
}else{
Serial.println("\nDisconnected from server.");
client.stop();
}
delay(1000);
return content;
}