jblanked 1 год назад
Родитель
Сommit
41d9da89ac

+ 127 - 101
assets/FlipperHTTP/Arduino/FlipperHTTP.h

@@ -30,8 +30,20 @@ public:
     }
 
     // Main methods for flipper-http.ino
-    void setup();
     void loop();
+    void setup()
+    {
+        Serial.begin(115200);
+        // Initialize SPIFFS
+        if (!SPIFFS.begin(true))
+        {
+            Serial.println("[ERROR] SPIFFS initialization failed.");
+            ESP.restart();
+        }
+
+        this->ledStart();
+        Serial.flush();
+    }
 
     // HTTP Methods
     String get(String url);
@@ -44,99 +56,10 @@ public:
     String delete_request(String url, String payload, const char *headerKeys[], const char *headerValues[], int headerSize);
 
     // stream data as bytes
-    bool get_bytes_to_file(String url, const char *headerKeys[], const char *headerValues[], int headerSize)
-    {
-        WiFiClientSecure client;
-        client.setInsecure(); // Bypass certificate
-
-        HTTPClient http;
-
-        File file = SPIFFS.open("/test.txt", FILE_WRITE);
-        if (!file)
-        {
-            Serial.println("[ERROR] Failed to open file for writing.");
-            return false;
-        }
-
-        http.collectHeaders(headerKeys, headerSize);
-
-        if (http.begin(client, url))
-        {
-
-            for (int i = 0; i < headerSize; i++)
-            {
-                http.addHeader(headerKeys[i], headerValues[i]);
-            }
-
-            int httpCode = http.GET();
-
-            if (httpCode > 0)
-            {
-                Serial.println("[GET/SUCCESS] GET request successful.");
-                http.writeToStream(&file);
-                file.close();
-                return true;
-            }
-            else
-            {
-                Serial.print("[ERROR] GET Request Failed, error: ");
-                Serial.println(http.errorToString(httpCode).c_str());
-            }
-            http.end();
-        }
-        else
-        {
-            Serial.println("[ERROR] Unable to connect to the server.");
-        }
-        return false;
-    }
-    bool post_bytes_to_file(String url, String payload, const char *headerKeys[], const char *headerValues[], int headerSize)
-    {
-        WiFiClientSecure client;
-        client.setInsecure(); // Bypass certificate
-
-        HTTPClient http;
-
-        File file = SPIFFS.open("/test.txt", FILE_WRITE);
-        if (!file)
-        {
-            Serial.println("[ERROR] Failed to open file for writing.");
-            return false;
-        }
-
-        http.collectHeaders(headerKeys, headerSize);
-
-        if (http.begin(client, url))
-        {
-
-            for (int i = 0; i < headerSize; i++)
-            {
-                http.addHeader(headerKeys[i], headerValues[i]);
-            }
-
-            int httpCode = http.POST(payload);
-
-            if (httpCode > 0)
-            {
-                Serial.println("[POST/SUCCESS] POST request successful.");
-                http.writeToStream(&file);
-                file.close();
-                return true;
-            }
-            else
-            {
-                Serial.print("[ERROR] POST Request Failed, error: ");
-                Serial.println(http.errorToString(httpCode).c_str());
-            }
-            http.end();
-        }
-        else
-        {
-            Serial.println("[ERROR] Unable to connect to the server.");
-        }
-        return false;
-    }
+    bool get_bytes_to_file(String url, const char *headerKeys[], const char *headerValues[], int headerSize);
+    bool post_bytes_to_file(String url, String payload, const char *headerKeys[], const char *headerValues[], int headerSize);
 
+    // send bytes from file to serial
     void print_bytes_file()
     {
         File file = SPIFFS.open("/test.txt", FILE_READ);
@@ -159,6 +82,23 @@ public:
     bool saveWifiSettings(String data);
     bool loadWifiSettings();
 
+    // returns a string of all wifi networks
+    String scanWifiNetworks()
+    {
+        int n = WiFi.scanNetworks();
+        String networks = "";
+        for (int i = 0; i < n; ++i)
+        {
+            networks += WiFi.SSID(i);
+
+            if (i < n - 1)
+            {
+                networks += ", ";
+            }
+        }
+        return networks;
+    }
+
     // Connect to Wifi using the loaded SSID and Password
     bool connectToWifi();
 
@@ -678,18 +618,98 @@ String FlipperHTTP::put(String url, String payload)
     return response;
 }
 
-void FlipperHTTP::setup()
+bool FlipperHTTP::get_bytes_to_file(String url, const char *headerKeys[], const char *headerValues[], int headerSize)
+{
+    WiFiClientSecure client;
+    client.setInsecure(); // Bypass certificate
+
+    HTTPClient http;
+
+    File file = SPIFFS.open("/test.txt", FILE_WRITE);
+    if (!file)
+    {
+        Serial.println("[ERROR] Failed to open file for writing.");
+        return false;
+    }
+
+    http.collectHeaders(headerKeys, headerSize);
+
+    if (http.begin(client, url))
+    {
+
+        for (int i = 0; i < headerSize; i++)
+        {
+            http.addHeader(headerKeys[i], headerValues[i]);
+        }
+
+        int httpCode = http.GET();
+
+        if (httpCode > 0)
+        {
+            Serial.println("[GET/SUCCESS] GET request successful.");
+            http.writeToStream(&file);
+            file.close();
+            return true;
+        }
+        else
+        {
+            Serial.print("[ERROR] GET Request Failed, error: ");
+            Serial.println(http.errorToString(httpCode).c_str());
+        }
+        http.end();
+    }
+    else
+    {
+        Serial.println("[ERROR] Unable to connect to the server.");
+    }
+    return false;
+}
+
+bool FlipperHTTP::post_bytes_to_file(String url, String payload, const char *headerKeys[], const char *headerValues[], int headerSize)
 {
-    Serial.begin(115200);
-    // Initialize SPIFFS
-    if (!SPIFFS.begin(true))
+    WiFiClientSecure client;
+    client.setInsecure(); // Bypass certificate
+
+    HTTPClient http;
+
+    File file = SPIFFS.open("/test.txt", FILE_WRITE);
+    if (!file)
     {
-        Serial.println("[ERROR] SPIFFS initialization failed.");
-        ESP.restart();
+        Serial.println("[ERROR] Failed to open file for writing.");
+        return false;
     }
 
-    this->ledStart();
-    Serial.flush();
+    http.collectHeaders(headerKeys, headerSize);
+
+    if (http.begin(client, url))
+    {
+
+        for (int i = 0; i < headerSize; i++)
+        {
+            http.addHeader(headerKeys[i], headerValues[i]);
+        }
+
+        int httpCode = http.POST(payload);
+
+        if (httpCode > 0)
+        {
+            Serial.println("[POST/SUCCESS] POST request successful.");
+            http.writeToStream(&file);
+            file.close();
+            return true;
+        }
+        else
+        {
+            Serial.print("[ERROR] POST Request Failed, error: ");
+            Serial.println(http.errorToString(httpCode).c_str());
+        }
+        http.end();
+    }
+    else
+    {
+        Serial.println("[ERROR] Unable to connect to the server.");
+    }
+    return false;
 }
 
 void FlipperHTTP::loop()
@@ -713,6 +733,12 @@ void FlipperHTTP::loop()
         {
             Serial.println("[PONG]");
         }
+        else if (_data.startsWith("[WIFI/SCAN]"))
+        {
+            Serial.println(this->scanWifiNetworks());
+            Serial.flush();
+            Serial.println();
+        }
         // Handle [WIFI/SAVE] command
         else if (_data.startsWith("[WIFI/SAVE]"))
         {

+ 21 - 2
assets/FlipperHTTP/flipper_http.h

@@ -31,6 +31,7 @@ bool flipper_http_send_data(const char *data);
 bool flipper_http_connect_wifi();
 bool flipper_http_disconnect_wifi();
 bool flipper_http_ping();
+bool flipper_http_scan_wifi();
 bool flipper_http_save_wifi(const char *ssid, const char *password);
 //---
 bool flipper_http_get_request(const char *url);
@@ -407,9 +408,8 @@ bool flipper_http_send_data(const char *data)
 
 // Function to send a PING request
 /**
- * @brief      Send a GET request to the specified URL.
+ * @brief      Send a PING request to check if the Wifi Dev Board is connected.
  * @return     true if the request was successful, false otherwise.
- * @param      url  The URL to send the GET request to.
  * @note       The received data will be handled asynchronously via the callback.
  * @note       This is best used to check if the Wifi Dev Board is connected.
  * @note       The state will remain INACTIVE until a PONG is received.
@@ -428,6 +428,25 @@ bool flipper_http_ping()
     return true;
 }
 
+// Function to scan for WiFi networks
+/**
+ * @brief      Send a command to scan for WiFi networks.
+ * @return     true if the request was successful, false otherwise.
+ * @note       The received data will be handled asynchronously via the callback.
+ */
+bool flipper_http_scan_wifi()
+{
+    const char *command = "[WIFI/SCAN]";
+    if (!flipper_http_send_data(command))
+    {
+        FURI_LOG_E("FlipperHTTP", "Failed to send WiFi scan command.");
+        return false;
+    }
+
+    // The response will be handled asynchronously via the callback
+    return true;
+}
+
 // Function to save WiFi settings (returns true if successful)
 /**
  * @brief      Send a command to save WiFi settings.

+ 9 - 0
assets/FlipperHTTP/flipper_http.js

@@ -84,6 +84,15 @@ let fhttp = {
         this.clear_buffer(true); // Clear the buffer
         return this.includes(this.to_string(response), "[PONG]");
     },
+    // Get Wifi network list
+    scan_wifi: function () {
+        serial.write("[WIFI/SCAN]");
+        let response = this.read_data(500);
+        if (response === undefined) {
+            return "";
+        }
+        return this.to_string(response);
+    },
     // Save wifi settings
     save_wifi: function (ssid, password) {
         if (ssid === "" || password === "") {

+ 8 - 0
assets/FlipperHTTP/flipper_http.py

@@ -113,6 +113,14 @@ def flipper_http_disconnect_wifi() -> bool:
     return False
 
 
+def flipper_http_scan_wifi() -> str:
+    """Scan for WiFi networks"""
+    flipper_http_send_data("[WIFI/SCAN]")
+    data = flipper_http_read_data(500)
+    clear_buffer()
+    return data
+
+
 def flipper_http_save_wifi(ssid, password) -> bool:
     """Save WiFi credentials"""
     if ssid is None or password is None:

BIN
assets/FlipperHTTP/flipper_http_bootloader.bin


BIN
assets/FlipperHTTP/flipper_http_firmware_a.bin


BIN
assets/FlipperHTTP/flipper_http_partitions.bin


+ 21 - 2
flipper_http.h

@@ -31,6 +31,7 @@ bool flipper_http_send_data(const char *data);
 bool flipper_http_connect_wifi();
 bool flipper_http_disconnect_wifi();
 bool flipper_http_ping();
+bool flipper_http_scan_wifi();
 bool flipper_http_save_wifi(const char *ssid, const char *password);
 //---
 bool flipper_http_get_request(const char *url);
@@ -411,9 +412,8 @@ bool flipper_http_send_data(const char *data)
 
 // Function to send a PING request
 /**
- * @brief      Send a GET request to the specified URL.
+ * @brief      Send a PING request to check if the Wifi Dev Board is connected.
  * @return     true if the request was successful, false otherwise.
- * @param      url  The URL to send the GET request to.
  * @note       The received data will be handled asynchronously via the callback.
  * @note       This is best used to check if the Wifi Dev Board is connected.
  * @note       The state will remain INACTIVE until a PONG is received.
@@ -432,6 +432,25 @@ bool flipper_http_ping()
     return true;
 }
 
+// Function to scan for WiFi networks
+/**
+ * @brief      Send a command to scan for WiFi networks.
+ * @return     true if the request was successful, false otherwise.
+ * @note       The received data will be handled asynchronously via the callback.
+ */
+bool flipper_http_scan_wifi()
+{
+    const char *command = "[WIFI/SCAN]";
+    if (!flipper_http_send_data(command))
+    {
+        FURI_LOG_E("FlipperHTTP", "Failed to send WiFi scan command.");
+        return false;
+    }
+
+    // The response will be handled asynchronously via the callback
+    return true;
+}
+
 // Function to save WiFi settings (returns true if successful)
 /**
  * @brief      Send a command to save WiFi settings.