Skip to content

Commit

Permalink
Fixing the examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jhagas committed Jul 19, 2024
1 parent 6ed7672 commit bac7e23
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 53 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ESPSupabase

Simple library to connect ESP32/8266 to Supabase via REST API, including user authentication
Simple library to connect ESP32/8266 to Supabase via REST API and WebSockets (for Realtime), including user authentication

## Installation

Expand Down
28 changes: 20 additions & 8 deletions examples/insert/insert.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
Supabase db;

// Put your supabase URL and Anon key here...
String supabase_url = "";
String anon_key = "";
String supabase_url = "https://yourproject.supabase.co";
String anon_key = "anonkey";

// put your WiFi credentials (SSID and Password) here
const char *ssid = "ssid";
const char *psswd = "pass";

// Put your target table here
String table = "";
Expand All @@ -22,25 +26,33 @@ String JSON = "";

bool upsert = false;

void setup() {
void setup()
{
Serial.begin(9600);

Serial.print("Connecting to WiFi");
WiFi.begin("ssid", "password");
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, psswd);
while (WiFi.status() != WL_CONNECTED)
{
delay(100);
Serial.print(".");
}
Serial.println("Connected!");
Serial.println("\nConnected!");

// Beginning Supabase Connection
db.begin(supabase_url, anon_key);

// Uncomment this line below, if you activate RLS in your Supabase Table
// db.login_email("email", "password");
// You can also use
// db.login_phone("phone", "password");

int code = db.insert(table, JSON, upsert);
Serial.println(code);
db.urlQuery_reset();
}

void loop() {
delay(10);
void loop()
{
delay(1000);
}
8 changes: 6 additions & 2 deletions examples/login-email/login-email.ino
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ONLY IF you activate RLS in your Supabase Table

#include <Arduino.h>
#include <ESPSupabase.h>

Expand Down Expand Up @@ -30,14 +32,16 @@ void setup() {
delay(100);
Serial.print(".");
}
Serial.println("Connected!");
Serial.println("\nConnected!");

db.begin(supabase_url, anon_key);

// ONLY IF you activate RLS in your Supabase Table
db.login_email(email, password);

// Your Query ...
}

void loop() {
delay(10);
delay(1000);
}
15 changes: 9 additions & 6 deletions examples/login-phone/login-phone.ino
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ONLY IF you activate RLS in your Supabase Table

#include <Arduino.h>
#include <ESPSupabase.h>

Expand All @@ -14,9 +16,8 @@ const String supabase_url = "";
const String anon_key = "";

// put your WiFi credentials (SSID and Password) here
const char* ssid = "your wifi ssid";
const char* psswd = "your wifi password";

const char *ssid = "your wifi ssid";
const char *psswd = "your wifi password";

// Put Supabase account credentials here
const String phone = "";
Expand All @@ -33,15 +34,17 @@ void setup()
delay(100);
Serial.print(".");
}
Serial.println("Connected!");
Serial.println("\nConnected!");

db.begin(supabase_url, anon_key);

// ONLY IF you activate RLS in your Supabase Table
db.login_phone(phone, password);

// Your Query
// Your Query ...
}

void loop()
{
delay(10);
delay(1000);
}
21 changes: 16 additions & 5 deletions examples/realtime-postgresChanges/realtime-postgresChanges.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@
#include <WiFi.h>
#endif

// Put your supabase URL and Anon key here...
String supabase_url = "https://yourproject.supabase.co";
String anon_key = "anonkey";

// put your WiFi credentials (SSID and Password) here
const char *ssid = "ssid";
const char *psswd = "pass";

SupabaseRealtime realtime;

void HandleChanges(String result)
{
JsonDocument doc;
deserializeJson(doc, result);

// Example of what you can do with the result
// EXAMPLE of what you can do with the result
String tableName = doc["table"];
String event = doc["type"];
String changes = doc["record"];
Expand All @@ -30,23 +38,26 @@ void setup()
{
Serial.begin(9600);

WiFi.begin("ssid", "password");
WiFi.begin(ssid, psswd);
while (WiFi.status() != WL_CONNECTED)
{
delay(100);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
Serial.println("\nConnected!");

realtime.begin(supabase_url, anon_key, HandleChanges);

realtime.begin("https://project.supabase.co", "apikey", HandleChanges);
realtime.login_email("email", "password"); // Only if you activate RLS in your Supabase Postgres Table
// Uncomment this line below, if you activate RLS in your Supabase Table
// realtime.login_email("email", "password");

// Parameter 1 : Table name
// Parameter 2 : Event type ("*" | "INSERT" | "UPDATE" | "DELETE")
// Parameter 3 : Your Supabase Table Postgres Schema
// Parameter 4 : Filter
// Please read : https://supabase.com/docs/guides/realtime/postgres-changes?queryGroups=language&language=js#available-filters
// empty string if you don't want to filter the result
// EXAMPLE :
realtime.addChangesListener("table1", "INSERT", "public", "id=eq.0");
// You can add multiple table listeners
realtime.addChangesListener("table2", "*", "public", "");
Expand Down
29 changes: 20 additions & 9 deletions examples/select/select.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
Supabase db;

// Put your supabase URL and Anon key here...
String supabase_url = "";
String anon_key = "";
String supabase_url = "https://yourproject.supabase.co";
String anon_key = "anonkey";

// put your WiFi credentials (SSID and Password) here
const char *ssid = "";
const char *psswd = "";
const char *ssid = "ssid";
const char *psswd = "pass";

void setup()
{
Expand All @@ -29,25 +29,36 @@ void setup()
delay(100);
Serial.print(".");
}
Serial.println("Connected!");
Serial.println("\nConnected!");

// Beginning Supabase Connection
db.begin(supabase_url, anon_key);

// Select query with filter and order, limiting the result is mandatory here
String read = db.from("examples").select("*").eq("column", "value").order("column", "asc", true).limit(1).doSelect();
// Uncomment this line below, if you activate RLS in your Supabase Table
// db.login_email("email", "password");
// You can also use
// db.login_phone("phone", "password");

// MAKE SURE YOU HAVE YOUR SUPABASE DATABASE SETUP FIRST, THIS LIBRARY CANNOT SET THE DATABASE FOR YOU
// Basic select query
String read = db.from("examples").select("*").doSelect();
Serial.println(read);

// Reset Your Query before doing everything else
db.urlQuery_reset();

// Join operation with other table that connected via PK or FK
// More advanced query, FOR DEMONSTRATION PURPOSES ONLY
String read = db.from("examples").select("*").eq("column", "value").order("column", "asc", true).limit(1).doSelect();
Serial.println(read);
db.urlQuery_reset();

// Join operation with other table that connected via PK or FK, FOR DEMONSTRATION PURPOSES ONLY
String read = db.from("examples").select("*, other_table(other_table_column1, other_table_column2, another_table(*))").order("column", "asc", true).limit(1).doSelect();
Serial.println(read);
db.urlQuery_reset();
}

void loop()
{
delay(10);
delay(1000);
}
30 changes: 21 additions & 9 deletions examples/update/update.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,49 @@
Supabase db;

// Put your supabase URL and Anon key here...
String supabase_url = "";
String anon_key = "";
String supabase_url = "https://yourproject.supabase.co";
String anon_key = "anonkey";

// put your WiFi credentials (SSID and Password) here
const char *ssid = "ssid";
const char *psswd = "pass";

// Put your target table here
String table = "";
String table = "tablename";

// Put your JSON that you want to insert rows
// You can also use library like ArduinoJson generate this
String JSON = "";

bool upsert = false;

void setup() {
void setup()
{
Serial.begin(9600);

Serial.print("Connecting to WiFi");
WiFi.begin("ssid", "password");
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, psswd);
while (WiFi.status() != WL_CONNECTED)
{
delay(100);
Serial.print(".");
}
Serial.println("Connected!");
Serial.println("\nConnected!");

// Beginning Supabase Connection
db.begin(supabase_url, anon_key);

// Uncomment this line below, if you activate RLS in your Supabase Table
// db.login_email("email", "password");
// You can also use
// db.login_phone("phone", "password");

int code = db.update("table").eq("column", "value").doUpdate(JSON);
Serial.println(code);
db.urlQuery_reset();
}

void loop() {
delay(10);
void loop()
{
delay(1000);
}
26 changes: 13 additions & 13 deletions examples/upload/upload.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
Supabase db;

// Put your supabase URL and Anon key here...
// Because Login already implemented, there's no need to use secretrole key
String supabase_url = "";
String anon_key = "";
String supabase_url = "https://yourproject.supabase.co";
String anon_key = "anonkey";

// put your WiFi credentials (SSID and Password) here
const char *ssid = "";
Expand Down Expand Up @@ -75,18 +74,19 @@ void setup()
delay(100);
Serial.print(".");
}

Serial.println("Connected!");
Serial.println("\nConnected!");

db.begin(supabase_url, anon_key);

int loginResponse = db.login_email(email, password);

if (loginResponse != 200)
{
Serial.printf("Login failed with code: %d.\n\r", loginResponse);
return;
}
// Uncomment this line below, if you activate RLS in your Supabase Table
// int loginResponse = db.login_email(email, password);
// You can also use
// int loginResponse = db.login_phone("phone", "password");
// if (loginResponse != 200)
// {
// Serial.printf("Login failed with code: %d.\n\r", loginResponse);
// return;
// }

int uploadResponse = db.upload(bucket, "supabase_example_" + String(millis()) + ".txt", "text/plain", &file, file.size());

Expand All @@ -104,5 +104,5 @@ void setup()

void loop()
{
delay(10000);
delay(1000);
}
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ framework = arduino
lib_deps =
bblanchon/ArduinoJson @ ^7.1.0
links2004/WebSockets@^2.4.1
Hash
build_flags = ${config.build_flags}
build_src_filter = ${config.src_filter}

0 comments on commit bac7e23

Please sign in to comment.