1. ติดตั้ง PI และ Set Headless
https://intranet.sci.com/blog.php?u=281&b=1770
boot เข้า Pi update upgrade set ntp
2. Fix ip wifi for pi zero
- nano /etc/dhcpcd.conf
Add
interface wlan0
static ip_address=192.168.0.164/24
static routers=192.168.0.2
static domain_name_servers=192.168.0.2 8.8.8.8
แล้ว Reboot เครื่อง
1. วัดอุณหภูมิ mlx90614 4 ขา ใช้ ไฟ 5V GNC SCL SDA
2. เปิดโหมด I2C
http://domoticx.com/raspberry-pi-i2c-bus-gebruiken/
3. เปิดดูไฟล์ Boot config ว่า
- nano /boot/config.txt
ถูกเปิดแล้วหรือยัง
dtparam=i2c_arm=on
dtparam=spi=on
4. Download
https://github.com/mcauser/micropython-mlx90614
5. ติดตั้ง Uzip และแตกไฟล์
- apt-get install unzip
6. เข้าไปใน Folder ที่แตกได้ ติดตั้งโปรแกรม
- python setup.py install
7. เขียน Code
- nano Tmp_Sensor.py
- import smbus
- class MLX90614():
- MLX90614_RAWIR1=0x04
- MLX90614_RAWIR2=0x05
- MLX90614_TA=0x06
- MLX90614_TOBJ1=0x07
- MLX90614_TOBJ2=0x08
- MLX90614_TOMAX=0x20
- MLX90614_TOMIN=0x21
- MLX90614_PWMCTRL=0x22
- MLX90614_TARANGE=0x23
- MLX90614_EMISS=0x24
- MLX90614_CONFIG=0x25
- MLX90614_ADDR=0x0E
- MLX90614_ID1=0x3C
- MLX90614_ID2=0x3D
- MLX90614_ID3=0x3E
- MLX90614_ID4=0x3F
- def __init__(self, address=0x5a, bus_num=1):
- self.bus_num = bus_num
- self.address = address
- self.bus = smbus.SMBus(bus=bus_num)
- def read_reg(self, reg_addr):
- return self.bus.read_word_data(self.address, reg_addr)
- def data_to_temp(self, data):
- temp = (data*0.02) - 273.15
- return temp
- #def get_amb_temp(self):
- # data = self.read_reg(self.MLX90614_TA)
- # return self.data_to_temp(data)
- def get_obj_temp(self):
- data = self.read_reg(self.MLX90614_TOBJ1)
- return self.data_to_temp(data)
- if __name__ == "__main__":
- sensor = MLX90614()
- #print(sensor.get_amb_temp())
- print(sensor.get_obj_temp())
http://domoticx.com/raspberry-pi-thermometer-ir-contactloos-mlx90614/
8. Test Run
- python Tmp_Sensor.py
9. จะได้ค่าอุณหภูมิ ของความร้อนที่ยิง
root@pi200:/home/pi# python Temp_Sensor.py
32.25
root@pi200:/home/pi# python Temp_Sensor.py
31.69
root@pi200:/home/pi#
การต่อตัววัดการสั่น 801S
1. การต่อ ตัววัดการสั่น 801S ตัว PI รับค่าได้เฉพาะ Digital ถ้าต้องการให้ได้รับค่า Analog ตัวใช้ตัวแปลงค่า
MCP3008
https://www.arduinoall.com/product/984/mcp3008-8-channel-10-bit-adc-with-spi-interfaceการต่อเข้า PI
ไฟ 3.3V
GND
Aout
ต่อ PI และ MCP3008
2. เขียน Code
- nano Vibration_Sensor.py
Code
- # Simple example of reading the MCP3008 analog input channels and printing
- # them all out.
- # Author: Tony DiCola
- # License: Public Domain
- import time
- import datetime
- # Import SPI library (for hardware SPI) and MCP3008 library.
- import Adafruit_GPIO.SPI as SPI
- import Adafruit_MCP3008
- # Software SPI configuration:
- ## GPIO ##
- CLK = 11
- MISO = 9
- MOSI = 10
- CS = 8
- ## PIN ##
- #CLK = 23
- #MISO = 21
- #MOSI = 19
- #CS = 24
- mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)
- print(datetime.datetime.now())
- # Hardware SPI configuration:
- # SPI_PORT = 0
- # SPI_DEVICE = 0
- # mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
- print('Reading MCP3008 values, press Ctrl-C to quit...')
- # Print nice channel column headers.
- #print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*range(8)))
- #print('{6:>4}|{7:>4}'.format(*range(8)))
- print('{7:>4}'.format(*range(8)))
- print('-' * 57)
- # Main program loop.
- while True:
- # Read all the ADC channel values in a list.
- values = [0]*8
- for i in range(8):
- # The read_adc function will get the value of the specified channel (0-7).
- values[i] = mcp.read_adc(i)
- # Print the ADC values.
- print('{7:>4}'.format(*values))
- #print('{6:>4}|{7:>4}'.format(*values))
- #print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*values))
- # Pause for half a second.
- time.sleep(1)
***สำคัญ SPI ใช้ GPIO ไม่ได้ใช่ PIN *** จะไม่ได้ค่า
https://learn.adafruit.com/raspberry-pi-analog-to-digital-converters/mcp3008
3. Test Run คำสั่ง จะได้ค่า ตัวเลข
python Vibration_Sensor.py
2020-05-21 18:37:18.288545
Reading MCP3008 values, press Ctrl-C to quit...
7
---------------------------------------------------------
0
1023
31
896
1023
////##########################////
Code สำเร็จที่ใช้ Run ทั้งวัดการสั่นและความร้อน
- # Simple example of reading the MCP3008 analog input channels and printing
- # them all out.
- # Author: Tony DiCola
- # License: Public Domain
- # Vibration 801S
- import time
- import datetime
- # Import SPI library (for hardware SPI) and MCP3008 library.
- import Adafruit_GPIO.SPI as SPI
- import Adafruit_MCP3008
- # Tmp Sensor
- import smbus
- # Vibration 801S
- # Software SPI configuration:
- ## GPIO ##
- CLK = 11
- MISO = 9
- MOSI = 10
- CS = 8
- ## PIN ##
- #CLK = 23
- #MISO = 21
- #MOSI = 19
- #CS = 24
- mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)
- print(datetime.datetime.now())
- # Hardware SPI configuration:
- # SPI_PORT = 0
- # SPI_DEVICE = 0
- # mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
- #print('Reading MCP3008 values, press Ctrl-C to quit...')
- # Print nice channel column headers.
- #print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*range(8)))
- #print('{6:>4}|{7:>4}'.format(*range(8)))
- #print('{7:>4}'.format(*range(8)))
- #print('-' * 57)
- # Tmp Sensor Use
- class MLX90614():
- MLX90614_RAWIR1=0x04
- MLX90614_RAWIR2=0x05
- MLX90614_TA=0x06
- MLX90614_TOBJ1=0x07
- MLX90614_TOBJ2=0x08
- MLX90614_TOMAX=0x20
- MLX90614_TOMIN=0x21
- MLX90614_PWMCTRL=0x22
- MLX90614_TARANGE=0x23
- MLX90614_EMISS=0x24
- MLX90614_CONFIG=0x25
- MLX90614_ADDR=0x0E
- MLX90614_ID1=0x3C
- MLX90614_ID2=0x3D
- MLX90614_ID3=0x3E
- MLX90614_ID4=0x3F
- def __init__(self, address=0x5a, bus_num=1):
- self.bus_num = bus_num
- self.address = address
- self.bus = smbus.SMBus(bus=bus_num)
- def read_reg(self, reg_addr):
- return self.bus.read_word_data(self.address, reg_addr)
- def data_to_temp(self, data):
- temp = (data*0.02) - 273.15
- return temp
- #def get_amb_temp(self):
- # data = self.read_reg(self.MLX90614_TA)
- # return self.data_to_temp(data)
- def get_obj_temp(self):
- data = self.read_reg(self.MLX90614_TOBJ1)
- return self.data_to_temp(data)
- #if __name__ == "__main__":
- # sensor = MLX90614()
- # #print(sensor.get_amb_temp())
- # print(sensor.get_obj_temp())
- # Main program loop.
- while True:
- # Read all the ADC channel values in a list.
- values = [0]*8
- for i in range(8):
- # The read_adc function will get the value of the specified channel (0-7).
- values[i] = mcp.read_adc(i)
- sensor = MLX90614()
- #print(sensor.get_amb_temp())
- #print(sensor.get_obj_temp())
- # Print the ADC values.
- print('{7:>4}'.format(*values))
- print(sensor.get_obj_temp())
- #print('{6:>4}|{7:>4}'.format(*values))
- #print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*values))
- # Pause for half a second.
- time.sleep(10)
นำข้อมูลเข้า Database
https://pynative.com/install-mysql-connector-python/
1. ติดตั้ง mysql-connector
2. Code
3. Test Run
ดูว่าข้อมูลเข้า Database ที่สร้างไว้หรือไม่
https://pynative.com/install-mysql-connector-python/
1. ติดตั้ง mysql-connector
- pip install mysql-connector-python
2. Code
- nano DB.py
- import mysql.connector
- from mysql.connector import Error
- from mysql.connector import errorcode
- try:
- connection = mysql.connector.connect(host='192.168.2.101',
- database='sci_pi',
- user='XXX',
- password='XXX')
- #INSERT INTO `Data` (`ID`, `Vibration`, `Tmp`, `TransDate`) VALUES (NULL, '50', '60', '2020-05-22');
- mySql_insert_query = """INSERT INTO Data (ID, Vibration, Tmp, TransDate, FromPI)
- VALUES
- (NULL, 50, 60, '2020-05-22','192.168.0.164') """
- cursor = connection.cursor()
- cursor.execute(mySql_insert_query)
- connection.commit()
- #print(cursor.rowcount, "Record inserted successfully into Laptop table")
- cursor.close()
- #except mysql.connector.Error as error:
- # print("Failed to insert record into Laptop table {}".format(error))
- finally:
- if (connection.is_connected()):
- connection.close()
- #print("MySQL connection is closed")
3. Test Run
- python DB.py
ดูว่าข้อมูลเข้า Database ที่สร้างไว้หรือไม่
No comments:
Post a Comment