Esp32 packet monitor

Hello!
i am using an ESP32-WROOM-32U dev module to make a packet monitor.
Here is the github repo:
Sketch file

Whenever i compile the code for the esp32, the compiler generated the following error:

C:\Users\uzzam\AppData\Local\Temp\.arduinoIDE-unsaved2023108-8724-tuo48h.ydo7j\sketch_nov8a\sketch_nov8a.ino:48:36: error: 'system_event_t' has not been declared
   48 | esp_err_t event_handler(void* ctx, system_event_t* event) {
      |                                    ^~~~~~~~~~~~~~
C:\Users\uzzam\AppData\Local\Temp\.arduinoIDE-unsaved2023108-8724-tuo48h.ydo7j\sketch_nov8a\sketch_nov8a.ino:48:36: error: 'system_event_t' has not been declared
   48 | esp_err_t event_handler(void* ctx, system_event_t* event) {
      |                                    ^~~~~~~~~~~~~~
C:\Users\uzzam\AppData\Local\Temp\.arduinoIDE-unsaved2023108-8724-tuo48h.ydo7j\sketch_nov8a\sketch_nov8a.ino:66:1: error: reference to 'byte' is ambiguous
   66 | byte palette_red[128], palette_green[128], palette_blue[128];
      | ^~~~
In file included from C:\Users\uzzam\AppData\Local\Temp\.arduinoIDE-unsaved2023108-8724-tuo48h.ydo7j\sketch_nov8a\sketch_nov8a.ino:14:
c:\users\uzzam\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\esp-12.2.0_20230208\xtensa-esp32-elf\include\c++\12.2.0\cstddef:69:14: note: candidates are: 'enum class std::byte'
   69 |   enum class byte : unsigned char {};
      |              ^~~~
In file included from C:\Users\uzzam\AppData\Local\Temp\arduino\sketches\537DCA36E8C396428D8F90FEAF9C9435\sketch\sketch_nov8a.ino.cpp:1:
C:\Users\uzzam\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.0-alpha2\cores\esp32/Arduino.h:145:17: note:                 'typedef uint8_t byte'
  145 | typedef uint8_t byte;
      |                 ^~~~
C:\Users\uzzam\AppData\Local\Temp\.arduinoIDE-unsaved2023108-8724-tuo48h.ydo7j\sketch_nov8a\sketch_nov8a.ino: In function 'void setup()':
C:\Users\uzzam\AppData\Local\Temp\.arduinoIDE-unsaved2023108-8724-tuo48h.ydo7j\sketch_nov8a\sketch_nov8a.ino:158:5: error: 'palette_red' was not declared in this scope
  158 |     palette_red[i] = i / 2;
      |     ^~~~~~~~~~~
C:\Users\uzzam\AppData\Local\Temp\.arduinoIDE-unsaved2023108-8724-tuo48h.ydo7j\sketch_nov8a\sketch_nov8a.ino:159:5: error: 'palette_green' was not declared in this scope
  159 |     palette_green[i] = 0;
      |     ^~~~~~~~~~~~~
C:\Users\uzzam\AppData\Local\Temp\.arduinoIDE-unsaved2023108-8724-tuo48h.ydo7j\sketch_nov8a\sketch_nov8a.ino:160:5: error: 'palette_blue' was not declared in this scope
  160 |     palette_blue[i] = i;
      |     ^~~~~~~~~~~~
C:\Users\uzzam\AppData\Local\Temp\.arduinoIDE-unsaved2023108-8724-tuo48h.ydo7j\sketch_nov8a\sketch_nov8a.ino:163:5: error: 'palette_red' was not declared in this scope
  163 |     palette_red[i] = i / 2;
      |     ^~~~~~~~~~~
C:\Users\uzzam\AppData\Local\Temp\.arduinoIDE-unsaved2023108-8724-tuo48h.ydo7j\sketch_nov8a\sketch_nov8a.ino:164:5: error: 'palette_green' was not declared in this scope
  164 |     palette_green[i] = 0;
      |     ^~~~~~~~~~~~~
C:\Users\uzzam\AppData\Local\Temp\.arduinoIDE-unsaved2023108-8724-tuo48h.ydo7j\sketch_nov8a\sketch_nov8a.ino:165:5: error: 'palette_blue' was not declared in this scope
  165 |     palette_blue[i] = 63 - i;
      |     ^~~~~~~~~~~~
C:\Users\uzzam\AppData\Local\Temp\.arduinoIDE-unsaved2023108-8724-tuo48h.ydo7j\sketch_nov8a\sketch_nov8a.ino:168:5: error: 'palette_red' was not declared in this scope
  168 |     palette_red[i] = 31;
      |     ^~~~~~~~~~~
C:\Users\uzzam\AppData\Local\Temp\.arduinoIDE-unsaved2023108-8724-tuo48h.ydo7j\sketch_nov8a\sketch_nov8a.ino:169:5: error: 'palette_green' was not declared in this scope
  169 |     palette_green[i] = (i - 64) * 2;
      |     ^~~~~~~~~~~~~
C:\Users\uzzam\AppData\Local\Temp\.arduinoIDE-unsaved2023108-8724-tuo48h.ydo7j\sketch_nov8a\sketch_nov8a.ino:170:5: error: 'palette_blue' was not declared in this scope
  170 |     palette_blue[i] = 0;
      |     ^~~~~~~~~~~~
C:\Users\uzzam\AppData\Local\Temp\.arduinoIDE-unsaved2023108-8724-tuo48h.ydo7j\sketch_nov8a\sketch_nov8a.ino:173:5: error: 'palette_red' was not declared in this scope
  173 |     palette_red[i] = 31;
      |     ^~~~~~~~~~~
C:\Users\uzzam\AppData\Local\Temp\.arduinoIDE-unsaved2023108-8724-tuo48h.ydo7j\sketch_nov8a\sketch_nov8a.ino:174:5: error: 'palette_green' was not declared in this scope
  174 |     palette_green[i] = 63;
      |     ^~~~~~~~~~~~~
C:\Users\uzzam\AppData\Local\Temp\.arduinoIDE-unsaved2023108-8724-tuo48h.ydo7j\sketch_nov8a\sketch_nov8a.ino:175:5: error: 'palette_blue' was not declared in this scope
  175 |     palette_blue[i] = i - 96;
      |     ^~~~~~~~~~~~
C:\Users\uzzam\AppData\Local\Temp\.arduinoIDE-unsaved2023108-8724-tuo48h.ydo7j\sketch_nov8a\sketch_nov8a.ino:185:3: error: 'tcpip_adapter_init' was not declared in this scope
  185 |   tcpip_adapter_init();
      |   ^~~~~~~~~~~~~~~~~~
In file included from C:\Users\uzzam\AppData\Local\Arduino15\packages\esp32\tools\esp32-arduino-libs\idf-release_v5.1-6b1f40b9bf/esp32/include/esp_hw_support/include/esp_intr_alloc.h:11,
                 from C:\Users\uzzam\AppData\Local\Arduino15\packages\esp32\tools\esp32-arduino-libs\idf-release_v5.1-6b1f40b9bf/esp32/include/esp_hw_support/include/esp_cpu.h:20,
                 from C:\Users\uzzam\AppData\Local\Arduino15\packages\esp32\tools\esp32-arduino-libs\idf-release_v5.1-6b1f40b9bf/esp32/include/esp_hw_support/include/spinlock.h:11,
                 from C:\Users\uzzam\AppData\Local\Arduino15\packages\esp32\tools\esp32-arduino-libs\idf-release_v5.1-6b1f40b9bf/esp32/include/freertos/FreeRTOS-Kernel/portable/xtensa/include/freertos/portmacro.h:74,
                 from C:\Users\uzzam\AppData\Local\Arduino15\packages\esp32\tools\esp32-arduino-libs\idf-release_v5.1-6b1f40b9bf/esp32/include/freertos/FreeRTOS-Kernel/include/freertos/portable.h:59,
                 from C:\Users\uzzam\AppData\Local\Arduino15\packages\esp32\tools\esp32-arduino-libs\idf-release_v5.1-6b1f40b9bf/esp32/include/freertos/FreeRTOS-Kernel/include/freertos/FreeRTOS.h:71,
                 from C:\Users\uzzam\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.0-alpha2\cores\esp32/Arduino.h:33:
C:\Users\uzzam\AppData\Local\Temp\.arduinoIDE-unsaved2023108-8724-tuo48h.ydo7j\sketch_nov8a\sketch_nov8a.ino:187:19: error: 'esp_event_loop_init' was not declared in this scope; did you mean 'esp_event_loop_run'?
  187 |   ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
      |                   ^~~~~~~~~~~~~~~~~~~
C:\Users\uzzam\AppData\Local\Arduino15\packages\esp32\tools\esp32-arduino-libs\idf-release_v5.1-6b1f40b9bf/esp32/include/esp_common/include/esp_err.h:117:30: note: in definition of macro 'ESP_ERROR_CHECK'
  117 |         esp_err_t err_rc_ = (x);                                        \
      |                              ^
C:\Users\uzzam\AppData\Local\Temp\.arduinoIDE-unsaved2023108-8724-tuo48h.ydo7j\sketch_nov8a\sketch_nov8a.ino: In function 'void do_sampling_FFT()':
C:\Users\uzzam\AppData\Local\Temp\.arduinoIDE-unsaved2023108-8724-tuo48h.ydo7j\sketch_nov8a\sketch_nov8a.ino:279:28: error: 'palette_red' was not declared in this scope
  279 |       unsigned int color = palette_red[k] << 11 | palette_green[k] << 5 | palette_blue[k];
      |                            ^~~~~~~~~~~
C:\Users\uzzam\AppData\Local\Temp\.arduinoIDE-unsaved2023108-8724-tuo48h.ydo7j\sketch_nov8a\sketch_nov8a.ino:279:51: error: 'palette_green' was not declared in this scope
  279 |       unsigned int color = palette_red[k] << 11 | palette_green[k] << 5 | palette_blue[k];
      |                                                   ^~~~~~~~~~~~~
C:\Users\uzzam\AppData\Local\Temp\.arduinoIDE-unsaved2023108-8724-tuo48h.ydo7j\sketch_nov8a\sketch_nov8a.ino:279:75: error: 'palette_blue' was not declared in this scope
  279 |       unsigned int color = palette_red[k] << 11 | palette_green[k] << 5 | palette_blue[k];
      |                                                                           ^~~~~~~~~~~~

exit status 1

Compilation error: 'system_event_t' has not been declared

for your reference, here is the code im using:

#include "arduinoFFT.h"
#include <Adafruit_GFX.h>   
#include <Adafruit_ST7735.h> 
#include <SPI.h>

#include "esp_wifi.h"
#include "esp_wifi_types.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_event_loop.h"
#include "nvs_flash.h"
#include <stdio.h>
#include <string>
#include <cstddef>
#include <Wire.h>
#include <Preferences.h>
using namespace std;

#define TFT_CS         14
#define TFT_RST        33
#define TFT_DC         27

Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS,  TFT_DC, TFT_RST);

#define TFT_SCLK 13   
#define TFT_MOSI 11   

arduinoFFT FFT = arduinoFFT();

#define MAX_CH 14       
#define SNAP_LEN 2324   

#define BUTTON_PIN 22    

#define MAX_X 128
#define MAX_Y 51

const uint16_t GRAY = 303131;
const uint16_t BLUE = 0x001f;
const uint16_t RED = 0xf800;
const uint16_t GREEN = 0x07e0;
const uint16_t BLACK = 0;
const uint16_t YELLOW = RED + GREEN;
const uint16_t CYAN = GREEN + BLUE;
const uint16_t MAGENTA = RED + BLUE;
const uint16_t WHITE = RED + BLUE + GREEN;

esp_err_t event_handler(void* ctx, system_event_t* event) {
  return ESP_OK;
}

Preferences preferences;

const int analogInputPin = 39;
const uint16_t samples = 256; 
const double samplingFrequency = 5000;

double attenuation = 10;

unsigned int sampling_period_us;
unsigned long microseconds;

double vReal[samples];
double vImag[samples];

byte palette_red[128], palette_green[128], palette_blue[128];

bool useSD = false;
bool buttonPressed = false;
bool buttonEnabled = true;
uint32_t lastDrawTime;
uint32_t lastButtonTime;
uint32_t tmpPacketCounter;
uint32_t pkts[MAX_X];       // here the packets per second will be saved
uint32_t deauths = 0;       // deauth frames per second
unsigned int ch = 1;        // current 802.11 channel
int rssiSum;

unsigned int epoch = 0;
unsigned int color_cursor = 2016;

double getMultiplicator() {
  uint32_t maxVal = 1;
  for (int i = 0; i < MAX_X; i++) {
    if (pkts[i] > maxVal) maxVal = pkts[i];
  }
  if (maxVal > MAX_Y) return (double)MAX_Y / (double)maxVal;
  else return 1;
}

void setChannel(int newChannel) {
  ch = newChannel;
  if (ch > MAX_CH || ch < 1) ch = 1;

  preferences.begin("packetmonitor32", false);
  preferences.putUInt("channel", ch);
  preferences.end();

  esp_wifi_set_promiscuous(false);
  esp_wifi_set_channel(ch, WIFI_SECOND_CHAN_NONE);
  esp_wifi_set_promiscuous_rx_cb(&wifi_promiscuous);
  esp_wifi_set_promiscuous(true);
}

void wifi_promiscuous(void* buf, wifi_promiscuous_pkt_type_t type) {
  wifi_promiscuous_pkt_t* pkt = (wifi_promiscuous_pkt_t*)buf;
  wifi_pkt_rx_ctrl_t ctrl = (wifi_pkt_rx_ctrl_t)pkt->rx_ctrl;

  if (type == WIFI_PKT_MGMT && (pkt->payload[0] == 0xA0 || pkt->payload[0] == 0xC0 )) deauths++;

  if (type == WIFI_PKT_MISC) return;            
  if (ctrl.sig_len > SNAP_LEN) return;           

  uint32_t packetLength = ctrl.sig_len;
  if (type == WIFI_PKT_MGMT) packetLength -= 4;  
  //Serial.print(".");
  tmpPacketCounter++;
  rssiSum += ctrl.rssi;
}

void draw() {
  double multiplicator = getMultiplicator();
  int len;
  int rssi;

  if (pkts[MAX_X - 1] > 0) rssi = rssiSum / (int)pkts[MAX_X - 1];
  else rssi = rssiSum;
}

void setup() {
  
      Serial.begin(115200);
     
      tft.initR(INITR_BLACKTAB);
      tft.setRotation(0);
      tft.fillScreen(ST7735_BLACK);
  
      tft.setTextWrap(false);
      tft.setCursor(10, 60);
      tft.setTextColor(WHITE);
      tft.setTextSize(1);
      tft.println("WiFiBox by");
      tft.setCursor(10, 70);
      tft.setTextColor(WHITE);
      tft.setTextSize(2);
      tft.println("CiferTech");
      tft.setCursor(45, 120);
      tft.setTextColor(WHITE);
      tft.setTextSize(1);
      tft.println("v1.2.0");
  
      delay(3000);
      tft.fillScreen(ST7735_BLACK);
  
  sampling_period_us = round(1000000*(1.0/samplingFrequency));

  for (int i = 0; i < 32; i++) {
    palette_red[i] = i / 2;
    palette_green[i] = 0;
    palette_blue[i] = i;
  }
  for (int i = 32; i < 64; i++) {
    palette_red[i] = i / 2;
    palette_green[i] = 0;
    palette_blue[i] = 63 - i;
  }
  for (int i = 64; i < 96; i++) {
    palette_red[i] = 31;
    palette_green[i] = (i - 64) * 2;
    palette_blue[i] = 0;        
  }
  for (int i = 96; i < 128; i++) {
    palette_red[i] = 31;
    palette_green[i] = 63;
    palette_blue[i] = i - 96;        
  }
   
  sampling_period_us = round(1000000*(1.0/samplingFrequency));

  preferences.begin("packetmonitor32", false);
  ch = preferences.getUInt("channel", 1);
  preferences.end();

  nvs_flash_init();
  tcpip_adapter_init();
  wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
  ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  //ESP_ERROR_CHECK(esp_wifi_set_country(WIFI_COUNTRY_EU));
  ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
  ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL));
  ESP_ERROR_CHECK(esp_wifi_start());

  esp_wifi_set_channel(ch, WIFI_SECOND_CHAN_NONE);

  pinMode(BUTTON_PIN, INPUT_PULLUP);

  esp_wifi_set_promiscuous_rx_cb(&wifi_promiscuous);
  esp_wifi_set_promiscuous(true);

}

  void loop() {

  tft.drawPixel(epoch, 0, color_cursor);
  do_sampling_FFT();
  delay(10);
  
  tft.drawPixel(epoch, 0, 0);
  epoch++;
  if (epoch >= tft.width())
    epoch = 0;



    if (digitalRead(BUTTON_PIN) == LOW) {
      if (buttonEnabled) {
        if (!buttonPressed) {
          buttonPressed = true;
        }
      }
    
      if (buttonPressed) {
        setChannel(ch + 1);
                 
      }
      buttonPressed = false;
      buttonEnabled = true;
    }
   
      pkts[127] = tmpPacketCounter;

      Serial.println((String)pkts[MAX_X - 1]);

      tmpPacketCounter = 0;
      deauths = 0;
      rssiSum = 0;
    
    
    if (Serial.available()) {
      ch = Serial.readString().toInt();
      if (ch < 1 || ch > 14) ch = 1;
      setChannel(ch);
    }
  } 

void do_sampling_FFT() {
   
  microseconds = micros();
  for(int i=0; i < samples; i++) {
      vReal[i] = tmpPacketCounter*300;
      vImag[i] = 1;
      while(micros() - microseconds < sampling_period_us){
        
      }
      microseconds += sampling_period_us;
  }
  
  double mean = 0;
  for (uint16_t i = 0; i < samples; i++)
    mean += vReal[i];
  mean /= samples;
  for (uint16_t i = 0; i < samples; i++)
    vReal[i] -= mean;
    
  microseconds = micros();
  
  FFT.Windowing(vReal, samples, FFT_WIN_TYP_HAMMING, FFT_FORWARD); 
  FFT.Compute(vReal, vImag, samples, FFT_FORWARD); 
  FFT.ComplexToMagnitude(vReal, vImag, samples); 

  unsigned int top_y = 155;
  int max_k = 0;
  for (int j = 0; j < samples >> 1; j++) {
      int k = vReal[j] / attenuation;
      if (k > max_k)
        max_k = k;      
      if (k > 127) k = 127;
      unsigned int color = palette_red[k] << 11 | palette_green[k] << 5 | palette_blue[k]; 
      tft.drawPixel(epoch, top_y - j, color);
  }
  double tattenuation = max_k / 127.0;
  if (tattenuation > attenuation)
    attenuation = tattenuation;

          tft.fillRect(0,0,128,10, ST7735_BLACK);
          //tft.fillScreen(ST7735_BLACK);
          tft.setTextWrap(false);
          tft.setCursor(20, 2);
          tft.setTextColor(ST7735_WHITE);
          tft.setTextSize(1);
          tft.print(ch);
            
          tft.setCursor(2, 2);
          tft.setTextColor(ST7735_WHITE);
          tft.print("Ch:");

          //tft.fillRect(50,0,15,15, ST7735_BLACK);
          tft.setTextWrap(true);
          tft.setCursor(80, 2);
          tft.setTextColor(ST7735_WHITE);
          tft.setTextSize(1);
          tft.print(tmpPacketCounter);

          tft.setCursor(50, 2);
          tft.setTextColor(ST7735_WHITE);
          tft.print("Pkts:");
 
          delay(10);
}

These are my options:

Any help will be appreciated!

Without comments the code is hard to follow. Also what are you monitoring IE what is the physical layer. An annotated schematic would be helpful as well as links to technical information on the hardware items.

This is the data sheet for my chip that i am using:

(WROOM-32U)

I am monitoring data packets sent being sent on the network ( network traffic).

You are using the latest alpha version for Arduino ESP32 Core.

The version 3.x.x introduces a lot of API changes because it's based on ESP-IDF Release v5.1 instead v4.4

You should update the sketch following the migration notes, or simply go back to the latest version 2.0.14 of the ESP32 core

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.