Lưu đồ giải thuật

Một phần của tài liệu ĐIỀU KHIỂN THIẾT bị BẰNG GIỌNG nói TRÊN RASPBERRY, có CODE (Trang 34 - 38)

Google assistant “Turn off the relay”

4.2 Lưu đồ giải thuật

Ô này thể hiện khi chúng ta nói vào Google Assistant câu “Turn on the relay” thì Relay sẽ được bật.

Ô này thể hiện khi chúng ta nói vào Google Assistant câu “Turn off the relay” thì Relay sẽ được tắt

4.3 Phần code

#Raspberry Pi 3 modules b

# Import standard python modules.

import sys

# Import blinka python modules.

import board

import digitalio

# This example uses the MQTTClient instead of the REST client

from Adafruit_IO import MQTTClient

# Set to your Adafruit IO key. # Remember, your key is a secret,

# so make sure not to publish it when you publish this code!

ADAFRUIT_IO_KEY='aio_pbuv43ehBiDwRMf6SebdYpuqkoGV'

# Set to your Adafruit IO username.

# (go to https://accounts.adafruit.com to find your username)

ADAFRUIT_IO_USERNAME ='vinh123456'

# Set to the ID of the feed to subscribe to for updates.

Google assistant“Turn on the relay” “Turn on the relay”

Google assistant“Turn off the relay” “Turn off the relay”

FEED_ID='digital'

relay = digitalio.DigitalInOut(board.D26)

relay.direction = digitalio.Direction.OUTPUT

# Xác định các hàm gọi lại sẽ được gọi khi một số sự kiện nhất định xảy ra.

defconnected(client):

""" Chức năng đã kết nối sẽ được gọi khi máy khách được kết nối với

Adafruit IO. Đây là một nơi tốt để đăng ký thay đổi nguồn cấp dữ liệu. Khách hàng

tham số được truyền cho hàm này là ứng dụng khách Adafruit IO MQTT nên bạn có thể thực hiện các cuộc gọi chống lại nó một cách dễ dàng.

"""

# Subscribe to changes on a feed named Counter.

print('Subscribing to Feed {0}'.format(FEED_ID))

client.subscribe(FEED_ID)

print('Waiting for feed data…') defdisconnected(client):

""" Chức năng ngắt kết nối sẽ được gọi khi máy khách ngắt kết nối."""

sys.exit(1)

defmessage(client, feed_id, payload):

""" Hàm tin nhắn sẽ được gọi khi nguồn cấp dữ liệu đã đăng ký có giá trị mới. Tham số feed_id xác định nguồn cấp dữ liệu và tham số tải trọng có

giá trị mới. """

print('Feed {0} received new value: {1}'.format(feed_id, payload))

if payload =="OFF": print("Turn off relay!")

relay.value =False elif payload =="ON": print("Turn on relay!")

relay.value =True

# Create an MQTT client instance.

client = MQTTClient(ADAFRUIT_IO_USERNAME,ADAFRUIT_IO_KEY)

# Setup the callback functions defined above.

client.on_connect = connected client.on_disconnect = disconnected client.on_message = message

# Connect to the Adafruit IO server.

# The first option is to run a thread in the background so you can continue # doing things in your program.

client.loop_blocking()

Một phần của tài liệu ĐIỀU KHIỂN THIẾT bị BẰNG GIỌNG nói TRÊN RASPBERRY, có CODE (Trang 34 - 38)