3. Programming for IoT Devices.docx (1).pdf
Document Details
Uploaded by TriumphantCalculus
ITE
Full Transcript
Topic No: 3 Programming for IoT Devices Upon completion of this topic, you will be able to: 1 Discuss the properties of various software tools used to configure IoT devices CLI IDE 2 Discuss the characteristics of various programming languages used to co...
Topic No: 3 Programming for IoT Devices Upon completion of this topic, you will be able to: 1 Discuss the properties of various software tools used to configure IoT devices CLI IDE 2 Discuss the characteristics of various programming languages used to configure IoT Ecosystem components 3 Discuss the various software constructs Commands Libraries Methods / Functions 1 IoT Devices Configuration All IoT Devices require some form of software configuration in order for it to perform its intended task. The devices that are sold in the consumer market are usually easier to configure than the devices that are required to perform a complex task in an industrial setting, requiring customization. One example of setting up a consumer device, the Belkin Wemo WiFi Smart Plug, can be found here. https://www.belkin.com/us/support-article?articleNum=8218 The Wemo app, once connected to WiFi, automatically detects Wemo devices. The wemo devices are also quite simple to use with ON / OFF being the typical commands. If customization is required, typical devices would require a good deal of software configurations and programming. In order to achieve this, the following tools would usually be necessary: - Command Line Interface (CLI) - Text / Code Editors Topic No: 3 Programming for IoT Devices - Integrated Development Environment (IDE) In this module, we will be using the Raspberry Pi to configure and program our own custom IoT Devices. 1.1 Command Line Interface Historical operating systems (OS) in the world were operated by issuing commands. This was before the Graphical User Interface (GUI) and computer mouse was invented. Figure 1 shows a Disk Operating System (DOS) variant that a typical computer user in the 1980s would face. Reading files, editing, deleting, moving and saving was done all through the use of commands by typing on a keyboard. Figure 1 Command Line Interfaces are used to process commands issued to a computer in the form of lines of text. Topic No: 3 Programming for IoT Devices Commands are executed in the Command Line Interface. In Windows, the default CLI is known as the command prompt. In Mac OS, the CLI is known as the terminal. Both the command prompt and terminal are shown in Figure 2. Command Prompt Terminal Figure 2 There are also other types of CLIs which are becoming popular for task automation and interactivity. One example would be Microsoft’s PowerShell. For the Raspberry Pi, the default CLI that comes with the Raspberry Pi OS is the LXTerminal, also known as terminal emulator. Figure 3 shows this. Figure 3 Topic No: 3 Programming for IoT Devices 1.2 Text / Code Editors A text editor is for writing/modifying/saving text/code. Some examples of text editors built into CLIs are - vi - nano Figure 4 shows a python application edited using the nano editor. Figure 4 Topic No: 3 Programming for IoT Devices We will be using the nano text editor for this module. A source-code editor is a text editor program designed specifically for editing source code of computer programs. It may be a standalone application or it may be built into an integrated development environment (IDE) or web browser. They also have many features. One example is NotePad++. Notepad++ is a free source code editor and Notepad replacement that supports several languages. 1.3 Integrated Development Environment An IDE is an application that facilitates program development. IDEs can be used as code editors but with a lot more features. Some typical features are code completion, syntax highlighting and error highlighting. IntelliSense IntelliSense is a code completion tool that helps developers by giving hints on possible code to type. Other benefits include parameter info, list info and member lists. Topic No: 3 Programming for IoT Devices Syntax Highlighting Syntax highlighting is used to visually differentiate various parts of a program such that it adds meaning. Error Highlighting This feature is a blessing to code developers. It visually tells the developer that something is wrong on a line by showing a red squiggly under the incorrect statement. 2 Programming Languages When building an IoT product, one of the most important decisions companies have to make is to choose a relevant programming language. There are a plethora of programming languages out in the market that are supported by various parts of the IoT Ecosystem, but certain characteristics allow companies to decide which programming language will be suitable for them to build their IoT products. These characteristics are : - Portability - Low energy consumption (for battery operated IoT Devices) - Flexibility - Ease in reading Below are 4 popular programming languages used in the IoT Ecosystem. 2.1 Java This language has many well-known advantages and portability is the key one. Java engineers can write and debug code on their work computers and then move it to any chip using a Java Virtual Machine. Therefore, developers can run the code on the smallest machines, and not just on smartphones and servers where JVMs are common. Topic No: 3 Programming for IoT Devices Even though Java may require different libraries to work with specific hardware, the nature of this language may be described as “write once, run anywhere.” 2.2 C and C++ C and C++are often being used for IoT projects for a variety of purposes. Given that both languages have relatively low energy consumption and advanced flexibility, developers can use them to effectively code for embedded systems that interface with the underlying hardware. 2.3 Python Nowadays Python has become very popular among IoT developers as it is very easy to read, flexible, quick and its power allows engineers to work with data-heavy apps. This will be the programming language used in this module. 2.4 JavaScript Today, JavaScript and its frameworks are actively used in IoT software development projects. For example, JavaScript and Node.js may be great for creating and managing public and private IoT systems and networks. Also, JavaScript has long been used by two microcontrollers, Tessel and Espruino. This may come in handy in cases when there is a need to use low-power microcontrollers such as Espruino or fast microcontrollers with a lot of memory like Tessel. Given the fact that both microcontrollers are based on JavaScript, even web developers can easily start working on IoT projects without spending much time on learning a new language. Topic No: 3 Programming for IoT Devices 3 Software constructs While configuring or programming IoT devices, one might need to know some software constructs in order to configure/program the device successfully. Typical software constructs are - Commands - Libraries - Methods / Functions 3.1 Commands Commands are executed in a shell. Some examples of commands could be creating a script and executing a script. For a command to be successfully executed, it needs a proper structure. Depending on the shell used, the command structure can vary. But in general, a command typically has 3 parts. a. The command – Action to be performed b. The options – To control the action c. The arguments – Values to be passed to the command An example of a command (without options or arguments) in the terminal is shown in Figure 5. The command whoami is used to find out the username of the logged in user. Figure 5 Topic No: 3 Programming for IoT Devices Another command that is used to list all the contents in a folder is ls. Figure 6 shows that. Figure 6 The ls command can also be controlled with the use of flags. A flag will alter the command to produce a desired output by setting options. By convention, flags begin with a '-' character, just to help the reader distinguish between flags and arguments. Figure 7 shows the command ls with a -al flag. Figure 7 Topic No: 3 Programming for IoT Devices The flag l displays the contents of the directory in a list, showing data such as their sizes and when they were last edited, and the flag a displays all files, including those beginning with a., known as 'dotfiles'. A command can also be executed with arguments. When running a python script, the command used will be python and the argument is the name of the script. Figure 8 shows an example. Figure 8 3.2 Libraries Software Libraries are well-organized pieces of code that are used to provide extra functionality to a user developed program. Usage of software libraries is meant to aid developers, such that the program development time is expedited. In Python, a library is also known as a module. Modules are imported to a code using the import keyword. Figure 9 shows this. Figure 9 In order to import a library, the library must first be installed, else the import statement in the code will fail. To install, we can use the command line tool called pip, which stands for python index package (pip). Figure 10 shows how pip is used to download the RPi.GPIO package (used in Figure 9). Topic No: 3 Programming for IoT Devices Figure 10 pip is the package installer for Python. You can use pip to install packages from the Python Package Index and other indexes. 3.3 Methods/Functions The libraries that are installed might have many built-in methods or functions. These methods/functions are what provide the support required to implement a solution. Figure 11 shows the setup function being called using the GPIO module imported earlier in Figure 9. Figure 11 Topic No: 3 Programming for IoT Devices GPIO is the alias given in Figure 9 for the imported library. The dot operator is used to access a method, thus GPIO.setup. In the parentheses (), certain values are passed to the method. Each value passed in the method is separated by a comma. In Figure 11, there are two values passed to the setup method. The first is the pin number of the RPi and second is the mode (output or input). Knowing what to pass to a function is important. This can be obtained by looking at the documentation. The RPI.GPIO documentation can be found here. https://sourceforge.net/p/raspberry-gpio-python/wiki/BasicUsage/