Skip to content

Commit

Permalink
feat(requests): fixed wifi and added ability to request to server to …
Browse files Browse the repository at this point in the history
…get playback info
  • Loading branch information
SreeDan committed Jun 29, 2024
1 parent bb0d43a commit e256084
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 35 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
workspace = { members = [".", "graphics", "graphics_sim"] }
workspace = { members = [".", "graphics", "graphics_sim", "models"] }
[package]
name = "spotify-esp"
version = "0.1.0"
Expand Down Expand Up @@ -41,6 +41,9 @@ embedded-svc = "=0.27.1"
heapless = "0.8.0"
dotenv = "0.15.0"
image = "0.25.1"
serde_json = "1.0.118"
models = { path = "models" }
graphics = { path = "graphics" }

[build-dependencies]
embuild = "0.31.3"
1 change: 1 addition & 0 deletions api_url.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
http://23.20.233.1:8080/current_playback
9 changes: 9 additions & 0 deletions models/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "models"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rspotify = "0.13.2"
27 changes: 27 additions & 0 deletions models/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use rspotify::model::{Device, RepeatState};

#[derive(Debug, Clone, Serialize)]
pub struct Artist {
name: String,
url: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct Track {
name: String,
artists: Vec<Artist>,
image_url: Option<String>,
url: Option<String>,
duration: u32,
}

#[derive(Debug, Clone, Serialize)]
pub struct CurrentlyPlaying {
device: Device,
track: Track,
progress_secs: u32,
shuffled: bool,
playing: bool,
repeat_status: RepeatState,
}

118 changes: 84 additions & 34 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
use dotenv::dotenv;
use esp_idf_hal::peripherals::Peripherals;
use embedded_hal::delay::DelayNs;
use embedded_svc::http::{client::Client, Headers, Status};
use esp_idf_hal::{delay::Delay, io::Read, peripherals::Peripherals, sys::esp_crt_bundle_attach};
use esp_idf_svc::{
eventloop::EspSystemEventLoop, nvs::EspDefaultNvsPartition,
wifi::EspWifi,
eventloop::EspSystemEventLoop,
http::client::{Configuration as HttpConfig, EspHttpConnection},
nvs::EspDefaultNvsPartition,
wifi::{BlockingWifi, EspWifi},
};
use models::CurrentlyPlaying;
use serde_json::json;
use std::{
thread::{self, sleep},
time::Duration,
};
use std::{thread::sleep, time::Duration};

fn main() {
esp_idf_svc::sys::link_patches();
Expand All @@ -15,50 +24,91 @@ fn main() {
let sys_loop = EspSystemEventLoop::take().unwrap();
let nvs = EspDefaultNvsPartition::take().unwrap();

let wifi_driver = EspWifi::new(
peripherals.modem,
// let mut wifi_driver = EspWifi::new(peripherals.modem, sys_loop, Some(nvs)).unwrap();
let mut wifi_driver = BlockingWifi::wrap(
EspWifi::new(peripherals.modem, sys_loop.clone(), Some(nvs)).unwrap(),
sys_loop,
Some(nvs)
).unwrap();
)
.unwrap();

wifi(&mut wifi_driver);

let httpconnection = EspHttpConnection::new(&HttpConfig {
// use_global_ca_store: true,
crt_bundle_attach: Some(esp_crt_bundle_attach),
..Default::default()
})
.expect("Could not establish http connection");

let mut httpclient = Client::wrap(httpconnection);

let check_playback = thread::spawn(|| {
loop {
let httpconnection = EspHttpConnection::new(&HttpConfig {
// use_global_ca_store: true,
crt_bundle_attach: Some(esp_crt_bundle_attach),
..Default::default()
})
.expect("Could not establish http connection");

let mut httpclient = Client::wrap(httpconnection);
let url_root = include_str!("../api_url.txt");
let formatted_url = std::format!("{}/current_playback", url_root);

let request = httpclient
.get(&formatted_url)
.expect("could not send current_playback request");

let mut response = request.submit().expect("could not get response");

let mut buf = vec![0u8; response.content_len().unwrap() as usize];
response.read_exact(&mut buf).unwrap();

let response_str = std::str::from_utf8(&buf);
let playing_json: Result<Option<CurrentlyPlaying>> =
serde_json::from_slice(&buf).unwrap();

wifi(wifi_driver);
if let Err(_) = playing_json {
Delay::new_default().delay_ms(5000);
continue;
}

println!("{:?}", playing_json);

Delay::new_default().delay_ms(1000);
}
});

loop {
Delay::new_default().delay_ms(1);
}
}

fn wifi(mut wifi_driver: EspWifi) {
fn wifi(wifi_driver: &mut BlockingWifi<EspWifi>) {
// TODO: Make wifi work for enterprise networks -> I will need it for college wifi
let wifi_ssid = include_str!("../wifi_ssid.txt");
let wifi_password = include_str!("../wifi_password.txt");
let auth_token = include_str!("../auth_token.txt");

wifi_driver
.set_configuration(&embedded_svc::wifi::Configuration::Client(embedded_svc::wifi::ClientConfiguration {
ssid: heapless::String::try_from(wifi_ssid).unwrap(),
password: heapless::String::try_from(wifi_password).unwrap(),
..Default::default()
}))
.set_configuration(&embedded_svc::wifi::Configuration::Client(
embedded_svc::wifi::ClientConfiguration {
ssid: heapless::String::try_from(wifi_ssid).unwrap(),
password: heapless::String::try_from(wifi_password).unwrap(),
..Default::default()
},
))
.unwrap();

wifi_driver.start().unwrap();
wifi_driver.connect().unwrap();
let mut seconds = 1;

while !wifi_driver.is_connected().unwrap() {
let config = wifi_driver.get_configuration().unwrap();
println!(
"Waiting for station {:?}, waiting {seconds} seconds",
config
);
sleep(Duration::from_secs(seconds));
seconds *= 2;
}

// let mut client = HttpClient::wrap(EspHttpConnection::new(&Default::default())?);
println!("Waiting for wifi connection");
wifi_driver.wait_netif_up().unwrap();

loop {
println!(
"Connected to IP {:?}",
wifi_driver.sta_netif().get_ip_info().unwrap()
);
sleep(Duration::from_secs(1));
}
}
println!(
"Connected to IP {:?}",
wifi_driver.wifi().sta_netif().get_ip_info()
);
}

0 comments on commit e256084

Please sign in to comment.