How can we imagine in the future, there should be a smart housekeeper in everyone's house that can get accurate and real-time data of temperature, humidity, light intensity, and so on? It's also like a "universal" remote control which can help you control your home's appliances. Of course, it should be like a smart speaker can understand our instructions, and give us a response!
Wio Terminal is a highly integrated development board, which comes with an LCD display, three buttons, a five-way switch, as well as microphones, speakers, accelerometers, infrared transmitters, etc. It is very suitable to use it as a core of the smart home control system.
2. system function- Display temperature, humidity, light intensity, atmospheric pressure, combustible gas content, and other data on the LCD screen. In order to save power, it will only display these data when the user picks it up.
- Control the light switch, the left button can control the light switch, and the middle and right buttons can lighten and darken respectively.
- When speaking to him, he will send an email to the user with data including temperature, humidity, light intensity, atmospheric pressure, and combustible gas content.
- The system will give voice feedback when the user controls the light or voice control to send mail.
Before we can start the project, we need to prepare your system with the necessary software, drivers, and tools in order to have a successful project.
The Arduino IDE allows us to write and debug our program. If you are the first to use Wio Terminal, please refer to Get Started with Wio Terminal wiki. In order to use Seeeduino V4.2, you will need to have the drivers installed.
Now, we can follow the below steps to realize the funning smart home control system.
3.1Control night light through theinfrared module
You can buy a night light with a controller.
Because Wio Terminal does not have infrared decoding function, you need to buy an infrared module with encoding and decoding, and of course a USB-TTL serial converter.
The idea is actually very simple. Read the data sent by the corresponding button on the remote control, and then use the infrared module to transmit it to decode it. You can use the serial port debugging assistant.
3.2 Get data through Seeed-BME680
The Grove-Temperature&Humidity&Pressure&Gas Sensor(BME680) is a multiple function sensor that can measure temperature, pressure, humidity, and gas at the same time. Please refer to this wiki to get the temperature, pressure, humidity data.
3.3 Display the data obtained by Seeed-BME680 with the WEB front-end and
Build aWEB server.
After you have got the temperature, pressure, humidity data, you can use Python to read the serial port data on the computer side. If you use the serial library, you can install it directly with pip.
```
import serial
import datetime
def getDatas():
serialPort = "COM10" # com
baudRate = 9600 # baurd rate
ser = serial.Serial(serialPort, baudRate, timeout=0.5)
while True:
str = ser.readline().decode('utf-8')
if str.strip()!='':
print(str)
updata_time = datetime.datetime.now().strftime('%F %T')
print(updata_time)
return str,updata_time
```
Next, we build a local WEB server so that all devices in the local area network can obtain the data returned by the sensor. You should learn how to start the Django frame work. Here is the Django download URL. Please refer to the main code views.py.
Put the method of reading serial port data here, and then call the content in the index method to store the data that the front-end page needs to display, and use the render method to return. In Pycharm, the service is only opened on the local machine. If the devices in the local area network can be accessed, we need to configure in settings.py:
```
ALLOWED_HOSTS = ['*']
```
Here I omitted the specific steps. At the end we can use the mobile phone to access the obtained data:
3.4 Wio Terminal access data and display
We need to connect to WIFI and make the HTTP request. Firstly, please refer to this wiki that introduces how to update the latest firmware for the Wireless Core Realtek RTL8720 on Wio Terminal, as well as installing all the dependent libraries for Wio Terminal to enable wireless connectivity. Secondly, please refer to this wiki introduces how to configure Wi-Fi connectivity on Wio Terminal using the Realtek RTL8720 core. Thirdly, get data and display it on the LCD screen.ArduinoJson library is mainly used to read data.
```
//ArduinoJson to parse data, plesae check ArduinoJson for more info
const size_t capacity = JSON_OBJECT_SIZE(5) + 100;
DynamicJsonDocument doc(capacity);
deserializeJson(doc, data);
float temperature = doc["temperature"];
float pressure = doc["pressure"];
float humidity = doc["humidity"];
float gas = doc["gas"];
String updataTime = doc["updataTime"];
```
When Wio Terminal recognizes that the ambient audio signal is fluctuating, it will send "send message!" to the serial port, and the PC will send an email after reading it.
Here is an encapsulated method, which can be called directly when used.
```
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
def send():
# Third-party SMTP service
mail_host="smtp.qq.com" #Set up server
mail_user="" #User
mail_pass="" #Password
sender = ''
receivers = [''] # Receive mail, can be set as your mailbox
#Create an instance with attachments
message = MIMEMultipart()
message['From'] = Header("Wio Terimal", 'utf-8')
message['To'] = Header("温湿度、大气压力、可燃气体检测数据", 'utf-8')
subject = '当前温湿度、大气压力、可燃气体检测数据'
message['Subject'] = Header(subject, 'utf-8')
#Message body content
message.attach(MIMEText('温湿度、大气压力、可燃气体检测数据', 'plain', 'utf-8'))
# Construct an attachment and transfer the test.txt file in the current directory
att = MIMEText(open('D:\TemperatureHumidityPressureGasData.txt', 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
#The filename here can be written arbitrarily
att["Content-Disposition"] = 'attachment; filename="TemperatureHumidityPressureGasData.txt"'
message.attach(att)
server = smtplib.SMTP_SSL(mail_host, 465) # The default port of SMTP protocol is 25
server.set_debuglevel(1)
server.login(mail_user, mail_pass)
try:
server.sendmail(sender, receivers, message.as_string())
print ("邮件发送成功")
except smtplib.SMTPException:
print ("Error: 无法发送邮件")
This is the mail I successes received.
In the Windows system, you can directly call the voice package of the system:
```
import win32com.client
speaker = win32com.client.Dispatch("SAPI.SpVoice")
text = "Enter the content to be speech synthesized"
speaker.Speak(text)
```
When you see here, this project is basically completed, I will post the complete code below.
Comments