Badger W - cannot open socket

I am trying to move a program that works on a Pico W to my Badger W but it falls over when I try to open a socket.

>>> %Run -c $EDITOR_CONTENT
Client True 192.168.0.15
None
Traceback (most recent call last):
  File "<stdin>", line 83, in <module>
  File "<stdin>", line 33, in open_socket
TypeError: can't convert 'NoneType' object to str implicitly
>>> 

Here is my code:

import badger2040w
badger = badger2040w.Badger2040W()
from badger2040w import WIDTH
import machine
from urllib import urequest
import gc
import qrcode
import badger_os
import network_manager
import socket
from time import sleep


ssid = 'bbbbbbbbb'
password = 'bbbbbbbb'
count = 0

def connect():
    #Connect to WLAN
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect(ssid, 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 > 3: count = 3
        elif request =='/DOWN?':
            count = count - 1
            if count < 0: count = 0
        html = webpage(count)
        client.send(html)
        client.close()

try:
    ip = badger.connect()
    print(ip)
    connection = open_socket(ip)   # Fails here
    serve(connection)
except KeyboardInterrupt:
    machine.reset()

I would be grateful for some help.

The code should increment and decrement the counter by pressing a button on a phone. The working Pico W code is here:

# WORKING - Pico W

import network
import socket
from time import sleep

import machine

ssid = 'sssssssss'
password = 'ppppppppp'
count = 0

def connect():
    #Connect to WLAN
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect(ssid, 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 > 3: count = 3
        elif request =='/DOWN?':
            count = count - 1
            if count < 0: count = 0
        html = webpage(count)
        client.send(html)
        client.close()

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

All Iā€™m trying to do is set up the Badger W as a very simple webserver. The code works on a Pico W but not on a Badger W. What am I missing?

Eventually got it working by ignoring that it was running on a Badger for the webserver. I then just added the minimum Badger bits to do the screen. You can see the code by following the link above.

1 Like