[ Linkit Smart 7688 ] 透過 Python Flask 控制 LED

前置準備作業

  1. 準備 Linkit Smart 7688 Duo 開發板

  2. 準備 Arduino Breakout for LinkIt Smart 7688 Duo

  3. 準備 Grove - LED

  4. 將 Linkit Smart 7688 Duo 與 Arduino Breakout for LinkIt Smart 7688 Duo 組裝、Grove - LED 裝到 D7 的位置並將 Linkit Smart 7688 Duo 連接至電腦

  5. 安裝 Arduino IDE (version: 1.6.6)
    https://www.arduino.cc/en/Main/OldSoftwareReleases

  6. 安裝 IDE ( Sublime Text or Visual Studio Code )

  7. Windows 平台,請安裝 Putty 與 FileZilla
    http://oranwind.org/-linkit/


Linkit Smart 7688 Duo 與 Grove - LED 連接圖


Linkit Smart 7688 Duo - MCU ( Arduino )

int ledPin = 7; 

void setup() {  
  // put your setup code here, to run once:

  Serial.begin(115200);  // open serial connection to USB Serial                           //port(connected to your computer)
  Serial1.begin(57600);  // open internal serial connection to MT7688
  pinMode(ledPin, OUTPUT); // in MT7688, this maps to device 
}

void loop() {  
  // put your main code here, to run repeatedly:
  int c = Serial1.read();      // read from MT7688
  if (c != -1) {
       switch(c) { 
           case '0':                // turn off D7 when receiving "0"
               digitalWrite(ledPin, 0); 
               break; 
           case '1':                // turn on D7 when receiving "1" 
               digitalWrite(ledPin, 1); 
               break; 
       } 
   } 
}


Linkit Smart 7688 Duo - MPU ( Python ) 端

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

s = None  
#f = os.popen('ifconfig br-lan | grep "inet\ addr" | cut -d: -f2 | cut -d" " -f1') # AP model
f = os.popen('ifconfig apcli0 | grep "inet\ addr" | cut -d: -f2 | cut -d" " -f1') # Station model  
inet_addr = f.read()  
app = Flask(__name__)

# ****************************************************
# open serial COM port to /dev/ttyS0, which maps to UART0(D0/D1)
# the baudrate is set to 57600 and should be the same as the one
# specified in the Arduino sketch uploaded to ATMega32U4.
# ****************************************************

def setup():  
  global s 
  s = serial.Serial("/dev/ttyS0", 57600) 

# ****************************************************
# POST http://mylinkit.local:5000/api/v1.0/turnOnOffLED
# ****************************************************
@app.route("/api/v1.0/turnOnOffLED", methods=['POST'])
def setvideoon():  
    value = request.form['value']
    if value == 'on':
        s.write("1") 
    else:
        s.write("0")    
    return json.dumps({"status": 200, "comment": "call turnOnOffLED Finish"})

if __name__ == '__main__':  
    setup() 
    app.debug = True
    app.run(
        host = inet_addr,
        port = 5000
    )



  • Step 3. 安裝相關套件於 Linkit Smart 7688
pip install flask  

  • Step 4. 執行剛傳到 Intel Edison 中的 Python Code
python 檔名.py  

  • Step 5. Console 執行畫面

  • Step 5. 透過 Advanced REST client 呼叫 API
開啟 LED
API -> POST http://IP:5000/api/v1.0/turnOnOffLED  
Parameter -> value:on  

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


GitHub
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