Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
MQTT-Group3/cbw-code
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
90 lines (71 sloc)
2.6 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, render_template, request, redirect, url_for, jsonify | |
import paho.mqtt.client as mqtt | |
import pyaudio | |
import numpy as np | |
import json | |
import time | |
def on_connect(client, userdata, flags, rc): | |
print("Connected with result code " + str(rc)) | |
client.subscribe("light/brightness") | |
client.subscribe("light/color_temperature") | |
def on_message(client, userdata, msg): | |
print(f"Topic: {msg.topic}\nMessage: {msg.payload.decode()}") | |
client = mqtt.Client() | |
client.on_connect = on_connect | |
client.on_message = on_message | |
client.connect("localhost", 1883, 60) | |
client.loop_start() | |
# 音频设置 | |
p = pyaudio.PyAudio() | |
stream = p.open(format=pyaudio.paInt16, | |
channels=1, | |
rate=44100, | |
input=True, | |
frames_per_buffer=1024) | |
p = pyaudio.PyAudio() | |
stream = p.open(format=pyaudio.paInt16, | |
channels=1, | |
rate=44100, | |
input=True, | |
frames_per_buffer=1024) | |
def get_rms(data): | |
count = len(data) / 2 | |
shorts = np.frombuffer(data, dtype=np.int16) | |
sum_squares = np.sum(np.square(shorts)) | |
rms = np.sqrt(sum_squares / count) | |
return rms | |
def adjust_brightness(client, rms): | |
max_rms = 32767 # Maximum value for 16-bit audio | |
brightness = (rms / max_rms) * 100 | |
brightness = min(100, max(0, brightness)) # Clamp the value between 0 and 100 | |
client.publish("light/brightness", int(brightness)) | |
# 发送MQTT消息 | |
def send_mqtt_message(message): | |
client.publish("light/control", message) | |
@app.route('/light_control') | |
def light_control(): | |
return render_template('light_control.html') | |
@app.route('/adjust_brightness', methods=['POST']) | |
def adjust_brightness_route(): | |
data = stream.read(1024, exception_on_overflow=False) | |
rms = get_rms(data) | |
adjust_brightness(client, rms) | |
return "亮度已调整", 200 | |
@app.route('/adjust_color_temperature', methods=['POST']) | |
def adjust_color_temperature(): | |
temperature = request.form['temperature'] | |
client.publish("light/color_temperature", temperature) | |
return "色温已调整", 200 | |
@app.route('/toggle_light', methods=['POST']) | |
def toggle_light(): | |
current_state = request.json.get('state') | |
new_state = 'OFF' if current_state == 'ON' else 'ON' | |
send_mqtt_message(json.dumps({"state": new_state})) | |
return jsonify({'state': new_state}), 200 | |
@app.route('/update_color', methods=['POST']) | |
def update_color(): | |
color = request.json.get('color') | |
if color: | |
send_mqtt_message(json.dumps({"color": color})) | |
return jsonify({'color': color}), 200 | |
return jsonify({'error': 'Invalid color'}), 400 | |