Now the web app is created and the hardware is assembled, it's time to connect them! The following module goes through the process of communicating between the physical sensors and the database that the web app uses.
On the Pi, we're using the Adafruit's DHT22 library to use our DHT22 on the AmbiLamp PCB. In order to use the Adafruit DHT22 library, it has to be installed.
On the Terminal of the Pi, clone the Adafruit DHT22 Python Library by executing the following command:
git clone https://github.com/adafruit/Adafruit_Python_DHT.gitChange into the Adafruit DHT22 Python Library directory:
cd Adafruit_Python_DHT/Here, we will install the library so we can use it with our wrapper libraries. To install the Adafruit DHT22 Python Library:
sudo python setup.py installAfter the installation is complete, there is now a Python module installed in your Python environment. We could use the Adafruit's DHT22 Python Library, however, the libraries we have is a wrapper of this library.
Now, we'll bring our own libraries. Clone the IoT AmbiLamp starter repository:
git clone https://github.com/rpatel26/intro_to_iot_python_starter_code.gitChange into the starter directory and check out its contend:
cd intro_to_iot_python_starter_codelsThere should be three python starter files: starter_app_integration.py, test_hardware.py, and test_sound_detector.py. The libraries directory contains all the wrapper libraries for this project.
The Python request library makes it easy to submit HTTP requests to a web API. In the Terminal of the Pi, execute:
sudo pip install requeststo obtain this library.
The code block below shows a skeleton script for using requests. Study and understand it, because you're about to fill in to make post and get requests to your own API! This is the same code provided in starter_app_integration.py.
import requests# import the DHT and MCP libraries# construct the DHT22# construct the ldr# the address we will make the request to# REPLACE WITH YOUR URLurl='http://intro-to-iot-lesson.herokuapp.com/api/data'# initialize an empty dictionarypacket = {}# get the temperature and humidity from DHT object# get the brightness from the ldr object# fill the packet with data in the format expected by the web APIpacket['temperature'] = round(temperature, 3)# just a debug, comment it out when you know the script worksprint(packet)# submit the post request.r = requests.post(url,json=packet)The challenge here is to fill in the skeleton code above so that the script:
Run the script by executing
sudo python starter_app_integration.pyYou can check that the script is working by looking at your MongoDB database, or by making a GET request through the web app for that /api/data route.
Once it's confirmed that data entry is posted to the web app every time the script runs, move on to the next challenge. Bonus, check out the details page -- you should see the data being used by the web app!
This part is a bit trickier. Every time we take a new data reading, we need to update the statistics document so that our homepage dashboard is accurate. To go this, we need to:
Now, the only script left for this project is the LED script that controls the LED. A test script on how to control the LED is provided in the intro_to_iot_python_starter_code/libraries/RGB_LED folder.
The test_rgb_led.py file provides an example on how to control the LED. Study and understand the code get a an idea of what it's doing.
Once familiar with the LED script, it's time to write the code that controls the LED! Create a new file with the following skeleton code:
import requests # import the RGB and Color libraries# the address we will make the request to # REPLACE WITH YOUR URLsettingUrl = 'http://intro-to-iot-lesson.herokuapp.com/api/settings' # construct the RGB LED and Color object while True:# make a get request to retrieve the current settings, and extract the JSON from it # set the color to the lightColor field of the settings object # set the RGB's color Fill in the skeleton. It's a pretty simple script - refer back to the test scripts provided in the libraries folder and it's documentation to figure out how to control the LED. It needs to:
Test that your script works by visiting your web app's home page and changing the color with our color form. You could also change the color with Postman, or even by making a put request to the settings document from the script.