[ Intel Edison ] 透過 API 開關 LED - Python

前置準備作業

  1. 準備 Intel® Edison 開發板

  2. 準備 Arduino Breakout Board

  3. 準備 Grove LED

  4. 組裝 Intel Edison 、 Arduino Breakout Board 與 LED 並連接到電腦 ( LED 接 D5 )

Intel Edison 與 Arduino Breakout Board 連接圖

Imgur

Intel Edison 端

  • Step 1. 透過 Python 控制 LED
import pyupm_grove as grove  
import time  
import os  
from flask import Flask  
from flask import json  
from flask import Response  
from flask import request

led = grove.GroveLed(5)

f = os.popen('ifconfig wlan0 | grep "inet\ addr" | cut -d: -f2 | cut -d" " -f1')  
inet_addr = f.read()  
app = Flask(__name__)

@app.route("/api/v1.0/led", methods=["POST"])
def turns():  
    if request.headers['Content-Type'] == 'application/x-www-form-urlencoded':
        value = request.form['turn']
        print "value: " + value
        if value == 'on':
            led.on()
        else:
            led.off()
        return json.dumps({"status": 200, "comment": "call led Finish"})
    else:
        print "error Content-Type"
        return json.dumps({"status": 400, "comment": "Content-Type error"})

if __name__ == "__main__":  
    #app.debug = True
    print "inet_addr: " + inet_addr
    app.run(
        host = inet_addr,
        port = 5000
    )

Imgur

pip install flask  
  • Step 4. 執行剛傳到 Intel Edison 中的 Python Code
python 檔名.py  
  • Step 5. Console 執行畫面 Imgur

  • Step 5. 透過 Advanced REST client 呼叫 API

開啟 LED
API -> POST http://IP:5000/api/v1.0/led  
Parameter -> turn:on  

Imgur Imgur

關閉 LED
API -> POST http://IP:5000/api/v1.0/led  
Parameter -> turn:off  

Imgur Imgur


List of blogs

Archer

Having being a full stack engineer. Experience with C, Python, Go, Swift, JavaScript, Node.js, Linkit 7697, Linkit Smart 7688, Raspberry Pi, ARM mbed, IoT solutions. Contact us : [email protected]

ALL RIGHTS RESERVED. COPYRIGHT © 2016. Designed and Coded by Makee.io