Table of contents
Abstract
This holiday season, I planned to make a device that can detect guests and play greeting music for them on I2S speaker. It can also play some mechanical animation for the guest to catch their attention. Best to have this device on the door of house.
Project
This is my first time to take part in element14's project14 series. And I am quite excited to be a part of it.
It was really a holiday time for all of us where we saw a nice vacation days and then Christmas and following that a happy new year. By the time, I decide something cool that is a tech device I also wanted to make it fun for everyone. Specifically for the guests that arrive to our home in this time.
I know there are many ideas around but making them a reality is totally different thing. I got some ideas of making some decorative lights or some PCB making and so on so forth. But I have already done that in Light Up Your Life design challenge. And I thought it would be nice to create something different this time.
I have got Unihiker K10 from DFRobot. This is an interesting device for this holiday hacking challenge. It has ESP32S3 module and several other sensors like camera, accelerometer, light sensor, etc. But quite interesting is that it also has SD card slot and a I2S speaker as well as microphone.
This would make it quite interesting to make something with playing music or using tiny servo motors for mechanical animation. Let's see how it goes.!
So, the first question was how to make the I2S microphone to play music. In the beginning I tried using ESP-IDF to create a relatively simple code that can play .mp3 music of user's choice. And I was quite satisfied with the quality of the music after making some changes to I2S configurations. Here is a simple "Merry Christmas" tone playing.
After than I decided to switch to MicroPython for my development. This would make it easy to develop something useful. I had a look at the MicroPython code page of Unihiker and found that it is possible to detect human faces using inbuilt camera.
So the idea is when the guest arrives, they will be greeted with music and then a simple servo animation.
I have make some changes to original code for face detection to start the music and servo motor.
Here is the full code.
import ai
import time
from unihiker_k10 import screen, speaker, rgb
import machine
from neopixel import NeoPixel
from DIYables_MicroPython_Servo import Servo
servo = Servo(1)
pin = machine.Pin(46)
np = NeoPixel(pin, 3)
from unihiker_k10 import button
bt_a=button(button.a)#Init button A
bt_b=button(button.b)#Init button B
#Init AI
ai.init_ai()
#Start camera
ai.camera_start()
#Start face recognition
ai.face_recognize_start()
screen.init()
#When the button A/B pressed
def button_a_pressed ():
print("button_a_pressed")
ai.register_face()
def button_b_pressed():
print("button_b_pressed")
ai.recognize_face()
bt_a.event_pressed = button_a_pressed
bt_b.event_pressed = button_b_pressed
try:
while True:
image_data = ai.camera_capture()
screen.show_camera_img(image_data)
if ai.is_ai_data_updated():
data = ai.get_ai_data()
print(f"Face: {data['face_flag']}")
if data['face_flag']:
print(f"Face ID: {data['face_detect']['face_id']}")
print(f"Left eye: {data['face_detect']['left_eye']}")
for i in range(3):
np[i] = (255,0,0)
np.write()
for i in range(3):
servo.move_to_angle(70)
time.sleep(2)
servo.move_to_angle(90)
time.sleep(2)
speaker.play_tf_music("sound1.wav")
else:
for i in range(3):
np[i] = (0,0,0)
np.write()
time.sleep_ms(1)
except KeyboardInterrupt:
print("\nCapture Ctrl+C interrupt!")
#screen.deinit()
ai.deinit_ai()
The original plan was to create something that can be fixed on entrance door but was not possible in short time.
References
MicroPython Examples - UNIHIKER Documentation