[ 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 連接圖


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':
        return json.dumps({"status": 200, "comment": "call led Finish"})
        print "error Content-Type"
        return json.dumps({"status": 400, "comment": "Content-Type error"})

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


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


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