Explorar o código

Add Python Library

jblanked hai 1 ano
pai
achega
70bcd7014c
Modificáronse 2 ficheiros con 259 adicións e 1 borrados
  1. 16 1
      assets/FlipperHTTP/README.md
  2. 243 0
      assets/FlipperHTTP/flipper_http.py

+ 16 - 1
assets/FlipperHTTP/README.md

@@ -45,7 +45,7 @@ Star the repository (https://github.com/jblanked/WebCrawler-FlipperZero) and fol
 | `flipper_http_delete_request_with_headers`          | `bool`           | `const char *url`, `const char *headers`, `const char *payload`                                                 | Sends a DELETE request with custom headers and a payload to the specified URL.                        |
 | `flipper_http_save_received_data`                | `bool`           | `size_t bytes_received`, `const char line_buffer[]`                                                            | Saves the received data to the SD card, with the specified size and buffer.                        |
 
-`In C, fhttp.received_data holds the received data from HTTP requests. In JavaScript, the response is returned directly from the function.`
+`In C, fhttp.received_data holds the received data from HTTP requests. In JavaScript and mPython, the response is returned directly from the function.`
 
 ## Usage in `JavaScript` (flipper_http.js):
 | **Function Name**                      | **Return Value** | **Parameters**                                       | **Description**                                                                                      |
@@ -63,3 +63,18 @@ Star the repository (https://github.com/jblanked/WebCrawler-FlipperZero) and fol
 | `fhttp.post_request_with_headers`      | `string`         | `url: string`, `headers: string`, `payload: string` | Sends a POST request with headers and payload, and returns the response of the response.       |
 | `fhttp.put_request_with_headers`       | `string`         | `url: string`, `headers: string`, `payload: string` | Sends a PUT request with headers and payload, and returns the response of the response.        |
 | `fhttp.delete_request_with_headers`       | `string`         | `url: string`, `headers: string`, `payload: string` | Sends a PUT request with headers and payload, and returns the response of the response.        |
+
+## Usage in `Python` (flipper_http.py):
+| **Function Name**                      | **Return Value** | **Parameters**                                       | **Description**                                                                                      |
+|----------------------------------------|------------------|-----------------------------------------------------|------------------------------------------------------------------------------------------------------|
+| `flipper_http_connect_wifi`                   | `bool`           | None                                                | Sends a command to connect to WiFi and returns whether the connection was successful.                 |
+| `flipper_http_disconnect_wifi`                | `bool`           | None                                                | Sends a command to disconnect from WiFi and returns whether the disconnection was successful.          |
+| `flipper_http_ping`                           | `bool`           | None                                                | Sends a ping request to test connectivity and returns whether a response was received.                |
+| `flipper_http_save_wifi`                      | `bool`           | `ssid: str`, `password: str`                  | Saves WiFi credentials and returns whether the save operation was successful.                         |
+| `flipper_http_send_data`                      | `void`           | `data: str`                                      | Sends the specified data to the serial port.                                                          |
+| `flipper_http_read_data`                      | `str`         | `sleep_ms: int`                                  | Reads data from the serial port with a specified delay and returns the response received.      |
+| `flipper_http_get_request`                    | `str`         | `url: str`                                       | Sends a GET request to the specified URL and returns the response of the response.             |
+| `flipper_http_get_request_with_headers`       | `str`         | `url: str`, `headers: str`                    | Sends a GET request with headers and returns the response of the response.                    |
+| `flipper_http_post_request_with_headers`      | `str`         | `url: str`, `headers: str`, `data: str` | Sends a POST request with headers and data, and returns the response of the response.       |
+| `flipper_http_put_request_with_headers`       | `str`         | `url: str`, `headers: str`, `data: str` | Sends a PUT request with headers and data, and returns the response of the response.        |
+| `flipper_http_delete_request_with_headers`       | `str`         | `url: str`, `headers: str`, `data: str` | Sends a PUT request with headers and data, and returns the response of the response.        |

+ 243 - 0
assets/FlipperHTTP/flipper_http.py

@@ -0,0 +1,243 @@
+"""
+This is only available in uPython in version 1.5.0. and above: https://ofabel.github.io/mp-flipper/reference.html#classes
+Author - JBlanked
+For use with Flipper Zero and the FlipperHTTP flash: https://github.com/jblanked/WebCrawler-FlipperZero/tree/main/assets/FlipperHTTP
+Individual functions to save memory (uPython has already pushed the limits)
+Lastly, be careful looping in the requests, you'll get a furi check error. I'm assuming it's a memory issue and will be fixed in future updates.
+"""
+
+import time
+import flipperzero as f0
+
+
+def flipper_print(*args, sep=" ", end="\n", clear_screen=True, y_start=0):
+    """Print text to the Flipper Zero screen since print() doesn't show on the screen"""
+    # Combine all arguments into a single string, manually joining without using splitlines
+    text = sep.join(map(str, args)) + end
+
+    # Optionally clear the screen before printing
+    if clear_screen:
+        f0.canvas_clear()
+        f0.canvas_update()  # Ensure the screen is cleared
+
+    # Manually handle line breaks by splitting on "\n"
+    lines = []
+    current_line = ""
+    for char in text:
+        if char == "\n":
+            lines.append(current_line)
+            current_line = ""
+        else:
+            current_line += char
+
+    lines.append(current_line)  # Add the last line
+
+    # Display each line on the screen
+    y_position = y_start
+    for line in lines:
+        f0.canvas_set_text(0, y_position, line)  # Display each line
+        y_position += 10  # Adjust line spacing
+
+    # Update the display to show the text
+    f0.canvas_update()
+
+
+def flipper_http_read_data(sleep_ms: int = 100) -> str:
+    """Read data from the Flipper Zero UART"""
+    with f0.uart_open(f0.UART_MODE_USART, 115200) as uart:
+        raw_data = uart.readline()
+        i = 0
+        while len(raw_data) == 0 and i < 5:
+            raw_data = uart.readline()
+
+            if len(raw_data) > 0:
+                data = raw_data.decode()
+                # flipper_print(data)
+                return data
+
+            i += 1
+
+            time.sleep_ms(sleep_ms)
+
+        return None
+
+
+def flipper_http_send_data(value: str):
+    """Send data to the Flipper Zero UART"""
+    if value is None:
+        return
+    with f0.uart_open(f0.UART_MODE_USART, 115200) as uart:
+        uart.write(value.encode())
+        uart.flush()
+
+
+def clear_buffer():
+    """Clear the buffer of the Flipper Zero UART"""
+    data = flipper_http_read_data()
+    while data is not None:
+        data = flipper_http_read_data()
+
+
+def flipper_http_ping() -> bool:
+    """Ping the WiFi Devboard to check if it is connected"""
+    flipper_http_send_data("[PING]")
+    data = flipper_http_read_data()
+    clear_buffer()
+    return "[PONG]" in data
+
+
+def flipper_http_connect_wifi() -> bool:
+    """Connect to WiFi"""
+    flipper_http_send_data("[WIFI/CONNECT]")
+    data = flipper_http_read_data()
+    clear_buffer()
+    # had to write it this way due to mPython limitations
+    if "[SUCCESS]" in data:
+        return True
+    if "[CONNECTED]" in data:
+        return True
+    elif "[INFO]" in data:
+        return True
+    return False
+
+
+def flipper_http_disconnect_wifi() -> bool:
+    """Disconnect from WiFi"""
+    flipper_http_send_data("[WIFI/DISCONNECT]")
+    data = flipper_http_read_data()
+    clear_buffer()
+    if "[DISCONNECTED]" in data:
+        return True
+    if "WiFi stop" in data:
+        return True
+    return False
+
+
+def flipper_http_save_wifi(ssid, password) -> bool:
+    """Save WiFi credentials"""
+    if ssid is None or password is None:
+        return False
+    flipper_http_send_data(
+        '[WIFI/SAVE]{"ssid":"' + ssid + '","password":"' + password + '"}'
+    )
+    data = flipper_http_read_data()
+    clear_buffer()
+    return "[SUCCESS]" in data
+
+
+def flipper_http_get_request(url) -> str:
+    """Send a GET request to the specified URL"""
+    if url is None:
+        return ""
+    flipper_http_send_data("[GET]" + url)
+    if "[GET/SUCCESS]" in flipper_http_read_data():
+        data = flipper_http_read_data()
+        clear_buffer()
+        return data
+    clear_buffer()
+    return ""
+
+
+def flipper_http_get_request_with_headers(url, headers) -> str:
+    """Send a GET request to the specified URL with headers"""
+    if url is None:
+        return ""
+    flipper_http_send_data('[GET/HTTP]{url:"' + url + '",headers:' + headers + "}")
+    if "[GET/SUCCESS]" in flipper_http_read_data():
+        data = flipper_http_read_data(500)
+        clear_buffer()
+        return data
+    clear_buffer()
+    return ""
+
+
+def flipper_http_post_request_with_headers(url, headers, data):
+    """Send a POST request to the specified URL with headers and data"""
+    if url is None:
+        return ""
+    flipper_http_send_data(
+        '[POST/HTTP]{"url":"'
+        + url
+        + '","headers":'
+        + headers
+        + ',"payload":'
+        + data
+        + "}"
+    )
+    if "[POST/SUCCESS]" in flipper_http_read_data():
+        data = flipper_http_read_data(500)
+        clear_buffer()
+        return data
+    clear_buffer()
+    return ""
+
+
+def flipper_http_put_request_with_headers(url, headers, data):
+    """Send a PUT request to the specified URL with headers and data"""
+    if url is None:
+        return ""
+    flipper_http_send_data(
+        '[PUT/HTTP]{"url":"'
+        + url
+        + '","headers":'
+        + headers
+        + ',"payload":'
+        + data
+        + "}"
+    )
+    if "[PUT/SUCCESS]" in flipper_http_read_data():
+        data = flipper_http_read_data(500)
+        clear_buffer()
+        return data
+    clear_buffer()
+    return ""
+
+
+def flipper_http_delete_request_with_headers(url, headers, data):
+    """Send a DELETE request to the specified URL with headers and data"""
+    if url is None:
+        return ""
+    flipper_http_send_data(
+        '[DELETE/HTTP]{"url":"'
+        + url
+        + '","headers":'
+        + headers
+        + ',"payload":'
+        + data
+        + "}"
+    )
+    if "[DELETE/SUCCESS]" in flipper_http_read_data():
+        data = flipper_http_read_data(500)
+        clear_buffer()
+        return data
+    clear_buffer()
+    return ""
+
+
+# Example of how to use the functions
+""" uncomment to run the example
+flipper_print("Starting HTTP example")
+clear_buffer()  # Clear the buffer before starting
+time.sleep(1)
+if flipper_http_ping() and flipper_http_save_wifi("JBlanked", "maingirl"):
+    flipper_print("WiFi saved successfully!")
+    time.sleep(2)
+    if flipper_http_connect_wifi():
+        flipper_print("WiFi connected successfully!")
+        time.sleep(2)
+        flipper_print(
+            flipper_http_get_request_with_headers(
+                "https://httpbin.org/get", "{Content-Type: application/json}"
+            )
+        )
+        time.sleep(2)
+
+        if flipper_http_disconnect_wifi():
+            flipper_print("WiFi disconnected successfully!")
+            time.sleep(2)
+else:
+    flipper_print("Failed to save WiFi credentials")
+    time.sleep(2)
+    flipper_print("Exiting...")
+    time.sleep(2)
+"""  # uncomment to run the example