# Fonction principale
def web_page():
  if led.value() == 1:
    led_state = 'checked'
  else:
    led_state = ""
    
  if relais.value() == 1:
    relais_state = 'checked'
  else:
    relais_state = ""
       
  html = """<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
  body{max-width: 300px; margin: 0px auto;}
  .switch{display: flex; flex-direction: column; align-items: center;}
  .switch input{display:none}
  .slider{position:relative;width:120px;height:68px;background-color:#ccc;border-radius:34px}
  .slider:before{position:absolute;content:"";height:52px;width:52px;left:8px;bottom:8px;background-color:#fff;-webkit-transition:.4s;transition:.4s;border-radius:68px}
  input:checked+.slider{background-color:#2196F3}
  input:checked+.slider:before{-webkit-transform:translateX(52px);-ms-transform:translateX(52px);transform:translateX(52px)}
</style>
<script>
function toggleCheckbox(element) {
  var xhr = new XMLHttpRequest();
  if(element.checked){
    xhr.open("GET", "/?led=on", true);
  } else {
    xhr.open("GET", "/?led=off", true);
  }
  xhr.send();
}
function toggleCheckbox2(element) {
  var xhr = new XMLHttpRequest();
  if(element.checked){
    xhr.open("GET", "/?relais=on", true);
  } else {
    xhr.open("GET", "/?relais=off", true);
  }
  xhr.send();
}
</script>
</head>
<body>
  <h1>ESP Web Server</h1>
  <table>
    <tr>
      <td>
        <label class="switch">
          <span>LED</span>
          <input type="checkbox" onchange="toggleCheckbox(this)" %s>
          <span class="slider"></span>
        </label>
      </td>
      <td>
        <label class="switch">
          <span>RELAIS</span>
          <input type="checkbox" onchange="toggleCheckbox2(this)" %s>
          <span class="slider"></span>
        </label>
      </td>
    </tr>
  </table>
</body>
</html>""" % (led_state, relais_state)
  return html
#****************************************
#Fonction pour controler la LED
def led_rec_state(req):
    led_on = req.find('/?led=on')
    led_off = req.find('/?led=off')
    if led_on != -1:
        print('LED ON')
        led.value(1)
    if led_off != -1:
        print('LED OFF')
        led.value(0)
#*****************************************
#Fonction pour controler le relais
def relais_rec_state(req):
    relais_on = req.find('/?relais=on')
    relais_off = req.find('/?relais=off')
    if relais_on != -1:
        print('RELAIS ON')
        relais.value(1)
    if relais_off != -1:
        print('RELAIS OFF')
        relais.value(0)
        
def main():

    # Demander à l'utilisateur de choisir entre le Mode AP ou client
    #mode_f = input("Veuillez Saisir 1 -> (Mode ACCESS PONT) & 2 -> (Mode Client) : ")
    try:
        #mode_f = int(mode_f)
        mode_f = 1
        if(mode_f == 1):
            accesspoint_wifi() #executer le mode access point
        elif(mode_f == 2):
            connect_wifi() # executer le mode client
        else:
            print("Veuillez entrer une valeur valide.")
    except:
        print("Veuillez entrer une valeur valide.")
    
    
    # Créer une socket TCP
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind(('', 80))
    server_socket.listen(5)

    while True:
        try :
            conn, addr = server_socket.accept()
            print(' Connexion de client -> %s' % str(addr))
            request = conn.recv(1024)
            # Analyser la requete pour changer l'etat de la LED :
            led_rec_state(str(request))
            relais_rec_state(str(request))
            print('Content -> %s' % str(request))
            #valeur = str(request)
            #print(valeur.find('led=off'))
            #Réponse du serveur Web****************************
            response = web_page()
            conn.send('HTTP/1.1 200 OK\n')
            conn.send('Content-Type: text/html\n')
            conn.send('Connection: close\n\n')
            conn.sendall(response)
        except Exception as e:
            print(f'Erreur lors de la communication avec le client {str(addr)} : {e}')
        finally:
            # Fermer la connexion du client avec le serveur
            conn.close()

if __name__ == "__main__":
    main()
NOCOMNCVCCGNDINLED1PWRRelay Module