From 3a9413ebd66c514508b20e4c73147b62b12ed35c Mon Sep 17 00:00:00 2001 From: structix Date: Thu, 17 Apr 2025 20:48:12 +0200 Subject: [PATCH] Add Raspberry Pi Pico project --- pico/README.md | 9 +++++ pico/delete_folder.py | 12 ++++++ pico/main.py | 23 ++++++++++++ pico/rate.py | 79 +++++++++++++++++++++++++++++++++++++++ pico/test_webserver.py | 16 ++++++++ pico/wifi_client.py | 84 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 223 insertions(+) create mode 100644 pico/README.md create mode 100644 pico/delete_folder.py create mode 100644 pico/main.py create mode 100644 pico/rate.py create mode 100644 pico/test_webserver.py create mode 100644 pico/wifi_client.py diff --git a/pico/README.md b/pico/README.md new file mode 100644 index 0000000..c47fc08 --- /dev/null +++ b/pico/README.md @@ -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. diff --git a/pico/delete_folder.py b/pico/delete_folder.py new file mode 100644 index 0000000..ceaa6f3 --- /dev/null +++ b/pico/delete_folder.py @@ -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 diff --git a/pico/main.py b/pico/main.py new file mode 100644 index 0000000..481e50e --- /dev/null +++ b/pico/main.py @@ -0,0 +1,23 @@ +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 + +# connect to laptop hotspot +wifi_client.wifi_connect(SSID, PASSWORD) +wifi_client.search_server(100,200, SERVER_PORT) +wifi_client.send_request(42) + + +# define Pins 0-5 for rate buttons +rate_button_pins:list = [0, 1, 2, 3, 4, 5] +send_button_pin:int = 6 +rate.start_rating(rate_button_pins, send_button_pin) + + + + diff --git a/pico/rate.py b/pico/rate.py new file mode 100644 index 0000000..0988621 --- /dev/null +++ b/pico/rate.py @@ -0,0 +1,79 @@ +from machine import Pin +import wifi_client +import time + + + +RATE_VALUE = 0 +BUTTON_SEND = None +RATE_BUTTONS = None + +# initialize a specific Pin as Input with an 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 RATE_VALUE, RATE_BUTTONS, BUTTON_SEND + + # get pin id + pin_id = str(pin).split('(')[1].split(',')[0] + pin_id = pin_id[len(pin_id)-1] + + RATE_VALUE = pin_id + print(f"Song is rated with: {RATE_VALUE}") + + # activate send pin, when rate pin was selected + activate_sendPin() + + + +# Reset button ist pressed +def sendButton_pressed(pin) -> None: + global RATE_VALUE, BUTTON_SEND + # deactivate send pin to prevent multiple sends + deactivate_sendPin() + print('send button pressed') + + # send a request to the access point with the selected rate value + wifi_client.send_request(RATE_VALUE) + + + +# start the rating program +# initialize it with the pin list for the rate_buttoms and the pin for the send button +def start_rating(rate_button_pins:list, button_send_pin:int) -> None: + + global RATE_BUTTONS, BUTTON_SEND + + # create list for all rate buttons + RATE_BUTTONS = [None] * len(rate_button_pins) + + # initialize all pins as buttons and add them to the rate_button list + for pin in rate_button_pins: + button = initialize_pin(pin) + button.irq(trigger=Pin.IRQ_RISING, handler=rate_button_pressed) + RATE_BUTTONS[pin] = button + + # define pin 6 as send button + # not activate it, to not be able to send before a rate pin is selected + BUTTON_SEND = initialize_pin(button_send_pin) + + print(RATE_BUTTONS) + print(BUTTON_SEND) + + while True: + pass diff --git a/pico/test_webserver.py b/pico/test_webserver.py new file mode 100644 index 0000000..b9ec5da --- /dev/null +++ b/pico/test_webserver.py @@ -0,0 +1,16 @@ +from flask import Flask, request + +app = Flask(__name__) + +# Endpoint zum Empfangen der Zahl +@app.route('/send_number', methods=['GET']) +def receive_number(): + rate_value = request.args.get('number') # Zahl aus der Anfrage holen + if rate_value: + print(f"Empfangene Zahl: {rate_value}") + return f"Recieved rate value {rate_value} !", 200 + else: + return "Keine Zahl gesendet", 400 + +if __name__ == '__main__': + app.run(host="0.0.0.0", port=8080) # Server auf allen IP-Adressen und Port 8080 diff --git a/pico/wifi_client.py b/pico/wifi_client.py new file mode 100644 index 0000000..2f2ae75 --- /dev/null +++ b/pico/wifi_client.py @@ -0,0 +1,84 @@ +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(rate_value:int) -> None: + + # create HTTP-URL + URL = f"http://{IP_ADRESS_SERVER}:{str(SERVER_PORT)}/rating/{rate_value}" + print(URL) + + # create request + response = urequests.get(URL) + + # print HTTP-answer of the access point + print("Server response:") + print(response.text) # Die Antwort des Servers anzeigen + 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 + +