We have successfully controlled a LED output earlier and read the input from digital input. However, in real life IoT, we are interested in something more useful such as ambient temperature and humidity.
The temperature and humidity values are read from a sensor. There are various types of sensors available in the market. For this project, we use a virtual sensor that is based on SHT21 from Sensirion. The sensor is connected to our board via the I2C interface.
I2C is one of the most popular 2 wire bus used to interface several components together. Each I2C device has a unique Device Address. In our case, the virtual sensor is located at address 0x40. Check out more about the I2C bus in the link given below.
To make sure the sensor is detected by the platform you could run the following command on terminal
sudo i2cdetect -y 1
This command will show you a table that will have non Zero values at some address. It shows that your sensor is responding to the queries on the I2C bus.
The code is a little complex hence we have not included the code as a part of the tutorial. One can look at the code in the terminal view by running the project.
Go to the command prompt and run
cd hello-iotsudo python sht21.py
You may have noticed the use of "sudo" command. This is required because reading i2c data requires root privileges. The program simply outputs the temperature and humidity values as read from the sensor.
~/hello-iot $ sudo python sht21.pyTemperature: 32.9983520508Humidity: 44.9948730469pi@raspberrypi ~/hello-iot $
To adjust the read values, you could use the knobs to set the sensor at desired values. The subsequent run of the programs will read the set value with a certain precision.
You could also set the desired temperature and humidity value to the current readings of any city. E.g. to read the current values for London, simply type London in the input text area below the knobs and hit Enter. Voila, the sensor will now read the current measurements for London.