Skip to content

Instantly share code, notes, and snippets.

@weiying-chen
Created March 25, 2024 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weiying-chen/af32ce18a2848ca3df8f763c83337cda to your computer and use it in GitHub Desktop.
Save weiying-chen/af32ce18a2848ca3df8f763c83337cda to your computer and use it in GitHub Desktop.
use reqwest::{
header::{HeaderMap, HeaderValue, AUTHORIZATION, CONTENT_TYPE},
Client,
};
use serde_json::json;
use std::error::Error;
use tokio::time::{sleep, Duration};
async fn post_request(client: &Client, payload: serde_json::Value) -> Result<(), Box<dyn Error>> {
let supabase_url = "https://your_supabase_url_here";
let supabase_key = "your_supabase_key_here";
let mut headers = HeaderMap::new();
headers.insert("apikey", HeaderValue::from_str(supabase_key)?);
headers.insert(
AUTHORIZATION,
HeaderValue::from_str(&format!("Bearer {}", supabase_key))?,
);
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
let response = client
.post(supabase_url)
.headers(headers)
.json(&payload)
.send()
.await?;
// You can log or inspect the response here
println!("Message sent successfully. Status: {}", response.status());
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let mut iterations = 0;
let client = Client::new();
loop {
if true {
// Replace this with your condition, e.g., `scale.is_ready()`
println!("Iteration {}", iterations);
let rounded_reading = 100; // Simulate reading. Replace with `scale.read_rounded().unwrap()`
let message = format!("This is a message from ESP32: {} g", rounded_reading);
let payload = json!({
"content": message
});
post_request(&client, payload).await?;
println!("Post request sent!");
}
sleep(Duration::from_secs(5)).await;
iterations += 1;
println!("Iteration: {}", iterations);
if iterations >= 10 {
break;
}
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment