AD7793的SPI接口和香蕉派的GPIO口连接:
GPIO.setup(3, GPIO.OUT) #CS
GPIO.setup(5, GPIO.OUT) #SCLK
GPIO.setup(7, GPIO.OUT) #DIN
GPIO.setup(11,GPIO.IN, pull_up_down=GPIO.PUD_UP) #DOUT
Python编程驱动AD7793
建立ad7793.py文件:
编写代码如下:
该文件包含ad7793的寄存器读写函数。
# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import time
#端口操作
def CS_H():
GPIO.output(3, GPIO.HIGH)
return
def CS_L():
GPIO.output(3, GPIO.LOW)
return
def SCLK_H():
GPIO.output(5, GPIO.HIGH)
return
def SCLK_L():
GPIO.output(5, GPIO.LOW)
return
def DIN_H():
GPIO.output(7, GPIO.HIGH)
return
def DIN_L():
GPIO.output(7, GPIO.LOW)
return
def AD_OUT():
return GPIO.input(11)
#采样通道
CHANNEL_1 = 1
CHANNEL_2 = 2
#寄存器地址
STATE = 0X40
MODE_W = 0X08
MODE_R = 0X48
CONFIG_W = 0X10
CONFIG_R = 0X50
DATA = 0X58
ID = 0X60
IO_W = 0X28
IO_R = 0X68
OFFSET_W = 0X30
OFFSET_R = 0X70
FULL_W = 0X38
FULL_R = 0X78
def Init():
# BOARD编号方式,基于插座引脚编号
GPIO.setmode(GPIO.BOARD)
# 输出模式
GPIO.setup(3, GPIO.OUT) #CS
GPIO.setup(5, GPIO.OUT) #SCLK
GPIO.setup(7, GPIO.OUT) #DIN
GPIO.setup(11,GPIO.IN, pull_up_down=GPIO.PUD_UP) #DOUT
Write(0xff)
Write(0xff)
Write(0xff)
Write(0xff)
time.sleep(0.001)
ConfigCH1()
return
def Write(data):
i = 0
SCLK_H()
CS_H()
time.sleep(0.000001)
CS_L()
time.sleep(0.000001)
for i in range(0,8):
SCLK_L()
time.sleep(0.000001)
if(data & 0x80):
DIN_H()
else:
DIN_L()
time.sleep(0.000001)
data <<= 1
SCLK_H()
time.sleep(0.000001)
CS_H
return
def Read():
DATA_OUT = 0
i = 0
CS_H()
CS_L()
time.sleep(0.000001)
for i in range(0,8):
DATA_OUT <<= 1
SCLK_L()
time.sleep(0.000001)
if(AD_OUT()):
DATA_OUT += 1
SCLK_H()
time.sleep(0.000001)
CS_H
return DATA_OUT
def ConfigCH1():
Write(MODE_W)
Write(0x00)
Write(0x41)
Write(CONFIG_W)
Write(0x10)
Write(0x90)
Write(IO_W)
Write(0x03)
return
def ConfigCH2():
Write(MODE_W)
Write(0x20)
Write(0x41)
Write(CONFIG_W)
Write(0x98)
Write(0x11)
Write(IO_W)
Write(0x03)
return
def ReadReg(RegAddr):
Write(RegAddr)
return Read()
def ReadConversion(channel):
data = 0
if(1 == channel):
ch = 8
else:
ch = 9
while(1):
flag = ReadReg(STATE)
if(ch == flag):
break
Write(DATA)
data += Read()
data <<= 8
data += Read()
data <<= 8
data += Read()
return data
def GetTemp(channel):
data = ReadConversion(channel)
Vol = 1.17 / 0xffffff * data #pt100上的电压:伏特
Res = Vol / 0.001 #除以1毫安获得电阻:欧姆
Temp = (Res/100 - 1)/0.00392 #获得温度:摄氏度
return Temp
建立test.py主程序函数,代码如下:
# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import time
import requests
import json
import ad7793 #相当于include “ad7793.h”
#设备URL,这里替换成你自己的APIURL
apiurl_temp = 'http://api.yeelink.net/v1.1/device/349360/sensor/391335/datapoints'
#请求头部,这里替换成你自己的ApiKey
apiheaders = {'U-ApiKey':'ac0c11e35e9f93618a846xxxxxxxxxxxxx','Host':'api.yeelink.net'}
#请求包体
payload = {'value':0}
ad7793.Init() #初始化AD7793
while(1):
#取平均
TempSum = 0
for i in range(0,10):
TempSum += ad7793.GetTemp(ad7793.CHANNEL_1)
Temp = TempSum/10
#发送到服务器
payload['value'] = Temp
r = requests.post(apiurl_temp,data=json.dumps(payload),headers=apiheaders)
#延时
time.sleep(15)
我们通过FileZilla把ad7793.py和test.py传输到香蕉派中,输入命令sudo python test.py后,程序运行:下图是我的室温变化图。
这样我们就可以通过网页、手机、微信等能连接互联网的应用远程查看到家中或大棚中的温度变化情况。
三 项目总结
本项目如题所示,完成了两件事情。其一:通过Yeelink服务器远程控制香蕉派的GPIO口,从而对外围硬件驱动并控制;其二:香蕉派采集室温,上传到Yeelink服务器上,在服务器上可以观测温度变化曲线。LED和温度只是一个切入点,香蕉派拥有着无限的可能!
这是第一次接触香蕉派,以前的学习中我只要倾向于硬件设计,但在学习香蕉派的过程中让我学习到了很多软件上的知识,例如Linux、Python、Web知识等等。感觉受益匪浅,在学习的过程,我也分享了很多篇试用报告,希望能够帮助到初学者!
最后分享一句话:真正的科技是让人感觉不到科技的存在!