-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Done implementing Realtime Postgres Changes
- Loading branch information
Showing
6 changed files
with
303 additions
and
415 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
106 changes: 31 additions & 75 deletions
106
examples/realtime-postgresChanges/realtime-postgresChanges.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,50 @@ | ||
#include <Arduino.h> | ||
#include <ArduinoJson.h> | ||
#include <ESP_Supabase.h> | ||
#include <ESP_Supabase_Realtime.h> | ||
|
||
#if defined(ESP8266) | ||
#include <ESP8266WiFi.h> | ||
#else | ||
#include <WiFi.h> | ||
#endif | ||
|
||
enum DEBUGTYPES { | ||
DEBUG_ERROR, | ||
DEBUG_INFO, | ||
DEBUG_MOTOR, | ||
DEBUG_VERBOSE, | ||
DEBUG_LED, | ||
DEBUG_FEED, | ||
DEBUG_SYNC | ||
}; | ||
SupabaseRealtime realtime; | ||
|
||
typedef enum DEBUGTYPES DEBUGTYPES; | ||
void DevicesTableHandler(String result) | ||
{ | ||
JsonDocument doc; | ||
deserializeJson(doc, result); | ||
|
||
|
||
DEBUGTYPES APP_DEBUG_LEVEL[4] = { | ||
DEBUG_ERROR, | ||
DEBUG_FEED, | ||
DEBUG_INFO, | ||
DEBUG_SYNC | ||
}; | ||
|
||
void DEBUG_PRINT(String str, DEBUGTYPES debuglevel) { | ||
for (int i = 0; i < 4; i++) { | ||
if (APP_DEBUG_LEVEL[i] == debuglevel) { | ||
Serial.println(String(APP_DEBUG_LEVEL[i]) + " " + str); | ||
return; | ||
} | ||
} | ||
|
||
return; | ||
} | ||
|
||
|
||
void wifiConnected() { | ||
DEBUG_PRINT("WiFi Connected: " + WiFi.SSID(), DEBUG_INFO); | ||
DEBUG_PRINT("IP Address: ", DEBUG_INFO); | ||
DEBUG_PRINT(String(WiFi.localIP()), DEBUG_INFO); | ||
// Example of what you can do with the result | ||
String state = doc["state"]; | ||
Serial.println(state); | ||
} | ||
|
||
bool waitForWifi(int timeout = 0, String method = "") { | ||
int cnt = 0; | ||
ProgressBar status = ProgressBar(timeout); | ||
DEBUG_PRINT("", DEBUG_INFO); | ||
|
||
DEBUG_PRINT("Attempting Connection: " + method, DEBUG_INFO); | ||
status.start(); | ||
|
||
while (WiFi.status() != WL_CONNECTED) { | ||
delay(500); | ||
status.step(); | ||
cnt++; | ||
|
||
if (cnt >= timeout && timeout > 0) { | ||
status.end(); | ||
return false; | ||
} | ||
} | ||
status.end(); | ||
|
||
return true; | ||
} | ||
|
||
Supabase db; | ||
|
||
void DevicesTableHandler(JsonDocument result) { | ||
serializeJson(result, Serial); | ||
} | ||
|
||
void setup() { | ||
void setup() | ||
{ | ||
Serial.begin(9600); | ||
|
||
WiFi.begin("ssid", "password"); | ||
if (waitForWifi(50, ".ENV")) { | ||
wifiConnected(); | ||
}; | ||
|
||
db.setupRealtime(SUPABASEURL, SUPABASEANONKEY); | ||
int numqueries = 0; | ||
SUPABASEQuery queries[numqueries] = {}; | ||
db.realtime.addEntry("Devices", "*", queries, numqueries, DevicesTableHandler); | ||
while (WiFi.status() != WL_CONNECTED) | ||
{ | ||
delay(100); | ||
Serial.print("."); | ||
} | ||
Serial.println("\nConnected to WiFi"); | ||
|
||
realtime.begin("https://project.supabase.co", "apikey"); | ||
realtime.login_email("email", "password"); // Only if you activate RLS in your Supabase Postgres Table | ||
|
||
// Parameter 1 : Table name | ||
// Parameter 2 : Event type ("*" | "INSERT" | "UPDATE" | "DELETE") | ||
// Parameter 3 : 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 | ||
// Parameter 4 : Callback function, how to handle the result (message) | ||
realtime.listen("table", "*", "", DevicesTableHandler); | ||
} | ||
|
||
void loop() { | ||
db.realtime.loop(); | ||
void loop() | ||
{ | ||
realtime.loop(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#ifndef ESP_Supabase_Realtime_h | ||
#define ESP_Supabase_Realtime_h | ||
|
||
#include <Arduino.h> | ||
#include <ArduinoJson.h> | ||
#include <WiFiClientSecure.h> | ||
#include <WebSocketsClient.h> | ||
|
||
#if defined(ESP8266) | ||
#include <ESP8266HTTPClient.h> | ||
#elif defined(ESP32) | ||
#include <HTTPClient.h> | ||
#else | ||
#error "This library is not supported for your board! ESP32 and ESP8266" | ||
#endif | ||
|
||
class SupabaseRealtime | ||
{ | ||
private: | ||
WebSocketsClient webSocket; | ||
|
||
String key; | ||
String hostname; | ||
|
||
String phone_or_email; | ||
String password; | ||
String data; | ||
String loginMethod; | ||
String USER_TOKEN = ""; | ||
bool useAuth; | ||
int _login_process(); | ||
unsigned int authTimeout = 0; | ||
unsigned long loginTime; | ||
|
||
const char *config = "{\"event\":\"phx_join\",\"topic\":\"realtime:ESP\",\"payload\":{\"config\":{\"postgres_changes\":[{\"event\":\"abcdfeghijklmbopq\",\"schema\":\"public\",\"table\":\"abcdfeghijklmbopq\"}]}},\"ref\":\"sentRef\"}"; | ||
unsigned int last_ms = millis(); | ||
const char *jsonRealtimeHeartbeat = R"({"event": "heartbeat","topic": "phoenix","payload": {},"ref": "sentRef"})"; | ||
const char *tokenConfig = R"({"topic": "realtime:ESP","event": "access_token","payload": { | ||
"access_token": "" | ||
},"ref": "sendRef"})"; | ||
|
||
void processMessage(uint8_t *payload, void (*func)(String)); | ||
void webSocketEvent(WStype_t type, uint8_t *payload, size_t length, String configJSON, void (*func)(String)); | ||
|
||
public: | ||
SupabaseRealtime() {} | ||
void begin(String hostname, String key); | ||
void listen(String table, String event, String filter, void (*func)(String)); | ||
void loop(); | ||
int login_email(String email_a, String password_a); | ||
int login_phone(String phone_a, String password_a); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.