SlideShare a Scribd company logo
1 of 45
Download to read offline
IOT: INTERNET OF THINGS WITH
PYTHON Lelio Campanile
lelio.campanile@gmail.com
@lelioc
HI THERE, I’M LELIO CAMPANILE
WORK AT UNICAMPANIA.IT
TEACHER AT APPLE FOUNDATION
COURSE FOR UNICAMPANIA.
Follow me: @lelioc
www.leliocampanile.it
ICROPYTHON
WHAT IS MICROPYTHON?
Micropython is a lean and efficient implementation of
Python 3 programming language.
Python on bare metal!
Open source project on github https://github.com/
micropython/micropython
FEATURES
▸ Support for many architectures
▸ Mostly compatible with normal python syntax
▸ Fit in 256Kb of code space and 16Kb of RAM
▸ Interactive prompt for easy development (REPL)
▸ Python hardware-specific modules
WHY
MICROPYTHON??
WHY???!!!
BECAUSE WE WANT
PROGRAMMING IN PYTHON
EVERYWHERE!!!
SUPPORTED BOARDS
▸ ESP 8266 (NodeMCU - SONOFF)
▸ PyBoard
▸ ESP32
▸ WiPy
MICROPYTHON ON ESP 8266 (NODEMCU)
firmware for ESP8266 from http://micropython.org/download#esp8266
You have here 4 kind of firmware:
- stable builds for device with 1024kb and above
- daily builds for device with 1024kb and above
- daily builds for device with 512kb
- work-in-progress OTA update Firmware
DEPLOYING THE FIRMWARE
▸ put your device in boot-loader mode
▸ put the firmware on device
DEPLOYING THE FIRMWARE/2
SIMPLY WAY WITH NODEMCU:
It has an usb connector with serial converter, it just works.
A LITTLE BIT TRICKY WAY:
But in other case you could need a usb to ttl converter (5v or
3,5 v) for example in sonoff you have to put GPIO0 to
ground-> on boot
WRITING TOOL FOR FIRMWARE
Now you need a writing tool
MicroPython supports esptool (https://github.com/espressif/
esptool/), a python-based, open source and platform
independent utility to write the ROM in ESP8266 and ESP32
chips:
pip install esptool
ERASE...
esptool.py --port /dev/cu.SLAB_USBtoUART erase_flash
esptool.py v2.3.1
Connecting........_
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
Chip erase completed successfully in 8.5s
Hard resetting via RTS pin...
...AND WRITE IT!
esptool.py --port /dev/cu.SLAB_USBtoUART --baud 460800
write_flash --flash_size=detect -fm dio 0 esp8266-20171101-
v1.9.3.bin
esptool.py v2.3.1
Connecting…….._
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Configuring flash size...
Auto-detected Flash size: 4MB
Flash params set to 0x0240
Compressed 600888 bytes to 392073…
Wrote 600888 bytes (392073 compressed) at 0x00000000 in 9.1 seconds (effective
527.6 kbit/s)...
Hash of data verified.
Leaving...
Hard resetting via RTS pin..
AT LAST!
REBOOT NODE MCU AND CONNECT TO IT VIA WIFI AP SSID: MICROPYTHON-XXXX AND PASSWD: micropythoN
Connect via serial and get REPL:
Read Evaluate Print Loop -> the easiest way to
test your code!
screen /dev/cu.SLAB_USBtoUART 115200
WEBREPL
EASIEST WAY TO TEST YOUR CODE OVER WIFI, WITH A BROWSER
LET'S ENABLE IT!
import webrepl_setup
FOLLOW THE ON-SCREEN INSTRUCTION.
NOW YOU ARE READY TO CONNECT TO MICROPYTHON VIA WEB REPL PROMPT.
HTTP://WWW.MICROPYTHON.ORG/WEBREPL/
HTTPS://GITHUB.COM/MICROPYTHON/WEBREPL FOR OFF-LINE USE AND FOR USE
COMMANDLINE TOOL, BASICALY TO COPY FILE IN YOUR DEVICE
python webrepl_cli.py boot.py 192.168.4.1:/boot.py
MAC OS X TIPS
Lists all usb device connected to your mac
ioreg -p IOUSB -w0
+-o Root <class IORegistryEntry, id 0x100000100, retain 15>
+-o AppleUSBXHCI Root Hub Simulation@14000000 <class
AppleUSBRootHubDevice, id 0x1000003f8, registered, matched,
active, busy 0 (1 ms), retain 10>
+-o iBridge@14200000 <class AppleUSBDevice, id
0x1000003fa, registered, matched, active, busy 0 (50 ms),
retain 36>
+-o CP2102 USB to UART Bridge Controller@14600000 <class
AppleUSBDevice, id 0x100002114, registered, matched, active,
busy 0 (68 ms), retain 11>
when I plugin the node mcu in my mac I view this new usb
device, but it don't load the right usb-serial driver…
INSTALL USB-SERIAL DRIVER
install CP210x USB to UART Bridge VCP Drivers for
nodemcu serial adapter built-in from:
https://www.silabs.com/products/development-tools/
software/usb-to-uart-bridge-vcp-drivers
after a reboot of your mac you could able to view /dev/
cu.SLAB_USBtoUART
LAST TIP:
make sure to try out different USB cables as well!
FIRST STEPS WITH
MICROPYTHON
SETTING NETWORK INTERFACE
>>> import network 

>>> client = network.WLAN(network.STA_IF) 

>>> ap = network.WLAN(network.AP_IF) 

>>> client.active() 

False 

>>> ap.active() 

True 

>>> client.active(True) 

#5 ets_task(4020ed88, 28, 3fffabe8, 10) 

>>> client.connect('Daddelio Network', '********') 

>>> client.ifconfig() 

('192.168.1.87', '255.255.255.0', '192.168.1.1', '192.168.1.1') 



INTERNAL FILESYSTEM
# internal filesystem
>>> import os
>>> os.listdir()
['boot.py', 'webrepl_cfg.py']
>>>
the boot.py is executed when device boot up. Insert in
boot.py the code that you want to execute when the board
start up.
After boot.py your board runs main.py.
MEET THE EXTERNAL WORLD: GPIO BASICS
# GPIO basics
>>> import machine
>>> led = machine.Pin(2, machine.Pin.OUT)
>>> led.value()
>>> 0
>>> led.on()
>>> led.off()
BLINK BUILT IN LED
# blinking led
>>> import time
>>> for i in range(10):
... led.on()
... time.sleep(0.5)
... led.off()
... time.sleep(0.5)
...
BREATHING LED
# breathing led
>>> import machine
>>> import time
>>> pwm_led = machine.PWM(machine.Pin(2))
>>> pwm_led.duty(1023)
>>> for i in range(3):
... for up in range(1023, -1, -50):
... pwm_led.duty(up)
... time.sleep(0.05)
... time.sleep(0.5)
... for down in range(0, 1023, 50):
... pwm_led.duty(down)
... time.sleep(0.05)
... time.sleep(0.5)
...
GPIO IN: BUTTON TURN ON LED
# button turn on led
>>> button = machine.Pin(14, machine.Pin.IN,
machine.Pin.PULL_UP)
>>> while True:
... if not button.value():
... led.value(not led.value())
... time.sleep(0.4)
... while not button.value():
... pass
TEMPERATURE AND HUMIDITY
z
# DHT 11 temperature
>>> import machine
>>> import dht
>>> dht_sensor = dht.DHT11(machine.Pin(2))
>>> dht_sensor.measure()
>>> dht_sensor.temperature()
>>> 22
>>> dht_sensor.humidity()
>>> 64
HOME AUTOMATION: HOME
ASSISTANT
INSTALL IT !
You need python3, then:
mkdir homeassistant
cd homeassistant
python3 -m venv .
source bin/activate
pip3 install wheel
pip3 install homeassistant
RUN HOME ASSISTANT
hass
and voilà http://127.0.0.1:8123
CONFIGURE HOME ASSISTANT
It is simple!!
you can configure mostly write one file:
~/.homeassistant/configuration.yaml
And now home assistant tries to discover supported
components on startup with no configuration!
CONFIGURE NETATMO
netatmo:
api_key: YOUR_CLIENT_ID
secret_key: YOUR_CLIENT_SECRET
username: YOUR_USERNAME
password: YOUR_PASSWORD
MQTT: MQ TELEMETRY TRANSPORT
IT IS A SIMPLE LIGHTWEIGHT MESSAGING
PROTOCOL, IT WORKS WITH LOW-BANDWITDH AND
LOW RESOURCE ENVIROMENTS.
IT IS PERFECT FOR IOT!
MQTT BASIC CONCEPTS
▸ Publish - Subscribe
▸ Messages
▸ Topics
▸ Broker
PUBLISH-SUBSCRIBE
A device can publish messages on a topic
A device can be subscibed on a topic and receive the
message
TOPICS AND MESSSAGES
Topics are a kind of channels
Topics are strings separate by slash "/"
For example :
home/living/lamp
for example you could send (publish) a message on topic
"home/living/lamp" to turn on the lamp in your living room.
The lamp receives this message because subscriber of that
topic.
A message is the information sent, it is a simple command or
data
BROKER
Broker is responsible to receive all messages, filtering and
route their to subscribed clients
Home assistant has an embedded MQTT Broker: hbmqtt, a
python mqtt broker
It exists many other MQTT Broker, for example:
Mosquitto broker - http://mosquitto.org)
WHAT IS MY
GOAL?
NODEMCU WITH DHT11 THAT COMMUNICATE TO
HOMEASSISTANT VIA MQTT PROTOCOL:
FULL STACK PYTHON FOR IOT
MICROPYTHON SIDE
import machine
import time
import dht
from umqtt.simple import MQTTClient
broker = "192.168.1.65"
topic = "home"
client_id = "nodemcu_" + machine.unique_id()
def main():
client = MQTTClient(client_it, broker)
client.connect()
while True:
dht_sensor.measure()
temp_data = dht_sensor.temperature()
client.publish(topic+'/'+client_id, bytes(str(temp_data),
'utf-8'))
time.sleep(60)
if __name__ == '__main__':
main()
HOME ASSISTANT SIDE
In configuration.yaml:
sensor:
- platform: mqtt
state_topic: "home/nodemcu_****unique_id****"
name: "nodemcu_temp"
THANK YOU!

More Related Content

What's hot

Espresso Lite v2 - ESP8266 Overview
Espresso Lite v2 - ESP8266 OverviewEspresso Lite v2 - ESP8266 Overview
Espresso Lite v2 - ESP8266 OverviewThe World Bank
 
Esp8266 NodeMCU
Esp8266 NodeMCUEsp8266 NodeMCU
Esp8266 NodeMCUroadster43
 
Nodemcu - introduction
Nodemcu - introductionNodemcu - introduction
Nodemcu - introductionMichal Sedlak
 
NodeMCU with Blynk and Firebase
NodeMCU with Blynk and FirebaseNodeMCU with Blynk and Firebase
NodeMCU with Blynk and FirebaseEueung Mulyana
 
Introduction to ESP32 Programming [Road to RIoT 2017]
Introduction to ESP32 Programming [Road to RIoT 2017]Introduction to ESP32 Programming [Road to RIoT 2017]
Introduction to ESP32 Programming [Road to RIoT 2017]Alwin Arrasyid
 
Raspberry Pi 3 + UART/Bluetooth issues
Raspberry Pi 3 + UART/Bluetooth issuesRaspberry Pi 3 + UART/Bluetooth issues
Raspberry Pi 3 + UART/Bluetooth issuesyeokm1
 
Rapidly developing IoT (Internet of Things) applications - Part 2: Arduino, B...
Rapidly developing IoT (Internet of Things) applications - Part 2: Arduino, B...Rapidly developing IoT (Internet of Things) applications - Part 2: Arduino, B...
Rapidly developing IoT (Internet of Things) applications - Part 2: Arduino, B...Raul Chong
 
Internet of Things
Internet of ThingsInternet of Things
Internet of ThingsAndy Gelme
 
Esp8266 hack sonoma county 4/8/2015
Esp8266 hack sonoma county 4/8/2015Esp8266 hack sonoma county 4/8/2015
Esp8266 hack sonoma county 4/8/2015mycal1
 
How to Install ESP8266 WiFi Web Server using Arduino IDE
How to Install ESP8266 WiFi Web Server using Arduino IDEHow to Install ESP8266 WiFi Web Server using Arduino IDE
How to Install ESP8266 WiFi Web Server using Arduino IDENaoto MATSUMOTO
 
Getting Started with Embedded Python: MicroPython and CircuitPython
Getting Started with Embedded Python: MicroPython and CircuitPythonGetting Started with Embedded Python: MicroPython and CircuitPython
Getting Started with Embedded Python: MicroPython and CircuitPythonAyan Pahwa
 
lesson2 - Nodemcu course - NodeMCU dev Board
 lesson2 - Nodemcu course - NodeMCU dev Board lesson2 - Nodemcu course - NodeMCU dev Board
lesson2 - Nodemcu course - NodeMCU dev BoardElaf A.Saeed
 
Micropython on MicroControllers
Micropython on MicroControllersMicropython on MicroControllers
Micropython on MicroControllersAkshai M
 

What's hot (20)

Espresso Lite v2 - ESP8266 Overview
Espresso Lite v2 - ESP8266 OverviewEspresso Lite v2 - ESP8266 Overview
Espresso Lite v2 - ESP8266 Overview
 
Esp8266 NodeMCU
Esp8266 NodeMCUEsp8266 NodeMCU
Esp8266 NodeMCU
 
Polstra 44con2012
Polstra 44con2012Polstra 44con2012
Polstra 44con2012
 
Nodemcu - introduction
Nodemcu - introductionNodemcu - introduction
Nodemcu - introduction
 
NodeMCU with Blynk and Firebase
NodeMCU with Blynk and FirebaseNodeMCU with Blynk and Firebase
NodeMCU with Blynk and Firebase
 
Introduction to ESP32 Programming [Road to RIoT 2017]
Introduction to ESP32 Programming [Road to RIoT 2017]Introduction to ESP32 Programming [Road to RIoT 2017]
Introduction to ESP32 Programming [Road to RIoT 2017]
 
Raspberry Pi 3 + UART/Bluetooth issues
Raspberry Pi 3 + UART/Bluetooth issuesRaspberry Pi 3 + UART/Bluetooth issues
Raspberry Pi 3 + UART/Bluetooth issues
 
Rapidly developing IoT (Internet of Things) applications - Part 2: Arduino, B...
Rapidly developing IoT (Internet of Things) applications - Part 2: Arduino, B...Rapidly developing IoT (Internet of Things) applications - Part 2: Arduino, B...
Rapidly developing IoT (Internet of Things) applications - Part 2: Arduino, B...
 
Internet of Things
Internet of ThingsInternet of Things
Internet of Things
 
Remote tanklevelmonitor
Remote tanklevelmonitorRemote tanklevelmonitor
Remote tanklevelmonitor
 
Let's begin io t with $10
Let's begin io t with $10Let's begin io t with $10
Let's begin io t with $10
 
Esp8266 hack sonoma county 4/8/2015
Esp8266 hack sonoma county 4/8/2015Esp8266 hack sonoma county 4/8/2015
Esp8266 hack sonoma county 4/8/2015
 
How to Install ESP8266 WiFi Web Server using Arduino IDE
How to Install ESP8266 WiFi Web Server using Arduino IDEHow to Install ESP8266 WiFi Web Server using Arduino IDE
How to Install ESP8266 WiFi Web Server using Arduino IDE
 
Espressif Introduction
Espressif IntroductionEspressif Introduction
Espressif Introduction
 
Getting Started with Embedded Python: MicroPython and CircuitPython
Getting Started with Embedded Python: MicroPython and CircuitPythonGetting Started with Embedded Python: MicroPython and CircuitPython
Getting Started with Embedded Python: MicroPython and CircuitPython
 
lesson2 - Nodemcu course - NodeMCU dev Board
 lesson2 - Nodemcu course - NodeMCU dev Board lesson2 - Nodemcu course - NodeMCU dev Board
lesson2 - Nodemcu course - NodeMCU dev Board
 
Micropython on MicroControllers
Micropython on MicroControllersMicropython on MicroControllers
Micropython on MicroControllers
 
How to Hack Edison
How to Hack EdisonHow to Hack Edison
How to Hack Edison
 
Embedded Objective-C
Embedded Objective-CEmbedded Objective-C
Embedded Objective-C
 
NVDK-ESP32 Quick Start Guide
NVDK-ESP32 Quick Start GuideNVDK-ESP32 Quick Start Guide
NVDK-ESP32 Quick Start Guide
 

Similar to IoT: Internet of Things with Python

PyCon_India_2017_MicroPython_Ayan
PyCon_India_2017_MicroPython_AyanPyCon_India_2017_MicroPython_Ayan
PyCon_India_2017_MicroPython_AyanAyan Pahwa
 
From printed circuit boards to exploits
From printed circuit boards to exploitsFrom printed circuit boards to exploits
From printed circuit boards to exploitsvirtualabs
 
Hardwear.io 2018 BLE Security Essentials workshop
Hardwear.io 2018 BLE Security Essentials workshopHardwear.io 2018 BLE Security Essentials workshop
Hardwear.io 2018 BLE Security Essentials workshopSlawomir Jasek
 
TinyOS installation Guide And Manual
TinyOS installation Guide And ManualTinyOS installation Guide And Manual
TinyOS installation Guide And ManualAnkit Singh
 
Iot Bootcamp - abridged - part 1
Iot Bootcamp - abridged - part 1Iot Bootcamp - abridged - part 1
Iot Bootcamp - abridged - part 1Marcus Tarquinio
 
Messaging for the Internet of Awesome Things
Messaging for the Internet of Awesome ThingsMessaging for the Internet of Awesome Things
Messaging for the Internet of Awesome ThingsAndy Piper
 
Introducing MQTT
Introducing MQTTIntroducing MQTT
Introducing MQTTAndy Piper
 
How to Use GSM/3G/4G in Embedded Linux Systems
How to Use GSM/3G/4G in Embedded Linux SystemsHow to Use GSM/3G/4G in Embedded Linux Systems
How to Use GSM/3G/4G in Embedded Linux SystemsToradex
 
Blockchain Software for Hardware: The Canaan AvalonMiner Open Source Embedded...
Blockchain Software for Hardware: The Canaan AvalonMiner Open Source Embedded...Blockchain Software for Hardware: The Canaan AvalonMiner Open Source Embedded...
Blockchain Software for Hardware: The Canaan AvalonMiner Open Source Embedded...Mike Qin
 
WiFi mesh network(ESP32 mStar and mesh topology)
WiFi mesh network(ESP32 mStar and mesh topology)WiFi mesh network(ESP32 mStar and mesh topology)
WiFi mesh network(ESP32 mStar and mesh topology)Raziuddin Khazi
 
Raspberry with laptop
Raspberry with laptopRaspberry with laptop
Raspberry with laptopProf Kingstan
 
From the internet of things to the web of things course
From the internet of things to the web of things courseFrom the internet of things to the web of things course
From the internet of things to the web of things courseDominique Guinard
 
Connected hardware for Software Engineers 101
Connected hardware for Software Engineers 101Connected hardware for Software Engineers 101
Connected hardware for Software Engineers 101Pance Cavkovski
 
Rapid Prototyping with AWS IoT and Mongoose OS on ESP8266, ESP32, and CC3200 ...
Rapid Prototyping with AWS IoT and Mongoose OS on ESP8266, ESP32, and CC3200 ...Rapid Prototyping with AWS IoT and Mongoose OS on ESP8266, ESP32, and CC3200 ...
Rapid Prototyping with AWS IoT and Mongoose OS on ESP8266, ESP32, and CC3200 ...Amazon Web Services
 
DEF CON 27 - XILING GONG PETER PI - exploiting qualcom wlan and modem over th...
DEF CON 27 - XILING GONG PETER PI - exploiting qualcom wlan and modem over th...DEF CON 27 - XILING GONG PETER PI - exploiting qualcom wlan and modem over th...
DEF CON 27 - XILING GONG PETER PI - exploiting qualcom wlan and modem over th...Felipe Prado
 

Similar to IoT: Internet of Things with Python (20)

PyCon_India_2017_MicroPython_Ayan
PyCon_India_2017_MicroPython_AyanPyCon_India_2017_MicroPython_Ayan
PyCon_India_2017_MicroPython_Ayan
 
From printed circuit boards to exploits
From printed circuit boards to exploitsFrom printed circuit boards to exploits
From printed circuit boards to exploits
 
Hardwear.io 2018 BLE Security Essentials workshop
Hardwear.io 2018 BLE Security Essentials workshopHardwear.io 2018 BLE Security Essentials workshop
Hardwear.io 2018 BLE Security Essentials workshop
 
TinyOS installation Guide And Manual
TinyOS installation Guide And ManualTinyOS installation Guide And Manual
TinyOS installation Guide And Manual
 
Iot Bootcamp - abridged - part 1
Iot Bootcamp - abridged - part 1Iot Bootcamp - abridged - part 1
Iot Bootcamp - abridged - part 1
 
Messaging for the Internet of Awesome Things
Messaging for the Internet of Awesome ThingsMessaging for the Internet of Awesome Things
Messaging for the Internet of Awesome Things
 
Tos tutorial
Tos tutorialTos tutorial
Tos tutorial
 
Introducing MQTT
Introducing MQTTIntroducing MQTT
Introducing MQTT
 
How to Use GSM/3G/4G in Embedded Linux Systems
How to Use GSM/3G/4G in Embedded Linux SystemsHow to Use GSM/3G/4G in Embedded Linux Systems
How to Use GSM/3G/4G in Embedded Linux Systems
 
Blockchain Software for Hardware: The Canaan AvalonMiner Open Source Embedded...
Blockchain Software for Hardware: The Canaan AvalonMiner Open Source Embedded...Blockchain Software for Hardware: The Canaan AvalonMiner Open Source Embedded...
Blockchain Software for Hardware: The Canaan AvalonMiner Open Source Embedded...
 
Capstone_Project.ppt
Capstone_Project.pptCapstone_Project.ppt
Capstone_Project.ppt
 
WiFi mesh network(ESP32 mStar and mesh topology)
WiFi mesh network(ESP32 mStar and mesh topology)WiFi mesh network(ESP32 mStar and mesh topology)
WiFi mesh network(ESP32 mStar and mesh topology)
 
Raspberry with laptop
Raspberry with laptopRaspberry with laptop
Raspberry with laptop
 
From the internet of things to the web of things course
From the internet of things to the web of things courseFrom the internet of things to the web of things course
From the internet of things to the web of things course
 
Connected hardware for Software Engineers 101
Connected hardware for Software Engineers 101Connected hardware for Software Engineers 101
Connected hardware for Software Engineers 101
 
Human Alert Sensor
Human Alert SensorHuman Alert Sensor
Human Alert Sensor
 
Rapid Prototyping with AWS IoT and Mongoose OS on ESP8266, ESP32, and CC3200 ...
Rapid Prototyping with AWS IoT and Mongoose OS on ESP8266, ESP32, and CC3200 ...Rapid Prototyping with AWS IoT and Mongoose OS on ESP8266, ESP32, and CC3200 ...
Rapid Prototyping with AWS IoT and Mongoose OS on ESP8266, ESP32, and CC3200 ...
 
One-Man Ops
One-Man OpsOne-Man Ops
One-Man Ops
 
Human Alert Sensor Design
Human Alert Sensor DesignHuman Alert Sensor Design
Human Alert Sensor Design
 
DEF CON 27 - XILING GONG PETER PI - exploiting qualcom wlan and modem over th...
DEF CON 27 - XILING GONG PETER PI - exploiting qualcom wlan and modem over th...DEF CON 27 - XILING GONG PETER PI - exploiting qualcom wlan and modem over th...
DEF CON 27 - XILING GONG PETER PI - exploiting qualcom wlan and modem over th...
 

Recently uploaded

Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 

Recently uploaded (20)

Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 

IoT: Internet of Things with Python

  • 1. IOT: INTERNET OF THINGS WITH PYTHON Lelio Campanile lelio.campanile@gmail.com @lelioc
  • 2. HI THERE, I’M LELIO CAMPANILE WORK AT UNICAMPANIA.IT TEACHER AT APPLE FOUNDATION COURSE FOR UNICAMPANIA. Follow me: @lelioc www.leliocampanile.it
  • 4. WHAT IS MICROPYTHON? Micropython is a lean and efficient implementation of Python 3 programming language. Python on bare metal! Open source project on github https://github.com/ micropython/micropython
  • 5. FEATURES ▸ Support for many architectures ▸ Mostly compatible with normal python syntax ▸ Fit in 256Kb of code space and 16Kb of RAM ▸ Interactive prompt for easy development (REPL) ▸ Python hardware-specific modules
  • 7. BECAUSE WE WANT PROGRAMMING IN PYTHON EVERYWHERE!!!
  • 8. SUPPORTED BOARDS ▸ ESP 8266 (NodeMCU - SONOFF) ▸ PyBoard ▸ ESP32 ▸ WiPy
  • 9. MICROPYTHON ON ESP 8266 (NODEMCU) firmware for ESP8266 from http://micropython.org/download#esp8266 You have here 4 kind of firmware: - stable builds for device with 1024kb and above - daily builds for device with 1024kb and above - daily builds for device with 512kb - work-in-progress OTA update Firmware
  • 10. DEPLOYING THE FIRMWARE ▸ put your device in boot-loader mode ▸ put the firmware on device
  • 11. DEPLOYING THE FIRMWARE/2 SIMPLY WAY WITH NODEMCU: It has an usb connector with serial converter, it just works. A LITTLE BIT TRICKY WAY: But in other case you could need a usb to ttl converter (5v or 3,5 v) for example in sonoff you have to put GPIO0 to ground-> on boot
  • 12. WRITING TOOL FOR FIRMWARE Now you need a writing tool MicroPython supports esptool (https://github.com/espressif/ esptool/), a python-based, open source and platform independent utility to write the ROM in ESP8266 and ESP32 chips: pip install esptool
  • 13. ERASE... esptool.py --port /dev/cu.SLAB_USBtoUART erase_flash esptool.py v2.3.1 Connecting........_ Detecting chip type... ESP8266 Chip is ESP8266EX Features: WiFi Uploading stub... Running stub... Stub running... Erasing flash (this may take a while)... Chip erase completed successfully in 8.5s Hard resetting via RTS pin...
  • 14. ...AND WRITE IT! esptool.py --port /dev/cu.SLAB_USBtoUART --baud 460800 write_flash --flash_size=detect -fm dio 0 esp8266-20171101- v1.9.3.bin esptool.py v2.3.1 Connecting…….._ Detecting chip type... ESP8266 Chip is ESP8266EX Features: WiFi Uploading stub... Running stub... Stub running... Changing baud rate to 460800 Configuring flash size... Auto-detected Flash size: 4MB Flash params set to 0x0240 Compressed 600888 bytes to 392073… Wrote 600888 bytes (392073 compressed) at 0x00000000 in 9.1 seconds (effective 527.6 kbit/s)... Hash of data verified. Leaving... Hard resetting via RTS pin..
  • 15. AT LAST! REBOOT NODE MCU AND CONNECT TO IT VIA WIFI AP SSID: MICROPYTHON-XXXX AND PASSWD: micropythoN Connect via serial and get REPL: Read Evaluate Print Loop -> the easiest way to test your code! screen /dev/cu.SLAB_USBtoUART 115200
  • 16.
  • 17. WEBREPL EASIEST WAY TO TEST YOUR CODE OVER WIFI, WITH A BROWSER LET'S ENABLE IT! import webrepl_setup FOLLOW THE ON-SCREEN INSTRUCTION. NOW YOU ARE READY TO CONNECT TO MICROPYTHON VIA WEB REPL PROMPT. HTTP://WWW.MICROPYTHON.ORG/WEBREPL/ HTTPS://GITHUB.COM/MICROPYTHON/WEBREPL FOR OFF-LINE USE AND FOR USE COMMANDLINE TOOL, BASICALY TO COPY FILE IN YOUR DEVICE python webrepl_cli.py boot.py 192.168.4.1:/boot.py
  • 18.
  • 19. MAC OS X TIPS Lists all usb device connected to your mac ioreg -p IOUSB -w0 +-o Root <class IORegistryEntry, id 0x100000100, retain 15> +-o AppleUSBXHCI Root Hub Simulation@14000000 <class AppleUSBRootHubDevice, id 0x1000003f8, registered, matched, active, busy 0 (1 ms), retain 10> +-o iBridge@14200000 <class AppleUSBDevice, id 0x1000003fa, registered, matched, active, busy 0 (50 ms), retain 36> +-o CP2102 USB to UART Bridge Controller@14600000 <class AppleUSBDevice, id 0x100002114, registered, matched, active, busy 0 (68 ms), retain 11> when I plugin the node mcu in my mac I view this new usb device, but it don't load the right usb-serial driver…
  • 20. INSTALL USB-SERIAL DRIVER install CP210x USB to UART Bridge VCP Drivers for nodemcu serial adapter built-in from: https://www.silabs.com/products/development-tools/ software/usb-to-uart-bridge-vcp-drivers after a reboot of your mac you could able to view /dev/ cu.SLAB_USBtoUART LAST TIP: make sure to try out different USB cables as well!
  • 22. SETTING NETWORK INTERFACE >>> import network 
 >>> client = network.WLAN(network.STA_IF) 
 >>> ap = network.WLAN(network.AP_IF) 
 >>> client.active() 
 False 
 >>> ap.active() 
 True 
 >>> client.active(True) 
 #5 ets_task(4020ed88, 28, 3fffabe8, 10) 
 >>> client.connect('Daddelio Network', '********') 
 >>> client.ifconfig() 
 ('192.168.1.87', '255.255.255.0', '192.168.1.1', '192.168.1.1') 
 

  • 23. INTERNAL FILESYSTEM # internal filesystem >>> import os >>> os.listdir() ['boot.py', 'webrepl_cfg.py'] >>> the boot.py is executed when device boot up. Insert in boot.py the code that you want to execute when the board start up. After boot.py your board runs main.py.
  • 24. MEET THE EXTERNAL WORLD: GPIO BASICS # GPIO basics >>> import machine >>> led = machine.Pin(2, machine.Pin.OUT) >>> led.value() >>> 0 >>> led.on() >>> led.off()
  • 25. BLINK BUILT IN LED # blinking led >>> import time >>> for i in range(10): ... led.on() ... time.sleep(0.5) ... led.off() ... time.sleep(0.5) ...
  • 26.
  • 27. BREATHING LED # breathing led >>> import machine >>> import time >>> pwm_led = machine.PWM(machine.Pin(2)) >>> pwm_led.duty(1023) >>> for i in range(3): ... for up in range(1023, -1, -50): ... pwm_led.duty(up) ... time.sleep(0.05) ... time.sleep(0.5) ... for down in range(0, 1023, 50): ... pwm_led.duty(down) ... time.sleep(0.05) ... time.sleep(0.5) ...
  • 28.
  • 29. GPIO IN: BUTTON TURN ON LED # button turn on led >>> button = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_UP) >>> while True: ... if not button.value(): ... led.value(not led.value()) ... time.sleep(0.4) ... while not button.value(): ... pass
  • 30.
  • 31. TEMPERATURE AND HUMIDITY z # DHT 11 temperature >>> import machine >>> import dht >>> dht_sensor = dht.DHT11(machine.Pin(2)) >>> dht_sensor.measure() >>> dht_sensor.temperature() >>> 22 >>> dht_sensor.humidity() >>> 64
  • 33. INSTALL IT ! You need python3, then: mkdir homeassistant cd homeassistant python3 -m venv . source bin/activate pip3 install wheel pip3 install homeassistant
  • 34. RUN HOME ASSISTANT hass and voilà http://127.0.0.1:8123
  • 35. CONFIGURE HOME ASSISTANT It is simple!! you can configure mostly write one file: ~/.homeassistant/configuration.yaml And now home assistant tries to discover supported components on startup with no configuration!
  • 36. CONFIGURE NETATMO netatmo: api_key: YOUR_CLIENT_ID secret_key: YOUR_CLIENT_SECRET username: YOUR_USERNAME password: YOUR_PASSWORD
  • 37. MQTT: MQ TELEMETRY TRANSPORT IT IS A SIMPLE LIGHTWEIGHT MESSAGING PROTOCOL, IT WORKS WITH LOW-BANDWITDH AND LOW RESOURCE ENVIROMENTS. IT IS PERFECT FOR IOT!
  • 38. MQTT BASIC CONCEPTS ▸ Publish - Subscribe ▸ Messages ▸ Topics ▸ Broker
  • 39. PUBLISH-SUBSCRIBE A device can publish messages on a topic A device can be subscibed on a topic and receive the message
  • 40. TOPICS AND MESSSAGES Topics are a kind of channels Topics are strings separate by slash "/" For example : home/living/lamp for example you could send (publish) a message on topic "home/living/lamp" to turn on the lamp in your living room. The lamp receives this message because subscriber of that topic. A message is the information sent, it is a simple command or data
  • 41. BROKER Broker is responsible to receive all messages, filtering and route their to subscribed clients Home assistant has an embedded MQTT Broker: hbmqtt, a python mqtt broker It exists many other MQTT Broker, for example: Mosquitto broker - http://mosquitto.org)
  • 42. WHAT IS MY GOAL? NODEMCU WITH DHT11 THAT COMMUNICATE TO HOMEASSISTANT VIA MQTT PROTOCOL: FULL STACK PYTHON FOR IOT
  • 43. MICROPYTHON SIDE import machine import time import dht from umqtt.simple import MQTTClient broker = "192.168.1.65" topic = "home" client_id = "nodemcu_" + machine.unique_id() def main(): client = MQTTClient(client_it, broker) client.connect() while True: dht_sensor.measure() temp_data = dht_sensor.temperature() client.publish(topic+'/'+client_id, bytes(str(temp_data), 'utf-8')) time.sleep(60) if __name__ == '__main__': main()
  • 44. HOME ASSISTANT SIDE In configuration.yaml: sensor: - platform: mqtt state_topic: "home/nodemcu_****unique_id****" name: "nodemcu_temp"