Skip to content

Commit b5b7a4e

Browse files
committed
Supabase library version 1.0.0
1 parent 14ba9ec commit b5b7a4e

6 files changed

Lines changed: 203 additions & 0 deletions

File tree

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# VSCode
2+
.vscode
3+
.vscode/.browse.c_cpp.db*
4+
.vscode/c_cpp_properties.json
5+
.vscode/launch.json
6+
.vscode/ipch
7+
8+
# macOS
9+
.DS_Store
10+
.AppleDouble
11+
.LSOverride
12+
._*
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Supabase Library for ESP32
3+
4+
Created by Matteo Subet - Last version 05 March 2024
5+
6+
Created based on the library https://github.com/clock27-lab/unofficial-esp32-supabase
7+
*/
8+
9+
#include <SupabaseESP32.h>
10+
11+
// Replace with your Supabase URL, table name and API key
12+
#define supabaseUrl "INSERT YOUR URL HERE"
13+
#define tableRealtime "INSERT YOUR TABLE NAME HERE"
14+
#define supabaseApiKey "INSERT YOUR API KEY HERE"
15+
16+
// Setup the Supabase client
17+
SUPABASE supabaseClient(supabaseUrl, supabaseApiKey, tableRealtime);
18+
19+
// Replace with your network credentials
20+
const char *ssid = "INSERT YOUR SSID HERE";
21+
const char *password = "INSERT YOUR PASSWORD HERE";
22+
23+
void setup()
24+
{
25+
Serial.begin(115200);
26+
27+
WiFi.mode(WIFI_STA);
28+
WiFi.begin(ssid, password);
29+
30+
// Wait for connection
31+
while (WiFi.status() != WL_CONNECTED){delay(500); Serial.print(".");}
32+
33+
//Begin the connection to the database
34+
supabaseClient.begin();
35+
36+
delay(2000);
37+
//Read the data from the table
38+
supabaseClient.read();
39+
40+
delay(2000);
41+
//Update a specific row with a new value
42+
int rowId = 1;
43+
String column = "column_name";
44+
double value = 100.0;
45+
supabaseClient.update(rowId, column, value);
46+
47+
delay(2000);
48+
//Insert a new row with a new value
49+
column = "column_name";
50+
value = 100.0;
51+
supabaseClient.insert(column, value);
52+
53+
delay(2000);
54+
//Read the data again to see the changes
55+
supabaseClient.read();
56+
}
57+
58+
void loop(){
59+
//Do nothing
60+
}

library.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "Supabase ESP32 Connection",
3+
"keywords": "supabase, arduino, realtime database, database, IoT",
4+
"description": "A library to connect to Supabase's Realtime Database from an ESP32 in realtime with API Key authentication.",
5+
"repository":
6+
{
7+
"type": "git",
8+
"url": "https://github.com/zumatt/Supabase-ESP32"
9+
},
10+
"authors":
11+
[
12+
{
13+
"name": "Matteo Subet",
14+
"email": "matteo.subet@supsi.ch",
15+
"url": "http://link.zumat.ch",
16+
"maintainer": true
17+
}
18+
],
19+
"dependencies":
20+
{
21+
},
22+
"version": "1.0.0",
23+
"frameworks": "arduino",
24+
"platforms": "espressif32",
25+
"examples": "examples/*/*.ino"
26+
}

library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=Supabase-ESP32
2+
version=1.0.0
3+
author=Matteo Subet
4+
maintainer=Matteo Subet https://github.com/zumatt
5+
sentence=Arduino Library to communicate with Supabase.io
6+
paragraph=Arduino Library to communicate with Supabase.io. The possible functions are the following: -insert -update -read
7+
category=Communication
8+
url=https://github.com/zumatt/Supabase-ESP32
9+
includes=WiFi.h, HTTPClient.h

src/SupabaseESP32.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include "SupabaseESP32.h"
2+
3+
// #if ESP32
4+
HTTPClient http;
5+
// #endif
6+
7+
String supabase_url;
8+
String supabase_key;
9+
String supabase_bearer;
10+
String supabase_table;
11+
12+
SUPABASE::SUPABASE(String url, String key, String tableName)
13+
{
14+
supabase_table = tableName;
15+
supabase_url = url + "/" + supabase_table;
16+
supabase_key = key;
17+
supabase_bearer = "Bearer " + key;
18+
}
19+
20+
SUPABASE::~SUPABASE() {}
21+
22+
void SUPABASE::begin()
23+
{
24+
Serial.println(supabase_url);
25+
http.begin(supabase_url.c_str());
26+
http.addHeader("apikey", supabase_key);
27+
http.addHeader("Authorization", supabase_bearer);
28+
http.addHeader("Content-Type", "application/json");
29+
http.addHeader("Prefer", "return=representation");
30+
}
31+
32+
int SUPABASE::update(int rowId, String column, double value)
33+
{
34+
String url = supabase_url + "?id=eq." + String(rowId);
35+
http.begin(url.c_str());
36+
http.addHeader("apikey", supabase_key);
37+
http.addHeader("Authorization", supabase_bearer);
38+
http.addHeader("Content-Type", "application/json");
39+
http.addHeader("Prefer", "return=minimal");
40+
String supabase_data = "{\"" + column + "\":" + String(value) + "}";
41+
int httpResponseCode = http.PATCH(supabase_data);
42+
http.end();
43+
return httpResponseCode;
44+
}
45+
46+
int SUPABASE::insert(String column, double value)
47+
{
48+
http.begin(supabase_url.c_str());
49+
http.addHeader("apikey", supabase_key);
50+
http.addHeader("Authorization", supabase_bearer);
51+
http.addHeader("Content-Type", "application/json");
52+
http.addHeader("Prefer", "return=minimal");
53+
String supabase_data = "{\"" + column + "\":" + String(value) + "}";
54+
int httpResponseCode = http.POST(supabase_data);
55+
http.end();
56+
return httpResponseCode;
57+
}
58+
59+
60+
String SUPABASE::read()
61+
{
62+
String url = supabase_url + "?select=*";
63+
http.begin(url.c_str());
64+
http.addHeader("apikey", supabase_key);
65+
int httpResponseCode = http.GET();
66+
String payload = http.getString();
67+
68+
Serial.println("DATA READ!");
69+
Serial.print("HTTP GET: ");
70+
Serial.println(httpResponseCode);
71+
Serial.print("HTTP Payload: ");
72+
Serial.println(payload);
73+
74+
http.end();
75+
return payload;
76+
}

src/SupabaseESP32.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Supabase Library for ESP32
3+
4+
Created by Matteo Subet - Last version 05 March 2024
5+
6+
Created based on the library https://github.com/clock27-lab/unofficial-esp32-supabase
7+
*/
8+
9+
#include <WiFi.h>
10+
#include <HTTPClient.h>
11+
class SUPABASE
12+
{
13+
public:
14+
SUPABASE(String url, String key, String tableName);
15+
~SUPABASE();
16+
void begin();
17+
int update(int rowId, String column, double value);
18+
int insert(String column, double value);
19+
String read();
20+
};

0 commit comments

Comments
 (0)