Hello IoT

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.

What you’ll learn

The project consists of an emulated Raspberry Pi connected to digital input/output and a temperature/humidity sensor.

Get Started

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.

1. Your First IoT Program

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.

# Import the GPIO controller module
import RPi.GPIO as GPIO
import time
print("Hello LED")
ledPin = 9
print("Setting Broadcom Mode")
# Pin Setup:
GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
print("Setting LED to output")
GPIO.setup(ledPin, GPIO.OUT)
print("Here we go! Press CTRL+C to exit")
try:
while 1:
print ("OFF");
GPIO.output(ledPin, GPIO.LOW)
time.sleep(1.00)
print ("ON");
GPIO.output(ledPin, GPIO.HIGH)
time.sleep(1)
except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly:
GPIO.cleanup() # cleanup all GPIO

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

cd hello-iot
sudo python hello-led.py

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.

2. Reading Digital IO

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:

# External module imports
import RPi.GPIO as GPIO
import time
print("Hello Button")
buttonPin = 9
prevButtonState = True
buttonState = True
print("Setting Broadcom Mode")
# Pin Setup:
GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
time.sleep(0.5)
#print initial settings
buttonState = GPIO.input(buttonPin);
print "Initial state is ", 'pressed' if buttonState else 'released';
try:
while 1:
buttonState = GPIO.input(buttonPin);
if prevButtonState != buttonState:
print "Button is ", 'pressed' if buttonState else 'released';
# save last state
prevButtonState = buttonState;
time.sleep(0.1)
except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly:
GPIO.cleanup() # cleanup all GPIO

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.

pi@raspberrypi ~ $ cd hello-iot
pi@raspberrypi ~ $ sudo python hello-button.py
Hello Button
Setting Broadcom Mode
Initial state is released
Button is released
Button is pressed
Button is released
Button is pressed

Congratulations! It completes your input/output programming practice.

Important
Reading in a while (1) tight loop is generally not a good programming practice. It is used here only for the demonstration.

3. Reading Temperature And Humidity

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

sudo i2cdetect -y 1

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

cd hello-iot
sudo 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.py
Temperature: 32.9983520508
Humidity: 44.9948730469
pi@raspberrypi ~/hello-iot $

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.

4. Connecting the dots

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)

Introducing Dweet.io and Freeboard.io

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.

https://dweet.io/follow/[your_unique_name]

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

cd hello-iot
sudo python hello-home.py

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

https://dweet.io/follow/[your_unique_name]

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.

Where to go further

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.

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.