Software apps and online services
If you've already followed my tutorial on how to play WAV files on your M5Stack then this should be easy for you. We will be using VScode and the m5stack plugin. To see how to get set up watch this video.
First thing we'll need to do is to get the UID (Unique Identifier) of our RFID card or tag. Usually they wont have this printed on them, so we will need to scan them and write the codes down. Probably the quickest way to do this is to make this incredibly simple program in Uiflow.
Optionally here is the python code:
from m5stack import * from m5ui import * from uiflow import * import face setScreenColor(0x222222) faces_rfid = face.get(face.RFID) label0 = M5TextBox(136, 90, "Text", lcd.FONT_Default,0xFFFFFF, rotate=0) while True: label0.setText(str(faces_rfid.readUid())) wait_ms(2)
Once we have the UID's we also need to make sure we have some WAV files on an SD card or the flash system of our device. By default there is a WAV file called "mix.wav" in the /flash/res folder of the M5Stack flash memory.
Now, let's open VScode and start to write our program. Create a new python file in the apps folder by clicking the + button next to the directory.
Firstly, with the code we need to initialize the I2S peripheral, and we can do that like so:
from machine import I2S import os, uos, wave #initialize the I2S device i2s = I2S( mode = I2S.MODE_MASTER | I2S.MODE_TX | I2S.MODE_DAC_BUILT_IN, rate = 16000, bits = 16, channel_format = I2S.CHANNEL_ONLY_RIGHT, data_format = I2S.FORMAT_I2S_MSB)
Above we imported os and uos. These modules both deal with file directories in MicroPython. We need them to load the WAV files from your sd card or the internal flash of the device. Generally a simple os.mountsd() command should suffice to mount the sd, however I have experienced problems with this working reliably. If you have issues like me, this line usually fixes things.
Since both the screen and the SD reader on M5Stack both use SPI, I wonder if there is some conflict. Moving on, we now need to create a function to load the WAV file into the buffer and write it to the I2S, we do so like this:
#create a function to play the wav def wav_player(fname): wav = wave.open(fname) i2s.set_dac_mode(I2S.DAC_RIGHT_EN) i2s.sample_rate(wav.getframerate()) i2s.bits(wav.getsampwidth() * 8) i2s.nchannels(wav.getnchannels()) i2s.volume(20) while True: data = wav.readframes(1024) if len(data) > 0: i2s.write(data) else: wav.close() break
Next we need to mount the SD if we are intending to play WAV files off the SD storage. This is generally recommended so as to save precious flash space.
try: uos.mountsd() except: #os.mountsd() lcd.print('sd card not mounted',0,50,0xffffff) pass
Now for the final part of adding the RFID card trigger.
while True: if (faces_rfid.readUid()) == 'Insert your cards UID here': wav_player('/flash/res/mix.wav') i2s.stop()
And thats all there is to it. Alternatively you could get the screen to display a picture
image0 = M5Img(0, 0, "res/default.jpg", True) image0.show()
or text with lcd.print("text", 0, 0, lcd.WHITE)
Give it a go and get creative. If you make something please share in the comments.