medusa
June 20, 2017, 1:13am
1
Hi, maybe this is a stupid question, but I'll do it anyway....
I am using the esp8266 in AP mode so that my table can connect to this e send a strings, these same characters should come out of the TXD pin of the EPS, the question is that I receive these characters in the serial monitor:
GET /HELLO HTTP/1.1
GET /HELLO HTTP/1.1
Messages are repeated and I need to filter the headers because the message is just "HELLO"
Can someone tell me how to correct this?
Here is my code:
void loop()
{
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
digitalWrite(LED_PIN, LOW);
delay(200);
digitalWrite(LED_PIN, HIGH);
}
cmiyc
June 20, 2017, 2:06am
3
Looks like client.ReadStringUntil() is working just fine. What is your doubt?
system
June 20, 2017, 8:56am
4
Here is my code:
That is SOME of your code. Take your snippets over to http://snippets-r-us.com for help with them. Here you are required to post ALL of your code.
medusa
June 20, 2017, 2:54pm
5
Sorry for the omission, here is the complete code:
#include <ESP8266WiFi.h>
//////////////////////
// WiFi Definitions //
//////////////////////
const char WiFiAPPSK[] = "123456";
/////////////////////
// Pin Definitions //
/////////////////////
const int LED_PIN = 2; // Thing's onboard, green LED
WiFiServer server(80);
void setup()
{
initHardware();
setupWiFi();
server.begin();
}
void loop()
{
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
digitalWrite(LED_PIN, LOW);
delay(200);
digitalWrite(LED_PIN, HIGH);
}
void setupWiFi()
{
WiFi.mode(WIFI_AP);
// Do a little work to get a unique-ish name. Append the
// last two bytes of the MAC (HEX'd) to "Thing-":
uint8_t mac[WL_MAC_ADDR_LENGTH];
WiFi.softAPmacAddress(mac);
String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
macID.toUpperCase();
String AP_NameString = "DEMO_WIFI_Nro" + macID;
char AP_NameChar[AP_NameString.length() + 1];
memset(AP_NameChar, 0, AP_NameString.length() + 1);
for (int i=0; i<AP_NameString.length(); i++)
AP_NameChar[i] = AP_NameString.charAt(i);
WiFi.softAP(AP_NameChar, WiFiAPPSK);
}
void initHardware()
{
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
}
system
June 20, 2017, 3:20pm
6
String req = client.readStringUntil('\r');
Serial.println(req);
What do you see if you comment out the anonymous print statement, or modify it to not be anonymous?
Serial.print("Client request: [");
Serial.print(req);
Serial.println("]")
;
I wonder if the client object is also printing the request.
medusa
June 20, 2017, 4:30pm
7
With this change, the same way the message repeats itself
Client request: [GET /Hello HTTP/1.1]
Client request: [GET /Hello HTTP/1.1]
system
June 20, 2017, 4:46pm
8
medusa:
With this change, the same way the message repeats itself
Almost immediately? Or is there a delay between the statements appearing? Feel free to offer as much information as possible.
You might try making req a global variable, and clearing it after printing it.
medusa
June 20, 2017, 5:21pm
9
Hi PaulS,
I already declare it as global, I am also initializing and cleaning it, but the same message is still being repeated.
The messages appear one after another there are no delays ...
String req = "";
void loop()
{
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Read the first line of the request
req = "";
req = client.readStringUntil('\r');
Serial.print("Client request: [");
Serial.print(req);
Serial.println("]");
Serial.flush();
digitalWrite(LED_PIN, LOW);
delay(200);
digitalWrite(LED_PIN, HIGH);
}
system
June 20, 2017, 5:31pm
10
The messages appear one after another there are no delays ...
Change the delay() value at the end to something you can clearly detect, like 10 seconds. If you still see two messages almost at once, the problem is with whatever is on the other end of the serial port.
medusa
June 20, 2017, 6:10pm
11
I was wrong, with a delay of 10 seconds I can see that the messages do not arrive together, just 10 seconds pass for the second to arrive!
system
June 21, 2017, 8:09am
12
medusa:
I was wrong, with a delay of 10 seconds I can see that the messages do not arrive together, just 10 seconds pass for the second to arrive!
So, your client is making two requests. That is not the Arduino's problem.
Of course, when the client makes a request, it IS expecting a response. Why aren't you sending one?