Explorar el Código

Add [WIFI/IP] command

jblanked hace 1 año
padre
commit
16418b844b

+ 37 - 3
assets/FlipperHTTP/Arduino/FlipperHTTP.h

@@ -3,7 +3,7 @@ Author: JBlanked
 Github: https://github.com/jblanked/WebCrawler-FlipperZero/tree/main/assets/FlipperHTTP
 Github: https://github.com/jblanked/WebCrawler-FlipperZero/tree/main/assets/FlipperHTTP
 Info: This library is a wrapper around the HTTPClient library and is used to communicate with the FlipperZero over serial.
 Info: This library is a wrapper around the HTTPClient library and is used to communicate with the FlipperZero over serial.
 Created: 2024-09-30
 Created: 2024-09-30
-Updated: 2024-10-17
+Updated: 2024-10-19
 
 
 Change Log:
 Change Log:
 - 2024-09-30: Initial commit
 - 2024-09-30: Initial commit
@@ -12,6 +12,7 @@ Change Log:
 .
 .
 - 2024-10-16: Fixed typos and added [GET/BYTES], [POST/BYTES], and [WIFI/SACN] commands
 - 2024-10-16: Fixed typos and added [GET/BYTES], [POST/BYTES], and [WIFI/SACN] commands
 - 2024-10-17: Added [LIST], [REBOOT], [PARSE], [PARSE/ARRAY], [LED/ON], and [LED/OFF], and [IP/ADDRESS] commands
 - 2024-10-17: Added [LIST], [REBOOT], [PARSE], [PARSE/ARRAY], [LED/ON], and [LED/OFF], and [IP/ADDRESS] commands
+- 2024-10-19: Added [WIFI/IP] command
 */
 */
 
 
 #include <WiFi.h>
 #include <WiFi.h>
@@ -730,6 +731,7 @@ bool FlipperHTTP::post_bytes_to_file(String url, String payload, const char *hea
     return false;
     return false;
 }
 }
 
 
+// Main loop for flipper-http.ino that handles all of the commands
 void FlipperHTTP::loop()
 void FlipperHTTP::loop()
 {
 {
     // Check if there's incoming serial data
     // Check if there's incoming serial data
@@ -749,7 +751,7 @@ void FlipperHTTP::loop()
         // print the available commands
         // print the available commands
         if (_data.startsWith("[LIST]"))
         if (_data.startsWith("[LIST]"))
         {
         {
-            Serial.println("[LIST],[PING], [REBOOT], [WIFI/SCAN], [WIFI/SAVE], [WIFI/CONNECT], [WIFI/DISCONNECT], [GET], [GET/HTTP], [POST/HTTP], [PUT/HTTP], [DELETE/HTTP], [GET/BYTES], [POST/BYTES], [PARSE], [PARSE/ARRAY], [LED/ON], [LED/OFF], [IP/ADDRESS]");
+            Serial.println("[LIST],[PING], [REBOOT], [WIFI/IP], [WIFI/SCAN], [WIFI/SAVE], [WIFI/CONNECT], [WIFI/DISCONNECT], [GET], [GET/HTTP], [POST/HTTP], [PUT/HTTP], [DELETE/HTTP], [GET/BYTES], [POST/BYTES], [PARSE], [PARSE/ARRAY], [LED/ON], [LED/OFF], [IP/ADDRESS]");
         }
         }
         // handle [LED/ON] command
         // handle [LED/ON] command
         else if (_data.startsWith("[LED/ON]"))
         else if (_data.startsWith("[LED/ON]"))
@@ -761,11 +763,43 @@ void FlipperHTTP::loop()
         {
         {
             this->useLED = false;
             this->useLED = false;
         }
         }
-        // handle [IP/ADDRESS] command
+        // handle [IP/ADDRESS] command (local IP)
         else if (_data.startsWith("[IP/ADDRESS]"))
         else if (_data.startsWith("[IP/ADDRESS]"))
         {
         {
             Serial.println(this->getIPAddress());
             Serial.println(this->getIPAddress());
         }
         }
+        // handle [WIFI/IP] command ip of connected wifi
+        else if (_data.startsWith("[WIFI/IP]"))
+        {
+            if (!this->isConnectedToWifi() && !this->connectToWifi())
+            {
+                Serial.println("[ERROR] Not connected to Wifi. Failed to reconnect.");
+                this->ledOff();
+                return;
+            }
+            // Get Request
+            String jsonData = this->get("https://httpbin.org/get");
+            if (jsonData == "")
+            {
+                Serial.println("[ERROR] GET request failed or returned empty data.");
+                return;
+            }
+            DynamicJsonDocument doc(1024);
+            DeserializationError error = deserializeJson(doc, jsonData);
+            if (error)
+            {
+                Serial.print("[ERROR] Failed to parse JSON.");
+                this->ledOff();
+                return;
+            }
+            if (!doc.containsKey("origin"))
+            {
+                Serial.println("[ERROR] JSON does not contain origin.");
+                this->ledOff();
+                return;
+            }
+            Serial.println(doc["origin"].as<String>());
+        }
         // Ping/Pong to see if board/flipper is connected
         // Ping/Pong to see if board/flipper is connected
         else if (_data.startsWith("[PING]"))
         else if (_data.startsWith("[PING]"))
         {
         {

+ 1 - 1
assets/FlipperHTTP/Arduino/flipper-http.ino

@@ -3,7 +3,7 @@ Author: JBlanked
 Github: https://github.com/jblanked/WebCrawler-FlipperZero/tree/main/assets/FlipperHTTP
 Github: https://github.com/jblanked/WebCrawler-FlipperZero/tree/main/assets/FlipperHTTP
 Info: This library is a wrapper around the HTTPClient library and is used to communicate with the FlipperZero over serial.
 Info: This library is a wrapper around the HTTPClient library and is used to communicate with the FlipperZero over serial.
 Created: 2024-09-30
 Created: 2024-09-30
-Updated: 2024-10-17
+Updated: 2024-10-19
 */
 */
 
 
 #include <FlipperHTTP.h>
 #include <FlipperHTTP.h>

+ 24 - 4
assets/FlipperHTTP/flipper_http.h

@@ -33,6 +33,7 @@ bool flipper_http_disconnect_wifi();
 bool flipper_http_ping();
 bool flipper_http_ping();
 bool flipper_http_scan_wifi();
 bool flipper_http_scan_wifi();
 bool flipper_http_save_wifi(const char *ssid, const char *password);
 bool flipper_http_save_wifi(const char *ssid, const char *password);
+bool flipper_http_ip_wifi();
 bool flipper_http_ip_address();
 bool flipper_http_ip_address();
 //---
 //---
 bool flipper_http_list_commands();
 bool flipper_http_list_commands();
@@ -456,7 +457,7 @@ bool flipper_http_list_commands()
 
 
 // Function to turn on the LED
 // Function to turn on the LED
 /**
 /**
- * @brief      Send a command to turn on the LED.
+ * @brief      Allow the LED to display while processing.
  * @return     true if the request was successful, false otherwise.
  * @return     true if the request was successful, false otherwise.
  * @note       The received data will be handled asynchronously via the callback.
  * @note       The received data will be handled asynchronously via the callback.
  */
  */
@@ -475,7 +476,7 @@ bool flipper_http_led_on()
 
 
 // Function to turn off the LED
 // Function to turn off the LED
 /**
 /**
- * @brief      Send a command to turn off the LED.
+ * @brief      Disable the LED from displaying while processing.
  * @return     true if the request was successful, false otherwise.
  * @return     true if the request was successful, false otherwise.
  * @note       The received data will be handled asynchronously via the callback.
  * @note       The received data will be handled asynchronously via the callback.
  */
  */
@@ -611,9 +612,9 @@ bool flipper_http_save_wifi(const char *ssid, const char *password)
     return true;
     return true;
 }
 }
 
 
-// Function to get IP address
+// Function to get IP address of WiFi Devboard
 /**
 /**
- * @brief      Send a command to get the IP address.
+ * @brief      Send a command to get the IP address of the WiFi Devboard
  * @return     true if the request was successful, false otherwise.
  * @return     true if the request was successful, false otherwise.
  * @note       The received data will be handled asynchronously via the callback.
  * @note       The received data will be handled asynchronously via the callback.
  */
  */
@@ -630,6 +631,25 @@ bool flipper_http_ip_address()
     return true;
     return true;
 }
 }
 
 
+// Function to get IP address of the connected WiFi network
+/**
+ * @brief      Send a command to get the IP address of the connected WiFi network.
+ * @return     true if the request was successful, false otherwise.
+ * @note       The received data will be handled asynchronously via the callback.
+ */
+bool flipper_http_ip_wifi()
+{
+    const char *command = "[WIFI/IP]";
+    if (!flipper_http_send_data(command))
+    {
+        FURI_LOG_E("FlipperHTTP", "Failed to send WiFi IP command.");
+        return false;
+    }
+
+    // The response will be handled asynchronously via the callback
+    return true;
+}
+
 // Function to disconnect from WiFi (returns true if successful)
 // Function to disconnect from WiFi (returns true if successful)
 /**
 /**
  * @brief      Send a command to disconnect from WiFi.
  * @brief      Send a command to disconnect from WiFi.

+ 12 - 3
assets/FlipperHTTP/flipper_http.js

@@ -93,11 +93,11 @@ let fhttp = {
         }
         }
         return this.to_string(response);
         return this.to_string(response);
     },
     },
-    // turn on the LED
+    // Allow the LED to display while processing
     led_on: function () {
     led_on: function () {
         serial.write("[LED/ON]");
         serial.write("[LED/ON]");
     },
     },
-    // turn off the LED
+    // Disable the LED from displaying while processing
     led_off: function () {
     led_off: function () {
         serial.write("[LED/OFF]");
         serial.write("[LED/OFF]");
     },
     },
@@ -152,7 +152,7 @@ let fhttp = {
             return false;
             return false;
         }
         }
     },
     },
-    // get IP address
+    // Get the IP address of the WiFi Devboard
     ip_address: function () {
     ip_address: function () {
         serial.write("[IP/ADDRESS]");
         serial.write("[IP/ADDRESS]");
         let response = this.read_data(500);
         let response = this.read_data(500);
@@ -161,6 +161,15 @@ let fhttp = {
         }
         }
         return this.to_string(response);
         return this.to_string(response);
     },
     },
+    // Get the IP address of the connected WiFi network
+    ip_wifi: function () {
+        serial.write("[WIFI/IP]");
+        let response = this.read_data(500);
+        if (response === undefined) {
+            return "";
+        }
+        return this.to_string(response);
+    },
     // Send a GET request to the board
     // Send a GET request to the board
     // I reduced this to return the first line of the response that isnt undefined
     // I reduced this to return the first line of the response that isnt undefined
     // You'll also get 'out of memory' errors if you try to read/return too much data
     // You'll also get 'out of memory' errors if you try to read/return too much data

+ 10 - 2
assets/FlipperHTTP/flipper_http.py

@@ -122,12 +122,12 @@ def flipper_http_list_commands() -> str:
 
 
 
 
 def flipper_http_led_on():
 def flipper_http_led_on():
-    """Turn on the LED"""
+    """Allow the LED to display while processing"""
     flipper_http_send_data("[LED/ON]")
     flipper_http_send_data("[LED/ON]")
 
 
 
 
 def flipper_http_led_off():
 def flipper_http_led_off():
-    """Turn off the LED"""
+    """Disable the LED from displaying while processing"""
     flipper_http_send_data("[LED/OFF]")
     flipper_http_send_data("[LED/OFF]")
 
 
 
 
@@ -184,6 +184,14 @@ def flipper_http_ip_address() -> str:
     return data
     return data
 
 
 
 
+def flipper_http_ip_wifi() -> str:
+    """Get the IP address of the connected WiFi network"""
+    flipper_http_send_data("[WIFI/IP]")
+    data = flipper_http_read_data()
+    clear_buffer()
+    return data
+
+
 def flipper_http_get_request(url: str) -> str:
 def flipper_http_get_request(url: str) -> str:
     """Send a GET request to the specified URL"""
     """Send a GET request to the specified URL"""
     if url is None:
     if url is None:

BIN
assets/FlipperHTTP/flipper_http_bootloader.bin


BIN
assets/FlipperHTTP/flipper_http_firmware_a.bin


BIN
assets/FlipperHTTP/flipper_http_partitions.bin