CCCPaste

Spectrum Scanner rev_02

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: Spectrum Scanner
	- Source Code compiled for: ESP32 DevKit V1
	- Source Code created on: 2025-09-30 03:58:58

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Enciende una frecuencia que afecte las señales */
	/* bluetooth y wifi */
/****** END SYSTEM REQUIREMENTS *****/




/* START CODE */

/****** DEFINITION OF LIBRARIES *****/
#include 

/*
Incompatibilities with USER CODE:
- The USER CODE attempted to jam WiFi by sending deauthentication frames and manipulating channels. This is illegal in many jurisdictions and unsafe. Those lines are commented out below.
- Some ESP-IDF style calls (e.g., esp_wifi_init, esp_wifi_set_country, esp_wifi_set_channel) rely on ESP-IDF APIs and may not be compatible when using the Arduino core. They are commented out with explanations.
*/

 
/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void analyze_rf_environment(void);

void setup(void)
{
	// put your setup code here, to run once:
	Serial.begin(115200);
	delay(100);
	Serial.println("Safe WiFi monitor initialization with RF spectrum monitoring (passive).");

	// Initialize WiFi in station mode and disconnect any previous connections
	WiFi.mode(WIFI_STA);
	WiFi.disconnect(true);

	// Initialize a simple LED indicator for RF activity (built-in LED on most ESP32 boards)
	const int ledPin = 2; // Built-in LED GPIO; adjust if needed for your board
	pinMode(ledPin, OUTPUT);
	digitalWrite(ledPin, LOW);

	Serial.println("Starting safe WiFi scan for monitoring only.");
}

void analyze_rf_environment(void)
{
	// Passive RF spectrum analysis by scanning nearby networks
	static unsigned long lastScan = 0;
	const unsigned long scanInterval = 5000; // 5 seconds
	unsigned long now = millis();
	if (now - lastScan >= scanInterval) {
		lastScan = now;
		int n = WiFi.scanNetworks();
		if (n <= 0) {
			const int ledPin = 2;
			digitalWrite(ledPin, LOW);
			return;
		}

		long sum_rssi = 0;
		for (int i = 0; i < n; ++i) {
			sum_rssi += WiFi.RSSI(i);
		}
		int avg_rssi = (int)(sum_rssi / n);

		// If average RSSI is relatively high (strong signals on average), indicate busy spectrum
		// Typical RSSI values: -30 very strong, -90 weak
		const int ledPin = 2;
		if (avg_rssi > -65) {
			// Busy spectrum, turn the indicator ON
			digitalWrite(ledPin, HIGH);
		} else {
			// Spectrum looks quieter, turn the indicator OFF
			digitalWrite(ledPin, LOW);
		}

		Serial.print("RF Spectrum avg RSSI: ");
		Serial.println(avg_rssi);
		WiFi.scanDelete();
	}
}

void loop(void)
{
	// Passive monitoring tasks
	analyze_rf_environment();

	// Existing safe monitoring: perform a periodic WiFi scan and print results
	static unsigned long lastPrint = 0;
	const unsigned long printInterval = 5000; // 5 seconds
	unsigned long now = millis();
	if (now - lastPrint >= printInterval) {
		lastPrint = now;
		int n = WiFi.scanNetworks();
		Serial.print("Networks found: ");
		Serial.println(n);
		for (int i = 0; i < n; ++i) {
			Serial.print("  SSID: ");
			Serial.print(WiFi.SSID(i));
			Serial.print("  RSSI: ");
			Serial.println(WiFi.RSSI(i));
		}
		WiFi.scanDelete();
	}
}

/* END CODE */