revoilà :
le code quand l'esp se connecte à mon routeur : l'android envoie les ordres on/off a l'adresse IP de l'esp, en "transitant" par la box/routeur , correct ?
#include <ESP8266WiFi.h>
WiFiClient client; //
WiFiServer server(80);
const char* ssid = "monrouteur";
const char* password = "mdpwifi";
String command =""; // Command received from Android device
// Set led/relay Pins
int switch1 = 5; //D1
int switch2 = 4;
int switch3 = 14;
int switch4 = 12;
void setup()
{
Serial.begin(115200);
pinMode(switch1, OUTPUT);
pinMode(switch2, OUTPUT);
pinMode(switch3, OUTPUT);
pinMode(switch4, OUTPUT);
// si on utilise des modules relais avec opto ils sont actifs
// avec un signal LOW
digitalWrite(switch1,HIGH); // les relais non actifs
digitalWrite(switch2,HIGH);
digitalWrite(switch3,HIGH);
digitalWrite(switch4,HIGH);
connectWiFi(); // fonction locale plus bas
server.begin();
}
void loop()
{
client = server.available();
if (!client) return;
command = checkClient ();
if (command == "sw1on") digitalWrite(switch1,LOW);
else if (command == "sw1off") digitalWrite(switch1,HIGH);
else if (command == "sw2on") digitalWrite(switch2,LOW);
else if (command == "sw2off") digitalWrite(switch2,HIGH);
else if (command == "sw3on") digitalWrite(switch3,LOW);
else if (command == "sw3off") digitalWrite(switch3,HIGH);
else if (command == "sw4on") digitalWrite(switch4,LOW);
else if (command == "sw4off") digitalWrite(switch4,HIGH);
else if (command == "allon")
{
digitalWrite(switch1,LOW);
digitalWrite(switch2,LOW);
digitalWrite(switch3,LOW);
digitalWrite(switch4,LOW);
}
else if (command == "alloff")
{
digitalWrite(switch1,HIGH);
digitalWrite(switch2,HIGH);
digitalWrite(switch3,HIGH);
digitalWrite(switch4,HIGH);
}
sendBackEcho(command); // send command echo back to android device
command = "";
}
/* connecting WiFi : fonction */
void connectWiFi()
{
Serial.println("Connecting to WIFI");
WiFi.begin(ssid, password);
while ((!(WiFi.status() == WL_CONNECTED)))
{
delay(300);
Serial.print("..");
}
Serial.println("");
Serial.println("WiFi connection established");
Serial.println("IP number is : ");
Serial.print((WiFi.localIP()));
}
/* check command received from Android Device */
String checkClient (void)
{
while(!client.available()) delay(1);
String request = client.readStringUntil('\r');
request.remove(0, 5);
request.remove(request.length()-9,9);
return request;
}
/* send command echo back to android device */
void sendBackEcho(String echo)
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println(echo);
client.println("</html>");
client.stop();
delay(1);
}
j'ai mis l'esp en AP avec le code suivant
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
/* Set these to your desired credentials. */
const char *ssid = "monesp8266";
const char *password = "";
ESP8266WebServer server(80);
/* Just a little test message. Go to http://192.168.4.1 in a web browser
connected to this access point to see it.
*/
void handleRoot() {
server.send(200, "text/html", "<h1>You are connected</h1>");
}
void setup() {
delay(1000);
Serial.begin(115200);
Serial.println();
Serial.print("Configuring access point...");
/* You can remove the password parameter if you want the AP to be open. */
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.on("/", handleRoot);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}
j'y accede depuis mon android avec le nom "monesp8266" adresse ip est 192.168.4.1 (adresse defaut esp )
que dois-je changer dans le premier code à flasher dans l'esp pour que le tel android accede
directement à l'esp ?
il y a des choses que j'ai du mal à suivre, je lis qu'on peut "lier" max 8 devices à l'esp en AP, 4 par défaut ? mais pas d'explication comment faire ?
bon l'anglais et moi...n'empeche
dans les options "carte 8266" on peut choisir erase flash : onlysketch, sketch+wifisettings, allflash contents : que choisir pour quelle raison ?

ESP8266 as HTTP Server using WiFi Access Point (AP) mode
const char* ssid = "monesp8266"; // Enter SSID here
const char* password = ""; //pas de mdp
/* Put IP Address details */
IPAddress local_ip(192,168,1,1); // adresse ip locale qu'on donne à l'esp : 192.168.1.5 est aussi bon ?
IPAddress gateway(192,168,1,1); // c'est le esp qui est passerelle ?
donc on change le 192.168.4.1 ??? et quand on televerse on choisit sketch+wifisettings ?
IPAddress subnet(255,255,255,0);
ESP8266WebServer server(80);
et dans le setup
WiFi.softAP(ssid, password);
WiFi.softAPConfig(local_ip, gateway, subnet);
delay(100);
le reste ok, le questionnement c'est comment configurer une adresse IP+ le gateway dans l'esp
pourquoi c'est 192.168.1.1 dans les 2 sur le croquis d'origine ? celà est aussi l'adresse de base de certains routeurs, çà prete à confusion
Merci