Course on Internet of Things
The scripts we are going to develop during this exercise session do not depend on any system issues: We do not use command line arguments not available in MicroPython nor do we use any hardware specific features which are not available on a PC. The exercises can therefore be developed and run on the ESP32 but also on any standard PC with Python3 installed.
Connect to your ESP32 with minicom or thonny. You may try the same thing running Python3 on your PC.
Using REPL:
print Hello World!
read in a text using input() and print it
Calculate
Calculate sin(30°)
If you see errors, how do you correct them? Are the results correct?
Write a script that assigns the values 5 and 3 to the variables a and b respectively
Print the results of 4 basic arithmetic operations:
a+b
a-b
a*b
a/b
Improve this program asking the user to enter 2 real numbers (floats) separated by a space. This is how it should look like:
If this happens:
when the user mistypes and enters something that is not a real number, can you capture the error and simply ask the user to repeat his input, until you get 2 correct floating point numbers?
Bonus points: Writing a parser for user input is rather easy if the numbers and operator are separated by spaces (have a look at the split method of strings). It is quite a bit more tricky if you allow entering calculations without spaces between the operands and the operator. Can you write a parser accomplishing this?
Hint: A very elegant solution to this is the use of regular expressions and Python provides a module helping you with this:
https://docs.micropython.org/en/latest/library/ure.html
A float is defined as follows:
zero or more spaces
followed be zero of more spaces
Input | ok or not ok |
---|---|
5.3+4.7 | ok |
5.3++4.7 | not ok |
5..3 + 4.7 | not ok |
.3+.7 | ok |
5.3+4b7 | not ok |
empty string | not ok |
5.3+. | not ok |
5.3+ | not ok |
-1--1 | ok |
--1--1 | not ok |
Like in exercise2, start with 2 numbers a,b with values 5 and 7. In your program check which of the 2 numbers is bigger and print the result.
Then improve the program asking the user for 2 integer numbers. Make sure he enters 2 correct numbers and capture possible errors. This is a typical output:
The Fibonacci number series is defined as:
F(0) = 0; F(1) = 1
F(n) = F(n-1) + F(n-2)
Write a script that asks the user for an angle in degrees. Calculate the sine of this angle.
Write a Python class with methods calculating mathematical number series. These series should be calculated:
Add these series to your class and try. Write a test script exercising the different methods.
Please also have a look at the Wikipedia page on number series: https://en.wikipedia.org/wiki/Series_(mathematics)
In order to make this exercise run on the ESP32 you must first upload your mathSeries.py module implementing the MathSeries class into the file system of the ESP32. Create a directory called lib on the ESP32 file system and put your module there:
ampy mkdir /lib
ampy put mathSeries.py /lib/mathSeries.py
Once this is done, go to the REPL window of thonny and
import mathSeries
You can now create an instance of the MathSeries class:
series = mathSeries.MathSeries()
When running on a PC you may want to plot the series as well. If you don't know how to plot data in Python, you will find all the information needed here:
https://matplotlib.org/3.2.1/index.html
Here is the exercise sheet in odt format:
I | Attachment | History | Action | Size | Date | Who | Comment |
---|---|---|---|---|---|---|---|
png | calcOk.png | r1 | manage | 21.7 K | 2020-05-03 - 19:52 | UliRaich | |
txt | calculatorV1.py.txt | r1 | manage | 0.4 K | 2020-05-28 - 09:34 | UliRaich | |
txt | calculatorV2.py.txt | r1 | manage | 1.3 K | 2020-05-28 - 09:34 | UliRaich | |
txt | calculatorV3.py.txt | r1 | manage | 3.2 K | 2020-05-28 - 09:34 | UliRaich | |
png | conditions.png | r1 | manage | 28.8 K | 2020-05-03 - 20:25 | UliRaich | |
png | conversionError.png | r1 | manage | 19.3 K | 2020-05-03 - 19:42 | UliRaich | |
png | e.png | r2 r1 | manage | 1.5 K | 2022-02-08 - 16:45 | UliRaich | |
png | euler.png | r1 | manage | 1.5 K | 2022-02-08 - 16:50 | UliRaich | |
odt | exercise_1.odt | r2 r1 | manage | 126.6 K | 2022-02-08 - 17:45 | UliRaich | |
png | factorial.png | r1 | manage | 2.8 K | 2020-05-04 - 12:48 | UliRaich | |
png | geometric.png | r1 | manage | 3.9 K | 2020-05-04 - 12:48 | UliRaich | |
png | harmonic.png | r1 | manage | 1.6 K | 2020-05-04 - 12:53 | UliRaich | |
png | invalidFloat.png | r1 | manage | 32.0 K | 2020-05-08 - 13:38 | UliRaich | |
png | ln2.png | r1 | manage | 2.0 K | 2020-05-04 - 12:48 | UliRaich | |
png | mathSeries.png | r3 r2 r1 | manage | 9.7 K | 2022-02-08 - 17:36 | UliRaich | |
png | pi.png | r1 | manage | 2.7 K | 2020-05-04 - 12:48 | UliRaich | |
png | wallis.png | r1 | manage | 6.1 K | 2020-05-05 - 14:31 | UliRaich | |
png | wallis1.png | r1 | manage | 14.8 K | 2020-05-05 - 14:35 | UliRaich | |
png | wallis_prod.png | r4 r3 r2 r1 | manage | 4.2 K | 2022-01-27 - 09:46 | UliRaich |