# 樹莓派接API測試程式碼 ### ngrok https://dashboard.ngrok.com/login ### URL http://11a9-122-116-105-235.ngrok-free.app ``` from flask import Flask, jsonify import requests import RPi.GPIO as GPIO app = Flask(__name__) GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.OUT) # API for turning on the device @app.route('/on', methods=['GET']) def on(): GPIO.output(18, GPIO.HIGH) result = {'Result': 'Success'} return jsonify(result) # API for turning off the device @app.route('/off', methods=['GET']) def off(): GPIO.output(18, GPIO.LOW) result = {'Result': 'Success'} return jsonify(result) # API for getting the device state @app.route('/state', methods=['GET']) def state(): if GPIO.input(18) : result = {'State': 'on'} else: result = {'State': 'off'} return jsonify(result) if __name__ == '__main__': app.run(debug=True) ```