seeedfarming: fix the session and example scripts - brcon2025-hackathons - Bitreichcon 2025 Hackathons
 (HTM) git clone git://bitreich.org/brcon2025-hackathons git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/brcon2025-hackathons
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Tags
       ---
 (DIR) commit 644c58d480adfced1f4d60c64b133816d60d6a42
 (DIR) parent a1d6045c7ac689f84ec6c48d9eecb89a384609a0
 (HTM) Author: Josuah Demangeon <me@josuah.net>
       Date:   Fri,  1 Aug 2025 10:14:52 +0200
       
       seeedfarming: fix the session and example scripts
       
       Diffstat:
         M seeedfarming/example.py             |       9 ++++-----
         M seeedfarming/run_on_cluster.sh      |       9 +++++++--
         M seeedfarming/session.sh             |      34 ++++++++++++++++---------------
         M seeedfarming/windowshell.py         |      27 ++++++++++++++-------------
       
       4 files changed, 43 insertions(+), 36 deletions(-)
       ---
 (DIR) diff --git a/seeedfarming/example.py b/seeedfarming/example.py
       @@ -3,19 +3,18 @@
        #
        # This is an example script that you can load into MicroPython prompt to
        # connect yourself to the WiFi access point.
       +#
       +
        import network
        import socket
       -import time
        
        wlan = network.WLAN()
        wlan.active(True)
       -wlan.scan()
       +print(wlan.scan())
        wlan.connect('seeeder', 'seeedrooots')
        
       -# Then wait until internet is up
        while not wlan.isconnected():
       -    print('.', end='')
       -print(' connected')
       +        print('.', end='')
        
        myip = wlan.ipconfig('addr4')[0]
        print(myip)
 (DIR) diff --git a/seeedfarming/run_on_cluster.sh b/seeedfarming/run_on_cluster.sh
       @@ -8,5 +8,10 @@ fi
        
        pyscript="$1"
        
       -cat "$pyscript" | ...
       -
       +{
       +        # Automatically select a board to run the script on and load it
       +        echo a
       +        echo l
       +        cat "$pyscript"
       +        echo .
       +} | nc -v -w 10000 bitreich.org 7000
 (DIR) diff --git a/seeedfarming/session.sh b/seeedfarming/session.sh
       @@ -1,6 +1,6 @@
        #!/bin/sh
        
       -SEEED_SCRIPTS=/tmp/seeed_scripts
       +SEEED_SCRIPTS=/var/tmp/seeed_scripts
        
        echo "Welcome to the MicroPython Seeed farm!"
        
       @@ -8,6 +8,9 @@ mkdir -p "$SEEED_SCRIPTS"
        
        num=0
        
       +# Show the errors on the network directly instead of hiding them
       +exec 2>&1
       +
        while true; do
                echo "What action do you want to run?"
                for tty in /dev/ttyACM*; do
       @@ -20,36 +23,34 @@ while true; do
                                echo " [$i]  select $tty (busy)"
                        else
                                echo " [$i]  select $tty (free)"
       +                        if [ "$free" = "" ]; then free=$i; fi
                        fi
                done
       +        echo " [a]  Auto-select a free board (for automation)"
                echo " [l]  Load a new script instead of the current one (if any)"
                echo " [p]  Get a python prompt on the current script"
                echo " [r]  Reset the board and remove the script"
       -        echo " [v]  View the script "
       -        echo -n "ttyACM$num>>> "
       +        echo " [v]  View the script"
       +        echo -n "seeedcluster[$num]>>> "
                read action || exit
        
       +        date +"%Y-%m-%d %H:%M:%S action '$action' on /dev/ttyACM$num" >>$SEEED_SCRIPTS/log.txt
       +
                case $action in
                ([0-9])
                        num=$action
       -                script=$SEEED_SCRIPTS/ttyACM$num.py
       +                ;;
       +        (a)
       +                num=$free
                        ;;
                (l)
                        echo "Paste your script and finish it by a dot character ('.') alone on its own line:"
       -                rm -f "$script"
       -                while read line; do
       -                        if [ "$line" = "." ]; then break; fi
       -                        echo "$line" >>$script
       -                        echo "$line"
       -                done
       +                script=$SEEED_SCRIPTS/ttyACM$num.py
       +                sed '/^\.$/ { d; q; }' >$script
       +                cp "$script" "$SEEED_SCRIPTS/archive_$(date +%Y_%m%d_%H%M)_ttyACM$num.py"
        
                        # Set the terminal in "paste" mode and load the script
       -                picocom --quiet --baud="115200" --initstring="$reset" "/dev/ttyACM$num" <$script
       -
       -                # Set the terminal back to "normal" mode and show the output of the script
       -                reset=$(printf '\x03''import machine; machine.reset()\r''\x05')
       -                done=$(printf '\x04\r')
       -                tr '\n' '\r' | picocom --quiet --baud="115200" --initstring="$done" "/dev/ttyACM$num"
       +                mpremote connect "/dev/ttyACM$num" soft-reset run "$script"
                        ;;
                (p)
                        # Just connect without resetting
       @@ -60,6 +61,7 @@ while true; do
                        ;;
                (r)
                        # Just connect without resetting or sending a script to see the ongoing execution
       +                script=$SEEED_SCRIPTS/ttyACM$num.py
                        reset=$(printf '\x03''import machine; machine.reset()\r')
                        picocom --quiet --baud="115200" --initstring="$reset" --exit-after="500" "/dev/ttyACM$num"
                        rm -f "$script"
 (DIR) diff --git a/seeedfarming/windowshell.py b/seeedfarming/windowshell.py
       @@ -6,7 +6,6 @@
        
        import network
        import socket
       -import time
        
        wlan = network.WLAN()
        wlan.active(True)
       @@ -15,6 +14,7 @@ wlan.connect('seeeder', 'seeedrooots')
        
        while not wlan.isconnected():
                print('.', end='')
       +
        print(' connected')
        
        myip = wlan.ipconfig('addr4')[0]
       @@ -26,32 +26,33 @@ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.bind((myip, myport))
        s.listen(9)
        
       -def gopher_info(line):
       +def gopher_info(conn, line):
                line = line.replace("\t", "    ")
       -        print("i%s\t\t\t\t\r\n" % (line))
       +        s = "i%s\t\t\t\t\r\n" % (line)
       +        conn.write(s.encode())
        
       -def gopher_print(text):
       +def gopher_print(conn, text):
                for line in text.split("\n"):
       -                gopher_info(line)
       +                gopher_info(conn, line)
        
       -def gopher_link(baseselector, newdir):
       +def gopher_link(conn, baseselector, newdir):
                newdir = newdir.replace("\t", "    ")
       -        printf("1%s\t%s/%s\t%s\t%s\r\n" % (newdir, baseselector, newdir,
       -                                    myserver, myport))
       +        s = "1%s\t%s/%s\t%s\t%s\r\n" % (newdir, baseselector, newdir, myserver, myport)
       +        conn.write(s.encode())
        
        endserial="66666666666666666"
        
        while True:
                conn, addr = s.accept()
                print('Connected with %s:%s' % (addr[0], addr[1]))
       -        selector = conn.read()
       +        selector = conn.read().decode()
       +        print('read %s from client\n' % selector)
                selectorpath = selector.split("/")
                serialnumber = "".join(selectorpath)
                if serialnumber == endserial:
       -                gopher_print("You found the right serial number!\nHave a nice day!\n")
       +                gopher_print(conn, "You found the right serial number!\nHave a nice day!\n")
                        print("%s:%s found the exit from windows hell!" % (addr[0], addr[1]))
                else:
       -                gopher_print("Welcome to Windows Hell!\nTry to find the Serial Number to exit one by one!\n Har Har Har Har ....\n")
       +                gopher_print(conn, "Welcome to Windows Hell!\nTry to find the Serial Number to exit one by one!\n Har Har Har Har ....\n")
                        for number in range(0, 9):
       -                        gopher_link(selector, number)
       -
       +                        gopher_link(conn, selector, number)