import network
import socket
import time
from machine import Pin, ADC
import urandom

# Define the ADC pins for solar panel, battery, and output current
solar_panel_pin = Pin(26, Pin.IN)
battery_pin = Pin(27, Pin.IN)
output_current_pin = Pin(28, Pin.IN)

solar_panel_adc = ADC(solar_panel_pin)
battery_adc = ADC(battery_pin)
output_current_adc = ADC(output_current_pin)

# Your network SSID and password
ssid = "Wokwi-GUEST";
password = ''
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)

html = """<!DOCTYPE html>
<html>
<head>
    <title>Solar Power Monitoring System</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
 background-image: url('https://media.istockphoto.com/id/525206743/photo/solar-panel-on-a-red-roof.jpg?s=612x612&w=0&k=20&c=xcAkdNj8dFDhu8734FpRDAZDtN2bjr48RKEd9j2FL0U=');
            background-size: cover;
            background-repeat: no-repeat;
        }

        .container {
            text-align: center;
            background-color: rgba(255, 255, 255, 0.8); /* Adding a semi-transparent white background for better readability */
            border: 1px solid #ccc;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
        }

        h1 {
            text-decoration: underline;
        }

        p {
            font-size: 24px;
        }

        button {
            font-size: 20px;
        }

        #credits {
            display: none;
            font-size: 18px;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>Solar Power Monitoring System</h1>
    <h2>Solar Power Monitoring System using PI PICO W</h2>
    <p>&bull; Voltage from Solar panel: %.2f V</p>
    <p>&bull; Battery Voltage: %.2f V</p>
    <p>&bull; Output Current: %.2f A</p>
    <button onclick="refreshPage()">Refresh</button>
    <button onclick="showCredits()">Credits</button>
    <div id="credits">
        <p><h3>Created by:</h3> A.S. Akash</p>
        <p><h4>College:</h4> HINDUSTAN INSTITUTE OF TECHNOLOGY AND SCIENCE</p>
    </div>
</div>

<script>
    function refreshPage() {
        location.reload();
    }

    // Auto-refresh every 10 seconds
    setTimeout(function () {
        location.reload();
    }, 5000); // 10000 milliseconds = 10 seconds

    function showCredits() {
        var credits = document.getElementById("credits");
        credits.style.display = "block";
    }
</script>
</body>
</html>

"""

max_wait = 10
while max_wait > 0:
    if wlan.status() < 0 or wlan.status() >= 3:
        break
    max_wait -= 1
    print('Waiting for connection...')
    time.sleep(1)

if wlan.status() != 3:
    raise RuntimeError('Network connection failed')
else:
    print('Connected')
    status = wlan.ifconfig()
    print('IP = ' + status[0])

addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]

s = socket.socket()
s.bind(addr)
s.listen(1)

print('Listening on', addr)

# Listen for connections
while True:
    try:
        cl, addr = s.accept()
        print('Client connected from', addr)
        
        # Read sensor values
    
        #
        R1 = 30000.0;
        R2 = 7500.0;
        solar_panel_value = ((solar_panel_adc.read_u16() * 3.3) / 65535)/ (R2/(R1+R2));
        battery_voltage = ((battery_adc.read_u16() * 3.3) / 65535)/ (R2/(R1+R2));
        output_current = (output_current_adc.read_u16() / 65535) * 3300  # Convert to voltage (3.3V reference voltage)
        output_current = ((output_current - 1650) / 100) - 0.10

        response = html % (solar_panel_value, battery_voltage, output_current)
        cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
        cl.send(response)
        cl.close()

    except OSError as e:
        cl.close()
        print('Connection closed')
pico:GP0
pico:GP1
pico:GND.1
pico:GP2
pico:GP3
pico:GP4
pico:GP5
pico:GND.2
pico:GP6
pico:GP7
pico:GP8
pico:GP9
pico:GND.3
pico:GP10
pico:GP11
pico:GP12
pico:GP13
pico:GND.4
pico:GP14
pico:GP15
pico:GP16
pico:GP17
pico:GND.5
pico:GP18
pico:GP19
pico:GP20
pico:GP21
pico:GND.6
pico:GP22
pico:RUN
pico:GP26
pico:GP27
pico:GND.7
pico:GP28
pico:ADC_VREF
pico:3V3
pico:3V3_EN
pico:GND.8
pico:VSYS
pico:VBUS