Table of Contents

MicroPython Utility

ESP-01 BMP180

This utility works like a shell interpreter but pastes the content of a script into a MicroPython device on a local UART interface or network address and displays any output from the device.

Install

hg clone http://hg.kewl.org/pub/mpu
cd mpu
make
sudo make install

Examples

Simple

Run script directly to interpret code on MicroPython device, else invoke with python in the shell to run locally.

#! /usr/local/bin/mpu /dev/ttyUSB1

# vim: shiftwidth=4 tabstop=4 softtabstop=4 expandtab

for i in range(1, 5):
    print("{} Hello".format(i))
    for i in range(1, 5):
        print("World!", end = ' ')
    print()

This is the output from MicroPython or python.

1 Hello
World! World! World! World!
2 Hello
World! World! World! World!
3 Hello
World! World! World! World!
4 Hello
World! World! World! World!
Temperature

BMP180 attached to ESP8266 as per pictured above.

#! /usr/local/bin/mpu /dev/ttyUSB1

import sys
import time
from machine import Pin, I2C
from ustruct import unpack

bus = I2C(scl=Pin(2), sda=Pin(0), freq=100000)

try:
    chipid = ord(bus.readfrom_mem(119, 0xD0, 1))
except:
    print("BMP180 NOT FOUND")
    sys.exit()

if (chipid != 0x55):
    print("BMP180 NOT DETECTED")
    sys.exit()

def read(addr):
    W = bus.readfrom_mem(119, addr, 2)
    W = unpack('>h', W)
    W = W[0]
    return W

AC1 = read(0xAA)
AC2 = read(0xAC)
AC3 = read(0xAE)
AC4 = read(0xB0)
AC5 = read(0xB2)
AC6 = read(0xB4)
B1  = read(0xB6)
B2  = read(0xB8)
MB  = read(0xBA)
MC  = read(0xBC)
MD  = read(0xBE)

bus.writeto_mem(119, 0xF4, bytearray([0x2E]))

time.sleep_ms(5)

UT = read(0xF6)
X1 = ((UT - AC6) * AC5) / 32768
X2 = (MC * 2048) / (X1 + MD)
B5 = X1 + X2
T = ((B5 + 8) / 16) / 10

print ("{} degrees centigrade".format(T))

Output

29.4593 degrees centigrade