// sketch_nov1a_modified.ino
#include <Arduino.h>
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include <WiFi.h>
// --- CHANGE 1: Define the channel you want to send on ---
#define PACKET_CHANNEL 6 // Set your desired Wi-Fi channel (1-13)
// Forward-declare the ROM function with C linkage (we provide a stub in c_hooks.c)
extern "C" esp_err_t esp_rom_phy_disable_cca(void);
static const char *TAG = "RAW_TX"; // kept for reference but not used for logging
void printHexMac(const uint8_t *mac) {
for (int i = 0; i < 6; ++i) {
if (mac[i] < 0x10) Serial.print('0');
Serial.print(mac[i], HEX);
if (i < 5) Serial.print(':');
}
}
void raw_packet_injection_task(void *pvParameters) {
(void)pvParameters;
uint8_t raw_frame[] = {
0x48, 0x00, // Frame Control (Data, Null)
0x00, 0x00, // Duration/ID
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, // Addr1: Broadcast
0x11,0x22,0x33,0x44,0x55,0x66, // Addr2: placeholder (will be replaced with real MAC)
0x11,0x22,0x33,0x44,0x55,0x66, // Addr3: placeholder (will be replaced with real MAC)
0x00,0x00 // Sequence Control
};
uint8_t mac[6];
if (esp_wifi_get_mac(WIFI_IF_STA, mac) == ESP_OK) {
// copy MAC to Addr2 and Addr3 fields in the 802.11 header
memcpy(&raw_frame[10], mac, 6); // Addr2
memcpy(&raw_frame[16], mac, 6); // Addr3
Serial.print("raw_task: using MAC: ");
printHexMac(mac);
Serial.println();
} else {
Serial.println("raw_task: Could not read MAC address; using placeholder.");
}
int packet_len = sizeof(raw_frame);
uint16_t *seq_ctrl = (uint16_t *)&raw_frame[22];
// Convert sequence control to host order printing convenience (it is two bytes)
uint16_t seq_num = 0;
Serial.println("raw_task: entering main loop. Sending every 4000 ms.");
while (1) {
// Read current sequence control (for logging). Note: seq_ctrl stored in little-endian order on ESP.
seq_num = *seq_ctrl;
unsigned long t_before = millis();
esp_err_t err = esp_wifi_80211_tx(WIFI_IF_STA, raw_frame, packet_len, true);
unsigned long t_after = millis();
if (err == ESP_OK) {
Serial.print("TX OK | len=");
Serial.print(packet_len);
Serial.print(" | seq=");
Serial.print(seq_num);
Serial.print(" | channel=");
Serial.print(PACKET_CHANNEL);
Serial.print(" | time(ms)=");
Serial.print(t_after);
Serial.print(" | duration(ms)=");
Serial.println(t_after - t_before);
} else {
Serial.print("TX FAIL | err=");
Serial.print((int)err);
Serial.print(" | len=");
Serial.print(packet_len);
Serial.print(" | seq=");
Serial.print(seq_num);
Serial.print(" | time(ms)=");
Serial.println(t_after);
}
// increment sequence control (wraps naturally)
(*seq_ctrl)++;
// Extra: print a short hex dump on every 10th packet for visibility (optional)
static int counter = 0;
counter++;
if ((counter % 10) == 0) {
Serial.print("raw_task: sample packet (hex): ");
for (int i = 0; i < packet_len; ++i) {
if (raw_frame[i] < 0x10) Serial.print('0');
Serial.print(raw_frame[i], HEX);
Serial.print(' ');
}
Serial.println();
}
// --- CHANGE 3: Wait 4 seconds (4000ms) before sending next packet ---
vTaskDelay(pdMS_TO_TICKS(4000));
}
}
void setup(void)
{
Serial.begin(115200);
delay(100); // allow Serial to initialize
Serial.println();
Serial.println("=== RAW TX SKETCH START ===");
Serial.print("Build time: ");
//Serial.print(_DATE_);
Serial.print(" ");
//Serial.println(_TIME_);
esp_err_t err;
err = nvs_flash_init();
if (err == ESP_OK) {
Serial.println("nvs_flash_init() OK");
} else {
Serial.print("nvs_flash_init() failed err=");
Serial.println((int)err);
}
err = esp_netif_init();
if (err == ESP_OK) {
Serial.println("esp_netif_init() OK");
} else {
Serial.print("esp_netif_init() failed err=");
Serial.println((int)err);
}
err = esp_event_loop_create_default();
if (err == ESP_OK) {
Serial.println("esp_event_loop_create_default() OK");
} else {
Serial.print("esp_event_loop_create_default() failed err=");
Serial.println((int)err);
}
esp_netif_create_default_wifi_sta();
Serial.println("esp_netif_create_default_wifi_sta() called");
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
err = esp_wifi_init(&cfg);
if (err == ESP_OK) {
Serial.println("esp_wifi_init() OK");
} else {
Serial.print("esp_wifi_init() failed err=");
Serial.println((int)err);
}
// store WiFi config in RAM (no flash)
err = esp_wifi_set_storage(WIFI_STORAGE_RAM);
if (err == ESP_OK) {
Serial.println("esp_wifi_set_storage(RAM) OK");
} else {
Serial.print("esp_wifi_set_storage() failed err=");
Serial.println((int)err);
}
err = esp_wifi_set_mode(WIFI_MODE_STA);
if (err == ESP_OK) {
Serial.println("esp_wifi_set_mode(STA) OK");
} else {
Serial.print("esp_wifi_set_mode() failed err=");
Serial.println((int)err);
}
err = esp_wifi_start();
if (err == ESP_OK) {
Serial.println("esp_wifi_start() OK");
} else {
Serial.print("esp_wifi_start() failed err=");
Serial.println((int)err);
}
// --- CHANGE 4: Set the Wi-Fi channel ---
Serial.print("Setting Wi-Fi channel to ");
Serial.println(PACKET_CHANNEL);
err = esp_wifi_set_channel(PACKET_CHANNEL, WIFI_SECOND_CHAN_NONE);
if (err == ESP_OK) {
Serial.print("esp_wifi_set_channel(");
Serial.print(PACKET_CHANNEL);
Serial.println(") OK");
} else {
Serial.print("esp_wifi_set_channel() failed err=");
Serial.println((int)err);
}
// Try to disable CCA — our stub returns ESP_ERR_NOT_SUPPORTED on cores
// where the real ROM function isn't available.
esp_err_t cca = esp_rom_phy_disable_cca();
if (cca == ESP_OK) {
Serial.println("CCA SUCCESSFULLY DISABLED! Low-latency transmission mode active.");
} else if (cca == ESP_ERR_NOT_SUPPORTED) {
Serial.println("esp_rom_phy_disable_cca() not supported on this target; CCA remains enabled.");
} else {
Serial.print("esp_rom_phy_disable_cca() returned err=");
Serial.println((int)cca);
}
// Print the station MAC address
uint8_t mac[6];
if (esp_wifi_get_mac(WIFI_IF_STA, mac) == ESP_OK) {
Serial.print("Station MAC: ");
printHexMac(mac);
Serial.println();
} else {
Serial.println("Could not read station MAC");
}
// Put Wi-Fi in promiscuous mode to allow raw TX and low-level access
err = esp_wifi_set_promiscuous(true);
if (err == ESP_OK) {
Serial.println("esp_wifi_set_promiscuous(true) OK");
} else {
Serial.print("esp_wifi_set_promiscuous() failed err=");
Serial.println((int)err);
}
Serial.println("Setup complete; raw TX task started.");
Serial.println();
Serial.print("My MAC Address is: ");
Serial.println(WiFi.macAddress());
}
void loop(void)
{
// Keep the main loop alive; real work in FreeRTOS task.
// vTaskDelay(pdMS_TO_TICKS(1000));
}Loading
esp32-s2-devkitm-1
esp32-s2-devkitm-1