Skip to content

Latest commit

 

History

History
339 lines (232 loc) · 8.54 KB

PITCHME.md

File metadata and controls

339 lines (232 loc) · 8.54 KB

Big thanks to this event supporters


Micropython Intro - Networks - PDPD May 2019


Reinterpretation of Python 3.4 optimised to run in constrained environments




Using micropython

Basic REPL interface


basics - esp8266 modules

  • esp8266 modules
  • esp, machine
  • micropython modules
  • gc, uos

main ways to interact with microcontrollers

  • direct REPL ~ quick testing & behaviour
  • paste mode ~ copy blocks of code from editor to REPL
  • IDE ~ use text editor and microcontroller filesystem
    • upy always runs boot.py then main.py on boot

'hello world' of microcontrollers

D1 mini has its own LED connected to GPIO2 (D4)

from machine import Pin

led = Pin(2, Pin.OUT)
led (0)
led (1)

  • TASK flash led 5 times
  • TASK create functions to support fault finding (program running OK, fault in network connection, cannot read sensor, etc)

PWM - Pulse Width Modulation

  • use a digital signal in an 'analog fashion'
  • Frequency (Hz)is how often a signal switches between low voltage and high voltage.
  • Duty cycle is percent of time that the signal stays at high level (0->1023)

+++

PWM control of on-board LED

from machine import Pin, PWM
import time

pwm = PWM(Pin(2)) #create PWM object

pwm.duty(0)       #led is always on
pwm.duty(1023)    #is always off

for i in range (1023):
  pwm.duty(i)
  time.sleep(0.01)

pwm.duty(512)     #set half way in between
pwm.freq(1)       #now set frequency at 1 hz


buzzer shield MLT-8540

+++

using the buzzer shield

from machine import Pin, PWM
import time

pwm = PWM(Pin(14), freq=440, duty=512)
time.sleep(1)
pwm.deinit()

Freq; c: 262, 'd': 294, 'e': 330, 'f': 349, 'g': 392, 'a': 440,'b': 494,'C': 523,

  • rhythm is achieved by gaps between tones

  • max frequency available on the D1 mini is only 1000hz

  • TASK develop a two tone alert

  • TASK RTTTL use the RTTTL library to perform a little 90's ringtone retro using the following string 'TakeOnMe:d=4,o=4,b=160:8f#5,8f#5,8f#5,8d5,8p,8b,8p,8e5,8p,8e5,8p,8e5,8g#5,8g#5,8a5,8b5,8a5,8a5,8a5,8e5,8p,8d5,8p,8f#5,8p,8f#5,8p,8f#5,8e5,8e5,8f#5,8e5,8f#5,8f#5,8f#5,8d5,8p,8b,8p,8e5,8p,8e5,8p,8e5,8g#5,8g#5,8a5,8b5,8a5,8a5,8a5,8e5,8p,8d5,8p,8f#5,8p,8f#5,8p,8f#5,8e5,8e5'


Temperature/Humidity shield DHT12

+++

explaining I2C

+++

polling sensor

use dht12 library also available using upip

import time
import dht12
from machine import I2C, Pin
i2c = I2C(scl=Pin(5), sda=Pin(4))
sensor = dht12.DHT12(i2c)

while True:
	sensor.measure()
	print('temp is: ', sensor.temperature())
	print('humidity is: ', sensor.humidity())
	time.sleep(10)

network module

connecting to local wifi network (not as a access point)

 import network
 sta = network.WLAN(network.STA_IF)
 sta.active(True)
 sta.connect("network-name", "password")

at this point the d1 mini should be connected to the network

sta.isconnected() #should return True
sta.ifconfig()    #should return your IP address

+++

Star Wars Asciimation using sockets

import socket
addr_info = socket.getaddrinfo("towel.blinkenlights.nl", 23)
addr = addr_info[0][-1]
s = socket.socket()
s.connect(addr)
while True:
   data = s.recv(500)
   print(str(data, 'utf8'), end='')

challenges


  • m2m lightweight messaging protocol
  • publish / subscribe model
  • for connections where a small code footprint is required
  • and/or network bandwidth is at a premium.

+++

+++

MQTT publish using uMQTT.simple

from umqtt.simple import MQTTClient

c=MQTTClient('phil_sensor', 'iot.eclipse.org') #default port is 1883

c.connect()
c.publish('RIFF/phil/temperature', 'here is my data')
c.disconnect()

+++

so develop your own temperature/humidity node!

import time
import dht12
from machine import I2C, Pin
from umqtt.simple import MQTTClient

i2c = I2C(scl=Pin(5), sda=Pin(4))
sensor = dht12.DHT12(i2c)
c=MQTTClient('phil_sensor', 'iot.eclipse.org')

while True:
	sensor.measure()
	print('temp is: ', sensor.temperature())
	print('humidity is: ', sensor.humidity())
  c.connect()
  c.publish('RIFF/phil/temperature', (sensor.temperature()))
  c.publish('RIFF/phil/humidity', (sensor.humidity())
  c.disconnect()
	time.sleep(10)

+++

if you want to download a simple MQTT client on your laptop, consider mosquitto

sudo apt-get install mosquitto mosquitto-clients

mosquitto_sub -h 'iot.eclipse.org' -t RIFF/#

a good simple phone MQTT client is MQTT dashboard for Android


other resources