Skip to content

Commit

Permalink
new file: ESP32_MSD_Shield_Wiring.jpg
Browse files Browse the repository at this point in the history
	modified:   README.md
	new file:   examples/sqlite3_sdspi/sqlite3_sdspi.ino
Include example and picture for SPI mode SD Card access
  • Loading branch information
siara-cc committed Oct 12, 2018
1 parent 8b59364 commit 8a326d7
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 19 deletions.
Binary file added ESP32_MSD_Shield_Wiring.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 25 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# Sqlite3 Arduino library for ESP32
This library enables access to SQLite database files from SPIFFS or SD Cards through ESP32 SoC. Given below is a picture of a board that has a ready-made Micro SD slot:

This library enables access to SQLite database files from SPIFFS or SD Cards through ESP32 SoC. Given below is a picture of a board that has a ready-made Micro SD slot (using SDMMC 4 bit mode - see example sqlite3_sdmmc):

![](ESP_WROOM_32_breakout.png?raw=true)

Also shown below is the wiring between ESP-WROOM-32 breakout board and Micro SD Shield (using SPI mode - see example sqlite3_sdspi):

![](ESP32_MSD_Shield_Wiring.jpg?raw=true)

## Usage

Sqlite3 C API such as `sqlite3_open` can be directly invoked. Before calling please invoke:

```c++
Expand Down Expand Up @@ -31,29 +37,29 @@ Please see the examples for full illustration of usage for the different file sy
While there is no wiring needed for SPIFFS, for attaching cards to SPI bus, please use the following connections:

```c++
* SD Card | ESP32
* DAT2 -
* DAT3 SS
* CMD MOSI
* VSS GND
* VDD 3.3V
* CLK SCK
* DAT0 MISO
* DAT1 -
* SD Card | ESP32
* DAT2 (1) -
* DAT3 (2) SS (D5)
* CMD (3) MOSI (D23)
* VDD (4) 3.3V
* CLK (5) SCK (D19)
* VSS (6) GND
* DAT0 (7) MISO (D18)
* DAT1 (8) -
```
And for SD card attached to High-speed 4-bit SD_MMC port, use:
```c++
* SD Card | ESP32
* DAT2 12
* DAT3 13
* CMD 15
* VSS GND
* VDD 3.3V
* CLK 14
* DAT0 2 (add 1K pull up after flashing)
* DAT1 4
* SD Card | ESP32
* DAT2 (1) D12
* DAT3 (2) D13
* CMD (3) D15
* VDD (4) 3.3V
* CLK (5) D14
* VSS (6) GND
* DAT0 (7) D2
* DAT1 (8) D4
```

If you are using a board such as shown in the picture above, this wiring is ready-made.
Expand Down
112 changes: 112 additions & 0 deletions examples/sqlite3_sdspi/sqlite3_sdspi.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
This example opens Sqlite3 databases from SD Card and
retrieves data from them.
Before running please copy following files to SD Card:
examples/sqlite3_sdmmc/data/mdr512.db
examples/sqlite3_sdmmc/data/census2000names.db
Connections:
* SD Card | ESP32
* DAT2 -
* DAT3 SS (D5)
* CMD MOSI (D23)
* VSS GND
* VDD 3.3V
* CLK SCK (D18)
* DAT0 MISO (D19)
* DAT1 -
*/
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
#include <SPI.h>
#include <FS.h>
#include "SD.h"

const char* data = "Callback function called";
static int callback(void *data, int argc, char **argv, char **azColName){
int i;
Serial.printf("%s: ", (const char*)data);
for (i = 0; i<argc; i++){
Serial.printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
Serial.printf("\n");
return 0;
}

int openDb(const char *filename, sqlite3 **db) {
int rc = sqlite3_open(filename, db);
if (rc) {
Serial.printf("Can't open database: %s\n", sqlite3_errmsg(*db));
return rc;
} else {
Serial.printf("Opened database successfully\n");
}
return rc;
}

char *zErrMsg = 0;
int db_exec(sqlite3 *db, const char *sql) {
Serial.println(sql);
long start = micros();
int rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
if (rc != SQLITE_OK) {
Serial.printf("SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
} else {
Serial.printf("Operation done successfully\n");
}
Serial.print(F("Time taken:"));
Serial.println(micros()-start);
return rc;
}

void setup() {
Serial.begin(115200);
sqlite3 *db1;
sqlite3 *db2;
char *zErrMsg = 0;
int rc;

SPI.begin();
SD.begin();

sqlite3_initialize();

// Open database 1
if (openDb("/sd/census2000names.db", &db1))
return;
if (openDb("/sd/mdr512.db", &db2))
return;

rc = db_exec(db1, "Select * from surnames where name = 'MICHELLE'");
if (rc != SQLITE_OK) {
sqlite3_close(db1);
sqlite3_close(db2);
return;
}
rc = db_exec(db2, "Select * from domain_rank where domain between 'google.com' and 'google.com.z'");
if (rc != SQLITE_OK) {
sqlite3_close(db1);
sqlite3_close(db2);
return;
}
rc = db_exec(db1, "Select * from surnames where name = 'SPRINGER'");
if (rc != SQLITE_OK) {
sqlite3_close(db1);
sqlite3_close(db2);
return;
}
rc = db_exec(db2, "Select * from domain_rank where domain = 'zoho.com'");
if (rc != SQLITE_OK) {
sqlite3_close(db1);
sqlite3_close(db2);
return;
}

sqlite3_close(db1);
sqlite3_close(db2);

}

void loop() {
}

0 comments on commit 8a326d7

Please sign in to comment.