I appologize for my newness. I have a program that I want to retreive a txt file. I then want to store that to a string. That string will then be called upon by my oled display. The client.read goes through and does the entire void loop one letter each loop. Is there a clean way to get the whole text file at once instead of one letter at a time? Below is what I have, that I want to create a loop that keeps going until the full txt file is downloaded and all letters have been added to my String thats called "mymessage".
I'm very new and trying to understand how the client.available, and client.read work.
// If there are incoming bytes, print them
if ( client.available() ) {
char c = client.read();
Serial.print(c);
mymessage += c;
}
Sorry but there are not many psychics here.
You retrieve a text file from.. where, by what kind of communication? Serial, Wifi, is a file on a SD... can you provide even fewer clues than you did?
Who is sending what, how, to whom? Communication means someone tries to makes itself understood by providing relevant context.
Where is setup, where is loop? Or are you talking about a secret library you-re trying to use and want to keep it secret?
Here is the entire code, except the domain and SSID changed. What I will ultimately have, don't make fun, is my wife will have this at work in her locker. "This" will be an ESP8266, and an OLED screen. I don't care about a program loop because I'll have a photoresistor that will provide power when her locker opens. The two devices will power on. A plain old text file will have text. I'm using @@@ as the delimiter or start of what I'm looking for. I have all that worked out. My "filteredmsg" ends up as a String with exactly what I want. I don't have much knowledge on how the client.read works. It seems like each pass through, it grabs the next character, but I don't know how this script knows that all the characters have been acquire, and disconnects. What I need to work out is to get all the characters, then filter it, before it moves on down to the rest of the loop which will be my oled programming. I have the wifi doing what I want, and I have my oled program taking a String and working. I just need to integrate the two. I believe if I can make the first piece of code I gave complete before moving on, I'll have what I need. What I got when I integraded the wifi and oled was one character at a time was retrived and went into the oled part of the program, instead of collecting the whole text from the text file and then moving on, so the oled could take the whole string and display.
This is just the wifi part I need to make gather each character into a string before moving on in the loop. I need something that will keep looping and adding characters to the string until there isn't anymore characters to get, then move on.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WString.h>
// WiFi information
const char WIFI_SSID[] = "myhotspot";
const char WIFI_PSK[] = "";
// Remote site information
const char http_site[] = "www.mydomain.com";
const int http_port = 80;
// Global variables
WiFiClient client;
// Just some variables
String luvmsg = "";
String filteredmsg = "";
int messagestartlocation = 0;
int messagelength = 0;
void setup() {
// Set up serial console to read web page
Serial.begin(9600);
Serial.print("Thing GET Example");
// Connect to WiFi
connectWiFi();
// Attempt to connect to website
if ( !getPage() ) {
Serial.println("GET request failed");
}
}
void loop() {
// If there are incoming bytes, print them
if ( client.available() ) {
char c = client.read();
Serial.print(c);
luvmsg += c;
}
// If the server has disconnected, stop the client and WiFi
if ( !client.connected() ) {
Serial.println();
// Close socket and wait for disconnect from WiFi
client.stop();
if ( WiFi.status() != WL_DISCONNECTED ) {
WiFi.disconnect();
}
// Do nothing
Serial.println("Here is the luvmsg");
Serial.println("Here we go!!");
Serial.println(messagestartlocation);
Serial.println(messagelength);
Serial.println("Ver 3.0");
messagelength = luvmsg.length(); // capture string length
messagestartlocation = luvmsg.indexOf('@@@');
filteredmsg = luvmsg.substring(messagestartlocation + 3, messagelength);
Serial.println("Here is the filtered msg");
Serial.println(filteredmsg);
Serial.println(messagestartlocation);
Serial.println(messagelength);
Serial.println("Finished Thing GET test");
while(true){
delay(1000);
}
}
}
// Attempt to connect to WiFi
void connectWiFi() {
// Set WiFi mode to station (client)
WiFi.mode(WIFI_STA);
// Initiate connection with SSID and PSK
WiFi.begin(WIFI_SSID, WIFI_PSK);
// Blink LED while we wait for WiFi connection
while ( WiFi.status() != WL_CONNECTED ) {
delay(100);
}
}
// Perform an HTTP GET request to a remote page
bool getPage() {
// Attempt to make a connection to the remote server
if ( !client.connect(http_site, http_port) ) {
return false;
}
// Make an HTTP GET request
client.println("GET /test2.txt HTTP/1.1");
client.print("Host: ");
client.println(http_site);
client.println("Connection: close");
client.println();
return true;
}
mapoff:
Is there a clean way to get the whole text file at once instead of one letter at a time?
...
...
I'm very new and trying to understand how the client.available, and client.read work.
If you look at the functions for the Serial class, you can get some ideas which functions are available. Most (or all) of those functions are also available in classes like the WiFiClient that you use.
If your text contains a unique character at the end, you can use readStringUntil() or readBytesUntil(). It takes the hard work out but will basically do what you're doing (if you add some modifications). Disadvantage of those functions is that you will not know if you have received a complete text.
Other disadvantage is that they block further processing by the processor till all characters are received or a timeout occurs; so if you want to e.g. blink a LED at the same time, you're out of luck. This might not be an issue for you in this case but 'old habits die hard'.
The code as you currently have it is the correct general approach (with the possible exception of the use of String (capital S)).
The available() function checks if one or more characters are received. If so, you can read it (as is done in your code) and add it to the luvmsg variable; else there is no use in reading.
You can have a look at the Serial Input Basics - updated thread for ideas how to read data (it applies equally to the WiFiClient) (and setup your text accordingly on the server).
Thank you I appreciate it. I'm still learning the terms. I like getting enough help to point me in the right direction, not hand the code to me. I really appreciate the help.