Skip to content

jpedrodias/MicroPython

Repository files navigation

MicroPython

MicroPython

MicroPython is a full Python compiler and runtime that runs on the bare-metal. You get an interactive prompt (the REPL) to execute commands immediately, along with the ability to run and import scripts from the built-in filesystem. The REPL has history, tab completion, auto-indent and paste mode for a great user experience.

I built these tools to help me use MicroPython in my classroom (This was tested on Wemos D1 Mini - ESP8266)

NOTE: You will find a custom version of MicroPython already with this files built in (wlan_manager, mqtt_manager, sensor_manager, board_manager and robot_manager) in the folder compiled with instructions.

Contents

  • how to use WLAN Manager
  • how to use MQTT Manager
  • how to use Sensors Manager
  • how to use Board Manager (at work)
  • how to use Robot Manager (at work)

NOTE: I created a custom version of MicroPython already with this files built in (wlan_manager, mqtt_manager, sensor_manager, board_manager and robot_manager). (You will find that custom version in the folder "compiled" with instructions explaining how to install it)

Wemos D1 mini :: GPIO MAP

PIN: D0D1D2D3D4D5D6D7D8
GPIO:16 5 4 0 214121315
PWM: N Y Y Y Y Y Y Y Y

Wemos D1 mini :: Boot Mode Options

GPIO15GPIO0GPIO2Mode Comment
D8 D3 D4 Comment
L H H Flashboot from SPI Flash
L L H UARTProgram via UART (TX/RX)
H any any SDIOBoot from SD card

Board Manager

The board_manager has some useful tools and the GPIO map.

Send board_manager.py to board using:

ampy -p /dev/ttyUSB0 put board_manager.py

Sample code to get GPIO:

from machine import Pin
from time import sleep
from board_manager import D4

led = Pin(D4, Pin.OUT)
    
for i in range(10):
    led.value(i % 2)
    sleep(0.5)

Sample code to use StatusLED:

from time import sleep
from board_manager import D4, StatusLED

led = StatusLED(D4)
led.on()
    
for i in range(10):
    led.toggle()
    sleep(0.5)

Sample code to use NTP:

import ntptime # ntptime.settime()
ntptime.host = 'ntp02.oal.ul.pt'
    
from board_manager import NTPClock as Clock
clock = Clock()
print(clock)

WLAN Manager :: Setup

Send wlan_manager.py to board using:

ampy -p /dev/ttyUSB0 put wlan_manager.py

The first time you need to run the setup() function. This function will creat the file wlan_manager.json to store SSID and password Sample code:

from wlan_manager import WLAN_Manager
wlan_client = WLAN_Manager()

wlan_client.setup() # creates wlan_manager.json file to store your SSID and password
wlan_client.setup('HOME', 'password', append=False) # overwrite the file and store this settings
wlan_client.setup('WORK', 'password', append=True)  # appends this settings to the file

wlan_client.start() # Start using data stored in wlan_manager.json file
wlan_client.start('HOME', 'password') # Start using this ssid and password

MQTT Manager :: Setup

Send mqtt_manager.py and mqtt_manager.json (change here your mqtt setting before send) to board using:

ampy -p /dev/ttyUSB0 put mqtt_manager.py
ampy -p /dev/ttyUSB0 put mqtt_manager.json

Install dependencies on mcu:

import mip
mip.install('umqtt.robust')
mip.install('umqtt.simple')
mip.install('ssd1306')

Sample code:

from mqtt_manager import MQTT_Manager
mqtt_client = MQTT_Manager()
mqtt_client.setup() # creates mqtt_manager.json file to store your broker settings

mqtt_client.check()
mqtt_client.send("debug", "Hello World")
print( "client_id:", mqtt_client.CONFIG["client_id"] )

WLAN and MQTT Manager :: main loop example

Sample code:

def reconnect():
  wlan_client.start()
  success = wlan_client.check() and mqtt_client.check()
  if success:
    mqtt_client.broker.subscribe(TOPIC_SUB)
  return success

def mqtt_callback(topic, msg):
  print('MSG! Topic: {}; Data {}'.format(topic, msg))

from wlan_manager import WLAN_Manager
wlan_client = WLAN_Manager()

from mqtt_manager import MQTT_Manager
mqtt_client = MQTT_Manager()

TOPIC_SUB = mqtt_client.get_topic("control") # You talking to the sensor
TOPIC_PUB = mqtt_client.get_topic("status")  # The sensor talking to you
chatty_client =  bool(mqtt_client.CONFIG.get("chatty", True))
mqtt_client.broker.set_callback(mqtt_callback)
print( "client_id:", mqtt_client.CONFIG["client_id"] )

connected = reconnect()
if connected:
  mqtt_client.send("debug", TOPIC_SUB)
  mqtt_client.send("debug", TOPIC_PUB)

Sensor Manager :: Setup

Send sensor_manager.py to board using:

ampy -p /dev/ttyUSB0 put sensors_manager.py

Sensors Manager :: Using DHT22 (or DHT11) (temperature and humidity sensor)

Sample code:

from machine import Pin
from time import sleep
from board_manager import D1 # D1, ... , D8
from sensor_manager import Sensor_DHT22 # or DHT11

sensor = Sensor_DHT22(D1)

while True:
  sensor.read()
  print(sensor.values, sensor.values_dict)
  sleep(1)

Sensor Manager :: Using DS18B20 (temperature sensor)

Sample code:

from machine import Pin
from time import sleep
from board_manager import D1 # D1, ... , D8
from sensor_manager import Sensor_DS18B20

sensor = Sensor_DS18B20(D1)

while True:
  sensor.read()
  print(sensor.values, sensor.values_dict)
  sleep(1)

Sensor Manager :: Using BMP085, BMP180 or BME280 (pressure, temperature and humidity sensor)

Sample code:

from machine import Pin, I2C
from time import sleep
from board_manager import D1, D2 # D1, ... , D8
from sensor_manager import Sensor_BME280 # Sensor_BME280 or Sensor_BMP180

i2c = I2C(scl=Pin(D1), sda=Pin(D2))
sensor = Sensor_BME280(i2c=i2c, address=0x76) # to find address use i2c.scan()

while True:
  sensor.read()
  print(sensor.values, sensor.values_dict)
  sleep(1)

Note: also need to put the file bme280.py (or bme280.mpy) in the root folder using:

ampy -p /dev/ttyUSB0 put bme280.py bme280.py

Sensor Manager :: Using HC-SR04 (UltraSonic distance sensor)

Sample code:

from machine import Pin
from time import sleep
from board_manager import D1, D2 # D1, ... , D8
from sensor_manager import Sensor_HCSR04

sensor = Sensor_HCSR04(trigger=D1, echo=D2) # or sensor = Sensor_HCSR04(D1, D2)

while True:
  sensor.read()
  print(sensor.values, sensor.values_dict, sensor.distance_mm, sensor.distance_cm)
  sleep(1)

Sensor Manager :: Using VL53L0X (Light distance sensor)

Sample code:

from machine import Pin, I2C
from time import sleep
from board_manager import D1, D2 # D1, ... , D8
from sensor_manager import Sensor_VL53L0X

i2c = I2C(scl=Pin(D1), sda=Pin(D2))
sensor = Sensor_VL53L0X(i2c=i2c, address=0x29) # to find address use i2c.scan()

while True:
  sensor.read()
  print(sensor.values, sensor.values_dict)
  sleep(1)

Sensor Manager :: Using BH1750FVI (Lux sensor)

Sample code:

from machine import Pin, I2C
from time import sleep
from board_manager import D1, D2 # D1, ... , D8
from sensor_manager import Sensor_BH1750FVI

i2c = I2C(scl=Pin(D1), sda=Pin(D2))
sensor = Sensor_BH1750FVI(i2c=i2c, address=0x23) # or 0x5c # to find address use i2c.scan()

while True:
  sensor.read()
  print(sensor.values, sensor.values_dict)
  sleep(1)

Address is 0x23 (Low) or 0x5c (High)

Sensor Manager :: Using PhotoGate

Sample code:

from micropython import const
from sensor_manager import PhotoGate
from board_manager import D7 as GATE_PIN

GATE_MODE = const(0) # 0 for always on | 1 for always off
DELAY_TIME = const(1)

gate1 = PhotoGate(GATE_PIN, mode=GATE_MODE) # mode = 1 | 0
while True:
  gate1.read()
  if gate1.event_change_to(1):
    gate1.start_time()
  if gate1.event_change_to(0):
    gate1.stop_time()
    print(gate1.millis)
  gate1.store()
  time.sleep_us(DELAY_TIME)
#End while loop

see an example

License

License is available in the LICENSE file.

End of file.