Hello IoT is your first project to begin IoT programming. View Source
In this project, we will show you an overview of the fundamental building block of IoT - measure, gather and analyze. We will measure Digital Inputs, will report to a cloud-based platform and take action based upon that intelligence.
- Basics of working on an embedded system such as working with command lines.
- Interaction with hardware such as reading digital input and output.
- Thinking regarding control logic, i.e. which component should do what and when.
The project consists of an emulated Raspberry Pi connected to digital input/output and a temperature/humidity sensor.
Signup with iotify.io if you haven’t yet. The free account will have sufficient credits to run this project. Once you are logged into the IoTIFY main application, go to the virtual lab and click on the Hello IoT project.
Provide a unique name for your project. Then simply click launch.
The project takes 3-5 minutes to launch. The project state will automatically change to running once it is ready. You could then simply click the Open button and it will launch a new window with the Workspace of Hello IoT project.
Let’s write our first program to toggle a LED connected with GPIO pin on our virtual target.
We are going to create a simple python program named hello-led.py with following contents.
We have already downloaded the code for you at the folder hello-iot. To run this program on the target, open a command prompt from click the the top left icon
and type following
The program will run in a loop and toggle the LED on PIN number 9. Switch the Webview tab to see the effect in action.
Press Ctrl+C to stop the program.
Congratulations! You now know how to control the GPIO pins.
In the previous step, we controlled a LED as output. Let’s take the next step and see if we could read the input state of a button.
In this step, we would read the input value of a button in a tight loop and print it on the console, every time we detect a change.
We will use Python code again for this step, which looks as follows:
Run this file just like last time. If you modify the state of Pin 9 in webview, you would see the program responds to the new state and prints the output as follows.
Congratulations! It completes your input/output programming practice.
Reading in a while (1) tight loop is generally not a good programming practice. It is used here only for the demonstration.
Let’s delve into something a bit complex now. We have successfully controlled a LED output earlier and read the input from a digital input. However, in real life IoT, we are interested in something more useful such as ambient temperature and humidity. How do we measure temperature and humidity values from connected sensors? That will be our next step.
The temperature and humidity values are read from a sensor. There are various types of sensors available in the market. For this project, we will be using a virtual sensor which is based on SHT21 from Sensirion. The sensor is connected to our board via I2C interface. Check out this great sensor here.
What is I2C
I2C is one of the most popular 2 wire bus used to interface several components togather. Each I2C device has a unique Device Address. In our case, the virtual sensor is located at address 0x40. Check out more about I2C bus here
To make sure sensor is detected by the platform you could run the following command on terminal
This command will show you a table which will have non Zero values at some address. It shows that your sensor is responding to the queries on I2C bus.
The code for this project is a bit complex, so we would not publish it here for the sake of brevity.
Go to the command prompt and run
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.
To adjust the read values, you could use the knobs to set the sensor at desired values. Subsequent run of the programs will read the set value with 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 input text area below the knobs and hit Enter. Voila, the sensor will now read the current measurements for London.
Now since we have all the elements in place to deal with hardware, it’s time to put together pieces of IoT.
In real life, IoT is not about individual devices making independent decisions. Often, the value measured by IoT are sent to a cloud-based infrastructure to visualize and report. Based on the decision logic it could also generate some actions, which are applied back to IoT device to enable a complete control loop.
In our next program, we will build a smart home monitor which allows you to control your door/window contacts as well as humidity and temperature values inside the house.
The system will automatically turn on the alarm lights on when it detects if temperature increases beyond a predefined threshold (simulating fire) or humidity goes above a particular value (simulating flooding)
For building our smart home monitor, we will use dweet.io and Freeboard.io, two awesome services from NYC based Buglabs. The dweet.io service is like Twitter for devices, while Freeboard is a beautiful dashboard which consumes the data from dweet.io and shows it in an excellent dashboard.
To begin with, you would need to choose a unique name which will become a URL for your project i.e.
It is important that you choose a unique name here, because dweet.io service is open to public by default and if your name is not unique, it may corrupt the data for another project using the same url, or your own project might receive unexpected data from another system.
Open the file editor on the left hand side and edit the file hello-home.py.
We will need to replace variable sender to something unique and then save the file.
Then go to the directory and run the command
Note that this file is dependent upon sht21.py being in the same directory.
Once the program starts running, you could open following in your browser
You will see the status of all the alarms in this web page.
Now change the temperature/humidity or toggle pin 7/8 to see it reflected on the web page. You would also notice that LED at pin 9 glows when any alarm condition is met.
Click on the top button “Create a custom dashboard” to create a beautiful dashboard and you will soon see a custom dashboard
Play with button 7/8 to simulate Door and Window breaks and Change the temperature and humidity values to simulate fire and water.
Now since you have built a beautiful dashboard for home alarm system, it is time to move forward to some more interesting actions. E.g.
- Try connecting your alarm system to an email trigger using this, to send you an email when something wrong happens.
- Try connecting alarms to send you an SMS using Twilio
- Trigger a large number of use cases using IFTTT maker channel from this guide.
You could use the pyftt library to the IFTTT trigger channel.
Liked the guide? Have some feeedback ?
Your feedback is very motivating for us. Please don’t forget to leave your comments in our forum below.