/*
* Simple TCP Server
*/
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
const char* ssid = "**********";
const char* password = "**********";
#define SERVER_PORT 9876
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(SERVER_PORT);
void setup() {
Serial.begin(9600);
#if 0
// config static IP
Serial.println();
Serial.println();
IPAddress ip(192, 168, 111,
90);
IPAddress gateway(192, 168,
111, 1);
IPAddress subnet(255, 255, 255, 0);
Serial.print(F("Setting static ip to : "));
Serial.println(ip);
WiFi.config(ip, gateway, subnet);
#endif
WiFi.mode(WIFI_STA);
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());
// Start the server
server.begin();
Serial.println("Server started");
// Start the mDNS responder for esp8266.local
if (!MDNS.begin("esp8266")) {
Serial.println("Error setting up
MDNS responder!");
}
Serial.println("mDNS responder started");
}
void loop() {
MDNS.update();
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String rmsg = client.readStringUntil('\0');
Serial.print(rmsg);
client.flush();
// Prepare the response
char smsg[128];
memset(smsg,0,sizeof(smsg));
for(uint32_t i = 0; i < rmsg.length(); i++) {
if(isalpha(rmsg[i])) {
smsg[i] = toupper(rmsg[i]);
} else {
smsg[i] = rmsg[i];
} // end if
} // end for
Serial.print("->");
Serial.println(smsg);
// Send the response to the client
client.print(smsg);
delay(1);
Serial.println("Client disonnected");
// The client will actually be disconnected
// when the function returns and 'client' object is
detroyed
}
|