In this lab, you will learn how to make voice output and alert system with Python. The system will be able to play speech output and voice alerts on the speaker of the single-board computer.
pyttsx3
library.pymongo
library.pygame
library.python-periphery
library.This lab assumes that you have completed the previous lab on MongoDB and that you have a working MongoDB database and sensors. If you have not completed the previous lab, please do so before continuing.
We will reuse the same MongoDB URI from previous lab for accessing the database.
In this lab, we will mainly use Python to develop the voice output and alert system. We will use the pyttsx3
library to play speech output and the pygame
library to play audio files. We will also use the pymongo
library to receive data from the MongoDB database.
To install the libraries, run the following command in a Python environment:
pip install pyttsx3 pygame pymongo
For pyttsx3
to work, you will also need to install espeak
library. On Ubuntu, you can install it with the following command:
sudo apt install espeak
On MacOS, you can install it with the brew
command: (If you don’t have brew
installed, you can install it from here
brew install espeak
For Windows, please download and install the executable file from here.
We will use Visual Studio Code as the development environment for this lab. You can download it from here.
In this section, we will learn how to play speech output with Python. We will use the pyttsx3
library to play speech output.
Please continue from the previous lab. We will use the same MongoDB URI from previous lab for accessing the database.
import datetime
import time
from pymongo import MongoClient
from smbus2 import SMBus
bus = SMBus(7)
client = MongoClient('mongodb+srv://db:<password>@cluster0...:27017/')
db = client.database
while True:
bus.write_i2c_block_data(0x38, 0xAC, [0x33, 0x00])
time.sleep(0.5)
data = bus.read_i2c_block_data(0x38, 0x00)
temp = ((data[3] & 0x0F) << 16) | (data[4] << 8) | data[5]
humi = ((data[1] << 16) | (data[2] << 8) | data[3]) >> 4
temperature = temp / 1048576 * 200 - 50
print(u'Temperature: {0:.1f}°C'.format(temperature))
# Exercise code: Read the humidity data from the sensor
humidity = humi / 1048576 * 100
print(u'Humidity: {0:.1f}%'.format(humidity))
record = {
"sensor_id": 1,
"temp": temperature,
"humi": humidity,
"date": datetime.datetime.now(),
}
db.sensors.insert_one(record)
time.sleep(60)
Please create a project folder named lab10
and create a file named main.py
inside the folder.
If you are working on the Rock Pi 4 SE, copy the main.py
file from the previous lab to the lab10
folder.
First, we need to import the libraries we will use in the program. We will use the pyttsx3
library to play speech output.
import pyttsx3
engine = pyttsx3.init()
engine.say("Program started")
engine.runAndWait()
If you are working on the Rock Pi 4 SE, please add the above code before the while
loop.
Next, we will output the temperature and humidity value with speech. We will use the pyttsx3
library to play speech output. Please add the following code to the program right after the print statements of temperature value, which is print(u'Temperature: {0:.1f}°C'.format(temperature))
.
engine = pyttsx3.init()
engine.say(u'Temperature is now {0:.1f}°'.format(temperature))
engine.runAndWait()
Please output the humidity value with speech. You can use the above code as a reference.
In this section, we will learn how to make an LED alert system with Python. We will use the python-periphery
library to control the LED connected to the single-board computer with GPIO.
General Purpose Input/Output (GPIO) pins are pins that can be used for input or output. They are usually used to connect to external devices such as sensors and actuators. The Raspberry Pi and Rock Pi 4 SE have GPIO pins that can be used to connect to external devices.
First, we will learn how to turn an LED on and off with GPIO pins. We will use the python-periphery
library to control the LED connected to the single-board computer with GPIO pins. The LED is connected to the GPIO pin 146
for the Red channel and 150
for the Green channel.
To control the LED, we need to import the GPIO
class from the periphery
module. We can then create a GPIO
object with the pin number of the LED. We can then use the write()
method to turn the LED on and off.
from periphery import GPIO
import time
red_led = GPIO(146, "out")
red_led.write(True)
time.sleep(3)
red_led.write(False)
Let’s add the above code to the program right before the while
loop. It will turn on the Red LED for 3 seconds and then turn it off.
Please turn on and off the Green LED. You can use the above code as a reference.
In this section, we will learn how to use LEDs as alert system when temperature is too high. We will integrate the LED controls with the temperature sensor codebase.
if-else
statementIn order to check if the temperature is too high, we will use the Python if
statement. The if
statement is used to check if a condition is true. If the condition is true, the code inside the if
statement will be executed. If the condition is false, the code inside the if
statement will be skipped.
if temperature > 28:
print("Temperature is too high")
else:
print("Temperature is normal")
Please add the above code to the program right after the temperature
variable is assigned. Please add it before the time.sleep(60)
statements at the while
loop.
Next, we will turn on the Red LED when the temperature is too high. We will modify the previous code to turn on the Red LED when the temperature is too high and turn it off when the temperature is normal.
if temperature > 28:
print("Temperature is too high")
red_led.write(True)
else:
print("Temperature is normal")
red_led.write(False)