diff --git a/.cargo/config.toml b/.cargo/config.toml index d8987aa..d1a938b 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,16 +1,20 @@ -[target.xtensa-esp32-none-elf] -runner = "espflash flash --monitor" + [target.xtensa-esp32-none-elf] + runner = "espflash flash --monitor" -[env] -ESP_LOG="info" + [env] + ESP_LOG="info" + ESP_WIFI_CONFIG_RX_QUEUE_SIZE = "16" + ESP_WIFI_CONFIG_STATIC_RX_BUF_NUM = "32" + ESP_WIFI_CONFIG_DYNAMIC_RX_BUF_NUM = "16" -[build] -rustflags = [ - "-C", "link-arg=-nostartfiles", -] + [build] + rustflags = [ + "-C", "link-arg=-nostartfiles" + ] -target = "xtensa-esp32-none-elf" + target = "xtensa-esp32-none-elf" -[unstable] -build-std = ["core"] + [unstable] + build-std = ["core", "alloc"] + \ No newline at end of file diff --git a/.gitignore b/.gitignore index 73fab07..ed84e50 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ target/ # MSVC Windows builds of rustc generate these, which store debugging information *.pdb + +.env* \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index a997cfa..bf76080 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,12 +14,6 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" -[[package]] -name = "az" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b7e4c2464d97fe331d41de9d5db0def0a96f4d823b8b32a2efd503578988973" - [[package]] name = "basic-toml" version = "0.1.10" @@ -35,6 +29,12 @@ version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f798d2d157e547aa99aab0967df39edd0b70307312b6f8bd2848e6abe40896e0" +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "bitflags" version = "2.9.0" @@ -42,10 +42,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" [[package]] -name = "byte-slice-cast" -version = "1.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7575182f7272186991736b70173b0ea045398f984bf5ebbb3804736ce1330c9d" +name = "blocking-network-stack" +version = "0.1.0" +source = "git+https://github.com/bjoernQ/blocking-network-stack.git?rev=b3ecefc222d8806edd221f266999ca339c52d34e#b3ecefc222d8806edd221f266999ca339c52d34e" +dependencies = [ + "embedded-io", + "heapless", + "log", + "smoltcp", +] [[package]] name = "bytemuck" @@ -101,7 +106,7 @@ dependencies = [ "proc-macro2", "quote", "strsim", - "syn 2.0.100", + "syn", ] [[package]] @@ -112,7 +117,7 @@ checksum = "fc34b93ccb385b40dc71c6fceac4b2ad23662c7eeb248cf10d529b7e055b6ead" dependencies = [ "darling_core", "quote", - "syn 2.0.100", + "syn", ] [[package]] @@ -123,36 +128,7 @@ checksum = "b9b6483c2bbed26f97861cf57651d4f2b731964a28cd2257f934a4b452480d21" dependencies = [ "proc-macro2", "quote", - "syn 2.0.100", -] - -[[package]] -name = "display-interface" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ba2aab1ef3793e6f7804162debb5ac5edb93b3d650fbcc5aeb72fcd0e6c03a0" - -[[package]] -name = "display-interface-i2c" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d964fa85bbbb5a6ecd06e58699407ac5dc3e3ad72dac0ab7e6b0d00a1cd262d" -dependencies = [ - "display-interface", - "embedded-hal 1.0.0", - "embedded-hal-async", -] - -[[package]] -name = "display-interface-spi" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f86b9ec30048b1955da2038fcc3c017f419ab21bb0001879d16c0a3749dc6b7a" -dependencies = [ - "byte-slice-cast", - "display-interface", - "embedded-hal 1.0.0", - "embedded-hal-async", + "syn", ] [[package]] @@ -187,6 +163,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1f878075b9794c1e4ac788c95b728f26aa6366d32eeb10c7051389f898f7d067" +[[package]] +name = "embassy-net-driver" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "524eb3c489760508f71360112bca70f6e53173e6fe48fc5f0efd0f5ab217751d" + [[package]] name = "embassy-sync" version = "0.6.2" @@ -235,29 +217,6 @@ dependencies = [ "nb 1.1.0", ] -[[package]] -name = "embedded-graphics" -version = "0.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0649998afacf6d575d126d83e68b78c0ab0e00ca2ac7e9b3db11b4cbe8274ef0" -dependencies = [ - "az", - "byteorder", - "embedded-graphics-core", - "float-cmp", - "micromath", -] - -[[package]] -name = "embedded-graphics-core" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba9ecd261f991856250d2207f6d8376946cd9f412a2165d3b75bc87a0bc7a044" -dependencies = [ - "az", - "byteorder", -] - [[package]] name = "embedded-hal" version = "0.2.7" @@ -332,7 +291,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.100", + "syn", ] [[package]] @@ -353,7 +312,7 @@ dependencies = [ "darling", "proc-macro2", "quote", - "syn 2.0.100", + "syn", ] [[package]] @@ -362,6 +321,19 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" +[[package]] +name = "esp-alloc" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "408c0d4c7f51efb256af18641047b0d83b58a485be9edf5a559da796abef0b63" +dependencies = [ + "cfg-if", + "critical-section", + "document-features", + "enumset", + "linked_list_allocator", +] + [[package]] name = "esp-backtrace" version = "0.15.1" @@ -380,7 +352,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8aa1c8f9954c9506699cf1ca10a2adcc226ff10b6ae3cb9e875cf2c6a0b9a372" dependencies = [ "quote", - "syn 2.0.100", + "syn", "termcolor", ] @@ -401,7 +373,7 @@ checksum = "a573175c540fd1d21a9cf07b0dee286b5a8f4cfde4b35da0f4f4657de7942c45" dependencies = [ "basic-toml", "bitfield", - "bitflags", + "bitflags 2.9.0", "bytemuck", "cfg-if", "chrono", @@ -452,7 +424,7 @@ dependencies = [ "proc-macro-error2", "proc-macro2", "quote", - "syn 2.0.100", + "syn", ] [[package]] @@ -490,6 +462,47 @@ dependencies = [ "riscv-rt-macros", ] +[[package]] +name = "esp-wifi" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "321b112db6629693fae49644b1ba28e8b917b58829533e9cdd0400183af1877d" +dependencies = [ + "cfg-if", + "critical-section", + "document-features", + "embassy-net-driver", + "embassy-sync", + "embedded-io", + "embedded-io-async", + "enumset", + "esp-alloc", + "esp-build", + "esp-config", + "esp-hal", + "esp-metadata", + "esp-wifi-sys", + "fugit", + "heapless", + "libm", + "num-derive", + "num-traits", + "portable-atomic", + "portable_atomic_enum", + "rand_core", + "smoltcp", + "xtensa-lx-rt", +] + +[[package]] +name = "esp-wifi-sys" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6b5438361891c431970194a733415006fb3d00b6eb70b3dcb66fd58f04d9b39" +dependencies = [ + "anyhow", +] + [[package]] name = "esp32" version = "0.35.0" @@ -500,15 +513,6 @@ dependencies = [ "vcell", ] -[[package]] -name = "float-cmp" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4" -dependencies = [ - "num-traits", -] - [[package]] name = "fnv" version = "1.0.7" @@ -582,6 +586,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad" dependencies = [ "hash32", + "portable-atomic", "stable_deref_trait", ] @@ -595,12 +600,16 @@ checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" name = "helloworld" version = "0.1.0" dependencies = [ - "embedded-graphics", + "blocking-network-stack", + "embedded-io", + "esp-alloc", "esp-backtrace", "esp-hal", "esp-println", + "esp-wifi", + "heapless", "log", - "ssd1306", + "smoltcp", ] [[package]] @@ -635,9 +644,21 @@ dependencies = [ "indoc", "proc-macro2", "quote", - "syn 2.0.100", + "syn", ] +[[package]] +name = "libm" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8355be11b20d696c8f18f6cc018c4e372165b1fa8126cef092399c9951984ffa" + +[[package]] +name = "linked_list_allocator" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9afa463f5405ee81cdb9cc2baf37e08ec7e4c8209442b5d72c04cfb2cd6e6286" + [[package]] name = "litrs" version = "0.4.1" @@ -654,17 +675,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" [[package]] -name = "maybe-async-cfg" -version = "0.2.4" +name = "managed" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1e083394889336bc66a4eaf1011ffbfa74893e910f902a9f271fa624c61e1b2" -dependencies = [ - "proc-macro-error", - "proc-macro2", - "pulldown-cmark", - "quote", - "syn 1.0.109", -] +checksum = "0ca88d725a0a943b096803bd34e73a4437208b6077654cc4ecb2947a5f91618d" [[package]] name = "memchr" @@ -672,12 +686,6 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" -[[package]] -name = "micromath" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3c8dda44ff03a2f238717214da50f65d5a53b45cd213a7370424ffdb6fae815" - [[package]] name = "minijinja" version = "2.9.0" @@ -702,6 +710,17 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8d5439c4ad607c3c23abf66de8c8bf57ba8adcd1f129e699851a6e43935d339d" +[[package]] +name = "num-derive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -735,6 +754,27 @@ version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "350e9b48cbc6b0e028b0473b114454c6316e57336ee184ceab6e53f72c178b3e" +[[package]] +name = "portable_atomic_enum" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30d48f60c43e0120bb2bb48589a16d4bed2f4b911be41e299f2d0fc0e0e20885" +dependencies = [ + "portable-atomic", + "portable_atomic_enum_macros", +] + +[[package]] +name = "portable_atomic_enum_macros" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a33fa6ec7f2047f572d49317cca19c87195de99c6e5b6ee492da701cfe02b053" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "proc-macro-crate" version = "3.3.0" @@ -744,30 +784,6 @@ dependencies = [ "toml_edit", ] -[[package]] -name = "proc-macro-error" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" -dependencies = [ - "proc-macro-error-attr", - "proc-macro2", - "quote", - "syn 1.0.109", - "version_check", -] - -[[package]] -name = "proc-macro-error-attr" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" -dependencies = [ - "proc-macro2", - "quote", - "version_check", -] - [[package]] name = "proc-macro-error-attr2" version = "2.0.0" @@ -787,7 +803,7 @@ dependencies = [ "proc-macro-error-attr2", "proc-macro2", "quote", - "syn 2.0.100", + "syn", ] [[package]] @@ -799,17 +815,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "pulldown-cmark" -version = "0.11.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "679341d22c78c6c649893cbd6c3278dcbe9fc4faa62fea3a9296ae2b50c14625" -dependencies = [ - "bitflags", - "memchr", - "unicase", -] - [[package]] name = "quote" version = "1.0.40" @@ -852,7 +857,7 @@ checksum = "f265be5d634272320a7de94cea15c22a3bfdd4eb42eb43edc528415f066a1f25" dependencies = [ "proc-macro2", "quote", - "syn 2.0.100", + "syn", ] [[package]] @@ -869,7 +874,7 @@ checksum = "30f19a85fe107b65031e0ba8ec60c34c2494069fe910d6c297f5e7cb5a6f76d0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.100", + "syn", ] [[package]] @@ -901,7 +906,7 @@ checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" dependencies = [ "proc-macro2", "quote", - "syn 2.0.100", + "syn", ] [[package]] @@ -914,17 +919,17 @@ dependencies = [ ] [[package]] -name = "ssd1306" -version = "0.10.0" +name = "smoltcp" +version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ea6aac2d078bbc71d9b8ac3f657335311f3b6625e9a1a96ccc29f5abfa77c56" +checksum = "dad095989c1533c1c266d9b1e8d70a1329dd3723c3edac6d03bbd67e7bf6f4bb" dependencies = [ - "display-interface", - "display-interface-i2c", - "display-interface-spi", - "embedded-graphics-core", - "embedded-hal 1.0.0", - "maybe-async-cfg", + "bitflags 1.3.2", + "byteorder", + "cfg-if", + "heapless", + "log", + "managed", ] [[package]] @@ -958,18 +963,7 @@ dependencies = [ "proc-macro2", "quote", "rustversion", - "syn 2.0.100", -] - -[[package]] -name = "syn" -version = "1.0.109" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", + "syn", ] [[package]] @@ -1032,12 +1026,6 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e87a2ed6b42ec5e28cc3b94c09982969e9227600b2e3dcbc1db927a84c06bd69" -[[package]] -name = "unicase" -version = "2.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75b844d17643ee918803943289730bec8aac480150456169e647ed0b576ba539" - [[package]] name = "unicode-ident" version = "1.0.18" @@ -1050,12 +1038,6 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77439c1b53d2303b20d9459b1ade71a83c716e3f9c34f3228c00e6f185d6c002" -[[package]] -name = "version_check" -version = "0.9.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" - [[package]] name = "void" version = "1.0.2" @@ -1190,5 +1172,5 @@ dependencies = [ "darling", "proc-macro2", "quote", - "syn 2.0.100", + "syn", ] diff --git a/Cargo.toml b/Cargo.toml index 03afef2..08739a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" license = "MIT OR Apache-2.0" [dependencies] -embedded-graphics = "0.8.1" +esp-alloc = "0.6.0" esp-backtrace = { version = "0.15.1", features = [ "esp32", "exception-handler", @@ -15,12 +15,13 @@ esp-backtrace = { version = "0.15.1", features = [ ] } esp-hal = { version = "0.23.1", features = [ "esp32" ] } esp-println = { version = "0.13.1", features = ["esp32", "log"] } +esp-wifi = { version = "0.12.0", features = ["esp32","wifi", "utils"] } log = { version = "0.4.22" } -ssd1306 = "0.10.0" -[profile.dev] -# Rust debug is too slow. -# For debug builds always builds with some optimization -opt-level = "s" +smoltcp = { version = "0.12.0", features = ["proto-ipv4", "socket-tcp", "socket-icmp", "socket-udp", "medium-ethernet", "proto-dhcpv4", "socket-raw", "socket-dhcpv4"], default-features = false } +heapless = { version = "0.8.0"} + +blocking-network-stack = { git = "https://github.com/bjoernQ/blocking-network-stack.git", rev = "b3ecefc222d8806edd221f266999ca339c52d34e" } +embedded-io = "0.6.1" [profile.release] codegen-units = 1 # LLVM can perform better optimizations using a single thread @@ -29,4 +30,4 @@ debug-assertions = false incremental = false lto = 'fat' opt-level = 's' -overflow-checks = false \ No newline at end of file +overflow-checks = false diff --git a/README.md b/README.md index 06c3eba..da69cb9 100644 --- a/README.md +++ b/README.md @@ -1,30 +1,40 @@ -# LoRa HelloWorld Project +# ESP32 Static IP Project -Welcome to the **LoRa HelloWorld** project! This is a simple project designed to demonstrate basic usage of the i2C interface using a ssd1306 display. The Lora model used was a Lora32 T3 v1.6.1 +Welcome to the **ESP32 Static IP** project! This is a working example of how to configure a static IP using `non_std`. ## Features -- Prints "Hello, World!" on a ssd1306 display. +- Connect to your static IP and serve an image. +- Tested on a Lora32 T3 v1.6.1. ## Requirements -- espup installed -- rust installed +- [espup](https://github.com/esp-rs/espup) installed +- [Rust](https://www.rust-lang.org/) installed ## Installation 1. Clone the repository: ```bash - git clone https://github.com/bibble235/lora-helloworld.git + git clone https://github.com/bibble235/esp32-static-ip.git ``` + 2. Navigate to the project directory: ```bash - cd lora-helloworld + cd esp32-static-ip + ``` + +3. Set the environment variables for your static IP and Wi-Fi credentials. Remember to escape special characters like `@` and `$`: + ```bash + export SSID="MyWifi" + export PASSWORD="MyPassword" + export STATIC_IP="10.1.2.3" + export GATEWAY_IP="10.1.1.99" ``` ## Usage -Run the application with the following command: +Build and run the application with the following commands: ```bash cargo build cargo run @@ -32,14 +42,19 @@ cargo run ## License -This project is licensed under the [License Name] - see the [LICENSE](LICENSE) file for details. +This project is licensed under the [License Name]. See the [LICENSE](LICENSE) file for details. ## Demonstration -![Screenshot of a Lora32 T3 v1.6.1 running this app.](./assets/images/Lora_ssd1306_hello.png) + +Below is a screenshot of a Lora32 T3 v1.6.1 running this application: + +![Screenshot of a Lora32 T3 v1.6.1 running this app.](./assets/images/esp32-static-ip.png) ## Contact -For questions or feedback, please contact iwiseman@bibble.co.nz. +For questions or feedback, please contact [iwiseman@bibble.co.nz](mailto:iwiseman@bibble.co.nz). + +--- ![Rust](https://img.shields.io/badge/Rust-Language-black?logo=rust&logoColor=white) ![ESP32](https://img.shields.io/badge/ESP32-Microcontroller-blue) \ No newline at end of file diff --git a/assets/images/Lora_ssd1306_hello.png b/assets/images/Lora_ssd1306_hello.png deleted file mode 100644 index f5908cb..0000000 Binary files a/assets/images/Lora_ssd1306_hello.png and /dev/null differ diff --git a/assets/images/esp32-static-ip.png b/assets/images/esp32-static-ip.png new file mode 100644 index 0000000..f59ec96 Binary files /dev/null and b/assets/images/esp32-static-ip.png differ diff --git a/src/main.rs b/src/main.rs index c94cafc..5f366b4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,49 +1,214 @@ #![no_std] #![no_main] +extern crate alloc; + +use blocking_network_stack::Stack; + +use embedded_io::*; -use embedded_graphics::{mono_font::{ascii::FONT_6X10, MonoTextStyleBuilder}, pixelcolor::BinaryColor, prelude::Point, text::{Baseline, Text}, Drawable}; use esp_backtrace as _; -use esp_hal::{delay::Delay, i2c::master::{Config, I2c}, time::Duration, xtensa_lx_rt::entry}; -use ssd1306::{mode::DisplayConfig, prelude::DisplayRotation, size::DisplaySize128x64, I2CDisplayInterface, Ssd1306}; +use esp_hal::{ + clock::CpuClock, delay::Delay, rng::Rng, time::{self, Duration}, timer::timg::TimerGroup, xtensa_lx_rt::entry +}; - #[entry] - fn main() -> ! { - let peripherals = esp_hal::init(esp_hal::Config::default()); - let delay = Delay::new(); - // let system = SystemControl::new(peripherals.SYSTEM); - // let delay = Delay::new(); +use esp_wifi::wifi::{utils::create_network_interface, AccessPointInfo, AuthMethod, ClientConfiguration, Configuration, WifiError, WifiStaDevice}; +use smoltcp::iface::{SocketSet, SocketStorage}; - esp_println::logger::init_logger_from_env(); +fn parse_ip(ip: &str) -> [u8; 4] { + let mut result = [0u8; 4]; + for (idx, octet) in ip.split(".").into_iter().enumerate() { + result[idx] = u8::from_str_radix(octet, 10).unwrap(); + } + result +} - // Create a new peripheral object with the described wiring - // and standard I2C clock speed - let i2c = I2c::new( - peripherals.I2C0, - Config::default() - ).unwrap().with_scl(peripherals.GPIO22).with_sda(peripherals.GPIO21); +#[entry] +fn main() -> ! { - let interface = I2CDisplayInterface::new(i2c); - let mut display = Ssd1306::new(interface, DisplaySize128x64, DisplayRotation::Rotate0) - .into_buffered_graphics_mode(); - display.init().unwrap(); + // Initialize the ESP-IDF logging system + esp_println::logger::init_logger(log::LevelFilter::Info); - let text_style = MonoTextStyleBuilder::new() - .font(&FONT_6X10) - .text_color(BinaryColor::On) - .build(); + // Setup the cpu clock config and initialize + let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max()); + let peripherals = esp_hal::init(config); - Text::with_baseline("Hello world!", Point::zero(), text_style, Baseline::Top) - .draw(&mut display) - .unwrap(); + // Create a delay object + let delay = Delay::new(); - Text::with_baseline("Hello Rust!", Point::new(0, 16), text_style, Baseline::Top) - .draw(&mut display) - .unwrap(); + // You need to allocate here and you must use the same version as esp-wifi + esp_alloc::heap_allocator!(72 * 1024); - display.flush().unwrap(); - loop { - log::info!("Hello world!"); - delay.delay(Duration::millis(500)); + // Initialize the timer group + let timg0 = TimerGroup::new(peripherals.TIMG0); + + // Initialize the random number generator + let mut rng = Rng::new(peripherals.RNG); + + // Initialize the wifi controller + let esp_wifi_ctrl = esp_wifi::init( + timg0.timer0, + rng.clone(), + peripherals.RADIO_CLK, + ) + .unwrap(); + + // Set credentials from environment variables + const SSID: &str = env!("SSID"); + const PASSWORD: &str = env!("PASSWORD"); + const GATEWAY_IP: &str = env!("GATEWAY_IP"); + const STATIC_IP: &str = env!("STATIC_IP"); + + // Get the wifi Peripheral + let mut wifi = peripherals.WIFI; + + // Initialize the iface and device and the controller + let (iface, device, mut controller) = + create_network_interface(&esp_wifi_ctrl, &mut wifi, WifiStaDevice).unwrap(); + + // Create the socket set which is like the c struct sockaddr + let mut socket_set_entries: [SocketStorage; 3] = Default::default(); + let socket_set = SocketSet::new(&mut socket_set_entries[..]); + + // Create the stack + let now = || time::now().duration_since_epoch().to_millis(); + let mut wifi_stack = Stack::new(iface, device, socket_set, now, rng.random()); + + // Configure Authentication + let mut auth_method = AuthMethod::WPA2Personal; + if PASSWORD.is_empty() { + auth_method = AuthMethod::None; + } + + // Configure the Wi-Fi client + let client_config = Configuration::Client(ClientConfiguration { + ssid: SSID.try_into().unwrap(), + password: PASSWORD.try_into().unwrap(), + auth_method, + ..Default::default() + }); + + let res = controller.set_configuration(&client_config); + log::info!("Wi-Fi set_configuration returned {:?}", res); + + controller.start().unwrap(); + log::info!("Is wifi started: {:?}", controller.is_started()); + + log::info!("Starting Wifi Scan"); + + // Scan for APs + let res: Result<(heapless::Vec, usize), WifiError> = controller.scan_n(); + if let Ok((res, _count)) = res { + for ap in res { + log::info!("{:?}", ap); + } + } + + log::info!("{:?}", controller.capabilities()); + log::info!("Wi-Fi connect: {:?}", controller.connect()); + + // wait to get connected + log::info!("Waiting to get connected"); + loop { + let res = controller.is_connected(); + match res { + Ok(connected) => { + if connected { + break; + } + } + Err(err) => { + log::info!("{:?}", err); + loop {} } } + } + log::info!("{:?}", controller.is_connected()); + + log::info!("Setting static IP {}", STATIC_IP); + + wifi_stack + .set_iface_configuration(&blocking_network_stack::ipv4::Configuration::Client( + blocking_network_stack::ipv4::ClientConfiguration::Fixed( + blocking_network_stack::ipv4::ClientSettings { + ip: blocking_network_stack::ipv4::Ipv4Addr::from(parse_ip(STATIC_IP)), + subnet: blocking_network_stack::ipv4::Subnet { + gateway: blocking_network_stack::ipv4::Ipv4Addr::from(parse_ip(GATEWAY_IP)), + mask: blocking_network_stack::ipv4::Mask(24), + }, + dns: None, + secondary_dns: None, + }, + ), + )) + .unwrap(); + + log::info!( + "Start busy loop on main. Point your browser to http://{}:8080/", + STATIC_IP + ); + + let mut rx_buffer = [0u8; 1536]; + let mut tx_buffer = [0u8; 1536]; + let mut socket = wifi_stack.get_socket(&mut rx_buffer, &mut tx_buffer); + + socket.listen(8080).unwrap(); + + loop { + socket.work(); + + if !socket.is_open() { + socket.listen(8080).unwrap(); + } + + if socket.is_connected() { + log::info!("Connected"); + + let mut time_out = false; + let deadline = time::now() + Duration::secs(20); + let mut buffer = [0u8; 1024]; + let mut pos = 0; + + while let Ok(len) = socket.read(&mut buffer[pos..]) { + let to_print = unsafe { core::str::from_utf8_unchecked(&buffer[..(pos + len)]) }; + + if to_print.contains("\r\n\r\n") { + log::info!("{}", to_print); + break; + } + + pos += len; + + if time::now() > deadline { + log::info!("Timeout"); + time_out = true; + break; + } + } + + if !time_out { + socket.write_all( + b"HTTP/1.0 200 OK\r\n\r\n\ + \ + \ +

Hello Rust! Hello esp-wifi!

\ + + \ + \r\n\ + " + ).unwrap(); + + socket.flush().unwrap(); + } + + socket.close(); + + log::info!("Done"); + } + + let deadline = time::now() + Duration::secs(5); + while time::now() < deadline { + socket.work(); + } + } +}