2017年9月14日 星期四

OrangePi One 搭配 Air2 電壓感測儀器






據說,要先刪掉 python 之後,等待5分鐘在從新執行 python

在 /etc 建立 rc.local
vi rc.local
#刪除 python
killall python  這行確定不用了,放了反而不執行。
#系統靜待300秒(5分鐘)
sleep 300
#轉換程式目錄並且執行
cd /root
python t.py &
exit 0

第二段 t.py

import serial
import time
def get_date_stamp():
date_stamp = time.strftime(“%Y-%m-%d")
return date_stamp
def get_datetime_stamp():
datetime_stamp = time.strftime(“%Y-%m-%d %H:%M:%S")
return datetime_stamp
port_name = ‘/dev/ttyUSB0’
baud = 9600
timeout = 1
port = None
csv_file = None
try:
port = serial.Serial(port_name, baud, timeout=timeout)
if port is None:
print(‘Cannot open the serial port.’)
print(‘Please unplug the USB cable and then reconnect Air 2.’)
else:
print(port)
port.flushInput() # flush serial input buffer, discarding all its contents
#data_number = 0
# discard firt read: it may be incomplete data
line = port.readline()
print(‘Discard first read: it may be incomplete data’)
if line is “:
print(‘Empty line.’)
air_voltage = float(‘nan’)
else:
print(line)
air_voltage = float(line)
date_stamp = get_date_stamp()
#csv_file_name = date_stamp + ‘ AirView’ + ‘.csv’
csv_file_name = ‘AirView2.csv’
print(csv_file_name)
print(‘AirLog will run forever.’)
print(‘If you want to stop AirLog, select Kernel->Interrupt.’)
with open(csv_file_name, ‘a’) as csv_file:
# write an empty data to make Bokeh plot a break point
datetime_stamp = get_datetime_stamp()
data = datetime_stamp + ‘,\n’
data_number = 0
# print(str(data_number), data)
csv_file.write(data)
csv_file.flush()
while True: # infinite loop: read line from serial port
buffer_size = 128
buffer = []
buffer_sum = 0
for i in range(buffer_size):
line = port.readline()
if line is “:
#print(‘Empty line.’)
air_voltage = float(‘nan’)
else:
#print(line)
air_voltage = float(line)
buffer.append(air_voltage)
buffer_sum += air_voltage
#datetime_stamp = get_datetime_stamp()
# print(str(i+1), ‘:’, datetime_stamp, line)
average_air_voltage = buffer_sum / buffer_size
datetime_stamp = get_datetime_stamp()
data = datetime_stamp + ‘,’ + str(average_air_voltage) + ‘\n’
data_number += 1
# print(str(data_number), data)
csv_file.write(data)
csv_file.flush()
#except:
#if port is None:
#print(‘Cannot open the serial port.’)
#print(‘Please unplug the USB cable and then reconnect Air 2.’)
finally:
#csv_file.close()
if csv_file is not None:
print(‘CSV file has been closed:’, csv_file.closed)
if port is not None:
port.close()
print(port)

目前先這樣,之前幾段是 BV7RR 開發,我目前在想怎樣自動轉換成圖檔(PNG、JPG),然後自動寄出,這樣就是一台無人值守觀測儀器。

沒有留言:

張貼留言

XBMC For BananaPi M1 設定方式

xbmc_12.2_frodo_bananaPI_4GB_RC3 login/password bananapi/bananapi