# IoT devices 交接文件:
- 硬體
- 震動sensors
- 溫度sensors
- 貼片
- 放大器
- 數據多工器
- 4.7k歐姆電阻
- 電路圖
- 底座
- 墊片
- 樹梅派3B + 腳位
- 樹梅派外殼
- 韌體:
- I2C重建教學
- one-wire temperature
- 程式碼備份
- IOT_device network settings:
## 硬體:
### 震動Sensors:

[使用的廠牌連結](https://www.taiwaniot.com.tw/product/grove-3-axis-digital-accelerometer-%c2%b116g-3%e8%bb%b8%e6%95%b8%e5%ad%97%e5%8a%a0%e9%80%9f%e5%ba%a6%e8%a8%88/)
[低配版--之後可以自己買這個來焊](https://www.taiwaniot.com.tw/product/gy-291-adxl345-%e6%95%b8%e5%ad%97%e4%b8%89%e8%bb%b8%e9%87%8d%e5%8a%9b%e5%8a%a0%e9%80%9f%e5%ba%a6%e5%82%be%e6%96%9c%e5%ba%a6%e6%a8%a1%e7%b5%84-iicspi%e5%82%b3%e8%bc%b8/)
[ADXL345 Data sheet](https://www.analog.com/media/en/technical-documentation/data-sheets/ADXL345.pdf)
Data sheet可以提供我們關於暫存器位址的資訊,可以把這個跟`python code: ADXL345`搭配一起看。
### 溫度Sensors :

[熱感貼片--IC-900 PT100](http://www.icenter-tw.com/web/products.php?key=90)
[溫度放大器](https://www.taiwaniot.com.tw/product/grove-1-wire-thermocouple-amplifier-max31850k-1線熱電偶放大器(max31850k)seeed原廠代理進/)
記得要加上一個4.7k或是1k以上歐姆電阻! 不然樹梅派會燒掉。
### 數據多工器TCA9548:

[數據多工器購買連結](https://goods.ruten.com.tw/item/show?21643091531410)
如果之後要新增震動Sensor的話,將震動sensor的`SCL`和`SDA`接到數據多工器的`SCx`和`SDx`
(例如: SC1和SD1)而`GND`和`VCC`則是連接到數據多工器的`GND`和`VCC`上統一由樹梅派供電。
[數據多工器Data sheet](http://www.ti.com/lit/ds/symlink/tca9548a.pdf)
### 電路圖:

需要注意的:
- 震動Sensors接到哪一個數據多工器的頻道上,則我們code就要改成相對應的頻道,否則會出現`raise error `
- 溫度sensor的訊號線和`vcc`要接上一顆歐姆電阻
- 記得一定要接地!! 而且溫度和震動的接地要分開!
### 底座:

### 墊片

萬一沒有檔案了,可以聯絡我,我筆電這邊都有備份!
### 樹梅派腳位:

### 樹梅派外殼:

## 韌體:
[I2C重建教學](/kvyZoDEPS8GzbSH1c6TzyA)
使用I2C模組的說明可以參考一下這篇: [ADXL345 Raspberry pi](https://pimylifeup.com/raspberry-pi-accelerometer-adxl345/)
使用溫度sensor可以參考一下這篇:
[one-wire temperature](https://thepihut.com/blogs/raspberry-pi-tutorials/ds18b20-one-wire-digital-temperature-sensor-and-the-raspberry-pi)
```python=
import os
import time
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
#temp_sensor = '/sys/bus/w1/devices/'
sensoid= ['3b-0000001817de','3b-0000001817d1']
temperature=[0,0]
def temp_raw():
f = open(temp_sensor,'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = temp_raw()
while lines[0].strip()[-3: ] != 'YES':
time.sleep(0.05)
lines = temp_raw()
temp_output = lines[1].find('t=')
if temp_output != -1:
temp_string = lines[1].strip()[temp_output+2:]
temp_c = float(temp_string)/1000.0
#temp_f = temp_c * 9.0/5.0+32.0
return temp_c
while True:
for sensor in range(len(sensoid)):
temp_sensor = '/sys/bus/w1/devices/'+sensoid[sensor]+'/w1_slave'
temperature[sensor] = read_temp()
print('the %d temp sensor is '%(sensor+1),temperature[sensor])
time.sleep(0.05)
```
若要新增溫度sensor,在加上電路之後(電阻記得加! 也確認線都有接對):
step 1 : `cd /sys/bus/w1/devices`
step 2 : `ls` 這時如果線有接對,則會看到除了`'3b-0000001817de','3b-0000001817d1','w1_bus_master1','w1_bus_master2'`以外的資料夾
step 3 : 進入多出來的資料夾,例如假設它是:`3b-0000001817d7`則輸入: `cd 3b-0000001817d7`
step 4 : 輸入`ls`,此時若有出現w1_slave,則表示連接成功,若沒有則跳出該資料夾,進入其他的資料夾看看,有時樹梅派會掃錯頻道。
step 5 : 進入`3b-0000001817d7`後輸入指令`cat w1_slave`若畫面有出現`crc=.. YES`和
`t=23000`則這就是新增的溫度感測器的值,接下來只需要將程式碼中的Array `sensoid`加入該資料夾名稱即可,例如: `sensoid= ['3b-0000001817de','3b-0000001817d1','3b-0000001817d7']`
step 6: 執行程式即會出現三個溫度值
#### 程式碼備份:
[ADXL345](https://hackmd.io/mGXzTb8dQ26opCIWEaLg1Q)
這部分只需要記得我們的單位是要`mm/s` 所以要從sensor拿的資料是`getAxes_in_mm`且`gforce=False`
[震動開機程式](https://hackmd.io/5dTRnrTXQ66nLP4MfZzblA?both)
[校正](https://hackmd.io/ljUFm4NHTLiFxsVa0sXr2Q?both)
[沒有卡爾曼濾波器的正式版](https://hackmd.io/2L6-1-mqTRGlw9FLhCHrGA)
[有卡爾曼濾波器的正式版](https://hackmd.io/_2j6YvdHR1Wy01VvZR2kog)
## IOT_device network settings:
## Setting the mask for Raspberry pi :(172.17.4.101/24)
remember to set the Mask to `255.255.255.0` => set the `IPv4` Address = `172.17.4.101/24`. If there are going to have multiple Raspberries, set the IP address to `172.17.4.10x/24`(x=2 , 3 , 4.....etc)
## Set the statistic Network :
### Facility :
step 1 : `cd /etc`
step 2 : `nano dhcpcd.conf `
step 3 : change the inform address to the address that specify above : `inform 172.17.4.101/24`
step 4 : `static ip_address = 172.17.4.101/24`
### H/Q :
step 1 : `cd /etc`
step 2 : `nano dhcpcd.conf `
step 3 : change the inform address to the address that specify above : `inform 10.98.222.98/24`
step 4 : `static ip_address = 10.98.222.98/24`
## MQTT Server address :
Facility : `172.17.4.100 ` / `port ` = 1883 /` survival_time` =60
H/Q : `10.98.192.99` / `port` = 1883 / `survival_time` = 60
## 安裝程序:
首先校正我們的震動Sensor,先開啟`tca_test.py`之後,在terminal端輸入`i2cdetect -y 1`
檢查是否有讀到我們數據多工器以及震動sensor的訊號(70和53),如果有,點下`rectication.py`進行校正,校正為16進制值,比如說當今天讀出來的數值為20.8mm/s^2,而我們的offset為0x00,則可以將其修改成0xFF,透過此動作將值修正至10mm/s^2以內,正負可以不需要管它,只需要確保它是在正負10mm/s^2內,校正完畢後開啟我們的程式碼,並且在關機前,開啟自動執行程式。
### **自動執行程式**:
- 輸入`cd /etc`
- 輸入`sudo nano rc.local`
- 有一個已經註解掉的指令`sudo python [所要執行的程式位置]/[所要執行的程式]` 將那個註解刪掉即可,若是要改成卡爾曼濾波器的程式則只需將後面的執行程式換成是卡爾曼濾波器的即可。完成上述步驟則完成IoT Device的架設><
## Trouble shooting:
`Raise error` : 檢查震動Sensor 接線是否有接好,如果用指令`i2cdetect -y 1`並沒有detect到53,則表示有震動sensor線掉了或是燒掉了。如果是兩種(70 53 )都沒顯示,那先將所有的震動sensor拔掉在重新輸入上述指令一次,確保數據多工器的線路是否有接好,如果還是沒有,摸摸看晶片,若有點溫度或者是會燙則極有可能是數據多工器燒掉了,請更換另外一顆數據多工器。
`Network unreachable` : 檢查網路是否有接對,可以先ping看看ip位址,`ping 172.17.4.100`
## 看核心溫度:
`/opt/vc/bin/vcgencmd measure_temp `