Skip to content
master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 

README.md

MicroPython

Some tools to help when using MicroPython (tested on Wemos D1 Mini - esp8266)

  • 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)

PS: My personilized version of MicroPython (in the folder "compiled") already has this files (wlan_manager, mqtt_manager, sensor_manager, board_manager and robot_manager).

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

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

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()

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
from mqtt_manager import MQTT_Manager
mqtt_client = MQTT_Manager()
mqtt_client.setup() # creates mqtt_manager.json file to store your broker settings
print( "client_id:", mqtt_client.CONFIG["client_id"] )

WLAN and MQTT Manager :: main loop example

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)
  mqtt_client.send("debug", app_name)

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)

from machine import Pin
from time import sleep
from board_manager import * # 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)

from machine import Pin
from time import sleep
from board_manager import * # 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)

from machine import Pin, I2C
from time import sleep
from board_manager import * # 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)

from machine import Pin
from time import sleep
from board_manager import * # 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)

from machine import Pin, I2C
from time import sleep
from board_manager import * # 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)

from machine import Pin, I2C
from time import sleep
from sensor_manager import Sensor_BH1750FVI

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

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

License

Any code placed here is released under the MIT License (MIT).
The MIT License (MIT)
Copyright (c) 2016 Peter Hinch
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

End of File

About

MicroPython

Resources

Releases

No releases published

Packages

No packages published

Languages

You can’t perform that action at this time.