Difference: IntroductionToPython (1 vs. 4)

Revision 42018-04-25 - UliRaich

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

Start Presentation

Slide 1: Sensors for IoT devices … or A virtual world reflecting the real world

Line: 240 to 240
 idle.png

Changed:
<
<

The same thing as a script in idle

>
>

The same thing as a script in idle

  Here you see the 2 idle windows.
Line: 289 to 289
  conditions.png
Changed:
<
<
As you can see, there are no braces around the conditional block but it is indented
# indicates an comment
In addition to the if statement above we have elif and else
>
>
As you can see, there are no braces around the conditional block but it is indented

# indicates an comment

In addition to the if statement above we have elif and else

 

while loop

Line: 300 to 304
  while.png
Changed:
<
<
This program uses multiple assignments a,b are assigned in the same line
Again you can see that the block starts with “:” and is indented.
>
>
This program uses multiple assignments a,b are assigned in the same line

Again you can see that the block starts with “:” and is indented.

 

Lists

Line: 382 to 388
  modules.png
Added:
>
>

Calling functions from a Module

This is how we call a function (method) from a module.

moduleCall.png

... and this is the result:

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

Command Line Arguments

Programs can be called with arguments. When calling an editor you my give

the name of the file to be edited as an argument.

There may also be options that are passed as arguments, e.g.

ls -ls $HOME

These arguments can be read (as strings) in Python:

commandLineArgs.png

You may then convert an argument from string to int with

maxFib=int(argv[1])

Running the module with args

idleArgs.png

This code checks if we are running in idle, in which case it asks the user for the arguments.

In case we are called from the command line the arguments are already passed into sys.argv

Files

In order to read of write a file it must first be opened:

f = open(‘filename’,’w’)

After which you can write the file. In read mode, the ‘w’ must be replaced by ‘r’

files.png

Classes

In object oriented programming languages you can define classes

encapsulating data and functions (methods in the oo jargon) that act on these data.

You can create sub-classes having most of the properties of the

base class but provide more data and/or methods or some of the methods are modified (overridden).

This is called inheritance.

Python implements these features.

While in many object oriented languages we distinguish between public and private data,

in Python all data are public.

A convention is to precede a variable name with an ‘_’ if it is not intended for use external to the class.

---+++

Creating Classes

The syntax for creation of classes is similar to functions

createClass.png



i is an instance variable

f is a method

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__

Classes and the Sensors

As classes describe real world objects they are particularly using for describing our sensors

Connection details, calibration parameters etc. can be described in the instance variables

Actions of the sensors like triggering, readout etc. are implemented in methods.

Instance initialization

There is a special method named _ _init__ which is called when the instance of a class is created.

This method allows to

  • Initialize instance variables
  • Initialize e.g. registers, addresses and the like
  • Setup I/O for sensors
and much more.

A Class Implementation realizing a simple Calculator

calculatorClass.png

A Sensor Demo

cabling.png

The cobbler PinOut

cobblerPinout.png

The Sensors

This is the idea:

The hardware includes:

  • a color sensor
  • an ultra-sonic distance sensor
  • an rgb LED (not strictly necessary)
We create a virtual world showing the ultra-sonic sensor.

When a colored paper sheet is placed in front of the senors then this sheet

with the right color will be seen in the virtual world.

The distance of the sheet to the sensors is also reflected in the virtual world.

The sensors in the virtual world

We see the distance sensor in front and the color sensor

with its LEDs switched off, in the back

The (white) paper sheet is in frontof the distance sensor

The red ball is the center of the coordinate system

scene-1.png

 %SLIDESHOWEND%

-- Uli Raich - 2018-04-24

Line: 426 to 591
 
META FILEATTACHMENT attachment="cobblerPinout.png" attr="" comment="" date="1524603041" name="cobblerPinout.png" path="cobblerPinout.png" size="288280" user="UliRaich" version="1"
META FILEATTACHMENT attachment="cabling.png" attr="" comment="" date="1524603054" name="cabling.png" path="cabling.png" size="1347268" user="UliRaich" version="1"
META FILEATTACHMENT attachment="twiki.png" attr="" comment="" date="1524641186" name="twiki.png" path="twiki.png" size="382724" user="UliRaich" version="1"
Added:
>
>
META FILEATTACHMENT attachment="commandLineArgs.png" attr="" comment="" date="1524658151" name="commandLineArgs.png" path="commandLineArgs.png" size="6504" user="UliRaich" version="1"
META FILEATTACHMENT attachment="idleArgs.png" attr="" comment="" date="1524658382" name="idleArgs.png" path="idleArgs.png" size="14588" user="UliRaich" version="1"
META FILEATTACHMENT attachment="createClass.png" attr="" comment="" date="1524658840" name="createClass.png" path="createClass.png" size="12041" user="UliRaich" version="1"
META FILEATTACHMENT attachment="calculatorClass.png" attr="" comment="" date="1524659276" name="calculatorClass.png" path="calculatorClass.png" size="43918" user="UliRaich" version="1"
META FILEATTACHMENT attachment="scene-1.png" attr="" comment="" date="1524659680" name="scene-1.png" path="scene-1.png" size="16801" user="UliRaich" version="1"

Revision 32018-04-25 - UliRaich

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

Start Presentation

Slide 1: Sensors for IoT devices … or A virtual world reflecting the real world

Line: 74 to 74
 https://www.raspberrypi.org

Added:
>
>

The TWiki server

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

The TWiki server (2)

twiki.png

 

Specs and price

The price (35 US $) of this board is a bit higher but still much less than the m6800 development board.

Line: 404 to 425
 
META FILEATTACHMENT attachment="files.png" attr="" comment="" date="1524603016" name="files.png" path="files.png" size="27780" user="UliRaich" version="1"
META FILEATTACHMENT attachment="cobblerPinout.png" attr="" comment="" date="1524603041" name="cobblerPinout.png" path="cobblerPinout.png" size="288280" user="UliRaich" version="1"
META FILEATTACHMENT attachment="cabling.png" attr="" comment="" date="1524603054" name="cabling.png" path="cabling.png" size="1347268" user="UliRaich" version="1"
Added:
>
>
META FILEATTACHMENT attachment="twiki.png" attr="" comment="" date="1524641186" name="twiki.png" path="twiki.png" size="382724" user="UliRaich" version="1"

Revision 22018-04-24 - IsaacArmahMensah

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

Start Presentation

Slide 1: Sensors for IoT devices … or A virtual world reflecting the real world

Line: 42 to 42
 

An IoT device today

Changed:
<
<
This year IO bought this micro-controller:
>
>
This year I bought this micro-controller:
  esp32price.png

Revision 12018-04-24 - UliRaich

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

Start Presentation

Slide 1: Sensors for IoT devices … or A virtual world reflecting the real world

A 2 day tutorial with hands-on exercises

held during AFNOG 2018 in Dakar, Sénégal

Uli Raich (uli.raich@gmail.com)

Slide 2: The Internet

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)

Slide 3: Microprocessors

When I started to work on microprocessors in the early 1980’s these were very simple devices

containing just a single CPU and address and data lines + a few control lines to interface

to external memory and I/O interfaces, typically : a serial and a parallel port.

m6800.png

The price for the chip was 176 US $.

The development board was sold for 300 US $.

You got 256 bytes of RAM, 2kBytes of EPROM and the serial and parallel port.

Programming was done in assembly language or even straight machine code.

No Internet!

Slide 4: An IoT device today

This year IO bought this micro-controller:

esp32price.png

Slide 5: The specs

esp32.png

Slide 6: The Raspberry Pi

… and this is what we are going to use:

RASPBERRY-PI-3_sml.jpg

Slide 7: Official source of information

The Raspberry Pi has been sold at more than 8 million pieces!

The user community is huge and is now increased by another 15 users!

An enormous wealth of information on all sorts of subjects using the Raspberry Pi

can be found on the WEB.

The official site is

https://www.raspberrypi.org

Slide 8: Specs and price

The price (35 US $) of this board is a bit higher but still much less than the m6800 development board.

And this is what you get:


broadcom.png

Slide 9: Interfaces and software

In addition to the GPIO (General Purpose I/O) you get

  • a serial interface
  • I2C
  • SPI
very useful to connect external sensors, ADCs or DACs

On the micro SD card (or a memory stick on one of the USB slots) you can install

a full-blown Linux operating system

(for the Windows gurus: there is also a Windows version)

All you need to have a stand alone computer is

  • an HDMI screen
  • a USB keyboard and mouse

Slide 10: Programming

Since we have a full Linux system programming can be done

  • in C or C++
  • Python
  • or any language that Linux supports … and there are many!

Slide 11: Accessing the RPI remotely

Of course we can use the interfaces on the Raspberry Pi to connect a screen,

keyboard and mouse and use it in stand-alone mode

but we can also make use of the PC resources and access it remotely

There are several ways to access the RPI remotely:

Using the VNC server on the Pi you can access it with a remote desktop from the PC

The secure shell (ssh) allows you to get a remote terminal in the Pi

With scp you can copy files back and forth between the Pi and the PC

With nfs you can mount part of the Pi file system into the PC file system tree

and access the PI SD card as if it was a local PC disk.

Slide 12: The remote Desktop

On the remote desktop the screen is replaced by the computer screen of the device

connected to the RPI and the keyboard by this computer's keyboard. The connected computer

acts like a graphics terminal for the RPI.

You see on the computer exactly what you would see on a directly connected screen.

remoteDesktop.png

Slide 13: ssh

In the case of ssh you have a single terminal window that is connected to a shell on the Pi.

The command is:

ssh userOnPi@piIPaddress

Where piIPaddress can be the Pi’s IP address or host name.

If you specify the -X option you can run X-11 based programs where

the X protocol is run over the ssh connection.

For Windows computers you need PuTTY in conjunction with

an X server (e.g. Xming) to connect to the RPi

Slide 14: ssh(2)

Here you see a screen dump from the PC with a remote terminal that

started an emacs session on the Pi

ssh.png

Slide 15: The Cobbler

As we have seen, the Raspberry Pi has a 40 pin connector onto which many interface signals are connected.

We use a flat cable and a “cobbler” to bring these signals onto a bread board

where we can build our own electronic circuits for acquisition or control by the Rpi.

Libraries in C/C++, Java and Python are available easing software access to these signals.

cobbler.png

Slide 16: Programming the RPi

As already mentioned, the Raspberry Pi can be programmed in

a large number of programming languages.

One of the most fashionable languages is Python.

Python is an interpreted language and code can be entered into a Python interpreter

which executes it immediately. Like this, Python can e.g. be used as a calculator.

It is however also possible to write whole programs (scripts) which the interpreter executes

Want to learn Python? Try the Python tutorial!

We will attempt to learn the language in 2 h!

Slide 17: Running the Interpreter

pythonInterpreter.png


The quit() function exits the interpreter

Slide 18: idle

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

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

Try idle

idle.png

The same thing as a script in idle

Here you see the 2 idle windows.

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

pythonScript.png

pythonShell.png

Slide 19: Running the Program

The script can berun from the Linux command shell with:

python scriptName

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

any compiled C program or any bash script:

pythonBash.png

Slide 20: Very basic Introduction to Python

Assignments look pretty much the same as assignments in other programming languages.

However, in Python you do not need to declare the variables.

The interpreter finds the right type by itself.

Here an example:

assignment.png

Slide 21: Strings in Python

Here some string examples:

strings.png

Slide 22: Conditions

We have seen conditions already in the very first example

conditions.png

As you can see, there are no braces around the conditional block but it is indented
# indicates an comment
In addition to the if statement above we have elif and else

Slide 23: while loop

Like in other programming languages we have different types of loops.

Here is the example of a while loop which calculates the Fibonnaci series:

while.png

This program uses multiple assignments a,b are assigned in the same line
Again you can see that the block starts with “:” and is indented.

Slide 24: Lists

Python knows a number of compound data structure the most versatile of which is the list

Lists may contain different data types but usually all members of lists have the same type.

Lists have similar properties as strings:

  • define sub-lists
  • get the length of lists
  • concatenate lists
lists.png

Slide 25: More data structures

Lists provide many different functions (methods) to operate on them:

Append, extend, remove, pop, index, count, sort … Have a look at the tutorial for details

The are also further complex data structures which we don’t have the time to go into:

  • tuples
  • sets
  • dictionaries

Slide 26: For loops

Lists are used in for loops. In Python the iterator can be of any type.

Using the range function you can create the same type of for loops we know from C.

Keywords like continue and break have the same meaning.

pass does nothing and is used only where a statement is syntactically needed

for.png

... and the result:

forResult.png

Slide 27: Function Definition

Like in other high level languages you can create functions

and call them later in your program

functionDef.png

Slide 28: Modules

We may want to keep function definitions in separate files

and use these definitions in several main programs.

In our example of the Fibonacci numbers we can save the definitions in a file called fibo.py

In order to use the definitions we must import them with

import fibo

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

fibo.fib(20)

or if we we want to import a single function:

from fibo import fib

fib(20)

The module name is found in the variable __name__

Slide 29: Module Example

modules.png

-- Uli Raich - 2018-04-24

Comments

<--/commentPlugin-->

META FILEATTACHMENT attachment="m6800.png" attr="" comment="" date="1524592166" name="m6800.png" path="m6800.png" size="26949" user="UliRaich" version="1"
META FILEATTACHMENT attachment="RASPBERRY-PI-3_sml.jpg" attr="" comment="" date="1524592172" name="RASPBERRY-PI-3_sml.jpg" path="RASPBERRY-PI-3_sml.jpg" size="307857" user="UliRaich" version="1"
META FILEATTACHMENT attachment="esp32price.png" attr="" comment="" date="1524592177" name="esp32price.png" path="esp32price.png" size="176083" user="UliRaich" version="1"
META FILEATTACHMENT attachment="esp32.png" attr="" comment="" date="1524592423" name="esp32.png" path="esp32.png" size="112426" user="UliRaich" version="1"
META FILEATTACHMENT attachment="broadcom.png" attr="" comment="" date="1524595104" name="broadcom.png" path="broadcom.png" size="54186" user="UliRaich" version="1"
META FILEATTACHMENT attachment="ssh.png" attr="" comment="" date="1524595647" name="ssh.png" path="ssh.png" size="300537" user="UliRaich" version="1"
META FILEATTACHMENT attachment="remoteDesktop.png" attr="" comment="" date="1524595653" name="remoteDesktop.png" path="remoteDesktop.png" size="387840" user="UliRaich" version="1"
META FILEATTACHMENT attachment="cobbler.png" attr="" comment="" date="1524597010" name="cobbler.png" path="cobbler.png" size="24360" user="UliRaich" version="1"
META FILEATTACHMENT attachment="babylon.py.txt" attr="" comment="" date="1524597329" name="babylon.py.txt" path="babylon.py.txt" size="326" user="UliRaich" version="1"
META FILEATTACHMENT attachment="conditions.py.txt" attr="" comment="" date="1524597329" name="conditions.py.txt" path="conditions.py.txt" size="298" user="UliRaich" version="1"
META FILEATTACHMENT attachment="fib1.py.txt" attr="" comment="" date="1524597329" name="fib1.py.txt" path="fib1.py.txt" size="366" user="UliRaich" version="1"
META FILEATTACHMENT attachment="forLoop.py.txt" attr="" comment="" date="1524597329" name="forLoop.py.txt" path="forLoop.py.txt" size="226" user="UliRaich" version="1"
META FILEATTACHMENT attachment="simpleForLoop.py.txt" attr="" comment="" date="1524597329" name="simpleForLoop.py.txt" path="simpleForLoop.py.txt" size="167" user="UliRaich" version="1"
META FILEATTACHMENT attachment="strings.py.txt" attr="" comment="" date="1524597329" name="strings.py.txt" path="strings.py.txt" size="999" user="UliRaich" version="1"
META FILEATTACHMENT attachment="while.py.txt" attr="" comment="" date="1524597329" name="while.py.txt" path="while.py.txt" size="120" user="UliRaich" version="1"
META FILEATTACHMENT attachment="pythonInterpreter.png" attr="" comment="" date="1524598269" name="pythonInterpreter.png" path="pythonInterpreter.png" size="39781" user="UliRaich" version="1"
META FILEATTACHMENT attachment="pythonScript.png" attr="" comment="" date="1524598270" name="pythonScript.png" path="pythonScript.png" size="36109" user="UliRaich" version="1"
META FILEATTACHMENT attachment="pythonShell.png" attr="" comment="" date="1524598271" name="pythonShell.png" path="pythonShell.png" size="33178" user="UliRaich" version="1"
META FILEATTACHMENT attachment="idle.png" attr="" comment="" date="1524598277" name="idle.png" path="idle.png" size="325413" user="UliRaich" version="1"
META FILEATTACHMENT attachment="assignment.png" attr="" comment="" date="1524598933" name="assignment.png" path="assignment.png" size="16232" user="UliRaich" version="1"
META FILEATTACHMENT attachment="pythonBash.png" attr="" comment="" date="1524598935" name="pythonBash.png" path="pythonBash.png" size="111336" user="UliRaich" version="1"
META FILEATTACHMENT attachment="strings.png" attr="" comment="" date="1524599218" name="strings.png" path="strings.png" size="79489" user="UliRaich" version="1"
META FILEATTACHMENT attachment="conditions.png" attr="" comment="" date="1524599268" name="conditions.png" path="conditions.png" size="19777" user="UliRaich" version="1"
META FILEATTACHMENT attachment="while.png" attr="" comment="" date="1524599664" name="while.png" path="while.png" size="7759" user="UliRaich" version="1"
META FILEATTACHMENT attachment="lists.png" attr="" comment="" date="1524599664" name="lists.png" path="lists.png" size="35830" user="UliRaich" version="1"
META FILEATTACHMENT attachment="for.png" attr="" comment="" date="1524600366" name="for.png" path="for.png" size="12474" user="UliRaich" version="1"
META FILEATTACHMENT attachment="forResult.png" attr="" comment="" date="1524600366" name="forResult.png" path="forResult.png" size="3494" user="UliRaich" version="1"
META FILEATTACHMENT attachment="functionDef.png" attr="" comment="" date="1524600586" name="functionDef.png" path="functionDef.png" size="25005" user="UliRaich" version="1"
META FILEATTACHMENT attachment="modules.png" attr="" comment="" date="1524600868" name="modules.png" path="modules.png" size="31530" user="UliRaich" version="1"
META FILEATTACHMENT attachment="moduleRun.png" attr="" comment="" date="1524603015" name="moduleRun.png" path="moduleRun.png" size="14588" user="UliRaich" version="1"
META FILEATTACHMENT attachment="moduleCallResult.png" attr="" comment="" date="1524603016" name="moduleCallResult.png" path="moduleCallResult.png" size="17710" user="UliRaich" version="1"
META FILEATTACHMENT attachment="moduleCall.png" attr="" comment="" date="1524603016" name="moduleCall.png" path="moduleCall.png" size="13343" user="UliRaich" version="1"
META FILEATTACHMENT attachment="files.png" attr="" comment="" date="1524603016" name="files.png" path="files.png" size="27780" user="UliRaich" version="1"
META FILEATTACHMENT attachment="cobblerPinout.png" attr="" comment="" date="1524603041" name="cobblerPinout.png" path="cobblerPinout.png" size="288280" user="UliRaich" version="1"
META FILEATTACHMENT attachment="cabling.png" attr="" comment="" date="1524603054" name="cabling.png" path="cabling.png" size="1347268" user="UliRaich" 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