This is my project for computer science. It watches the air in a room and sends me an email when the reading gets high.
There is a Kitronik air quality board that clips onto a micro:bit and measures the air around it. The micro:bit sends the score out over serial and a python script on my laptop reads it. If the score is over 94 the script logs into my mail account and sends me a message.
That is the whole idea really. I picked 94 as the limit because I wanted a number I can change later without touching anything else.
There are two micro:bits. One has the air quality board on it and does the measuring. Press button A and it starts reading the sensor and writing the number out once a second. The other micro:bit takes a reading off the radio and puts it out over the serial port instead, which is the one I use when I want the sensor somewhere else in the room and the laptop beside me.
Either way the laptop just sees numbers arriving as text on a serial port. The script opens the port at 115200 baud, reads a line, turns it into a number and compares it to 94. When it goes over, the script connects to the mail server on port 587, starts TLS, logs in and sends the message. Then it goes back to reading again.
send.py runs on the micro:bit with the sensor on it. It asks the board for a percentage and writes it to serial. I put the button press in front of it so it is not sending anything while I am still setting everything up.
radioreceivebit.py runs on the second micro:bit and is about four lines long. It listens on radio group 1 and when a number turns up it writes it to serial.
serialreceive.py runs on the laptop. It lists the serial ports it can find, asks me which one I want, and then loops forever. Read a line, wait five seconds, check the number, send an email if it is too high.
I wanted a warning I would notice. A number on a small screen is easy to walk past and I am not going to sit and watch a display all day. An email turns up on my phone wherever I am, which is the part that makes it useful.
I also wanted to build the whole chain instead of one piece of it. The sensor, the radio and the email do not know anything about each other and getting them to work together is where most of the learning happened. A lot of the project was working out why the serial port would not open, or why the email kept failing when everything else looked fine.
The mail password is written straight into the python file. It works and it is quick and for a school project I do not think it matters much, but it is a bad habit and the first thing I would fix. It should be in a separate file that I do not upload anywhere.
The other problem is that it sends an email every time it sees a bad reading, which is roughly every five seconds. Leave it in a shut room for an hour and you will come back to a very full inbox. I would add a timer so it only emails once every ten minutes or so.