Creating your own IoT Network (IoT Part 1 of 3)
In this series, we are going to look at how you can create your own IoT device without relying on a third party’s server to manage the communication. This tutorial is ideal if you are looking to setup a few IoT devices around your own home using your local network.
We will cover the software required at the server side, and the microcontroller side. This tutorial will also involve some hardware modifications of the off the shelf components. However, in general I’ve tried to keep it as straightforward as possible so that it may be used as a foundation for a new IoT project. You should have some programming knowledge, and knowledge of C++ and Python will help a lot.
At the end of this series, you will have a server that can accept incoming connections from any number of microcontrollers. Each microcontroller will be sending two pieces of data to the server: the value of a counter, and its internal temperature. The server will be able to turn on and off an LED on the microcontroller.
Here we go!
Figure 1 – System architecture of the IoT network we are going to build. All of the boxes shaded green are implemented in software.
Figure 1 shows the architecture of the system we are going to build. On the server side, there is a PC running a WebSocket server implemented in Python. I chose to use WebSockets because conveniently there were some implementations of the WebSocket protocol available for the microcontroller I am using, and a simple server that I could run on the PC. More practically, WebSockets allow for full duplex communication over a single TCP connection, and are very scalable.
On the microcontroller side, I have been using the NUCLEO-F401RE development board designed for ARM’s mbed platform. In addition to the NUCLEO board, you will also require the Wifi Shield V2.0 from Seeed Studio. The Wifi Shield is a bit on the expensive side, but very powerful. It is based on the RN-171 module that was originally developed by Roving Networks, but they have since been acquired by Microchip. It is also commonly out of stock, so buy them while you can! I have been able to locate it on Mouser, which is able to ship them more quickly than Seeed Studio.
Before we can get started on the good stuff, some administrative tasks need to be completed first: upgrading firmware, downloading software, and a few small hardware modifications.
You will need to prepare a few items on your computer for this tutorial:
- Download and install the latest version of Python v3.5 from this link.
- (Optional) Install a Python integrated development environment. I have been using, and highly recommend, PyCharm. You can get a free license as a student. Once installed, make sure it can find your Python executable, and ensure it is using v3.5. You may already have a Python v2.7 installed on your system.
- Using Python, install Tornado. Tornado will be implementing the WebSocket server on your computer, and more information about it is available here. To install Tornado with Windows:
- Open a command prompt window with administrative privileges
- Change the current directory to the one which contains pip.exe by typing: “cd C:\...some path you need to fill in…\Python\Python35-32\Scripts” and press enter. pip.exe is used to install and manage software packages written in Python.
- Type “pip install tornado”, and press enter. Once the installer completes, you are all done!
- Signup for an account at https://developer.mbed.org/. All of the microcontroller projects are saved in their online compiler, and can be easily accessed once you have an account.
- First, download and install the latest PC drivers for the NUCLEO board from this link.
- Upgrade the firmware of the NUCLEO board to its latest version by following the instructions on this website.
Wifi Shield Hardware
These are the only hardware modifications which are required in this tutorial, and once complete will result in a more reliable operation of the Wifi Shield, and the WebSocket server.
Figure 2 – Photos of the modified Wifi Shield.
- Short together the two pins of P6 on the bottom of the Wifi Shield. This will connect the TCP Status pin of the RN-171 to D5.
- Solder a 300Ω resistor onto P5. This allows code to reset the Wifi Shield, and is useful when trying to automatically reconnect to an access point. In reality this should be a diode, but I only had a resistor available at the time.
- Solder a wire from WIFI_RX to D8. That’s the small red wire in Figure 2(b). This allows you to use the serial port of the mbed to debug, at the same time as the Wifi Shield.
- Configure the jumpers on the Wifi Shield as shown in Figure 2(a). One of the jumpers is not being used, hence it is not ‘jumping’ anything. The jumpers are used to connect the UART module of the RN-171 to an appropriate TX and RX pin on the NUCLEO board.
Preparing the Wifi Shield Firmware
The firmware that comes preinstalled with the wifi shield is most likely to be v4.41. I have developed this tutorial using the latest firmware available at the time, v4.75. Therefore to ensure that your IoT device works as reliably as possible, I recommend updating the firmware by following these steps:
- Physically install the Wifi Shield onto the NUCLEO board.
- Compile and program the NUCLEO board with the WifiFwUpdater program. This program simply lets you communicate directly with the RN-171 module.
- Connect to the NUCLEO board using a program like Tera Term so you can directly communicate with the Wifi Shield.
You are now going to connect the Wifi Shield with your home network, and the shield will download the latest firmware from the internet.
- Type ‘set wlan ssid MyHomeNetwork’ into Tera Term, and press enter, where ‘MyHomeNetwork’ is the ssid of your network. It should respond with ‘AOK’.
- Type ‘set wlan pass MyHomeNetworkPassword’ into Tera Term, and press enter, where ‘MyHomeNetworkPassword’ is the password for your network. It should respond with ‘AOK’.
- Type ‘join’ into Tera Term, and press enter. You should now see an output which looks like:
<4.41> Auto-Assoc VM855234-2G chan=1 mode=MIXED SCAN OK Joining MyHomeNetwork now.. Associated! DHCP: Start DHCP in 3599ms, lease=86400s IF=UP DHCP=ON IP=192.168.0.27:2000 NM=255.255.255.0 GW=192.168.0.1 Listen on 2000
4. Once connected to the network, you are ready to start upgrading the firmware. This process takes a while, and sometimes will fail. You can just try again until it succeeds. Type ‘ftp update wifly7-400.mif’ into Tera Term, and press enter to start the process.
More details about the Wifi Shield, and this firmware upgrade process can be found in the WiFly Command Reference, Advanced Features and Applications User’s Guide.
At this point you should be ready to start coding. Check out Part 2 of this series to learn about the microcontroller code.