Everything Becomes Programmable

2.1 Apply Basic Programming to Support IoT Devices

  • Objectives:
    • Use Python to create programs that accept user input and read/write to external files.
    • Describe basic programming variables and fundamentals.
    • Apply basic programming variables and fundamentals in Blockly.
    • Apply basic programming variables and fundamentals using Python.

Basic Programming Concepts

  • Flowcharts:
    • Diagrams used to represent processes or workflows.
    • Illustrate how a process should work.
    • Show input states, decisions, and results of those decisions.

Questions related to a flowchart example:

  • The sensor is checking for whether the door is open.
  • The frequency of sensor checks is not explicitly stated, but is implied to be periodic.
  • An alarm will not sound if the door is open for 5 seconds.
  • The alarm will sound if the door is open for 10 seconds.
  • The alarm will not sound if the door is open for 5 seconds, shut for 5 seconds, then reopened for 5 seconds

System Software, Application Software, and Computer Languages

  • Two Common Types of Computer Software:
    • System Software: Works between the computer hardware and application programs.
    • Application Software: Programs created to accomplish specific tasks.
  • Both system software and application software are created using a programming language.
  • Python is an example of an interpreted programming language.

Programming Variables

  • Programming languages use variables to hold phrases, numbers, or other important information.
  • Variables can hold the result of a calculation, a database query, or other values.
  • Example:
    • x+y=zx + y = z
    • x, y, and z are variables representing characters, character strings, numeric values, or memory addresses.
    • a=10a = 10 associates the value 10 to variable "a".
  • Variables allow programmers to quickly create simple or complex programs.

Basic Program Structures

  • Most common logic structures:
    • IF-THEN: Allows the computer to make a decision based on an expression's result.
      • Example: myVar > 0
      • If true (the value stored in myVar is greater than zero), the computer executes the associated action before moving to the next instruction.
      • If false, the computer moves on to the next structure.
    • FOR Loops: Executes a specific set of instructions a set number of times, based on an expression.
      • A variable acts as a counter within a range of values (minimum and maximum).
      • The counter variable is incremented each time the loop is executed.
      • When the counter equals the maximum value, the loop is abandoned, and execution moves to the next instruction.
    • WHILE Loops: Executes a specific set of instructions while an expression is true.

Basic Programming Using Blockly

  • What is Blockly?
    • A visual programming tool designed to help beginners understand programming concepts.
    • Allows users to create programs without writing lines of code.
    • Assigns different programming structures to colored blocks with slots and spaces for entering values.
    • Programmers connect structures by dragging and attaching blocks.
    • Specific blocks represent functions; select and drag them to the work space and fill in the required slots.

Programming with Python

  • What is Python?
    • A popular language designed to be easy to read and write.
    • Philosophy of the language:
      • Beautiful is better than ugly.
      • Explicit is better than implicit.
      • Simple is better than complex.
      • Complex is better than complicated.
      • Readability counts.

The Python Interpreter

  • The Python interpreter understands and executes Python code.
  • Python code can be created in any text editor; interpreters are available for many operating systems.
  • On Linux machines, the Python interpreter is usually installed in /usr/bin/python or /usr/bin/python3.
  • With the new Windows Python installer, Python is installed by default into the user's home directory.
  • When called with no arguments, the interpreter reads and executes commands interactively.
  • When called with a file name argument or with a file as standard input, it reads and executes a script from that file.
  • To start the interpreter, type python or python3 at the shell prompt.
  • In interactive mode, the primary prompt is represented by three greater-than signs (>>>). Continuation lines are represented by three dots (...).
  • The >>> prompt indicates the interpreter is ready and waiting for commands.

Variables and Basic Statements in Python

  • The interpreter receives and executes statements interactively.
  • It can act as a simple calculator.
  • The special variable _ holds the result of the last expression issued.
  • Use the = sign to assign values to variables.
  • Attempts to use an undefined variable will result in an error.
  • Functions allow for a block of code to be given a name and reused as needed.
  • Use the backslash character () to escape characters.
  • Single or double quotes can be used to wrap strings.
  • The print statement prints the result of the expression it was given.

Useful Functions and Data Types in Python

  • Range() - Generates a list of numbers, usually used to iterate with FOR loops.

    • range(stop) - The number of integers (whole numbers) to generate, starting from zero.
    • range([start], stop[, step]) - Starting number of the sequence, the ending number in the sequence, and the difference between each number in the sequence.
  • Python supports many useful functions and datatypes. Some of the more important ones are as follows:

  • Tuples - sequences, separated by parentheses.

  • Lists - sequence of changeable Python objects created by putting different comma-separated values between square brackets.

  • Sets - unordered collections of unique elements. Common uses include membership testing, removing duplicates from a sequence, and computing standard math operations on sets such as intersection, union, difference, and symmetric difference.

  • Dictionaries:

    • A list of elements separated by commas.
    • Each element is a combination of a value and a unique key.
    • Each key is separated from its value by a colon.
    • Dictionary elements can be accessed, updated, and deleted.

Programming Structures in Python

  • IF-THEN, ELSE, ELIF
    • Make decisions based upon the result of an expression.
    • ELSE specifies instructions to be executed if the expression is false.
    • ELIF is used to perform a second test.
  • FOR Loop
    • Iterates the items of any sequence (a list or a string) in the order they appear in the sequence.
  • WHILE Loop
    • Executes a block of code if the expression is true.

2.2 Prototyping Your Idea

  • Defining Prototyping

    • Prototyping is the process of creating a working model of a product or system.
    • In IoT, it helps to have design, electrical, physical/mechanical, and programming skills, and understand how TCP/IP works.
    • Because the IoT is still developing, there are still unknown tasks to discover.
    • This is a great time to invent something that is part of the IoT.
  • How to Prototype

    • A team at Google used the “Rapid Prototyping Method” to create Google Glass.
    • Kickstarter, Indiegogo, and Crowdfunder are examples of online crowd funding programs.

Prototyping Resources

  • Physical Materials

    • The Internet is a great place to start; people who have never physically met can now collaborate and work together.
    • Maker Media is a global platform for connecting makers to exchange projects and ideas.
    • Making Society has a section on modeling plastic and clay.
    • LEGO Mindstorms has a large community of contributors and fans.
    • Meccano (Erector Set) is a model construction system with reusable metal strips, plates, angle girders, wheels, axles, gears, nuts, and bolts.
    • 3D printing is the process of making a solid object based on a 3D model computer file.
  • Electronic Toolkits

    • While you can create programs for almost any computer, some platforms are designed for beginners. Examples:
      • Arduino: An open-source physical computing platform based on a simple microcontroller board and a development environment.
        • Allows you to develop interactive objects that take input from switches or sensors to control lights, motors, and other physical objects.
      • Raspberry Pi: A low-cost, credit-card-sized computer that plugs into a computer monitor or TV.
        • Capable of browsing the Internet, playing high-definition video, making spreadsheets, word-processing, and playing games.
      • Beaglebone: Similar to Raspberry Pi in size, power requirements, and application.
        • Has more processing power; therefore, is a better choice for applications with higher processing requirements.
  • Programming Resources

    • Programming is critical to the IoT. Creating custom code is very useful when developing an IoT solution.
    • MIT OpenCourseWare (OCW): A web-based publication of almost all MIT course content.
      • A great place to get familiar with computer programming for free.
      • OCW programming related courses can be found at http://ocw.mit.edu/courses/intro-programming.
    • Khan Academy: A non-profit educational website created to provide “a free, world-class education for anyone, anywhere”.
      • Lectures related to computer programming can be found at https://www.khanacademy.org/computing/cs.
    • Code Academy: Relies on interactivity to help people learn how to write computer programs; http://www.codeacademy.com.
  • Community Inventor and Entrepreneurship Workshops

    • Places to get help exposing your idea or prototype to others.
    • Investigate what is available in your community.
    • The Internet has many resources to help your idea get exposure.
    • Quirky: Allows users to share their ideas; other users can vote and choose whether or not they want to support the idea.
      • If an idea is good, it may become a real product; https://www.quirky.com/how-it-works.

Chapter Summary

  • Flowcharts are diagrams used to represent processes.
  • Two common types of computer software: system software and application software.
    • Application software programs are created to accomplish a certain task.
    • System software works between the computer hardware and the application program.
  • The most common logic structures are IF – THEN, FOR Loops, and WHILE Loops.
  • Blockly is a visual programming tool created to help beginners understand programming concepts using colored blocks.
  • Python is a popular language designed to be easy to read and write, requiring an interpreter to parse and execute code.
  • Python supports many useful functions and datatypes including Range(), Tuples, Lists, Sets, Dictionary, ELSE and ELIF.