import network
import time
import socket


def web_page():
  html = """<html>
                <head>
                    <meta name="viewport" content="width=device-width, initial-scale=1">
                    <script type="text/javascript">
                    <!-- Begin
                    function reFresh() {
                      location.reload(true)
                    }
                    /* Definir le temp de refraichir le nombre en  in milliseconds, 1 minute = 60000 milliseconds. */
                    window.setInterval("reFresh()",500);
                    // End -->
                    </script>
                </head>
                <body>
                    <h1>Weather Station</h1>
                    <p>Temperature : {t}</p>
                    <p>Pression Atmoshérique : {p}</p>
                    <p>Humiditée : {h}</p>
                    <p>Taux de CO2 : {co2}</p>
                </body>
            </html>
         """
  return html

# if you do not see the network you may have to power cycle
# unplug your pico w for 10 seconds and plug it in again
def ap_mode(ssid, password):
    """
        Description: This is a function to activate AP mode
        
        Parameters:
        
        ssid[str]: The name of your internet connection
        password[str]: Password for your internet connection
        
        Returns: Nada
    """
    # Just making our internet connection
    ap = network.WLAN(network.AP_IF)
    ap.config(essid=ssid, password=password)
    ap.active(True)
    
    while ap.active() == False:
        pass
    print('AP Mode Is Active, You can Now Connect')
    print('IP Address To Connect to:: ' + ap.ifconfig()[0])
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)   #creating socket object
    s.bind(('', 80))
    s.listen(5)

    while True:
      conn, addr = s.accept()
      print('Got a connection from %s' % str(addr))
      request = conn.recv(1024)
      print('Content = %s' % str(request))
      response = web_page()
      conn.send(response)
      conn.close()
      
ap_mode('Weather Station',
        'Miam1234')