sudo python setup.py install
下载 requests-2.7.0.tar.gz 到/home/pi/
tar xvzf requests-2.7.0.tar.gz
cd requests-2.7.0
sudo python setup.py install
rm requests-2.7.0.tar.gz
rm RPi.GPIO-0.5.11.tar.gz
sudo rm -rf requests-2.7.0
sudo rm -rf RPi.GPIO-0.5.11
【基于Python的GPIO】
四个GPIO针脚控制步进电机,三个GPIO针脚控制RGB三色LED灯。
编写unlockhandler.py
源代码:
import json
import requests
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
led_R = 40
led_G = 38
led_B = 36
stepper_A = 37
stepper_B = 35
stepper_C = 33
stepper_D = 31
GPIO.setwarnings(False)
GPIO.setup(led_R, GPIO.OUT)
GPIO.setup(led_G, GPIO.OUT)
GPIO.setup(led_B, GPIO.OUT)
GPIO.setup(stepper_A, GPIO.OUT)
GPIO.setup(stepper_B, GPIO.OUT)
GPIO.setup(stepper_C, GPIO.OUT)
GPIO.setup(stepper_D, GPIO.OUT)
GPIO.setwarnings(True)
GPIO.output(led_R, 0)
GPIO.output(led_G, 0)
GPIO.output(led_B, 0)
GPIO.output(stepper_A, 0)
GPIO.output(stepper_B, 0)
GPIO.output(stepper_C, 0)
GPIO.output(stepper_D, 0)
def setStep(w1, w2, w3, w4):
GPIO.output(stepper_A, w1)
GPIO.output(stepper_B, w2)
GPIO.output(stepper_C, w3)
GPIO.output(stepper_D, w4)
def forward(delay, steps):
for i in range(0, steps):
setStep(1, 0, 1, 0)
time.sleep(delay)
setStep(0, 1, 1, 0)
time.sleep(delay)
setStep(0, 1, 0, 1)
time.sleep(delay)
setStep(1, 0, 0, 1)
time.sleep(delay)
def backwards(delay, steps):
for i in range(0, steps):
setStep(1, 0, 0, 1)
time.sleep(delay)
setStep(0, 1, 0, 1)
time.sleep(delay)
setStep(0, 1, 1, 0)
time.sleep(delay)
setStep(1, 0, 1, 0)
time.sleep(delay)
apiurl = 'http://api.yeelink.net/v1.0/device/*****/sensor/*****/datapoints'
apiheaders = {'U-ApiKey': '********************************'}
payload={'value': 0}
rpost = requests.post(apiurl, headers=apiheaders, data=json.dumps(payload))
print time.strftime('%H:%M:%S'),
print("Ready.")
GPIO.output(led_R, 1)
time.sleep(0.5)
GPIO.output(led_R, 0)
GPIO.output(led_G, 1)
time.sleep(0.5)
GPIO.output(led_G, 0)
GPIO.output(led_B, 1)
time.sleep(0.5)
GPIO.output(led_B, 0)
while True:
rget = requests.get(apiurl,headers=apiheaders)
dic = json.loads(rget.text)
#print time.strftime('%H:%M:%S'),
if dic['value'] == 1:
print time.strftime('%H:%M:%S'),
print("Unlocking!")
GPIO.output(led_R, 1)
# unlock work flow
forward(0.01, 128)
time.sleep(2)
backwards(0.01, 128)
setStep(0, 0, 0, 0)
payload={'value': 0}
rpost = requests.post(apiurl, headers=apiheaders, data=json.dumps(payload))
print("Done.")
GPIO.output(led_R, 0)
time.sleep(1)
else:
#print("Stand by.")
GPIO.output(led_G, 1)
time.sleep(0.2)
GPIO.output(led_G, 0)
time.sleep(4.8)
简述:
用 requests 库操作HTTP请求,用 json 库处理返回的 json 数据。
刚运行时,配置好针脚,将value初始化为0,三灯闪烁。
每5s读取一次value值:如果是1则操作步进电机开门,然后将value置回0,过程中亮红灯;如果是0则闪绿灯。
【安卓app】
主要源代码:
public class MainActivity extends ActionBarActivity {
PostHandler handler = null;
TextView textView = null;
Button startButton = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new PostHandler();
textView = (TextView) findViewById(R.id.textView);
startButton = (Button) findViewById(R.id.startButton);
startButton.setOnClickListener(new OCL());
}
private class OCL implements View.OnClickListener {
public void onClick(View v) {
PostThread pt = new PostThread();
new Thread(pt).start();
}
}
class PostHandler extends Handler {
public PostHandler() {
}
public PostHandler(Looper L) {
super(L);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
// 接收消息更新UI
Integer statusCode = msg.what;
String status = null;
switch (statusCode){
case 0: status = "未能发送指令,请检查网络!";break;
case 200: status = "指令发送成功!";startButton.setEnabled(false);break;
default: status = "错误的返回值:" + statusCode + " !";break;
}
textView.append("\n\n" + status);
}
}
class PostThread implements Runnable {
public void run() {
HttpClient httpClient = null;
HttpGet httpGet = null;
HttpPost httpPost = null;
HttpResponse response = null;
Integer statusCode = 0;
httpClient = new DefaultHttpClient();
httpGet = new HttpGet("http://api.yeelink.net/v1.0/device/*****/sensor/*****/datapoints");
httpPost = new HttpPost("http://api.yeelink.net/v1.0/device/*****/sensor/*****/datapoints");
httpGet.addHeader("U-ApiKey", "***************************************");
httpPost.addHeader("U-ApiKey", "******************************************");
JSONObject obj = new JSONObject();
try {
obj.put("value", 1);
httpPost.setEntity(new StringEntity(obj.toString()));
response = httpClient.execute(httpPost);
statusCode = response.getStatusLine().getStatusCode();
} catch (Exception e) {
e.printStackTrace();
}
// 网络线程发送消息
Message msg = new Message();
msg.what = statusCode;
handler.sendMessage(msg);
}
}