Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ESP32 + SupaBase Database
- //-----------------------------------------------
- #include <WiFi.h>
- #include <HTTPClient.h>
- #include <WiFiClientSecure.h>
- // Replace with your network credentials
- const char* ssid = "network";
- const char* password = "123456789";
- // supabase credentials
- String API_URL = "https://bveaubgawnodhwekmcbs.supabase.co";
- String API_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImJ2ZWF1Ymdhd25vZGh3ZWttY2JzIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MDAwNjk3NTUsImV4cCI6MjAxNTY0NTc1NX0.wOhopH3j08KudJUI8g1pufYBAtUcKiErBNn4SEbHdmM";
- String TableName = "maintable";
- const int httpsPort = 443;
- // Sending interval of the packets in seconds
- //int sendinginterval = 1200; // 20 minutes
- int sendinginterval = 5; // 2 minutes
- HTTPClient https;
- WiFiClientSecure client;
- float h =1;
- float t=2;
- int m=3;
- void setup() {
- // HTTPS is used without checking credentials
- client.setInsecure();
- // Connect to the WIFI
- Serial.begin(115200);
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- // Print local IP address
- Serial.println("");
- Serial.println("WiFi connected.");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- void loop() {
- // If connected to the internet turn the Builtin led On and attempt to send a message to the database
- if (WiFi.status() == WL_CONNECTED) {
- // Read all sensors
- h += 1;
- t += 1;
- m += 1;
- // Send the a post request to the server
- https.begin(client,API_URL+"/rest/v1/"+TableName);
- https.addHeader("Content-Type", "application/json");
- https.addHeader("Prefer", "return=representation");
- https.addHeader("apikey", API_KEY);
- https.addHeader("Authorization", "Bearer " + API_KEY);
- int httpCode = https.POST("{\"temperature\":" + String(t)+ ",\"humidity\":"+ String(h)+",\"moisture\":" + String(1024 - m)+"}" ); //Send the request
- String payload = https.getString();
- Serial.println(httpCode); //Print HTTP return code
- Serial.println(payload); //Print request response payload
- https.end();
- }else{
- Serial.println("Error in WiFi connection");
- }
- delay(1000*sendinginterval); //wait to send the next request
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement