//Charger la librairie Wifi pour l'Esp
#include <ESP8266WiFi.h>
#define lm 1 //marche moteur gauche
#define ls 3 //sens moteur gauche
#define rm 2 //marche moteur droit
#define rs 4
//declaration des parametres wifi
const char* ssid = "choisis_ICN";
const char* password = "";
WiFiServer server(80); //Utilise le port http
void setup() {
Serial.begin(115200);
delay(10);
Serial.println(WiFi.localIP());
// prepare GPIO2
//Declare la sortie BUILTIN_LED
pinMode(lm, OUTPUT);
pinMode(ls, OUTPUT);
pinMode(rm, OUTPUT);
pinMode(rs, OUTPUT);
digitalWrite(lm, LOW);
digitalWrite(ls, LOW);
digitalWrite(rm, LOW);
digitalWrite(rs, LOW);
// Connection au Wifi de la salle
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
//Déclaration du point d'accès
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
//Demarrage du serveur
server.begin();
Serial.println("Serveur Esp Ok");
//Affiche l'adresse IP de l'Esp
Serial.println(WiFi.localIP());
}
void loop() {
//Teste si un client est connecté
WiFiClient client = server.available();
if (!client) {
return;
}
// Attend que le client envoie des données
while (!client.available()) {
delay(1);
}
//Récupère la 1ère ligne de la requète
String req = client.readStringUntil('\r');
client.flush();
//Traite la requète
if (req.indexOf("") != -10) { //Vérifie si c'est la page principale
if (req.indexOf("/A") != -1) { //Vérifie si OFF
digitalWrite(lm, HIGH);
digitalWrite(ls, LOW);
digitalWrite(rm, HIGH);
digitalWrite(rs, LOW);
Serial.println("Advance");
} else if (req.indexOf("/B") != -1) { //Vérifie si ON
digitalWrite(lm, HIGH);
digitalWrite(ls, HIGH);
digitalWrite(rm, HIGH);
digitalWrite(rs, HIGH);
Serial.println("back");
} else if (req.indexOf("/R") != -1) { //Vérifie si ON
digitalWrite(lm, LOW);
digitalWrite(ls, LOW);
digitalWrite(rm, HIGH);
digitalWrite(rs, LOW);
Serial.println("right");
} else if (req.indexOf("/L") != -1) { //Vérifie si ON
digitalWrite(lm, HIGH);
digitalWrite(ls, LOW);
digitalWrite(rm, LOW);
digitalWrite(rs, LOW);
Serial.println("left");
} else if (req.indexOf("/S") != -1) { //Vérifie si ON
digitalWrite(lm, LOW);
digitalWrite(ls, LOW);
digitalWrite(rm, LOW);
digitalWrite(rs, LOW);
Serial.println("stop");
}
}
else {
Serial.println("invalid request");
client.stop();
return;
}
//Créer la réponse
String s = "HTTP/1.1 200 OK\r\n";
s += "Content-Type: text/html\r\n\r\n";
s += "<!DOCTYPE HTML>\r\n";
s += "<html>\r\n";
s += "<head>\r\n";
s += "<meta charset=\"utf-8\"/>\r\n";
s += "<title>Esp8266 en SIN</title>\r\n";
s += "<style>\r\n";
s += "td { width:33%; height : 200px; }\r\n";
s += ".bleu { background: #0000ff; }\r\n";
s += ".rouge { background : #ff0000; }\r\n";
s += "</style>\r\n";
s += "</head>\r\n";
s += "<body>\r\n";
s += "<table style='width:100%;margin:20px'>\r\n";
s += "<tr>\r\n";
s += "<td></td>\r\n";
s += "<td class='bleu' onclick=\"location.href='/A'\"></td>\r\n";
s += "<td></td>\r\n";
s += "</tr>\r\n";
s += "<tr>\r\n";
s += "<td class='bleu' onclick=\"location.href='/L'\"></td>\r\n";
s += "<td class='rouge' onclick=\"location.href='/S'\"></td>\r\n";
s += "<td class='bleu' onclick=\"location.href='/R'\"></td>\r\n";
s += "</tr>\r\n";
s += "<tr>\r\n";
s += "<td></td>\r\n";
s += "<td class='bleu' onclick=\"location.href='/B'\"></td>\r\n";
s += "<td></td>\r\n";
s += "</tr>\r\n";
s += "</table>\r\n";
s += "</body>\r\n";
s += "</html>\n";
client.flush(); //initialise le client.print
//Envoie la page au client
client.print(s);
delay(1);
}