IoT Smart Room Part 1/3 — Automated Lighting
Smart lighting though Python and the Watson IoT Cloud Platform.
This is the first part in a three part series outlining the integration of different components in a smart room through the Watson IoT Cloud Python modules, as well as a documentation of my learning from the project. I’d like to thank the team at IBM IoT, especially Ashek Mahmood and Santanu Sarker, for the guidance and support during development. All code can be found in my Github at https://github.com/alisyakainth/WatsonIoT_Smart_Room.
For this part, in terms of hardware I used:
- A Raspberry Pi 3B
- 2 Sunfounder Light Seeking Sensor modules (regular photocells can also be used)
- 6 female to female jumper wires
- A Philips Hue light bulb
- A Philips Hue Bridge
This approach to automated lighting can be divided into two different components that work together: photocells (sensors) and our Philips Hue light bulb (the actuator). Light sensors (photocells) need to be set up first to continuously monitor the amount of light in a room. These sensors can then send this information to the IoT cloud platform, which will be used later on by the light bulbs.
Any photocells can be used to collect the needed lighting information. I used the Sunfounder sensors mentioned above, which have a regular photocell attached to one end along with a LED bar to display results. To gain sensory information I only needed to hook up the photocell to the Raspberry Pi, not the LED bar since it was not necessary. An image is shown below of the configuration of two of these devices attached to the Raspberry Pi.
As seen on the 40 pin Raspberry Pi, both modules are connected through 3V pins for power. This imitates the functionality of analog pins (which Raspberry Pis do not have) and allows us to read the potential sensory information that the photocell collects. The next jumper wire on each module is connected to ground, and the last wire is connected to a GPIO pin for input (this can be, of course, any GPIO pin), I chose numbers 7 and 40.
The Python script shown below goes with the specific setup mentioned above.
For now, we will only be looking at the
rc_time() function and the associated setup since it is responsible for reading data from the two sensors. After importing the necessary modules for using the pins of the Pi and setting the mode of the board, I define two pin variables,
pin_b (lines 15–16). These pins, as mentioned before, are used for the two light modules later on in the script.
Now, we will be going through the
rc_time() function. The first lines of the funtion set up the GPIO pin passed in for reading the photocell and sets a local
count variable to 0.
count logs the amount of iterations that go by when the photocell stops sending an electrical signal (there is no light in the room), and in this way acts as a timer. You can see from the while loop that after 100000 iterations, the room is seen as dark since that amount of iteration passes before an electrical signal is restored and the loop breaks.
The series of if statements that come next just measure the
count variable to determine in plain text if the room is “light” or “dark”, then reassigns
count to that string.
count (now a string) is then returned from the function.
Sending Data to the IoT Cloud
Now that the sensory component is working, the next step is to send all information to the Watson IoT cloud platform. On the same Python script, I import a few modules for access to the cloud. All devices used on the cloud (in this case the Raspberry Pi) must first be registered on the IoT platform, then accessed with a configuration file parsed in our Python script. You can find the format and documentation for creating a device configuration file here.
Our configuration file has all the necessary information stored in JSON format with the .yaml extension. This allows access to the IoT platform, which allows us to send data from the Pi also in the form of JSON.
Looking at the previous Python script, lines 9–11 of the script does the setup mentioned above. We use
client.connect() to connect to the IoT cloud platform in order to send data later on, which is seen in lines 38–43. The while loop here runs continuously and begins by receiving the lighting status of both modules, which are held in variables
data variable is then set to hold these results in a JSON format to prepare for sending it to the cloud, which is done in the
eventPublishCallback() needs to be defined in order to run every time data is sent to the cloud, and is done so in lines 5–6. For now, this function has a simple print statement just to verify that sensory data was sent successfully, and can even be modified to show exactly what data was sent by passing the data variable.
For the simplicity of testing purposes, the data in this script is sent every 60 seconds (or how ever much time you choose) through the
time.sleep() function that completes the loop.
Smart Light Bulb Setup
Now that sensory data is sent to the cloud based on whether a room is dark or light, we can setup the remaining hardware to automate the smart lighting. The Philips Hue Bridge will need to be connected to your home router in order to get an IP address, so that it can be used in the Python script. The bulb will need to be connected to the Bridge in order for it to be accessed through Python as well.
Receiving Data from the Cloud
This new Python script will act as an application that controls the smart light bulb and will start by importing another
wiotp module, this time being the application class instead of device.
The configuration file will need to be changed slightly to adapt to the application status and you will need to generate keys on the Watson IoT platform to connect. You can find the documentation and format for creating an application configuration file here. This setup is shown in the first few lines of the following script.
The setup of the Philips Hue Bridge is also done in the next few lines and connected to by using the
b.connect() function. Now that we are connected to both the bridge and the cloud, we can start listening for data sent by the Pi. This is done in the continuous while loop at the end of the script, which means we are continuously listening for the JSON lighting status from the cloud.
Once an event is found, it is passed to the
myEventCallback() function for processing. In the if statements of the function, we turn the light bulb on or off depending on the state of both the light modules. I used both states to make sure the lights turn on or off only when we need it, but one light module will also work.
Now, while running both scripts, the lighting system has achieved autonomy! You can add as many light bulbs as the Bridge allows to control using the same script. You can even add a basic GUI to the application component in order to directly control if the lights are on or off, and change their colour using the same
phue Python module.
All code for this component of the project can be found on my Github. Below are some links to recommended reading and the documentation for the used Python modules.