Advertisement
safwan092

Untitled

Nov 15th, 2023
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. // ESP32 + SupaBase Database
  2. //-----------------------------------------------
  3. #include <WiFi.h>
  4. #include <HTTPClient.h>
  5. #include <WiFiClientSecure.h>
  6.  
  7. // Replace with your network credentials
  8. const char* ssid = "network";
  9. const char* password = "123456789";
  10.  
  11. // supabase credentials
  12. String API_URL = "https://bveaubgawnodhwekmcbs.supabase.co";
  13. String API_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImJ2ZWF1Ymdhd25vZGh3ZWttY2JzIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MDAwNjk3NTUsImV4cCI6MjAxNTY0NTc1NX0.wOhopH3j08KudJUI8g1pufYBAtUcKiErBNn4SEbHdmM";
  14. String TableName = "maintable";
  15. const int httpsPort = 443;
  16.  
  17. // Sending interval of the packets in seconds
  18. //int sendinginterval = 1200; // 20 minutes
  19. int sendinginterval = 5; // 2 minutes
  20.  
  21. HTTPClient https;
  22. WiFiClientSecure client;
  23.  
  24.  
  25. float h =1;
  26. float t=2;
  27. int m=3;
  28.  
  29. void setup() {
  30.  
  31. // HTTPS is used without checking credentials
  32. client.setInsecure();
  33.  
  34. // Connect to the WIFI
  35. Serial.begin(115200);
  36.  
  37. Serial.print("Connecting to ");
  38. Serial.println(ssid);
  39. WiFi.begin(ssid, password);
  40. while (WiFi.status() != WL_CONNECTED) {
  41. delay(500);
  42. Serial.print(".");
  43. }
  44.  
  45. // Print local IP address
  46. Serial.println("");
  47. Serial.println("WiFi connected.");
  48. Serial.println("IP address: ");
  49. Serial.println(WiFi.localIP());
  50. }
  51.  
  52. void loop() {
  53.  
  54. // If connected to the internet turn the Builtin led On and attempt to send a message to the database
  55. if (WiFi.status() == WL_CONNECTED) {
  56.  
  57. // Read all sensors
  58. h += 1;
  59. t += 1;
  60. m += 1;
  61.  
  62. // Send the a post request to the server
  63. https.begin(client,API_URL+"/rest/v1/"+TableName);
  64. https.addHeader("Content-Type", "application/json");
  65. https.addHeader("Prefer", "return=representation");
  66. https.addHeader("apikey", API_KEY);
  67. https.addHeader("Authorization", "Bearer " + API_KEY);
  68. int httpCode = https.POST("{\"temperature\":" + String(t)+ ",\"humidity\":"+ String(h)+",\"moisture\":" + String(1024 - m)+"}" ); //Send the request
  69. String payload = https.getString();
  70. Serial.println(httpCode); //Print HTTP return code
  71. Serial.println(payload); //Print request response payload
  72. https.end();
  73.  
  74.  
  75. }else{
  76. Serial.println("Error in WiFi connection");
  77. }
  78. delay(1000*sendinginterval); //wait to send the next request
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement