Skip to content

Instantly share code, notes, and snippets.

@kashimAstro kashimAstro/tcp.ino
Created Mar 13, 2017

Embed
What would you like to do?
TCP server for ESP-07
#include <ESP8266WiFi.h>
const char* ssid = "************";
const char* password = "************";
WiFiServer server(5055);
int pinLeft = 13;
int pinRight = 12;
bool ValLeft = false;
bool ValRight = false;
void setup() {
Serial.begin(115200);
delay(100);
pinMode(pinLeft, OUTPUT);
pinMode(pinRight, OUTPUT);
digitalWrite(pinLeft, 0);
digitalWrite(pinRight, 0);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
server.begin();
Serial.println("Server started");
Serial.println(WiFi.localIP());
}
void loop() {
WiFiClient client = server.available();
if (!client) {
return;
}
Serial.println("new client");
while(!client.available()){
delay(1);
}
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
if (req.indexOf("gpio13") != -1)
{
ValLeft = !ValLeft;
digitalWrite(pinLeft, (ValLeft)?HIGH:LOW);
}
else if (req.indexOf("gpio12") != -1)
{
ValRight = !ValRight;
digitalWrite(pinRight, (ValRight)?HIGH:LOW);
}
else {
client.stop();
return;
}
client.flush();
delay(1);
Serial.println("disonnected");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.