Interface and Application Programming

Assignments

individual assignment:
-write an application that interfaces a user with an input &/or output device that you made
group assignment:
-compare as many tool options as possible

Research

pico webserver

throughout the week

Group Work

group site link
Microcontroller Html based beserver

Pros

* Low cost: Microcontrollers are generally cheaper than full-fledged computers, making them an attractive option for simple web server applications.
* Low power consumption: Microcontrollers are designed to be low-power devices, which can be an advantage in applications where power consumption is a concern.
* Customizability: With a microcontroller, you have full control over the hardware and software, allowing you to tailor your web server to your specific needs.
* Reliability: Microcontrollers are often used in industrial and embedded applications, where reliability is a critical requirement. They are designed to be robust and durable.

Cons:

* Limited resources: Microcontrollers have limited processing power, memory, and storage compared to full-fledged computers. This can limit the complexity of the web server and the number of concurrent users it can support.
* Limited connectivity: Microcontrollers may not have built-in networking capabilities or may only support limited networking protocols. This can limit the types of devices and networks that can connect to the web server.
* Limited software support: Microcontrollers often use proprietary software development environments and may have limited support for third-party software libraries and frameworks.
* Security concerns: Microcontrollers may not have the same level of security features and protections as full-fledged computers, making them potentially vulnerable to attacks.


Overall, using a microcontroller to host a web server can be a good choice for simple, low-traffic applications where cost and power consumption are concerns. However, more complex applications with higher traffic and security requirements may require a full-fledged computer or server.

Setting up a web server.

The first step this week was to set up a web server to host an interface that I designed. To do this at the suggestion of fellow Fab Academy student Adam Stone I followed the tutorial linked above to learn how to host a web server on a raspberry pi pico W. Following this and uploaded the code it says to right here.

import network
import socket
import time

from machine import Pin

led = Pin(15, Pin.OUT)

ssid = 'YOUR NETWORK NAME'
password = 'YOUR NETWORK PASSWORD'

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)

html = """<!DOCTYPE html>
<html>
    <head> <title>Pico W</title> </head>
    <body> <h1>Pico W</h1>
        <p>%s</p>
    </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)
        request = cl.recv(1024)
        print(request)

        request = str(request)
        led_on = request.find('/light/on')
        led_off = request.find('/light/off')
        print( 'led on = ' + str(led_on))
        print( 'led off = ' + str(led_off))

        if led_on == 6:
            print("led on")
            led.value(1)
            stateis = "LED is ON"

        if led_off == 6:
            print("led off")
            led.value(0)
            stateis = "LED is OFF"

        response = html % stateis

        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')


With this code I changed the SSID to my hotspot name and the password to the password, and it connected to it allowing it to host a web server. Here is a picture of the running site.
image
After looking through the code to understand how it works I realised that the actual websitet of HTML which I am confident in and can code in. So I quickly changed some stuff to see if it would even change by changing this part of the code.

<html>
    <head> <title>Pico W</title> </head>
    <body> <h1>Pico W</h1>
        <p>%s</p>
    </body>
</html>


And made it into my name just to see if any changes happened and here is a picture of the site after.
image
After changing this I looked into the output this code has where by changing the link you can turn on and off an LED as shown here in the console here.


But I did not like having to put the link in so using my HTML knowledge I added buttons you could click by changing the html part of the code to this.

    html = """<!DOCTYPE html>
    <html>
        <head> <title>Pico W</title> </head>
        <body> <h1>Pico W</h1>
            <h3>%s
            <br>
            <br>
            Use the button below to turn on the LED.
            <br>
            <button onclick="location.href='/light/on'">Turn On LED</button>
            <br>
            <button onclick="location.href='/light/off'">Turn Off LED</button>
            <br>
            <br>
            <br>
        Make sure that your LED works before trying this because it keeps failing for me because the LED was burned out.
        </h3>
     </body>
    </html>
    '''
<br>

By changing it to this I added two buttons that when clicked will automatimatically change the sites link. This caused the site too look like this.
image
And here is a video of the site working.


Setting up the LED

The next step was to actually hook it up to an LED or another output device. To do this I just uploaded the whole code shown below onto the board making the led in the code connect up to the on-board LED to show the final product.

import network
import socket
import time
import machine

from machine import Pin

led = Pin('LED', Pin.OUT)

ssid = 'Smc'
password = 'stuart7459'

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)

html = """<!DOCTYPE html>
<html>
    <head> <title>Pico W</title> </head>
    <body> <h1>Pico W</h1>
        <h3>%s
        <br>
        <br>
        Use the button below to turn on the LED.
        <br>
        <button onclick="location.href='/light/on'">Turn On LED</button>
        <br>
        <button onclick="location.href='/light/off'">Turn Off LED</button>
        <br>
        <br>
        <br>
        Make sure that your LED works before trying this because it keeps failing for me because the LED was burned out.
        </h3>
    </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)
        request = cl.recv(1024)
        print(request)

        request = str(request)
        print(request)
        led_on = request.find('/light/on')
        led_off = request.find('/light/off')
        print( 'led on = ' + str(led_on))
        print( 'led off = ' + str(led_off))

        if led_on > 1:
            print("led on")
            led.value(1)
            stateis = "LED is ON"

        if led_off > 6:
            print("led off")
            led.value(0)
            stateis = "LED is OFF"

        response = html % "Recieved"

        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')

And here is a video of the final project working.


Summary

This week I learned how to host a web server from a raspberry pi pico w and connect to it over the internet. If possible I would love it if I could add this to my final project so that I can control it too my phone but that will be a later addition if I can finish early. This was a really good skill to learn and I plan to keep messing with it. If I do anything interesting I will doccument below otherwise that is what I learned from this week.

Files

Week 14 Downloads