Difference: BasicPythonProgramming (1 vs. 4)

Revision 42023-11-27 - UliRaich

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

Start Presentation

Changed:
<
<

Lecture 1: Basic Python Programming

CSC 321: Embedded Sysytem

First Semeter 2020/2021

>
>

Lecture 1: Basic Python Programming

CSC 321: Embedded Sysytem

First Semester 2020/2021

 

Introduction of Lecturers

  1. Uli Raich -- Formally CERN, Geneva, Switzerland
  2. Isaac Armah-Mensah -- University of Cape Coast Ghana
Line: 72 to 72
 Design decisions:

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

The Raspberry Pi

Changed:
<
<
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
raspberry_pi.png
>
>
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
raspberry-pi-3-b.jpg
 

Arduino + WiFi shield

Changed:
<
<
arduino.png
>
>
uno.png
 
Total cost: ~12-15 US $

The ideal IoT solution?

Changed:
<
<
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$
sheild.png
>
>
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$
wemosShields.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

Changed:
<
<
mqtt.png
>
>
mosquittoDemo.png
 

Programming language

Line: 139 to 139
  Type print(“Hello world”). Hit enter and it prints Hello world on the screen.
Changed:
<
<
commandline.png
>
>
commandline.png
  To exit from the interactive mode, type quit().

Line: 153 to 153
  helloworld.png
Deleted:
<
<
script.png
 

Using Thonny

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

Line: 218 to 217
  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

Line: 237 to 236
  Python accepts either pair of single, double quotes and triples(single/double) quotes.
Changed:
<
<
string.png
>
>
string.png
 

List in Python

Line: 476 to 475
 
<--/commentPlugin-->
Changed:
<
<
META FILEATTACHMENT attachment="Home_page.png" attr="" comment="" date="1612709232" name="Home_page.png" path="Home page.png" size="2045509" user="IsaacArmahMensah" version="1"
META FILEATTACHMENT attachment="commandline.png" attr="" comment="" date="1612710005" name="commandline.png" path="commandline.png" size="349473" user="IsaacArmahMensah" version="1"
>
>
META FILEATTACHMENT attachment="Home_page.png" attr="" comment="" date="1701078185" name="Home_page.png" path="Home_page.png" size="547486" user="UliRaich" version="1"
META FILEATTACHMENT attachment="commandline.png" attr="" comment="" date="1701079377" name="commandline.png" path="commandline.png" size="24513" user="UliRaich" version="1"
 
META FILEATTACHMENT attachment="script.png" attr="" comment="" date="1612710405" name="script.png" path="script.png" size="143266" user="IsaacArmahMensah" version="1"
META FILEATTACHMENT attachment="raspberry-pi.webp" attr="" comment="" date="1612725902" name="raspberry-pi.webp" path="raspberry-pi.webp" size="597460" user="IsaacArmahMensah" version="1"
META FILEATTACHMENT attachment="raspberry_pi.png" attr="" comment="" date="1612726040" name="raspberry_pi.png" path="raspberry_pi.png" size="935969" user="IsaacArmahMensah" version="1"
Line: 485 to 484
 
META FILEATTACHMENT attachment="sheild.png" attr="" comment="" date="1612726453" name="sheild.png" path="sheild.png" size="760798" user="IsaacArmahMensah" version="1"
META FILEATTACHMENT attachment="mqtt.png" attr="" comment="" date="1612726563" name="mqtt.png" path="mqtt.png" size="54762" user="IsaacArmahMensah" version="1"
META FILEATTACHMENT attachment="while.png" attr="" comment="" date="1612729561" name="while.png" path="while.png" size="149841" user="IsaacArmahMensah" version="1"
Changed:
<
<
META FILEATTACHMENT attachment="variable.png" attr="" comment="" date="1612729570" name="variable.png" path="variable.png" size="244797" user="IsaacArmahMensah" version="1"
>
>
META FILEATTACHMENT attachment="variable.png" attr="" comment="" date="1701080372" name="variable.png" path="variable.png" size="34345" user="UliRaich" version="1"
 
META FILEATTACHMENT attachment="list.png" attr="" comment="" date="1612729579" name="list.png" path="list.png" size="211812" user="IsaacArmahMensah" version="1"
Changed:
<
<
META FILEATTACHMENT attachment="string.png" attr="" comment="" date="1612729692" name="string.png" path="string.png" size="284981" user="IsaacArmahMensah" version="1"
>
>
META FILEATTACHMENT attachment="string.png" attr="" comment="" date="1701080034" name="string.png" path="string.png" size="31906" user="UliRaich" version="1"
 
META FILEATTACHMENT attachment="dictionary.png" attr="" comment="" date="1612729836" name="dictionary.png" path="dictionary.png" size="222984" user="IsaacArmahMensah" version="1"
Added:
>
>
META FILEATTACHMENT attachment="raspberry-pi-3-b.jpg" attr="" comment="" date="1701078319" name="raspberry-pi-3-b.jpg" path="raspberry-pi-3-b.jpg" size="80255" user="UliRaich" version="1"
META FILEATTACHMENT attachment="wemosShields.png" attr="" comment="" date="1701078905" name="wemosShields.png" path="wemosShields.png" size="397621" user="UliRaich" version="1"
META FILEATTACHMENT attachment="uno.png" attr="" comment="" date="1701078909" name="uno.png" path="uno.png" size="260706" user="UliRaich" version="1"
META FILEATTACHMENT attachment="mosquittoDemo.png" attr="" comment="" date="1701080095" name="mosquittoDemo.png" path="mosquittoDemo.png" size="534522" user="UliRaich" version="1"

Revision 32021-02-18 - TWikiAdminUser

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

Start Presentation

Lecture 1: Basic Python Programming

CSC 321: Embedded Sysytem

First Semeter 2020/2021

Line: 18 to 18
  This is our Twiki server:
Changed:
<
<
https://afnog.iotworkshop.africa/do/view/UCC_Course_2020/WebHome
>
>
https://afnog.iotworkshop.africa/do/view/UCC_Course_2021/WebHome
  Other pages for class resources

Revision 22021-02-07 - IsaacArmahMensah

Line: 1 to 1
Added:
>
>
META TOPICPARENT name="Slides"

Start Presentation

Lecture 1: Basic Python Programming

CSC 321: Embedded Sysytem

First Semeter 2020/2021

Slide 1: Introduction of Lecturers

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

Slide 2: The TWiki server

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

TWiki uses the same documentation format as Wikipedia.

This makes it very simple for the lecturer to provide on-line documentation,

which can be extended by students.

This is our Twiki server:

https://afnog.iotworkshop.africa/do/view/UCC_Course_2020/WebHome

Other pages for class resources

https://github.com/uraich/IoT-Course

Course Page

Home_page.png

What is the “I” in 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
raspberry_pi.png

Arduino + WiFi shield

arduino.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$
sheild.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

mqtt.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.

Python is an interpreted language which means that the statements which make up the python program is processed at run-time but not compiled first.

Python also supports object oriented programming style which employs the use of encapsulating codes within objects.

What can python be used for?

  • Used to create web applications.

  • Used to connect to database systems.

  • It can be used to perform complex mathematics

  • It can be used in data analysis.

If you want to learn Python, Try the Python tutorial!

For Python beginners: https://docs.python.org/3/tutorial/index.html

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

Writing Python Programs

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

  • Python is powerful... and fast;

  • plays well with others;

  • runs everywhere;

  • is friendly & easy to learn;

  • 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.

Using the Command Line (Interactive mode)

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

Type print(“Hello world”). Hit enter and it prints Hello world on the screen.

commandline.png

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

Using the script mode

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

But in an ideal situation we might want to keep the codes for future reference.

Hence we make use of an editor like the nano, emacs, etc.

helloworld.png

script.png

Using Thonny

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

an Integrated Development Environment (IDE) created specifically for Python instead.

Thonny is Easy to get started. Thonny comes with Python 3.7 built in,
so just one simple installer is needed and you're ready to learn programming.

It is very suitable for beginners and programming micro-controllers

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

Lets try thonny

Thonny Python Shell and Editor

Here you see the 2 thonny windows.

The top one contains the editor, the second the Python shell

thonny.png

Running a python script

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

python3 scriptName

or we can make it executable and run it like we would run

any compiled C program or any bash script:

runpython.png

Command Line Arguments

Programs can be called with arguments.

Python has a module that helps users parse command-line options and arguments.

Many programs can be run to provide basic information about how they
should be run and it employs the use of command line arguments.

The Python sys module provides access to any command-line arguments via the sys.argv .

argscript.png

runcmdlineArg.png

Variables

Variables are just memory allocations for storing values.

Every variable created has a space in memory allocated for it.

Based on the data type of a variable, the interpreter allocates memory and
decides what can be stored in the reserved memory.

Python variables do not need explicit declaration to reserve memory space.

The declaration happens automatically when you assign a value to a variable.

The equal sign (=) is used to assign values to variables.

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.

variable.png

Data Types in Python

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

We discuss the following data types

  • Strings
  • Lists
  • Tuple
  • Dictionary
  • ByteArray

Strings in Python

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

Python accepts either pair of single, double quotes and triples(single/double) quotes.

string.png

List in Python

The list is a most versatile datatype available in Python.

It can be written as a list of comma-separated values (items) between square [] brackets.

A list need not be of the same type.

The values present in a list are indexed starting from zero.

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

Tuple in Python

Tuples are much more like a list.

They are enclosed in brackets().

Their content cannot be changed unlike the list whose content can be changed.

Thus tuples are read only.

tuples.png

Dictionary in Python

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

In Python dictionaries are written with curly brackets, and they have keys and values.

Python's dictionaries allow you to connect pieces of related information.

Each piece of information in a dictionary is stored as a key-value pair.

When you provide a key, Python returns the value associated with that key.

You can loop through all the key-value pairs, all the keys, or all the values.

dictionary.png

Basic Operators

Python supports multiple operators including

  • arithmetic,
  • comparison,
  • assignment,
  • logical,
  • bitwise,
  • membership and
  • identity operators.

Arithmetic Operators:

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

operators.png

Python Bitwise Operators

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

binaryops.png

Conditional Statements

We have seen conditions already in the very first example

if.png

elif.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
while.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 simply group modules under one name.

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

__init__.py 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

Class

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 a Class

The syntax for creating a class 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 = MyClass)

The short description of the class can be found in the instance variable __Doc.__

Class Example

classes.png

Libraries

Python library is a collection of functions and methods that allows you to

perform many actions without writing your code

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

mathplotlibCode.png

mathplotlib.png

Callbacks

Callbacks are very often used in Graphical User Interface (GUI systems)

GUI elements (widgets) are set up by the programmer and events are attached to them.

Typical examples are:

  • Selection of an intem in a list
  • Selection on a menu item
  • pushing a push button
  • checking a check box
The GUI systems runs in an endless loop and calls pre-registered functions

(the callback functions) when any of the above events happens. The event is then

acted upon and control is returned to the GUI main loop.

A Dummy GUI

Our dummy GUI allows to register a callback and if one is registered it will call it every 2 s.

dummyGUI.png

The application running the dummy GUI

The application first registers the callback and then gives control to the dummy GUI main loop.

The main loop will wait for an event (2 s elapsed) and call the callback function when

the event has occurred.

callbackReg.png


%SLIDESHOWEND%

-- Isaac Armah-Mensah - 2021-02-07

Comments


<--/commentPlugin-->
 
META FILEATTACHMENT attachment="Home_page.png" attr="" comment="" date="1612709232" name="Home_page.png" path="Home page.png" size="2045509" user="IsaacArmahMensah" version="1"
META FILEATTACHMENT attachment="commandline.png" attr="" comment="" date="1612710005" name="commandline.png" path="commandline.png" size="349473" user="IsaacArmahMensah" version="1"
META FILEATTACHMENT attachment="script.png" attr="" comment="" date="1612710405" name="script.png" path="script.png" size="143266" user="IsaacArmahMensah" version="1"
Added:
>
>
META FILEATTACHMENT attachment="raspberry-pi.webp" attr="" comment="" date="1612725902" name="raspberry-pi.webp" path="raspberry-pi.webp" size="597460" user="IsaacArmahMensah" version="1"
META FILEATTACHMENT attachment="raspberry_pi.png" attr="" comment="" date="1612726040" name="raspberry_pi.png" path="raspberry_pi.png" size="935969" user="IsaacArmahMensah" version="1"
META FILEATTACHMENT attachment="arduino.png" attr="" comment="" date="1612726316" name="arduino.png" path="arduino.png" size="195679" user="IsaacArmahMensah" version="1"
META FILEATTACHMENT attachment="sheild.png" attr="" comment="" date="1612726453" name="sheild.png" path="sheild.png" size="760798" user="IsaacArmahMensah" version="1"
META FILEATTACHMENT attachment="mqtt.png" attr="" comment="" date="1612726563" name="mqtt.png" path="mqtt.png" size="54762" user="IsaacArmahMensah" version="1"
META FILEATTACHMENT attachment="while.png" attr="" comment="" date="1612729561" name="while.png" path="while.png" size="149841" user="IsaacArmahMensah" version="1"
META FILEATTACHMENT attachment="variable.png" attr="" comment="" date="1612729570" name="variable.png" path="variable.png" size="244797" user="IsaacArmahMensah" version="1"
META FILEATTACHMENT attachment="list.png" attr="" comment="" date="1612729579" name="list.png" path="list.png" size="211812" user="IsaacArmahMensah" version="1"
META FILEATTACHMENT attachment="string.png" attr="" comment="" date="1612729692" name="string.png" path="string.png" size="284981" user="IsaacArmahMensah" version="1"
META FILEATTACHMENT attachment="dictionary.png" attr="" comment="" date="1612729836" name="dictionary.png" path="dictionary.png" size="222984" user="IsaacArmahMensah" version="1"

Revision 12021-02-07 - IsaacArmahMensah

Line: 1 to 1
Added:
>
>

META FILEATTACHMENT attachment="Home_page.png" attr="" comment="" date="1612709232" name="Home_page.png" path="Home page.png" size="2045509" user="IsaacArmahMensah" version="1"
META FILEATTACHMENT attachment="commandline.png" attr="" comment="" date="1612710005" name="commandline.png" path="commandline.png" size="349473" user="IsaacArmahMensah" version="1"
META FILEATTACHMENT attachment="script.png" attr="" comment="" date="1612710405" name="script.png" path="script.png" size="143266" 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