Difference: IntroductionToNopIoTAndPython (7 vs. 8)

Revision 82019-06-11 - IsaacArmahMensah

Line: 1 to 1
 
META TOPICPARENT name="WorkshopSlides"

Start Presentation

Slide 1: Introduction to Internet of Things (IoT)

Line: 6 to 6
 

Introduction of Lecturers

  1. Uli Raich -- Formally CERN, Geneva, Switzerland
  2. Isaac Armah-Mensah -- University of Cape Coast Ghana
Changed:
<
<
>
>

 

The TWiki server

All information about this tutorial is available on a TWiki server.

Line: 26 to 26
 Micropython Demos:

https://github.com/uraich/MicroPython_IoTDemos and
https://github.com/uraich/MicropythonCayenneMQTTClient

C++ version:
https://github.com/uraich/C-IoTDemos

Changed:
<
<
>
>

 

Workshop Page

workshop.png

Added:
>
>

What is the “I” in IoT ?

 
Changed:
<
<

The Internet of Things (IoT)

When the Internet was invented it was used for communications between humans

Typical applications where:

  • Email

  • Remote login

  • File transfers

With cheaper and more powerful micro-controllers devices can communicate with each other

or with centralized servers over the network and they can observe their environment

with dedicated sensors.

This is what we call the Internet of Things (IoT)

>
>
The “I” stands for Internet. This means:

We need a processor (IoT node) that can connect to the Internet.

It must be powerful enough to run Internet protocols and

it needs an interface to the Internet.

  • Ethernet interface

  • WiFi interface

  • GSM
  • … connection to a gateway with access to the internet

Standard way to access the Internet

Usually the Internet is used by humans

Typical applications are

  • WEB browsing

  • Email

  • Social media

  • Telephone and chats
Human to machine or human to human communication.

!IoT: “Things” communicate also without human intervention.

Things, Sensors, Actuators

  • Coffee machines, dish washer, washing machine, …
  • Burglar alarm system
  • Intelligent farming
  • Weather station
  • Scientific measurements
  • Industrial factory control
  • Car electronics, ignition system, ABS,…
  • You name it!
The processor reads the state of “things” though sensors and controls them through actuators.

Our processor needs interfaces to these sensors and actuators.

The IoT system

We need a system with a number of processors

that read out sensors and/or control actuators

These processors communicate with each other

over the Internet with or without human intervention.

Design decisions:

  • Which processors?
  • Which communication protocol
  • Which GUI to be able to see what is going on?
  • Which programming language?

The Raspberry Pi

The Raspberry Pi is a small computer powerful enough to run a full blown Linux operating system:

A quad core 1.2 GHz Broadcom 64 bit ARM CPU

  • 1 Gbytes of Ram
  • Ethernet and wireless networks
  • 4 USB2 ports
  • Micro SD connector
  • 40 pin extended GPIO connector with
  • GPIO pins
  • SPI and I2C bus interface
  • Cost ~ 80-100 US $
  • No ADC
rpiBoard.png

Arduino + WiFi shield

arduinoSpecs.png

mega.png wifishield.png
Total cost: ~12-15 US $

The ideal IoT solution?

ESP8266 or ESP32 processor board

ESP8266 specs:

  • WiFi on chip
  • 10 bit ADC on chip
  • 80 kByte RAM
  • 4 Mbyte flash
  • Can run a Micropython interpreter
  • Can be programmed through the Arduino IDE
  • SPI, I2C interfaces
  • 11 GPIO pins
  • Many sensor and actuator boards
  • Can be plugged together without bread board or soldering
  • Cost of processor board: ~ 2.5 US$

ESP32 is even more powerful
Cost: ~ 4.5 US$
shields.png

Communication

MQTT (Message Queuing Telemetry Transport: a publish-subscribe based

Message protocol running of top of TCP.

A processor can subscribe to messages of a certain “topic” and/or it can push its

results on a certain topic

mqttPostOfffice.png

Programming language

  • Only the data recovered from the communication protocol is seen
  • Any programming language will do
  • Cayenne/MQTT libraries are available for

    • C, C++

    • Python

    • Java

    • Java Script

 

Introduction to Python

The python programming language is a high level programming language that is very interactive and object oriented.

Line: 64 to 124
  For Python beginners: https://docs.python.org/3/tutorial/index.html

and for everybody the Python docs: https://docs.python.org/3/index.html
Added:
>
>

 

Writing Python Programs

Python is a programming language that lets you work more quickly and integrate your systems more effectively.

Line: 74 to 135
 
  • is Open.

Python programs can either be written using any standard editor like nano, emacs as a script or can be written from the python command line.
Added:
>
>

 

Using the Command Line (Interactive mode)

Simply type python in your terminal and press the enter key to start the interactive python mode.

Line: 83 to 145
 commandline.png

To exit from the interactive mode, type quit().

Changed:
<
<
>
>

 

Using the script mode

Using the interactive mode doesn’t keep the statements of code we wrote permanently .

Line: 95 to 157
 helloworld.png

helloworld2.png

Changed:
<
<
>
>

 

Using Thonny

Instead of running a standard editor like nano or emacs you may want to run

Line: 109 to 171
 For basic description of Thonny check https://thonny.org/ and https://realpython.com/python-thonny/

Lets try thonny

Added:
>
>

 

Thonny Python Shell and Editor

Here you see the 2 idle windows.

Line: 116 to 179
 The top one contains the editor, the second the Python shell

thonny.png

Changed:
<
<
>
>

 

Running a python script

Script in python can be run from the Linux command shell with:

Line: 128 to 191
 any compiled C program or any bash script:

runpython.png

Changed:
<
<
>
>

 

Command Line Arguments

Programs can be called with arguments.

Line: 142 to 205
 argscript.png

runcmdlineArg.png

Changed:
<
<
>
>

 

Variables

Variables are just memory allocations for storing values.

Line: 159 to 222
  The operand to the left of the = operator is the name of the variable and
the operand to the right of the = operator is the value stored in the variable.
Changed:
<
<
variable.png
>
>
variable.png

 

Data Types in Python

Programs store data in one form or the other and must be of a certain type.

Line: 171 to 234
 
Changed:
<
<

>
>

 

Strings in Python

Strings in Python are identified as a contiguous set of characters represented in the quotation marks.

Line: 179 to 242
 Python accepts either pair of single, double quotes and triples(single/double) quotes.

string.png

Changed:
<
<
>
>

 

List in Python

The list is a most versatile datatype available in Python.

Line: 193 to 256
 The values stored in a list can be accessed using the slice operator ([ ] and [:]) with
indexes starting at 0 in the beginning of the list and working their way to end -1.

list.png

Changed:
<
<
>
>

 

Tuple in Python

Tuples are much more like a list.

Line: 205 to 268
 Thus tuples are read only.

tuples.png

Added:
>
>

 

Dictionary in Python

A dictionary is a collection which is unordered, changeable and indexed.

Line: 220 to 284
 You can loop through all the key-value pairs, all the keys, or all the values.

dict.png

Changed:
<
<
>
>

 

Basic Operators

Python supports multiple operators including

Line: 231 to 295
 
  • bitwise,
  • membership and
  • identity operators.
Added:
>
>

 

Arithmetic Operators:

The arithmetic operators supported by python is addition(+) , subtraction(-),
multiplication(* ), division(/), modulus(%), exponent(**)

operators.png

Added:
>
>

 

Python Bitwise Operators

Bitwise operator works on bits and performs bit-by-bit operation

binaryops.png

Added:
>
>

Conditional Statements

We have seen conditions already in the very first example

if.png

Loops

A loop statement allows us to execute a statement or group of statements multiple times.

Python has two primitive loop commands:

  • while loop
  • for loops
loops.png

Functions

Programs are written in blocks of codes which are reusable but performs similar actions.

Python comes with builtin functions such as print().

Users can define and use their own functions in the code.

User defined functions begin with the keyword def followed by the name of the function.

A function may take parameters

function.png

Modules

Modules in a python are file with .py extensions containing

definitions of functions or variables.

We may want to keep function definitions in separate files

and use these definitions in several main programs.

In our example of the functions we can save the definitions in a file called function.py

In order to use the definitions we must import them with

import function

In order to call the functions we have to use the module name:

function.sum(20,30)

or if we we want to import a single function:

from function import sum

sum(20,30)

The module name is found in the variable name

Modules Examples

modules.png

Executing Modules

Often modules contain only function or class (see later) definitions

However it is also possible to include the function calls (the main routine)

into the module e.g. to demonstrate how the functions are to be used.

In this case the name of the module changes to __main__

We can therefore find out if the module is imported or executed by checking the name variable:

if _name_ == "__main__"

this executes the main code only if not imported into a main program

Packages

Packages simple group of modules under one name.

Each package is a directory which MUST contain a a specific file calle

__init__.py which indicating the directory is a package.

the init.py file may be empty.

Package can be imported the same way as module.

Suppose we have several modules prime.py, fibonacci.py, factorial.py

in a directory, we may group them in a package called priff.

package.png

Classes

Almost everything in Python is an object, with its properties and methods.

A class defines the behavior of an object and the kind of information an object can store.

The information in a class is stored in attributes, and functions that belong to a class are called methods.

A child class inherits the attributes and methods from its parent class.

Creating Classes

The syntax for creation of classes is similar to functions

class.png

You may define a method with name __init__which is called when

the instance object of the class is created (in the above example: mc = !<a href="https://afnog.iotworkshop.africa/do/edit/AFNOG/MyClass?topicparent=AFNOG.IntroductionToPython;nowysiwyg=0" rel="nofollow" title="MyClass (this topic does not yet exist; you can create it)"> MyClass </a> ()

The short description of the class can be found in the instance variable _ _doc__

Classes Example

classes.png

Libraries

Libraries are a collection python packages that are found in different that gives a package hierarchy.

The only difference is python libraries or packages have a init.py file present in its directories.

A very typical example of a python library is matplotlib for plotting.

plot.png

Callbacks

callback.png

 
%SLIDESHOWEND%
Line: 274 to 473
 
META FILEATTACHMENT attachment="function.png" attr="" comment="" date="1560207036" name="function.png" path="function.png" size="164022" user="IsaacArmahMensah" version="1"
META FILEATTACHMENT attachment="modules.png" attr="" comment="" date="1560207048" name="modules.png" path="modules.png" size="87708" user="IsaacArmahMensah" version="1"
META FILEATTACHMENT attachment="package.png" attr="" comment="" date="1560207164" name="package.png" path="package.png" size="34308" user="IsaacArmahMensah" version="1"
Added:
>
>
META FILEATTACHMENT attachment="class.png" attr="" comment="" date="1560214454" name="class.png" path="class.png" size="46273" user="IsaacArmahMensah" version="1"
META FILEATTACHMENT attachment="classes.png" attr="" comment="" date="1560214464" name="classes.png" path="classes.png" size="164398" user="IsaacArmahMensah" version="1"
META FILEATTACHMENT attachment="plot.png" attr="" comment="" date="1560214845" name="plot.png" path="plot.png" size="98448" user="IsaacArmahMensah" version="1"
META FILEATTACHMENT attachment="callback.png" attr="" comment="" date="1560216243" name="callback.png" path="callback.png" size="58686" user="IsaacArmahMensah" version="1"
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback