Introduction: Basic Internet of Things Framework With Intel Edison
The Internet of Things stack can involve sensors, small computing devices, Cloud Computing services and APIs. For most beginners, implementing each one of these parts individually is cheap and straightforward, with several available options for affordable hardware and online tutorials.
Implementing the entire stack, however, involves the additional work of adapting such hardware and tutorials so all parts can be adequately integrated. This Instructable aims to provide a straightforward full stack guide for building a simple but complete IoT example that can be easily shown in a lecture or a talk.
To avoid redundancy, for some of the steps we simply reference existing tutorials because no adaptation is needed. But, in this case, we prioritize official Intel material or other existing Instructables to minimize the risk of broken links in the future.
The main topics covered are:
1) Gathering and processing data from a sensor. In our example, the sensor is an hygrometer that measures soil humidity from a plant vase, as shown in the picture above.
2) Recording data in a cloud database service. In our example, we use the Firebase service to store the humidity data mentioned above.
3) Reading a database service to feed a dynamic web page. In our example, we show the humidity in a site hosted in Python Anywhere.
4) Pushing data to a social network. In our example, we send tweets whenever the plant has just received water.
Step 1: Setting Up the Edison
Due to its small size, high processing power and embedded Wi-Fi, the Intel Edison is a good ground for building an Internet of Things device that is complete, portable, and presentable without clumsy Ethernet cables getting in your way. Furthermore, with the official Arduino Breakout Board, you can easily access data from sensors.
First of all, make sure you follow the official Intel guide for assembling the board, updating the firmware and connecting to an available Wi-Fi network:
In our experience, updating the firmware was particularly important to ensure fast Internet speeds. We will, however, ignore the "Set up your IDE" step in favor of working via terminal and direct file transfer.
- If you use Windows, this other Instructable shows how to do this with PuTTY and WinSCP.
- If you use Linux, this Intel article shows how to do this with SSH and SCP.
Once in Edison's terminal, add AlexT's repositories to the opkg package manager, by writing the lines below to the file /etc/opkg/base-feeds.conf. You can use the default text editor vi to do this, but if you have difficulties with it you can also overwrite the file with a direct transfer.
src/gz all http://repo.opkg.net/edison/repo/all
src/gz edison http://repo.opkg.net/edison/repo/edison
src/gz core2-32 http://repo.opkg.net/edison/repo/core2-32
Despite not being official, these repositories are well-established and still constantly updated as of writing this.
To conclude this step, run the commands to update the opkg database and install the pip package manager:
opkg install python-pip
This is particularly useful for installing Python modules that are not available in the repositories.
Step 2: Setting Up the Sensor
With the Edison up and running, we can gather and process data from a sensor. We use one of the most popular sensors in home automation Arduino examples, an hygrometer [FIG 1] measuring soil moisture in a plant vase.
Plugging the sensor into the Breakout Board is straightforward, as seen in the Fritzing model [FIG 2]. In this scheme, the humidity data comes from the first analog pin (A0).
The first Python code [FIG 3] reads the signal value from A0 and prints it.
This value is an integer between 0 and 1023 that gets lower when humidity increases. The second code [FIG 4] maps it to a more readable form before printing.
If you want to run one of these codes from Edison just save it to a file, preferably with the .py extension, transfer this file and run the command
where PYNAME is the file name. Although these programs are not part of the stack we will build, it is recommended to run them to confirm that the sensor is working.
Step 3: Setting Up Firebase
There are many data storage services available in the Cloud. We consider Firebase a good choice due to the simplicity of its default configurations. Just create an account and you will be able to set up, for free, a new storage with the address FBNAME.firebaseio.com, where you choose what FBNAME is.
Use the following commands to install the Python interface for the service:
pip install requests
pip install python-firebase
The code above writes the value 10.0 in the key value of FBNAME.firebaseio.com/humidity. You can easily use the Firebase App Dashboard to check if the code worked after running it.
Don't forget to replace FBNAME before actually running it. Again, these programs are not part of the stack we will build, but it is recommended to run them to confirm that the storage is working.
It should also be mentioned that we are using the default security settings, which in principle allow anyone with an Internet access to write data. This is definitely not recommended for production, but more than enough for a lecture or talk.
Step 4: Setting Up Django
Like data storage, there are many web hosting services available in the Cloud. And again due to the simplicity of its default configurations, we choose PythonAnywhere to host a minimal Django site. After you create a new Django project in PythonAnywhere, it is mostly straightforward to adapt the directions of the official documentation. In fact, we demand less than the documentation, since you can skip all steps involving the development server and database models. We simply require a view [FIG 1] and an index.html template [FIG 2].
This should be enough for setting up a dynamic page that reads and displays the humidity data recorded in Firebase. We will not go into details on how to improve the view with images and style sheets, but the guide should be more than enough to get you started on that.
Step 5: Setting Up Twitter
Finally, we set up an account that can programatically publish tweets. Create a Twitter account, if you do not already have one, and create a Twitter App. From this app management, go to Keys and Access Tokens and copy the Consumer Key and Consumer Secret, which we will refer to as COKEY and COSEC respectively.
There is a third key you need before publishing. To find it, first install the authentication library:
pip install requests-oauthlib
After installing it, run the first code [FIG 1].
Do not forget to replace COKEY and COSEC before running. It will print an URL and wait for your input. Follow this URL in your browser and log in with your Twitter account, if you are not logged already. The number you will receive is the input that is being waited for. Enter it and the code should print you the Client Key and Client Secret, which we will refer to as CLKEY and CLSEC respectively.
You now have all the necessary information to publish tweets. To test it, run the second code [FIG 2].
Again, do not forget to replace COKEY, COSEC, CLKEY, CLSEC.
Step 6: Putting All Together
We now consolidate all previous steps into a single program that:
- on each second, gets and prints the humidity level;
- on each ten seconds, sends to Firebase the mean of the last ten humidity levels;
- when the humidity increases by 10% from one second to another, a tweet with the message "Not thirsty anymore!" is published.
Do not forget to replace FBNAME, COKEY, COSEC, CLKEY, and CLSEC in the code above.
And that is it! You have completed your basic framework to introduce the Internet of Things with a live demonstration using Intel Edison. And there is a lot of flexibility in this framework for you to put your personality into it.