PJ Can you look and Help

PJ can you take a look at this and see if you can get it running

It says Platform IO but i do not have that

I use Arduino IDE

everything in the rep is blank… it seems you have to build the code from PlatformIO

Thanks!

Hi there,

So you do know the same is always available in Arduino.
“Sqlite3” is , So that’s all you really need.,:wink"
more or less.

Format fat 32 SD card.
Include LIB and GO. :+1:

// ==============================================================================
//  PROJECT:   XIAO ESP32-S3 Sense Wi-Fi Scanner to SQLite Database
//  AUTHOR:    PJ (SmartIPTech) & Gemini AI Co-Pilot
//  HARDWARE:  Seeed Studio XIAO ESP32-S3 Sense (with SD card daughterboard)
//  STORAGE:   MicroSD FAT32 -> /sd/wifi.sqlite3
// ==============================================================================

#include <Arduino.h>
#include <WiFi.h>
#include <FS.h>
#include <SD.h>
#include <SPI.h>
#include <sqlite3.h>

// XIAO ESP32-S3 Sense SD Card SPI Pinout
#define SD_CS_PIN    21
#define SD_MOSI_PIN  9
#define SD_MISO_PIN  8
#define SD_SCK_PIN   7

sqlite3 *db = NULL;

// Helper function to execute SQL commands
int executeSQL(sqlite3 *dbHandle, const char *sql) {
  char *errMsg = NULL;
  int rc = sqlite3_exec(dbHandle, sql, NULL, NULL, &errMsg);
  if (rc != SQLITE_OK) {
    Serial.printf("[SQL ERROR] %s: %s\n", sql, errMsg);
    sqlite3_free(errMsg);
  }
  return rc;
}

// Convert encryption enum to human-readable string
const char* getEncryptionType(wifi_auth_mode_t authmode) {
  switch (authmode) {
    case WIFI_AUTH_OPEN:            return "OPEN";
    case WIFI_AUTH_WEP:             return "WEP";
    case WIFI_AUTH_WPA_PSK:         return "WPA_PSK";
    case WIFI_AUTH_WPA2_PSK:        return "WPA2_PSK";
    case WIFI_AUTH_WPA_WPA2_PSK:   return "WPA_WPA2_PSK";
    case WIFI_AUTH_WPA2_ENTERPRISE: return "WPA2_ENTERPRISE";
    case WIFI_AUTH_WPA3_PSK:        return "WPA3_PSK";
    case WIFI_AUTH_WPA2_WPA3_PSK:   return "WPA2_WPA3_PSK";
    default:                        return "UNKNOWN";
  }
}

void initDatabase() {
  sqlite3_initialize();

  // Open / Create sqlite file on SD card mount (/sd)
  if (sqlite3_open("/sd/wifi.sqlite3", &db) != SQLITE_OK) {
    Serial.printf("[ERROR] Failed to open SQLite database: %s\n", sqlite3_errmsg(db));
    return;
  }
  Serial.println("[OK] SQLite Database opened successfully.");

  // Create table matching the original project schema
  const char *createTableSQL = 
    "CREATE TABLE IF NOT EXISTS wifis ("
    "id INTEGER PRIMARY KEY AUTOINCREMENT, "
    "ssid TEXT, "
    "bssid TEXT, "
    "channel INTEGER, "
    "rssi INTEGER, "
    "encryption TEXT"
    ");";

  executeSQL(db, createTableSQL);
}

void saveNetworkToDB(const char *ssid, const char *bssid, int channel, int rssi, const char *enc) {
  if (!db) return;

  sqlite3_stmt *stmt;
  const char *insertSQL = "INSERT INTO wifis (ssid, bssid, channel, rssi, encryption) VALUES (?, ?, ?, ?, ?);";

  if (sqlite3_prepare_v2(db, insertSQL, -1, &stmt, NULL) == SQLITE_OK) {
    sqlite3_bind_text(stmt, 1, ssid, -1, SQLITE_STATIC);
    sqlite3_bind_text(stmt, 2, bssid, -1, SQLITE_STATIC);
    sqlite3_bind_int(stmt, 3, channel);
    sqlite3_bind_int(stmt, 4, rssi);
    sqlite3_bind_text(stmt, 5, enc, -1, SQLITE_STATIC);

    if (sqlite3_step(stmt) != SQLITE_DONE) {
      Serial.printf("[ERROR] Insert failed: %s\n", sqlite3_errmsg(db));
    }
    sqlite3_finalize(stmt);
  }
}

void setup() {
  Serial.begin(115200);
  delay(1000);

  Serial.println("\n--- Starting XIAO ESP32-S3 Wi-Fi SQLite Scanner ---");

  // Initialize custom SPI pins for the S3 Sense SD slot
  SPI.begin(SD_SCK_PIN, SD_MISO_PIN, SD_MOSI_PIN, SD_CS_PIN);

  if (!SD.begin(SD_CS_PIN)) {
    Serial.println("[ERROR] MicroSD card mount failed! Check card insertion.");
    while (1) { delay(500); }
  }
  Serial.println("[OK] MicroSD card mounted.");

  // Initialize SQLite
  initDatabase();

  // Set Wi-Fi to Station mode and disconnect
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);
}

void loop() {
  Serial.println("\n[SCAN] Scanning airwaves...");
  int n = WiFi.scanNetworks();

  if (n == 0) {
    Serial.println("[INFO] No networks found.");
  } else {
    Serial.printf("[INFO] Found %d networks. Storing in SQLite...\n", n);

    // Begin a single transaction for fast SD writes
    executeSQL(db, "BEGIN TRANSACTION;");

    for (int i = 0; i < n; ++i) {
      String ssid = WiFi.SSID(i);
      String bssid = WiFi.BSSIDstr(i);
      int channel = WiFi.channel(i);
      int rssi = WiFi.RSSI(i);
      const char *enc = getEncryptionType(WiFi.encryptionType(i));

      Serial.printf("  [%d] SSID: %-20s | BSSID: %s | CH: %2d | RSSI: %3d dBm | ENC: %s\n",
                    i + 1, ssid.c_str(), bssid.c_str(), channel, rssi, enc);

      saveNetworkToDB(ssid.c_str(), bssid.c_str(), channel, rssi, enc);
    }

    // Commit transaction to flash
    executeSQL(db, "COMMIT;");
    Serial.println("[OK] All records saved to /sd/wifi.sqlite3");
  }

  WiFi.scanDelete();

  // Wait 15 seconds before next scan loop
  delay(15000);
}

WorksGreat!

HTH
GL :slight_smile: PJ :v:

Dinner! :grin:

thanks… im gonna check it out… I always get crazy compile errors

1 Like

Hi there,

No problem_o,
I gave the Local Agen AI , the original PLIO version, it gave me that back , I compiled and ran It aOK. :+1:

Also told me to load the Sqlight3 for Arduino.
:v:
PJ

I am going to completely uninstall my arduino IDE… I think it is broke

c:\Arduino\libraries\Sqlite3Esp32\src\esp32.cpp: In function 'int ESP32Open(sqlite3_vfs*, const char*, sqlite3_file*, int, int*)':
c:\Arduino\libraries\Sqlite3Esp32\src\esp32.cpp:450:12: error: ordered comparison of pointer with integer zero ('FILE*' and 'int')
  450 |   if( p->fp<=0){
      |       ~~~~~^~~
c:\Arduino\libraries\Sqlite3Esp32\src\esp32.cpp: In function 'int ESP32Delete(sqlite3_vfs*, const char*, int)':
c:\Arduino\libraries\Sqlite3Esp32\src\esp32.cpp:492:12: error: ordered comparison of pointer with integer zero ('FILE*' and 'int')
  492 |     if( dfd<=0 ){
      |         ~~~^~~
exit status 1

Compilation error: exit status 1

This looks like an error in the Library?

Hi there,
No , can’t be unless it’s corrupt, Compiled and ran on my hardware. AS-is.

:v:

PJ
Check you BSP.

yeah… I used several different units, i deleted all the IDE and restarted clean…