Skip to content

Instantly share code, notes, and snippets.

@aalonzolu
Created May 4, 2018 04:40
Show Gist options
  • Save aalonzolu/a0878e6a9689d8f988d723e47df7854e to your computer and use it in GitHub Desktop.
Save aalonzolu/a0878e6a9689d8f988d723e47df7854e to your computer and use it in GitHub Desktop.
ESP8266 Arduino compatible Wifi Car (AP Mode)
#include <ESP8266WiFi.h>
const char WiFiAPPSK[] = "holamundo";
WiFiServer server(80);
void setup()
{
initHardware();
setupWiFi();
server.begin();
}
void loop()
{
WiFiClient client = server.available();
if (!client) {
return;
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
int val = -1; // We'll use 'val' to keep track of both the
// request type (read/set) and value if set.
if (req.indexOf("/adelante") != -1){
adelante();
}
else if (req.indexOf("/atras") != -1) {
atras();
}
else if (req.indexOf("/parar") != -1)
{
parar();
}
else if (req.indexOf("/derecha") != -1)
{
derecha();
}
else if (req.indexOf("/izquierda") != -1)
{
izquierda();
}
else if (req.indexOf("/recto") != -1)
{
recto();
}
client.flush();
// Prepare the response. Start with the common header:
String s = "HTTP/1.1 200 OK\r\n";
s += "Content-Type: text/html\r\n\r\n";
s += "<!DOCTYPE HTML>\r\n<html>\r\n";
s += "<style>.btn{font-family: Arial; color: #ffffff; font-size: 20px; background: #3498db; padding: 10px 20px 10px 20px; text-decoration: none;}.btn:hover{background: #3cb0fd; text-decoration: none;}</style><script>function httpGet(theUrl){var xmlHttp=new XMLHttpRequest(); xmlHttp.open( \"GET\", theUrl, false ); xmlHttp.send( null ); return xmlHttp.responseText;}</script><div style=\"text-align:center;width:100%;\"> <button onclick=\"httpGet('/adelante')\" class=\"btn\">Adelante</button><br><br><button onclick=\"httpGet('/izquierda')\" class=\"btn\">Izquierda</button> <button onclick=\"httpGet('derecha')\" class=\"btn\">Derecha</button><br><br><button onclick=\"httpGet('atras')\" class=\"btn\">Atras</button></div></div><hr><div style=\"text-align:center;width:100%;\"> <button onclick=\"httpGet('/recto')\" class=\"btn\">RECTO</button> <button onclick=\"httpGet('/parar')\" class=\"btn\">PARAR</button></div>";
s += "</html>\n";
// Send the response to the client
client.print(s);
delay(1);
Serial.println("Client disonnected");
// TEST
// derecha();
// delay(2000);
// izquierda();
// delay(2000);
// recto();
// delay(1000);
}
void atras() {
parar();
digitalWrite (D4, HIGH);
}
void adelante(){
parar();
digitalWrite (D3, HIGH);
}
void parar() {
digitalWrite (D4, LOW);
digitalWrite (D3, LOW);
recto();
}
void derecha() {
digitalWrite (D8, HIGH);
digitalWrite (D7, LOW);
}
void izquierda() {
digitalWrite (D7, HIGH);
digitalWrite (D8, LOW);
}
void recto(){
digitalWrite (D7, LOW);
digitalWrite (D8, LOW);
}
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 = "A3 Car " + 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(115200);
//llantas delanteras
pinMode (D4, OUTPUT);
pinMode (D3, OUTPUT);
// llantas traceras
pinMode (D7, OUTPUT);
pinMode (D8, OUTPUT);
// Don't need to set ANALOG_PIN as input,
// that's all it can be.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment