Trying to get connection between an app and an esp8266.
No problem sending a string from app to esp8266 and back again. But when I want the answer back from esp8266 to be one char, a pause, next char, a pause and so on I get stuck. In the IDE Serial Monitor it works but not in the app. All chars shows up at the same time when the loop is restarting.
I am polling from the app every 0,5 second.
Any suggestions how to solve it?
```cpp
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <SoftwareSerial.h>
// #include <DFRobotDFPlayerMini.h>
// #include <EspSoftwareSerial.h>
#ifndef APSSID
#define APSSID "ESPap"
#define APPSK "thereisnospoon"
#endif
/* Set these to your desired credentials. */
const char* ssid = "abcde";
const char* password = "123456789";
WiFiServer server(80);
void setup() {
Serial.begin(115200);
Serial.println();
Serial.print("Configuring access point...");
/* You can remove the password parameter if you want the AP to be open. */
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
// server.on("/", handleRoot);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
WiFiClient client = server.available();
if (!client) {
return;
}
Serial.println("New msg");
while (!client.available()) {
delay(1);
}
String in = client.readStringUntil('/r');
Serial.println(in);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
Serial.println("H");
client.println("H");
delay(2000);
Serial.println("e");
client.println("e");
delay(2000);
Serial.println("l");
client.println("l");
delay(2000);
Serial.println("l");
client.println("l");
delay(2000);
Serial.println("o");
client.println("o");
delay(2000);
}
please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. (also make sure you indented the code in the IDE before copying, that's done by pressing ctrlT on a PC or cmdT on a Mac)
the HTTP answer is collected and sent asynchronously and you might also get a time out on the the other side.
HTTP by default is transactional. You send a request - you get the data back in one go and the connection is closed
If you want to have a different behavior you need to handle that differently. Not using http and Using sockets for example or handle multiple requests, …
Can you clarify the set up and what/who is the client ?
My goal is to send a string from an Android app (the client, done in App Inventor). The string tells the esp8266 to execute function A for 5 seconds then function D for 7 seconds and so on depending om what is in the string. Before A begins, I would like to esp8622 to send "A" to the client and just before D starts it shall send "D" to the client.
So 3-20 sendings of one char with 2-20 seconds in-between.
Its my first time connecting Arduino to a phone app so my code above should be a simple test I thought...
You are sending an HTML page. The browser doesn't render the page as the characters arrive but when the page is complete. You need to use "Dynamic HTML" so you can tell the browser to keep the connection open and allow you to send new values for named divisions or something like that. That will take some research.