Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
projects:mpu [2020/08/21 15:07] – [Example] darron | projects:mpu [2023/07/31 18:36] (current) – [Examples] darron | ||
---|---|---|---|
Line 1: | Line 1: | ||
==== MicroPython Utility === | ==== MicroPython Utility === | ||
- | This utility works like a shell interpreter but pastes the content of a script into a MicroPython device on a local UART interface and displays any output from the device. | + | <wrap right> |
+ | {{: | ||
+ | </ | ||
+ | |||
+ | This utility works like a shell interpreter but pastes the content of a script into a MicroPython device on a local UART interface | ||
=== Install === | === Install === | ||
Line 12: | Line 16: | ||
</ | </ | ||
- | === Example | + | === Examples |
- | ==Script== | + | |
+ | ==Simple== | ||
Run script directly to interpret code on MicroPython device, else invoke with python in the shell to run locally. | Run script directly to interpret code on MicroPython device, else invoke with python in the shell to run locally. | ||
Line 29: | Line 34: | ||
print() | print() | ||
</ | </ | ||
- | |||
- | ==Output== | ||
This is the output from MicroPython or python. | This is the output from MicroPython or python. | ||
Line 43: | Line 46: | ||
4 Hello | 4 Hello | ||
World! World! World! World! | World! World! World! World! | ||
+ | </ | ||
+ | |||
+ | ==Temperature== | ||
+ | |||
+ | BMP180 attached to ESP8266 as per pictured above. | ||
+ | |||
+ | < | ||
+ | #! / | ||
+ | |||
+ | import sys | ||
+ | import time | ||
+ | from machine import Pin, I2C | ||
+ | from ustruct import unpack | ||
+ | |||
+ | bus = I2C(scl=Pin(2), | ||
+ | |||
+ | try: | ||
+ | chipid = ord(bus.readfrom_mem(119, | ||
+ | except: | ||
+ | print(" | ||
+ | sys.exit() | ||
+ | |||
+ | if (chipid != 0x55): | ||
+ | print(" | ||
+ | sys.exit() | ||
+ | |||
+ | def read(addr): | ||
+ | W = bus.readfrom_mem(119, | ||
+ | W = unpack('> | ||
+ | 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, | ||
+ | |||
+ | 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" | ||
+ | </ | ||
+ | |||
+ | Output | ||
+ | |||
+ | < | ||
+ | 29.4593 degrees centigrade | ||
</ | </ | ||