Created
March 13, 2017 14:54
-
-
Save kashimAstro/c6bd989e83930c71b9e8c3d3df57d502 to your computer and use it in GitHub Desktop.
TCP server for ESP-07
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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