# CSI Project
###### tags: `CSI`
>
>
[期初報告](https://drive.google.com/file/d/1CvrSwlJjDZglbu5QYnSc84ktFCQvajGW/view?pli=1)
## 目標
### 裝置架設
- [x] routers準備好
- [x] 測試可以接收到CSI
>自行編譯的send/receive 執行檔不成功 用openwrt裡內建的 每50ms傳一次封包
- [ ] cross compile
>Cross-compile is the act of compiling code for one computer system on a different system. The
system the compiler—which is a program to turn source code into executable code—runs on is
called the host, and the system the new programs run on is called the target.
- [ ] real time
>[use for transfer data to laptop](https://github.com/shuspieler/Athero-CSI-tool-Python-RemoteReceive-Liveview-AmplitudeScaled)
>[putterer](https://github.com/putterer/csi-server)
- [ ] 處理資料
### preprocessing and modeling
- [ ] open dataset
:::info
https://figshare.com/articles/dataset/Dataset_for_Human_Activity_Recognition_using_Wi-Fi_Channel_State_Information_CSI_data/14386892/1
https://github.com/parisafm/CSI-HAR-Dataset
:::
- [ ] ..
- [ ] ..
## progress
try to complie [Athero csi tool 2.0](https://github.com/wands-wireless/Atheros_CSI_tool_OpenWRT_src) because i am facing low receving frequency issuse, try to improve buffer.
manly stuck on the step below.
> Update feeds of OpenWrt by the following commands:
>```
>$ ./scripts/feeds update -a
>$ ./scripts/feeds install -a
>```
```
hcc@hcc-Aspire-A715-71G:~/Atheros_CSI_tool_OpenWRT_src/scripts$ sudo ./feeds update -a
Updating feed 'packages' from 'https://git.openwrt.org/feed/packages.git' ...
Already up-to-date.
Create index file './feeds/packages.index'
fatal: Invalid revision range ee53a240ac902dc83209008a2671e7fdcf55957a..HEAD
fatal: Invalid revision range ee53a240ac902dc83209008a2671e7fdcf55957a..77818826c670bdf69a291e53aacfc477bba11d0b
fatal: Invalid revision range ee53a240ac902dc83209008a2671e7fdcf55957a..HEAD
fatal: Invalid revision range ee53a240ac902dc83209008a2671e7fdcf55957a..77818826c670bdf69a291e53aacfc477bba11d0b
fatal: Invalid revision range ee53a240ac902dc83209008a2671e7fdcf55957a..HEAD
fatal: Invalid revision range ee53a240ac902dc83209008a2671e7fdcf55957a..77818826c670bdf69a291e53aacfc477bba11d0b
Updating feed 'luci' from 'https://git.openwrt.org/project/luci.git' ...
Already up-to-date.
Create index file './feeds/luci.index'
fatal: Invalid revision range ee53a240ac902dc83209008a2671e7fdcf55957a..HEAD
fatal: Invalid revision range ee53a240ac902dc83209008a2671e7fdcf55957a..77818826c670bdf69a291e53aacfc477bba11d0b
fatal: Invalid revision range ee53a240ac902dc83209008a2671e7fdcf55957a..HEAD
fatal: Invalid revision range ee53a240ac902dc83209008a2671e7fdcf55957a..77818826c670bdf69a291e53aacfc477bba11d0b
fatal: Invalid revision range ee53a240ac902dc83209008a2671e7fdcf55957a..HEAD
fatal: Invalid revision range ee53a240ac902dc83209008a2671e7fdcf55957a..77818826c670bdf69a291e53aacfc477bba11d0b
Updating feed 'routing' from 'https://git.openwrt.org/feed/routing.git' ...
Already up-to-date.
Create index file './feeds/routing.index'
fatal: Invalid revision range ee53a240ac902dc83209008a2671e7fdcf55957a..HEAD
fatal: Invalid revision range ee53a240ac902dc83209008a2671e7fdcf55957a..77818826c670bdf69a291e53aacfc477bba11d0b
fatal: Invalid revision range ee53a240ac902dc83209008a2671e7fdcf55957a..HEAD
fatal: Invalid revision range ee53a240ac902dc83209008a2671e7fdcf55957a..77818826c670bdf69a291e53aacfc477bba11d0b
fatal: Invalid revision range ee53a240ac902dc83209008a2671e7fdcf55957a..HEAD
fatal: Invalid revision range ee53a240ac902dc83209008a2671e7fdcf55957a..77818826c670bdf69a291e53aacfc477bba11d0b
Updating feed 'telephony' from 'https://git.openwrt.org/feed/telephony.git' ...
Already up-to-date.
Create index file './feeds/telephony.index'
fatal: Invalid revision range ee53a240ac902dc83209008a2671e7fdcf55957a..HEAD
fatal: Invalid revision range ee53a240ac902dc83209008a2671e7fdcf55957a..77818826c670bdf69a291e53aacfc477bba11d0b
fatal: Invalid revision range ee53a240ac902dc83209008a2671e7fdcf55957a..HEAD
fatal: Invalid revision range ee53a240ac902dc83209008a2671e7fdcf55957a..77818826c670bdf69a291e53aacfc477bba11d0b
fatal: Invalid revision range ee53a240ac902dc83209008a2671e7fdcf55957a..HEAD
fatal: Invalid revision range ee53a240ac902dc83209008a2671e7fdcf55957a..77818826c670bdf69a291e53aacfc477bba11d0b
```
try to fix the error
[reference](https://github.com/dragino/openwrt_lede-18.06/issues/2)
after I change `REBOOT=77818826c670bdf69a291e53aacfc477bba11d0b` in /scripts/getver.sh it work.
then try to install
```
hcc@hcc-Aspire-A715-71G:~/Atheros_CSI_tool_OpenWRT_src/scripts$ sudo ./feeds install -a
```
a lot of warning happen, and can just capture a part of it.
```
WARNING: Makefile 'package/feeds/telephony/spandsp/Makefile' has a dependency on 'libtiff', which does not exist
WARNING: Makefile 'package/feeds/telephony/spandsp3/Makefile' has a dependency on 'libjpeg-turbo', which does not exist
WARNING: Makefile 'package/feeds/telephony/spandsp3/Makefile' has a dependency on 'libtiff', which does not exist
WARNING: Makefile 'package/feeds/luci/ucode-mod-html/Makefile' has a dependency on 'libucode', which does not exist
WARNING: Makefile 'package/feeds/luci/ucode-mod-lua/Makefile' has a dependency on 'libucode', which does not exist
WARNING: Makefile 'package/feeds/telephony/yate/Makefile' has a dependency on 'perlbase-data', which does not exist
WARNING: Makefile 'package/feeds/telephony/yate/Makefile' has a dependency on 'alsa-lib', which does not exist
WARNING: Makefile 'package/feeds/telephony/yate/Makefile' has a dependency on 'libmysqlclient-r', which does not exist
WARNING: Makefile 'package/feeds/telephony/yate/Makefile' has a dependency on 'libpq', which does not exist
WARNING: Makefile 'package/feeds/telephony/yate/Makefile' has a dependency on 'libspeex', which does not exist
WARNING: Makefile 'package/feeds/telephony/yate/Makefile' has a dependency on 'libsqlite3', which does not exist
WARNING: Makefile 'package/feeds/telephony/yate/Makefile' has a build dependency on 'fakeuname/host', which does not exist
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
Installing all packages from feed packages.
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
feeds/packages/net/gnunet/Makefile: invalid group spec :postgres=5432
Installing all packages from feed luci.
Installing all packages from feed routing.
Installing all packages from feed telephony.
```
[refence1](https://github.com/xieyaxiongfly/Atheros_CSI_tool_OpenWRT_src/issues/63)
althought i am not using root user but i still try to use the method
`export FORCE_UNSAFE_CONFIGURE=1`
And then I run it again after clearing up / deleteing and recloning the repo as a normal user. find out it wasn't working...
[refence2](https://forum.openwrt.org/t/run-scripts-feeds-update-a-and-scripts-feeds-install-a-have-some-warning/68197/2)
I try to remove the tmp dir in buildroot , and nothing work.
https://github.com/openwrt/openwrt/issues/9645
Someone said "The OpenWrt version of the ath csi tool is very old (IIRC it's CC or DD) so it will not be able to fetch the current feeds correctly."
### try to using ubuntu ver.
https://github.com/wands-wireless/Atheros-CSI-Tool
https://github.com/xieyaxiongfly/Atheros_CSI_tool_OpenWRT_src/wiki/Collect-CSI#ap-client-mode
`cd /Atheros-CSI-Tool-UserSpace-APP/hostapd-2.5/hostapd `
`./hostapd`
## Model
### ...
## ..
### ..
## memo
### terminal instruction
```
ssh root@192.168.1.2
scp /home/hcc/Atheros-CSI-Tool-UserSpace-APP-old/recvCSI/recv_csi
scp -r ./Client-Router root@192.168.1.2:/root/
root@192.168.1.2:/root/
./sendData wlan0 MACaddr
sudo ./recv_csi filename
```
### referance web
[tool web](https://wands.sg/research/wifi/AtherosCSI/#Credits)
[youtube](https://www.youtube.com/watch?v=5l0wP_kMbc8)
[use for transfer data to laptop](https://github.com/shuspieler/Athero-CSI-tool-Python-RemoteReceive-Liveview-AmplitudeScaled)
[set the cross compile environment](https://oldwiki.archive.openwrt.org/doc/devel/crosscompile)
[Atheros-CSI-Tool介绍与使用](https://segmentfault.com/a/1190000022191613)
[Atheros-CSI-Tool-User-Guide(OpenWrt)](https://wands.sg/research/wifi/AtherosCSI/document/Atheros-CSI-Tool-User-Guide(OpenWRT))
[crosscompile](https://oldwiki.archive.openwrt.org/doc/devel/crosscompile)
:rocket:
## ESP-32
### AI-Thinker NodeMCU ESP32-S V1.3

[Nodemcu-32 v1.3 開發板規格書](https://docs.ai-thinker.com/_media/esp32/docs/nodemcu-32_v1.3%E8%A7%84%E6%A0%BC%E4%B9%A6_-.pdf)
[Nodemcu-32s WIFI 模組規格書 v1](https://docs.ai-thinker.com/_media/esp32/docs/nodemcu-32s_product_specification.pdf)
### ESP32-DevKitC V4 with esp32-wroom-32 module

[ESP32-DevKitC V4 started guided](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/hw-reference/esp32/get-started-devkitc.html)
[esp32-wroom-32 模組規格書](https://www.espressif.com/sites/default/files/documentation/esp32-wroom-32_datasheet_en.pdf)
> U結尾是外接ipex天線,D是板載天線
## 使用新的 CSI 工具 [ESP-CSI-Tool](https://github.com/StevenMHernandez/ESP32-CSI-Tool)
Better using in root mode.
```
. /home/hcc/esp/esp-idf/export.sh
```
This shortcut opens the Command Prompt and sets up all the required environment variables.
Then get into your project dir.
```
idf.py set-target esp32
idf.py menuconfig
```
Set the config file.
The following configurations are important for this project:
Serial flasher config > 'idf.py monitor' baud rate > Custom Baud Rate
Serial flasher config > Custom baud rate value > 1552000 This allows more data to be transmitted on the Serial port
Component config > Common ESP32-related > Channel for console output > Custom UART
Component config > Common ESP32-related > UART console baud rate > 1552000
Component config > Wi-Fi > WiFi CSI(Channel State Information) (Press space to select)
Component config > FreeRTOS > Tick rate (Hz) > 1000
ESP32 CSI Tool Config > **** all options in this menu can be specified per your experiment requirements.
for NODEMCU V.4 broad baud rate value 1552000 is not working well, can change to 1152000
```
idf.py build
```
Build bineary file.
```
idf.py -p PORT [-b BAUD] flash
```
flash the bineary file into your MCU.
or use `idf.py flash monitor` for direct monitor
```
idf.py monitor | python ../python_utils/serial_plot_csi_live.py
```
Using for cheak CSI data in real time.
run`pip install matplotlib numpy`first, every time you open a new terminal.
but don't know why, it pretty hard to run successfully.
Should cheak memory usage of my laptop.
## 使用espressif官方CSI工具[esp-csi](https://github.com/espressif/esp-csi)
```shell
. /home/hcc/esp/esp-idf-v4.4.1/export.sh
# csi_send
cd esp/esp-csi/examples/get-started/csi_send
idf.py set-target esp32
idf.py build
idf.py flash -b 921600 -p /dev/ttyUSB0 monitor
# csi_recv
cd esp/esp-csi/examples/get-started/csi_recv
idf.py set-target esp32
idf.py build
idf.py flash -b 921600 -p /dev/ttyUSB1
```
To exit monitoring, use ctrl+]
#### Run csi_data_read_parse.py in csi_recv for data analysis, Please close idf.py monitor before running
```
cd esp-csi-gitlab/examples/get-started/tools
# Install python related dependencies
pip install -r requirements.txt
# Graphical display
python csi_data_read_parse.py -p /dev/ttyUSB1 -s ./test.csv
```
## 心律偵測工具ky-039 用esp-idf開發獲取資料

[ESP32_ADC](https://docs.espressif.com/projects/esp-idf/en/release-v4.4/esp32/api-reference/peripherals/adc.html)
pulsesensor

## acquire data
從頭到尾的實驗過程
step1:裝置環境
tx端接上電源
rx端接上電腦
```
ls /dev/ttyUSB*
```
確認裝置有接上電腦並確定port名稱
```
python csi_data_read.py -p /dev/ttyUSB0 -s test.csv
```
### linux 802.11n csi tool
[linux 802.11n csi tool](https://dhalperi.github.io/linux-80211n-csitool/)
### CSI Data的物理意義
#### referance
[IEEE 802.11n WLAN Standard](https://www.electronics-notes.com/articles/connectivity/wifi-ieee-802-11/802-11n.php)
[Create a reference script for Subcarrier Order](https://github.com/StevenMHernandez/ESP32-CSI-Tool/issues/12)
[TensorBeat: Tensor Decomposition for Monitoring Multiperson Breathing Beats p.8:5](https://dl.acm.org/doi/pdf/10.1145/3078855)
#### physical meaning
The WiFi OFDM channel can be regarded as a narrowband flat fading channel, which can be expressed in the frequency domain as
$$
\vec{Y} = H · \vec{X} + \vec{N} ,
$$
where $\vec{Y}$ and $\vec{X}$ denote the received and transmitted signal vectors, respectively; $\vec{N}$ is the additive
white Gaussian noise; and $H$ represents the channel frequency response, which can be estimated
from $\vec{Y}$ and $\vec{X}$ .
The channel frequency response of subcarrier $i$, denoted by $H_i$ , is a complex value, given as
$$
H_i = I_i + jQ_i = |H_i|exp(j\angle H_i)
$$
where $I_i$ and $Q_i$ are the in-phase component and quadrature component, respectively, and $|H_i|$ and $∠Hi$ are the amplitude and phase response of subcarrier $i$, respectively. For indoor environments with multipath components, the channel frequency response of subcarrier $i$, $H_i$ , can also be written as
$$
H_i = \sum_{k=0}^K r_k \cdot e^{-j2\pi f_i \tau_k}
$$
where $K$ is the number of multipath components, and $r_k$ and $τ_k$ are the attenuation and propagation
#### ESP32 channel

Channel state information (CSI) refers to the channel information of a Wi-Fi connection. In ESP32, this information consists of channel frequency responses of sub-carriers and is estimated when packets are received from the transmitter. **Each channel frequency response of sub-carrier is recorded by two bytes of signed characters.** **The first one is imaginary part and the second one is real part.** There are up to three fields of channel frequency responses according to the type of received packet. **They are legacy long training field (LLTF), high throughput LTF (HT-LTF), and space time block code HT-LTF (STBC-HT-LTF).** For different types of packets which are received on channels with different state, the sub-carrier index and total bytes of signed characters of CSI are shown in the following table.

### 實驗設計
短期目標:用
###