import socket
import gc
import machine
import time


def conectar_wifi():
    import network
    sta_if = network.WLAN(network.STA_IF)
    if not sta_if.isconnected():
        print('Conectando a la red...')
        sta_if.active(True)
        sta_if.connect('CERROAZULNET', '19861986') #Modificar por datos de WiFi
        while not sta_if.isconnected():
            print ("+")
        print ("OK")
    red = sta_if.ifconfig()
    print('Configuracion de la red:', red[0:3])

# ===============================================
# Segmentos del display
#          4
#        #####
#       #     #
#       #     #
#    0  #     #  5
#       #  2  #
#        #####
#       #     #
#       #     #
#   14  #     # 13
#       #     #
#        #####
#          12
#
# ==============================================

ninguno = {0:0, 14:0, 2:0, 13:0, 4:0, 5:0, 12:0}
cero =    {0:1, 14:1, 2:0, 13:1, 4:1, 5:1, 12:1}
uno =     {0:0, 14:0, 2:0, 13:1, 4:0, 5:1, 12:0}
dos =     {0:0, 14:1, 2:1, 13:0, 4:1, 5:1, 12:1}
tres =    {0:0, 14:0, 2:1, 13:1, 4:1, 5:1, 12:1}
cuatro =  {0:1, 14:0, 2:1, 13:1, 4:0, 5:1, 12:0}
cinco =   {0:1, 14:0, 2:1, 13:1, 4:1, 5:0, 12:1}
seis =    {0:1, 14:1, 2:1, 13:1, 4:1, 5:0, 12:1}
siete =   {0:0, 14:0, 2:0, 13:1, 4:1, 5:1, 12:0}
ocho =    {0:1, 14:1, 2:1, 13:1, 4:1, 5:1, 12:1}
nueve =   {0:1, 14:0, 2:1, 13:1, 4:1, 5:1, 12:1}
osup  =   {0:1, 14:0, 2:1, 13:0, 4:1, 5:1, 12:0}
oinf =    {0:0, 14:1, 2:1, 13:1, 4:0, 5:0, 12:1}


def numero(lista):
    for i in lista:
        if lista[i]==1:
            pin=machine.Pin(i,machine.Pin.OUT)
            pin.on()
        else:
            pin=machine.Pin(i,machine.Pin.OUT)
            pin.off()


conectar_wifi()

for v in range(0,3):
    numero(osup)
    time.sleep(0.5)
    numero(oinf)
    time.sleep(0.5)

numero(ninguno)

# For more details and step by step guide visit: Microcontrollerslab.com
led_state = "OFF"

led = machine.Pin(2,machine.Pin.OUT)

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)

while True:
    try:
        if gc.mem_free() < 102000:
            gc.collect()
        conn, addr = s.accept()
        conn.settimeout(3.0)
        print('Received HTTP GET connection request from %s' % str(addr))
        request = conn.recv(1024)
        conn.settimeout(None)
        request = str(request)
        print('GET Rquest Content = %s' % request)
        led_on = request.find('/?led_2_on')
        led_off = request.find('/?led_2_off')

        display_0 = request.find('/?disp0')
        display_1 = request.find('/?disp1')
        display_2 = request.find('/?disp2')
        display_3 = request.find('/?disp3')
        display_4 = request.find('/?disp4')
        display_5 = request.find('/?disp5')
        display_6 = request.find('/?disp6')
        display_7 = request.find('/?disp7')
        display_8 = request.find('/?disp8')
        display_9 = request.find('/?disp9')
        display_n = request.find('/?dispN')

        if led_on == 6:
            print('LED ON -> GPIO2')
            led_state = "ON"
            led.on()
        if led_off == 6:
            print('LED OFF -> GPIO2')
            led_state = "OFF"
            led.off()
        if display_0 == 6:
            print ("CERO")
            numero(cero)
        if display_1 == 6:
            print ("UNO")
            numero(uno)
        if display_2 == 6:
            print ("DOS")
            numero(dos)
        if display_3 == 6:
            print ("TRES")
            numero(tres)
        if display_4 == 6:
            print ("CUATRO")
            numero(cuatro)
        if display_5 == 6:
            print ("CINCO")
            numero(cinco)
        if display_6 == 6:
            print ("SEIS")
            numero(seis)
        if display_7 == 6:
            print ("SIETE")
            numero(siete)
        if display_8 == 6:
            print ("OCHO")
            numero(ocho)
        if display_9 == 6:
            print ("NUEVE")
            numero(nueve)
        if display_n == 6:
            print ("APAGADO")
            numero(ninguno)
        response = "OK"
        conn.send('HTTP/1.1 200 OK\n')
        conn.send('Content-Type: text/html\n')
        conn.send('Connection: close\n\n')
        conn.sendall(response)
        conn.close()
    except OSError as e:
        conn.close()
        print('Connection closed')
