Site icon David Yin's Blog

Make an AQI device with Raspberry Pi Pico W and ENS160+AHT21 in 5 Steps

I received the ENS160+AHT21 sensor module yesterday. I can not wait for any more for the stuff. So, I build the AQI device right away.

I have to give credit to TimHanewich for his project on GitHub.

I changed it a little bit and recorded it below.

Step 1

Get a USB cable with a Micro USB B port to connect Raspberry Pi Pico W to a PC (Windows).

Go to the MicroPython website to download the latest firmware for Pico W.  The one I download is v1.23.0.

Hold the BOOTSEL button on Pico W, then connect to the PC.

After a few seconds, the PC will find a drive. Drag the uf2 file I downloaded above to it.

Step 2

Connect the two stuff. as below.

Step 3

Download the Thonny IDE and install it on the Windows. I downloaded v4.1.4.

Step 4

Prepare the code. The Code I updated can be downloaded here. src

Four files.

Edit settings.py to add the WiFI information and the post_url.  You can change the sample_time to 300 for measuring every five minutes.

In the main.py, I changed line 27 to match the wiring above.

Step 5

Connect the Pico W with the PC.

Launch Thonny IDE. Click Run > Interpreter, and select MicroPyton (Raspberry Pi Pico). click OK.

 

Then, open the files one by one and save a copy to the device.

Select the main.py file. Then click the Green Play button.

I post the data to a php file to save it to the MySQL database.

The 4th item with high eco2 and tvoc, because I opened the bottle of Alcool isophopylique.

Here is what it looks like for the setup. I mounted them on a 2X4 block with screws. I can move it anywhere in the house. I can also use the battery to power it.

Option:

The database structure of database aqi_1

The setting of the URL in settings.py.

post_url:str = "https://gthree.win/tools/aqi-data.php"

The main part of  aqi-data.php is as follows:

 
$data = json_decode(file_get_contents('php://input'));

$humidity = $data->{'humidity'};
$temperature = $data->{'temperature'};
$eco2 = $data->{'eco2'};
$aqi = $data->{'aqi'};
$tvoc = $data->{'tvoc'};
 
// Database connection 
$conn = new mysqli('localhost', 'username', 'password', 'aqi_1'); 
 
// Insert data Query 
$sql = "INSERT INTO env_status ( humidity, temperature, eco2, aqi, tvoc) 
VALUES ($humidity, $temperature, $eco2, $aqi, $tvoc)"; 
 
if ($conn->query($sql) === TRUE) { 
  echo "Insert your JSON record successfully"; 
}

Received the data and saved them to the database.

Exit mobile version