Created
September 23, 2017 06:13
-
-
Save binitshah/728a531c6e93a8e5ca6c37c01679dfa3 to your computer and use it in GitHub Desktop.
simple web server on the linknode d1 knockoff for esp8266
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 = "Binit's iPhone"; | |
const char* password = "pass"; | |
int rightPin = 13; | |
int leftPin = 12; | |
WiFiServer server(80); | |
void setup() { | |
Serial.begin(115200); | |
delay(10); | |
pinMode(rightPin, OUTPUT); | |
pinMode(leftPin, OUTPUT); | |
digitalWrite(rightPin, LOW); | |
digitalWrite(leftPin, LOW); | |
// Connect to WiFi network | |
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"); | |
// Start the server | |
server.begin(); | |
Serial.println("Server started"); | |
// Print the IP address | |
Serial.print("Use this URL : "); | |
Serial.print("http://"); | |
Serial.print(WiFi.localIP()); | |
Serial.println("/"); | |
} | |
void loop() { | |
// 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 request = client.readStringUntil('\r'); | |
Serial.println(request); | |
client.flush(); | |
// Match the request | |
String turnPath = "NONE"; | |
if (request.indexOf("/TURN=FORWARD") != -1) { | |
digitalWrite(rightPin, HIGH); | |
digitalWrite(leftPin, HIGH); | |
turnPath = "FORWARD"; | |
} | |
if (request.indexOf("/TURN=BACKWARD") != -1){ | |
digitalWrite(rightPin, LOW); | |
digitalWrite(leftPin, LOW); | |
turnPath = "BACKWARD"; | |
} | |
if (request.indexOf("/TURN=FORWARDLEFT") != -1){ | |
digitalWrite(rightPin, LOW); | |
digitalWrite(leftPin, HIGH); | |
turnPath = "FORWARDLEFT"; | |
} | |
if (request.indexOf("/TURN=FORWARDRIGHT") != -1){ | |
digitalWrite(rightPin, HIGH); | |
digitalWrite(leftPin, LOW); | |
turnPath = "FORWARDRIGHT"; | |
} | |
if (request.indexOf("/TURN=BACKWARDLEFT") != -1){ | |
digitalWrite(rightPin, LOW); | |
digitalWrite(leftPin, LOW); | |
turnPath = "BACKWARDLEFT"; | |
} | |
if (request.indexOf("/TURN=BACKWARDRIGHT") != -1){ | |
digitalWrite(rightPin, LOW); | |
digitalWrite(leftPin, LOW); | |
turnPath = "BACKWARDRIGHT"; | |
} | |
if (request.indexOf("/TURN=STOP") != -1){ | |
digitalWrite(rightPin, LOW); | |
digitalWrite(leftPin, LOW); | |
turnPath = "STOP"; | |
} | |
// Return the response | |
client.println("HTTP/1.1 200 OK"); | |
client.println("Content-Type: text/html"); | |
client.println(""); // do not forget this one | |
client.println("<!DOCTYPE HTML>"); | |
char* headerAndSome = "<html>" | |
"<head>" | |
"<title>Jank Arduicar Controls</title>" | |
"<style>html, body {background-color: #cccbcb;}h1 {color: #070707;font-family: 'Comic Sans MS', sans-serif;text-align: center;margin-top: 100px;}a {padding: 10px;background-color: #070707;color: #cccbcb;text-align: center;display: block;align-self: center;margin-left: 10px;margin-right: 10px;}@media only screen and (max-device-width: 500px) {h1 {font-size: 6em;margin-top: 200px;}}</style>" | |
"</head>" | |
"<body>" | |
"<h1>"; | |
client.println(headerAndSome); | |
if(turnPath.equals("FORWARD")) { | |
client.print("We moved forwards."); | |
} else if(turnPath.equals("BACKWARD")) { | |
client.print("We moved backwards."); | |
} else if(turnPath.equals("FORWARDRIGHT")) { | |
client.print("We moved forwards and turned right."); | |
} else if(turnPath.equals("FORWARDLEFT")) { | |
client.print("We moved forwards and turned left."); | |
} else if(turnPath.equals("BACKWARDRIGHT")) { | |
client.print("We moved backwards and turned right."); | |
} else if(turnPath.equals("BACKWARDLEFT")) { | |
client.print("We moved backwards and turned left."); | |
} else { | |
client.print("Use the controls to move the car."); | |
} | |
char* restOfMessage = "</h1> \ | |
<br><br> \ | |
<a href=\"/TURN=FORWARD\">Forward</a><br> \ | |
<a href=\"/TURN=FORWARDLEFT\">Forward Left</a><br> \ | |
<a href=\"/TURN=FORWARDRIGHT\">Forward Right</a><br> \ | |
<a href=\"/TURN=STOP\">Stop</a><br> \ | |
</body> \ | |
</html>"; | |
client.println(restOfMessage); | |
delay(3000); | |
digitalWrite(rightPin, LOW); | |
digitalWrite(leftPin, LOW); | |
Serial.println("Client disconnected"); | |
Serial.println(""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment