%=============================================================================== % Zweck: KTR-Präsentation-Vorlage % Erstellt: 15.04.2013 % Update: 04.07.2016 % Autor: M.G. %=============================================================================== \newcommand\ratio{169} \documentclass[10pt,aspectratio=\ratio, %draft, %handout, compress ]{beamer} \newcommand\meta{./meta} \input{\meta/config/commands} \def\signed #1{{\leavevmode\unskip\nobreak\hfil\penalty50\hskip2em \hbox{}\nobreak\hfil(#1)% \parfillskip=0pt \finalhyphendemerits=0 \endgraf}} \newsavebox\mybox \newenvironment{aquote}[1] {\savebox\mybox{#1}\begin{fancyquotes}} {\signed{\usebox\mybox}\end{fancyquotes}} \input{\meta/config/hyphenation} \setbeamertemplate{caption}[numbered] %\numberwithin{figure}{section} \begin{document} %=============================================================================== % Zum Kompilieren latexmk ausführen. % Konfiguration in texmaker: Options -> Configure Texmaker -> Quick Build -> Select Latexmk + ViewPD % Entsprechende Informationen in den config/metainfo verändern % Zur Auswahl der Sprache im folgenden Befehl % ngerman für deutsch eintragen, english für Englisch. %=============================================================================== \selectlanguage{ngerman} \ifnum\ratio<169 \frame{\titlepage} \else \frame[plain]{\titlepage} \fi \AtBeginSection[] { \frame { \frametitle{Outline} \tableofcontents[currentsection,hideallsubsections] } } %\AtBeginSubsection[] %{ % \frame % { % \frametitle{Outline} % \tableofcontents[sectionstyle=show/hide,subsectionstyle=show/shaded/hide,subsubsectionstyle=hide] % } %} % %\AtBeginSubsubsection[] %{ % \frame % { % \frametitle{Outline} % \tableofcontents[sectionstyle=show/hide,subsectionstyle=show/shaded/hide,subsubsectionstyle=show/shaded/hide] % } %} \newcommand<>{\highlighton}[1]{% \alt#2{\structure{#1}}{{#1}} } \newcommand{\icon}[1]{\pgfimage[height=1em]{#1}} \begin{frame}{Content} \tableofcontents \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% Content starts here %%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{ESP Microcontroller} \subsection{Hardware} \begin{frame}{ESP*} ESP8266 \begin{itemize} \item 80 MHz, 64 kB RAM (Code), 96 kB RAM (Daten) \item 4 MB Flashspeicher (extern) \item Ardunio-kompatibel \item Open Source Hardware \item Wlan \end{itemize} ESP32 \begin{itemize} \item 160/240 MHz, 520 kB RAM (Daten) \item Dualcore! \item 4 MB Flashspeicher (extern) \item Ardunio-kompatibel \item Open Source Hardware \item Wlan, Bluetooth 4.2, BLE, Ethernet (Treiber) \end{itemize} \end{frame} \begin{frame}{ESP8266 - Pins} \image{.6\textwidth}{pinmap}{ESP8266 Pins \cite{pinmap}}{img:pins} \end{frame} \begin{frame}{ESP32 - Pins} \image{\textwidth}{ESP32-Development-Board-Pinout}{ESP32 Pins \cite{pinmap32}}{img:pins} \end{frame} \subsection{Software} \begin{frame}{Programmierung} \href{http://nodemcu.com/index_en.html}{NodeMCU} \begin{itemize} \item Open Source Firmware \item Lua-Skriptbare Umgebung \item Doku: \url{https://nodemcu.readthedocs.io/en/master/} \end{itemize} Cooler: \href{https://micropython.org/}{MicroPython} \begin{itemize} \item Basiert auf Python 3 \item Frei \& Open Source \item Einige Funktionalität eingeschränkt (z.B. Multithreading) \item Verscheidene Boards/Controller unterstützt: PyBoard, ESP32, \dots \item Doku: \url{https://docs.micropython.org/en/latest/esp8266/quickref.html} bzw. \url{https://docs.micropython.org/en/latest/esp32/quickref.html} \end{itemize} Lowlevel: C, Arduino Studio \end{frame} \section{Setup} \subsection{Tools} \begin{frame}{Tools} Linux \begin{itemize} \item Debian, Gentoo (testing): \texttt{\{apt install,emerge\} esptool picocom} \item Ubuntu: \texttt{pip3 install esptool \&\& apt install picocom} \item \texttt{adduser \$USER dialout} \item neuer Login (sudo su \$USER) %\item Alternative: Docker (bin/docker): \textit{docker-compose run esp esptool} \end{itemize} Windows \begin{itemize} \item \url{https://www.python.org}, z.B. 3.6.2 \begin{itemize} \item Option \textit{Add Python to PATH} aktivieren \end{itemize} \item \texttt{pip install esptool} \item \url{https://www.chiark.greenend.org.uk/~sgtatham/putty/download.html} \item CH340G Treiber: \url{https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers} \end{itemize} Mac \begin{itemize} \item Homebrew (\url{https://brew.sh}) installieren \item \texttt{brew install python3 \&\& pip install esptool} \end{itemize} \end{frame} \subsection{Firmware} \begin{frame}{Firmware ESP8266} Windows-Spezial: \begin{itemize} \item Portbezeichner: Gerätemanager $\rightarrow$ Serielle Geräte (COM+LPT) $\rightarrow$ COM$X$ (z.B. COM12) \end{itemize} Flashen: \begin{itemize} \item MicroPython Download: \url{https://micropython.org/download\#esp8266} (esp8266-20190125-v1.10.bin) \item[*nix] \texttt{esptool -{}-port /dev/ttyUSB0 erase\_flash} \item[win] \texttt{python -m esptool -{}-port COM12 erase\_flash} \item[*nix] \texttt{esptool -{}-port /dev/ttyUSB0 -{}-baud 460800 write\_flash -{}-flash\_size=detect 0 esp8266-20190125-v1.10.bin} \item[win] \texttt{python -m esptool -{}-port COM12 -{}-baud 460800 write\_flash -{}-flash\_size=detect 0 esp8266-20190125-v1.10.bin} \item reset \end{itemize} \end{frame} \begin{frame}{Firmware ESP32} Windows-Spezial: \begin{itemize} \item Portbezeichner: Gerätemanager $\rightarrow$ Serielle Geräte (COM+LPT) $\rightarrow$ COM$X$ (z.B. COM12) \end{itemize} Flashen: \begin{itemize} \item MicroPython Download: \url{https://micropython.org/download\#esp32} (esp32-20190128-v1.10-15-gdeb67569f.bin) \item[*nix] \texttt{esptool -{}-chip esp32 -{}-port /dev/ttyUSB0 -{}-baud 460800 write\_flash -z 0x1000 esp32-20190128-v1.10-15-gdeb67569f.bin} \item[win] \texttt{python -m esptool -{}-chip esp32 -{}-port COM12 -{}-baud 460800 write\_flash -z 0x1000 esp32-20190128-v1.10-15-gdeb67569f.bin} \item Während dessen wild auf Boot drücken... \item reset \item Troubleshooting: Evtl. \texttt{esptool.py} verwenden \end{itemize} \end{frame} \subsection{REPL} \begin{frame}{Serielle Konsole} *nixe: \begin{itemize} \item \texttt{picocom -b 115200 /dev/ttyUSB0} \item $n$ beherzte \textit{}-Drücke %\item Docker: \texttt{docker-compose run esp} (evtl. TTY anpassen) \item[exit] Ctrl-a Ctrl-x \item Alternativ: screen, \dots \end{itemize} Windows: \begin{itemize} \item PuTTY \item Serielle Verbindung \item 115200 Baud \item $n$ beherzte \textit{}-Drücke \end{itemize} \end{frame} \lstset{language=Python,captionpos=b} \begin{frame}{REPL} \framesubtitle{Hello World!} \image{.7\textwidth}{repl}{REPL}{img:repl} \end{frame} \subsection{WLAN} \begin{frame}[fragile]{WLAN}\framesubtitle{Put the I in IoT} \begin{lstlisting}[caption={Wifi Setup},label=DescriptiveLabel] import network sta_if = network.WLAN(network.STA_IF) ap_if = network.WLAN(network.AP_IF) ap_if.active(False) sta_if.active(True) sta_if.connect('wiaiot', 'wiaiotoiaiw') sta_if.isconnected() sta_if.ifconfig() \end{lstlisting} \end{frame} \begin{frame}[fragile]{WebREPL} \begin{lstlisting}[caption={Setup für WebREPL},label=webrepl] import webrepl_setup \end{lstlisting} \begin{itemize} \item \textit{import webrepl\_setup} \item WebREPL einrichten (\textit{import webrepl \& webrepl.start()}) \item WebREPL Client \url{http://micropython.org/webrepl/} \item IP des ESP's für WebSocket verwenden (\textit{sta\_if.ifconfig()}) \item Datei Up-/Download (z.B. für main.py) \end{itemize} \end{frame} \subsection{Init} \begin{frame}[fragile]{main.py} Wird beim starten \dots gestartet \begin{lstlisting}[caption={main.py},label=mainpy] from secrets import SSID, PASS def connect_wifi(ssid=SSID, passphrase=PASS): #TODO ;) pass def setup(): #TODO configure sensors, ... pass connect_wifi() setup() \end{lstlisting} \end{frame} \subsection{Exkurs: Grafana} \begin{frame}[fragile]{Gegenseite: InfluxDB + Grafana} \begin{lstlisting}[caption={Collection Server (docker-compose)},label=cnc] version: "3" services: influxdb: image: influxdb:1.7-alpine grafana: image: grafana/grafana:5.4.3 networks: - traefik_net - default labels: - "traefik.enable=true" - "traefik.docker.network=traefik_net" - "traefik.http.frontend.rule=Host:iot.uni.clkl.de" \end{lstlisting} \end{frame} \begin{frame}[fragile]{Daten übertragen} \begin{lstlisting}[caption={HTTP Requests},label=http] import urequests host = "http://iot-db.uni.clkl.de" #response = urequests.get(host) import machine board_id = int.from_bytes(machine.unique_id(), 'little') data = "machine_id,user=,other_data=foobar value=" + str(board_id) post_url = "{host}/write?db={db}".format(host=host, db="ids") urequests.post(post_url, data=data) \end{lstlisting} \end{frame} \section{Basteln} \subsection{Blinkende LED} \begin{frame}{LED anschließen} \image{.4\textwidth}{node-led}{LED \cite{node-led}}{img:led} \end{frame} \begin{frame}[fragile]{LEDs} \setbeamercovered{invisible} \lstset{tabsize=6} \begin{lstlisting}[caption={LED},label=led,escapeinside={(*@}{@*)}] import machine, time, math pin = machine.Pin(0, machine.Pin.OUT) pin.on() pin.off() (*@\onslide<2->@*) pwm = machine.PWM(pin) pwm.duty(512) pwm.deinit() (*@\onslide<3->@*) def pulse(l, t): for i in range(20): l.duty(int(math.sin(i / 10 * math.pi) * 500 + 500)) time.sleep_ms(t) led = machine.PWM(machine.Pin(2), freq=1000) pulse(led, 50) for i in range(10): pulse(led, 20) \end{lstlisting} \end{frame} \subsection{Schalter und Taster} \begin{frame}[fragile]{Input} \image{.35\textwidth}{button-nodemcu}{Taster \cite{node-button}}{img:button} \begin{lstlisting}[caption={Input},label=input] import machine pin = machine.Pin(0, machine.Pin.IN, machine.Pin.PULL_UP) # status = pin.value() def callback(p): print('changed', p) pin.irq(trigger=machine.Pin.IRQ_RISING | machine.Pin.IRQ_FALLING, handler=callback) \end{lstlisting} \end{frame} \subsection{Sensoren} \begin{frame}[fragile]{DHT22} \image{.35\textwidth}{nodemcu-dht22}{DHT22 \cite{node-dht}}{img:dht} \begin{lstlisting}[caption={Sensoren},label=senors] import dht import machine d = dht.DHT22(machine.Pin(4)) d.measure() d.temperature() d.humidity() \end{lstlisting} \end{frame} \begin{frame}[fragile]{BME280} \image{.25\textwidth}{esp-bme}{BME 280 \cite{espbme}}{img:bme} Modul auf ESP hochladen: bem280.py \cite{bmemod} \begin{lstlisting}[caption={Sensoren},label=senors] import bme280 import machine i2c=machine.I2C(scl=machine.Pin(26),sda=machine.Pin(25)) bme=bme280.BME280(i2c=i2c) bme.values \end{lstlisting} \end{frame} \subsection{Interrupts} \begin{frame}{Interrupts} \framesubtitle{Here be dragons} \begin{itemize} \item Z.B. Callbacks von Tastern \item pausieren main \item dürfen keinen Speicher allozieren \begin{itemize} \item[\checkmark] \textit{alarm = True \#only with previously allocated variable!} \item[\checkmark] \textit{counter = counter + 1} \item[\checkmark] \textit{interrupts[2] += 1} \item[X] \textit{events.append(Interrupt())} \item[X] \textit{} \end{itemize} \item Emergency exception buffer: \textit{micropython.alloc\_emergency\_exception\_buf(100)} \end{itemize} Siehe Doku :) \cite{mp-interrupt} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% References %%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section*{} \begin{frame}[allowframebreaks]{References} \def\newblock{\hskip .11em plus .33em minus .07em} \scriptsize \bibliographystyle{IEEEtran} \bibliography{content/bib.bib} \normalsize \end{frame} \section*{Ausblick} \begin{frame}{Was geht noch?} \begin{itemize} \item \url{https://learn.adafruit.com/micropython-hardware-i2c-devices/overview} \item \url{http://luftdaten.info/} \item \url{https://micropython-on-esp8266-workshop.readthedocs.io/en/latest/index.html} \item \url{http://micropython-iot-hackathon.readthedocs.io/en/latest/index.html} \item MQTT, \href{http://nsq.io/}{nsq.io} \item Dateisystem \end{itemize} \end{frame} \end{document}