Tufty 2040 / Badger 2040W: screen control by iPhone

Dear friends,
I’m trying to help two students teams for a national contest to interface the Tufty and new Badger (both wifi connected) with an iPhone, so to change screen images from an app (change a static image on eink badger and change color image or video on the Tufty).
Unluckily I’m new to MicroPython programming.

Does any tutorial / example exist to reach such a target in a few days?

Really thanks if you can help me/us out!

The tufty does not have WLAN (unless you add it using the UART-pins). With the badger2040w it should be possible. But without knowledge of MP (or CP) it will be a rough ride.

The road to go: you would have to setup the badger2040w as a web-server. Then the iphone could connect to it and select an image (maybe from a list of available images). You might find parts of these tasks by searching the web (e.g. “micropython webserver raspberry pi pico”), but putting all of this together will take some time (and you will learn a lot).

1 Like

Ops… thanks for the prompt answer… I thought - by error - that the newer Tufty had a wifi capability as well: my fault.

For the badger, instead, wouldn’t it be easier (and less power consuming) to connect the badger to an external server? So to gather updated information and act accordingly? Using the RTC it should be possible to exit from sleep and check some data every N seconds/minutes and act accordingly.
For serious usages it would be even so good to be able to play a sound when an action received: would it be hard to include a little beeper?

For instance, a potential use case: I’m in a team with other people (each one with the badge W) and some actions or awards/score might be updated to every badge, notifying the person with a sound when this happens…

Yes, connecting to a server would be far better for battery life. But it complicates things because you would have three players: the iphone would update the server, the badger would poll the server. The iphone would need a gui, the badger some sort of api.

Adding a little beeper depends on your soldering skills. The badger2040w has no soldering pads, so you would have to solder directly to the pico.

1 Like

I’ve been trying to set up a webserver on my Badger W but it falls over when I try to add the socket. Code works perfectly on a Pico W.

Badger W - cannot open socket - Support - Pimoroni Buccaneers

I could do with some help.

1 Like

Got the original request working.

I have a Samsung phone, not an iPhone, but this is working for me. At the moment there are only 3 jpg images but it should be easily expandable. Create your jpgs as outlined in the Badger W guide using Gimp and no progression.

On the phone you probably need to expand the size of the web page to access the buttons.

# Use a mobile phone to change the pictures on a Badger 2040 W
# Tony Goodhew 28th Feb 2023

import network
import socket
from time import sleep
import machine
import secrets

import badger2040w
import jpegdec

pix =["M Duck2.jpg","Sqrl2.jpg","badgerpunk.jpg"]

badger = badger2040w.Badger2040W()
jpeg = jpegdec.JPEG(badger.display)
count = 0

def show_jpg(pix,i):
    jpeg.open_file("/images/" +pix[i])
    jpeg.decode(0, 0)
    badger.update()
    
def connect():
    #Connect to WLAN
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect(secrets.ssid, secrets.password)
    while wlan.isconnected() == False:
        print('Waiting for connection...')
        sleep(1)        
    ip = wlan.ifconfig()[0]
    print(f'Connected on {ip}')
    return ip

def open_socket(ip):
    # Open a socket
    address = (ip, 80)
    connection = socket.socket()
    connection.bind(address)
    connection.listen(1)
    return connection

def webpage(count):
    #Template HTML

    html = f"""
            <!DOCTYPE html>
            <html>
            <form action="./UP">
            <input type="submit" value="UP" />
            </form>
            <form action="./DOWN">
            <input type="submit" value="DOWN" />
            </form>
            <p>Count : {str(count)}</p>
            </body>
            </html>
            """
    return str(html)

def serve(connection):
    #Start a web server
    global count

    while True:
        client = connection.accept()[0]
        request = client.recv(1024)
        request = str(request)
        try:
            request = request.split()[1]
        except IndexError:
            pass
        if request == '/UP?':
            count = count + 1
            if count > 2:
                count = 2
            show_jpg(pix,count)
        elif request =='/DOWN?':
            count = count - 1
            if count < 0:
                count = 0
            show_jpg(pix,count)
        html = webpage(count)
        print(count)                 # Why does this print twice in Shell?
        client.send(html)
        client.close()

try:
    ip = connect()
    connection = open_socket(ip)
    serve(connection)
except KeyboardInterrupt:
    machine.reset()

@jlm70 I hope this gives your students some ideas.

I do not understand why the single print(count) statement produces 2 lines on the Shell ???

Files

One observation about the change from .bin image files to .jpg on the Badger W. The Sqrl2.bin is only 5K but the .jpg is 17.8K. I think the bin one on the Badger looks better than the jpg on Badger W.

2 Likes

Just seen yr kind reply. Super thanks! I’ll look it asap!
Very very kind :)