Advertisement
pleasedontcode

**WiFi Connection** rev_01

Apr 22nd, 2025
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **WiFi Connection**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-04-22 18:11:00
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Create a firmware that function as a MAC sniffer */
  21.     /* and post data to an api */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <WiFi.h>
  28. #include <WiFiClient.h>
  29. #include <HTTPClient.h>
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void startWiFi();
  35. void sniffMACAddresses();
  36. void postDataToAPI(const String &macAddress);
  37.  
  38. /****** GLOBAL VARIABLES *****/
  39. const char* ssid = "your_SSID"; // Replace with your WiFi SSID
  40. const char* password = "your_PASSWORD"; // Replace with your WiFi password
  41. const char* apiEndpoint = "http://your_api_endpoint"; // Replace with your API endpoint
  42.  
  43. void setup(void)
  44. {
  45.     Serial.begin(115200);
  46.     startWiFi();
  47. }
  48.  
  49. void loop(void)
  50. {
  51.     sniffMACAddresses();
  52.     delay(10000); // Delay between scans
  53. }
  54.  
  55. void startWiFi() {
  56.     // Connect to WiFi
  57.     Serial.print("Connecting to WiFi...");
  58.     WiFi.begin(ssid, password);
  59.     while (WiFi.status() != WL_CONNECTED) {
  60.         delay(1000);
  61.         Serial.print(".");
  62.     }
  63.     Serial.println("Connected to WiFi");
  64. }
  65.  
  66. void sniffMACAddresses() {
  67.     // Scan for available networks
  68.     int n = WiFi.scanNetworks();
  69.     Serial.println("Scan complete, found networks:");
  70.     for (int i = 0; i < n; ++i) {
  71.         String macAddress = WiFi.BSSIDstr(i); // Get MAC address of the network
  72.         Serial.print("Network: ");
  73.         Serial.print(WiFi.SSID(i));
  74.         Serial.print(" | MAC: ");
  75.         Serial.println(macAddress);
  76.        
  77.         // Post MAC address to API
  78.         postDataToAPI(macAddress);
  79.     }
  80. }
  81.  
  82. void postDataToAPI(const String &macAddress) {
  83.     if (WiFi.status() == WL_CONNECTED) {
  84.         HTTPClient http;
  85.         http.begin(apiEndpoint); // Specify the URL
  86.         http.addHeader("Content-Type", "application/json"); // Specify content-type header
  87.  
  88.         // Create JSON payload
  89.         String payload = "{\"macAddress\":\"" + macAddress + "\"}";
  90.  
  91.         int httpResponseCode = http.POST(payload); // Send the request
  92.         if (httpResponseCode > 0) {
  93.             String response = http.getString(); // Get response
  94.             Serial.println(httpResponseCode); // Print return code
  95.             Serial.println(response); // Print response
  96.         } else {
  97.             Serial.print("Error on sending POST: ");
  98.             Serial.println(httpResponseCode);
  99.         }
  100.         http.end(); // Free resources
  101.     } else {
  102.         Serial.println("WiFi not connected");
  103.     }
  104. }
  105.  
  106. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement