Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
projects:mpu [2020/08/21 15:06] darronprojects: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> 
 +{{:projects:espbmp180.jpg?200|ESP-01 BMP180}} 
 +</wrap> 
 + 
 +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 === === Install ===
Line 12: Line 16:
 </code> </code>
  
-=== Example ===+=== Examples ===
  
-==Script== 
  
-Run script directly to interpret code on MicroPython device, else called using +==Simple== 
-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.
  
 <code> <code>
Line 30: Line 34:
     print()     print()
 </code> </code>
- 
-==output== 
  
 This is the output from MicroPython or python. This is the output from MicroPython or python.
Line 44: Line 46:
 4 Hello 4 Hello
 World! World! World! World! World! World! World! World!
 +</code>
 +
 +==Temperature==
 +
 +BMP180 attached to ESP8266 as per pictured above.
 +
 +<code>
 +#! /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))
 +</code>
 +
 +Output
 +
 +<code>
 +29.4593 degrees centigrade
 </code> </code>