Add pico 2026 update
Vibecoded: - User binary input of the user id - confirms with send button - User input of the rating - Send button sends a get request to the server
This commit is contained in:
9
pico_2026/README.md
Normal file
9
pico_2026/README.md
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# Rate music for Raspberry Pi Pico
|
||||||
|
|
||||||
|
This is the MicroPython project to connect the Raspberry Pi Pico to the rate_music database via HTTP.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
1. Follow the [instructions](https://www.raspberrypi.com/documentation/microcontrollers/micropython.html) to install MicroPython on the Raspberry Pi Pico.
|
||||||
|
2. Install [rshell](https://github.com/dhylands/rshell).
|
||||||
|
3. Modify `SSID` and `Password` in the `main.py` file.
|
||||||
|
4. Copy the files `main.py, rate.py, wifi_client.py` to the Pi Pico.
|
||||||
12
pico_2026/delete_folder.py
Normal file
12
pico_2026/delete_folder.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
def delete_folder(path):
|
||||||
|
for file in os.listdir(path):
|
||||||
|
full_path = path + "/" + file
|
||||||
|
try:
|
||||||
|
os.remove(full_path) # Datei löschen
|
||||||
|
except OSError:
|
||||||
|
delete_folder(full_path) # Falls es ein Ordner ist, rekursiv löschen
|
||||||
|
os.rmdir(path) # Ordner selbst löschen
|
||||||
|
|
||||||
|
delete_folder("/mfrc522") # Beispiel: Löscht den gesamten MFRC522-Ordner
|
||||||
25
pico_2026/main.py
Normal file
25
pico_2026/main.py
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import wifi_client
|
||||||
|
import rate
|
||||||
|
|
||||||
|
|
||||||
|
# set SSID and PASSWORD variables to the configurations of the laptop access point
|
||||||
|
SSID = 'Oger-fi'
|
||||||
|
PASSWORD = 'Karlfreitag!'
|
||||||
|
SERVER_PORT = 3000
|
||||||
|
SERVER_IP_LOWER = 100
|
||||||
|
SERVER_IP_UPPER = 102
|
||||||
|
|
||||||
|
# connect to laptop hotspot
|
||||||
|
wifi_client.wifi_connect(SSID, PASSWORD)
|
||||||
|
wifi_client.search_server(SERVER_IP_LOWER, SERVER_IP_UPPER, SERVER_PORT)
|
||||||
|
#wifi_client.send_request(42)
|
||||||
|
|
||||||
|
|
||||||
|
# define Pins 0-4 for rate buttons (5 buttons)
|
||||||
|
rate_button_pins:list = [0, 1, 2, 3, 4]
|
||||||
|
send_button_pin:int = 6
|
||||||
|
rate.start_rating(rate_button_pins, send_button_pin)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
109
pico_2026/rate.py
Normal file
109
pico_2026/rate.py
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
from machine import Pin
|
||||||
|
import wifi_client
|
||||||
|
import time
|
||||||
|
|
||||||
|
# State Constants
|
||||||
|
STATE_USER_ID = 0
|
||||||
|
STATE_RATING = 1
|
||||||
|
|
||||||
|
# Global variables
|
||||||
|
CURRENT_STATE = STATE_USER_ID
|
||||||
|
USER_ID = 0
|
||||||
|
RATING = 0
|
||||||
|
BUTTON_SEND = None
|
||||||
|
RATE_BUTTONS = []
|
||||||
|
|
||||||
|
# initialize a specific Pin as Input with a pull-down resistor
|
||||||
|
def initialize_pin(pin_number:int):
|
||||||
|
button = Pin(pin_number, Pin.IN, Pin.PULL_DOWN)
|
||||||
|
return button
|
||||||
|
|
||||||
|
# activates send pin
|
||||||
|
def activate_sendPin() -> None:
|
||||||
|
BUTTON_SEND.irq(trigger=Pin.IRQ_RISING, handler=sendButton_pressed)
|
||||||
|
|
||||||
|
# deactivates send pin
|
||||||
|
def deactivate_sendPin() -> None:
|
||||||
|
BUTTON_SEND.irq(handler=None)
|
||||||
|
|
||||||
|
# rate button is pressed
|
||||||
|
def rate_button_pressed(pin:Pin) -> None:
|
||||||
|
global USER_ID, RATING, CURRENT_STATE, RATE_BUTTONS
|
||||||
|
|
||||||
|
# Find which button index was pressed
|
||||||
|
button_index = -1
|
||||||
|
for i, button in enumerate(RATE_BUTTONS):
|
||||||
|
if button == pin:
|
||||||
|
button_index = i
|
||||||
|
break
|
||||||
|
|
||||||
|
if button_index == -1:
|
||||||
|
return
|
||||||
|
|
||||||
|
if CURRENT_STATE == STATE_USER_ID:
|
||||||
|
# Binary input: each button toggles a bit
|
||||||
|
# Assume rightmost (highest index) is bit 0
|
||||||
|
bit_pos = len(RATE_BUTTONS) - 1 - button_index
|
||||||
|
USER_ID ^= (1 << bit_pos)
|
||||||
|
print(f"Current User ID: {USER_ID} (binary: {bin(USER_ID)})")
|
||||||
|
|
||||||
|
elif CURRENT_STATE == STATE_RATING:
|
||||||
|
# Rating selection (1 to 5/6)
|
||||||
|
# Assuming the button index + 1 is the rating
|
||||||
|
RATING = button_index + 1
|
||||||
|
print(f"Current Rating selected: {RATING}")
|
||||||
|
|
||||||
|
# Activate send pin to confirm selection
|
||||||
|
activate_sendPin()
|
||||||
|
|
||||||
|
# Send/Confirm button is pressed
|
||||||
|
def sendButton_pressed(pin) -> None:
|
||||||
|
global CURRENT_STATE, USER_ID, RATING
|
||||||
|
# deactivate send pin to prevent multiple sends
|
||||||
|
deactivate_sendPin()
|
||||||
|
|
||||||
|
if CURRENT_STATE == STATE_USER_ID:
|
||||||
|
print(f"Confirmed User ID: {USER_ID}")
|
||||||
|
CURRENT_STATE = STATE_RATING
|
||||||
|
print("Please select rating (1-5) and press send.")
|
||||||
|
|
||||||
|
elif CURRENT_STATE == STATE_RATING:
|
||||||
|
if RATING == 0:
|
||||||
|
print("Please select a rating first!")
|
||||||
|
activate_sendPin()
|
||||||
|
return
|
||||||
|
|
||||||
|
print(f"Confirmed Rating: {RATING}")
|
||||||
|
print(f"Sending request: User {USER_ID}, Rating {RATING}")
|
||||||
|
|
||||||
|
# send a request to the access point
|
||||||
|
try:
|
||||||
|
wifi_client.send_request(USER_ID, RATING)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error sending request: {e}")
|
||||||
|
|
||||||
|
# Reset for next rating
|
||||||
|
USER_ID = 0
|
||||||
|
RATING = 0
|
||||||
|
CURRENT_STATE = STATE_USER_ID
|
||||||
|
print("Done. Ready for next User ID input.")
|
||||||
|
|
||||||
|
# start the rating program
|
||||||
|
def start_rating(rate_button_pins:list, button_send_pin:int) -> None:
|
||||||
|
global RATE_BUTTONS, BUTTON_SEND
|
||||||
|
|
||||||
|
# initialize all pins as buttons and add them to the list
|
||||||
|
RATE_BUTTONS = []
|
||||||
|
for pin_num in rate_button_pins:
|
||||||
|
button = initialize_pin(pin_num)
|
||||||
|
button.irq(trigger=Pin.IRQ_RISING, handler=rate_button_pressed)
|
||||||
|
RATE_BUTTONS.append(button)
|
||||||
|
|
||||||
|
# initialize send button
|
||||||
|
BUTTON_SEND = initialize_pin(button_send_pin)
|
||||||
|
|
||||||
|
print(f"System started. Pins: {rate_button_pins}, Send: {button_send_pin}")
|
||||||
|
print("Please enter User ID (binary) and press send.")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
time.sleep(1)
|
||||||
13
pico_2026/test_webserver.py
Normal file
13
pico_2026/test_webserver.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
from flask import Flask, request
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
# Endpoint for receiving ratings
|
||||||
|
@app.route('/ratings/<int:user_id>/<int:rating>', methods=['GET'])
|
||||||
|
def receive_rating(user_id: int, rating: int):
|
||||||
|
print(f"Received rating: User {user_id} -> {rating}")
|
||||||
|
return f"Received rating {rating} for user {user_id}!", 200
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
# Listen on all interfaces, port 3000 as defined in main.py
|
||||||
|
app.run(host="0.0.0.0", port=3000)
|
||||||
82
pico_2026/wifi_client.py
Normal file
82
pico_2026/wifi_client.py
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
import network
|
||||||
|
import urequests
|
||||||
|
import time
|
||||||
|
import usocket
|
||||||
|
import socket
|
||||||
|
import ssl
|
||||||
|
|
||||||
|
# global variable for the IP-Adress of the laptop hotspot
|
||||||
|
IP_ADRESS_SERVER:str = '192.168.0.100' #None
|
||||||
|
IP_ADRESS_ACCESSPOINT = None
|
||||||
|
SERVER_PORT:int = 3000 #None
|
||||||
|
|
||||||
|
# configures the raspberry pi as wifi-client and connects it to an access point
|
||||||
|
def wifi_connect(SSID:str, PASSWORD:str) -> None:
|
||||||
|
|
||||||
|
# configure raspberry pi as client
|
||||||
|
wifi = network.WLAN(network.STA_IF)
|
||||||
|
# activate client
|
||||||
|
wifi.active(True)
|
||||||
|
# connect to laptop hotspot
|
||||||
|
wifi.connect(SSID, PASSWORD)
|
||||||
|
|
||||||
|
# wait until it has connected
|
||||||
|
while not wifi.isconnected():
|
||||||
|
print("Connect to", SSID, "...")
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
# connected to laptop hotspot
|
||||||
|
print("Connected to", SSID)
|
||||||
|
# geat and print IP-adress of raspberry
|
||||||
|
ip_adress_pico = wifi.ifconfig()[0]
|
||||||
|
print("IP-Adresse Pico:", ip_adress_pico)
|
||||||
|
# get ip-adress of access point and store it in the global variable
|
||||||
|
global IP_ADRESS_ACCESSPOINT
|
||||||
|
IP_ADRESS_ACCESSPOINT = wifi.ifconfig()[2]
|
||||||
|
print("IP-adress access point:", IP_ADRESS_ACCESSPOINT)
|
||||||
|
print()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# send a HTTP-request to the access point with the rate value
|
||||||
|
def send_request(user_id:int, rating:int) -> None:
|
||||||
|
|
||||||
|
# create HTTP-URL
|
||||||
|
URL = f"http://{IP_ADRESS_SERVER}:{str(SERVER_PORT)}/ratings/{user_id}/{rating}"
|
||||||
|
print(URL)
|
||||||
|
|
||||||
|
# create request
|
||||||
|
response = urequests.get(URL)
|
||||||
|
|
||||||
|
# print HTTP-answer of the access point
|
||||||
|
print("Server response:")
|
||||||
|
print(response.text)
|
||||||
|
print()
|
||||||
|
response.close()
|
||||||
|
|
||||||
|
|
||||||
|
# test, if there can be a connection to the given ip adress and port
|
||||||
|
def ping_ip(ip:str, port:int) -> bool:
|
||||||
|
s = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
|
||||||
|
s.settimeout(1)
|
||||||
|
try:
|
||||||
|
s.connect((ip, port))
|
||||||
|
print(f"{ip} reachable")
|
||||||
|
s.close()
|
||||||
|
return True
|
||||||
|
except:
|
||||||
|
print(f"{ip} not reachable")
|
||||||
|
s.close()
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
# search for the server in the lokal network
|
||||||
|
# in the ip range from lower_limit to upper_limit
|
||||||
|
def search_server(lower_limit:int, upper_limit:int, port:int) -> str:
|
||||||
|
global IP_ADRESS_SERVER
|
||||||
|
for i in range(lower_limit, upper_limit):
|
||||||
|
ip = IP_ADRESS_ACCESSPOINT[:-1] + str(i)
|
||||||
|
if ping_ip(ip, port):
|
||||||
|
print(f"Server found unter: {ip}:{port}")
|
||||||
|
IP_ADRESS_SERVER = ip
|
||||||
|
return ip
|
||||||
Reference in New Issue
Block a user