timeular/notes.org
2020-09-09 23:06:54 -04:00

2.7 KiB

MicroPython Implementation Notes

Circuit Layout

Pin Used For
37 SDA - MPU6050
36 SCL - MPU6050

SSD 1306

from machine import I2C, Pin
import ssd1306

rst = Pin(16, Pin.OUT)
rst.value(1)
scl = Pin(15, Pin.OUT, Pin.PULL_UP)
sda = Pin(4, Pin.OUT, Pin.PULL_UP)
i2c = I2C(scl=scl, sda=sda, freq=450000)
oled = ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3c)

oled.fill(0)
oled.text('ESP32', 45, 5)
oled.text('MicroPython', 20, 20)
oled.show()

MPU 6050

from machine import I2C, Pin
from mpu6050 import accel

scl=Pin(36, Pin.OUT)
sda=Pin(37, Pin.OUT)
i2c = I2C(scl=scl, sda=sda, freq=450000)
a = accel(i2c)
a.get_values()

Turn off MPU (through vext, also turns off screen)

from machine import Pin

vext = Pin(21, Pin.OUT, Pin.PULL_HOLD)
vext.value(0) # Turns ON Vext
vext.value(1) # Turns OFF Vext
machine.deep_sleep()

Wifi Connection Example, outbound requests.

import network
import machine
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
binary_id = machine.unique_id()
hostname = 'timeular-{:02x}{:02x}{:02x}{:02x}'.format(binary_id[0],binary_id[1], binary_id[2], binary_id[3])
wlan.config(dhcp_hostname='timeular')
wlan.connect("Hart's",'D187wn644GeH')
wlan.if_config()

Getting a Unique ID for this instance

import machine
binary_id=machine.unique_id()
hex_id = '{:02x}{:02x}{:02x}{:02x}'.format(binary_id[0],binary_id[1],binary_id[2],binary_id[3])

This can be used to append to e.g. the DHCP hostname to make it likely to be unique.

NTP

import ntptime
ntptime.settime()
import machine
rtc = machine.RTC()
rtc.datetime()

Web Server

import picoweb
from network import WLAN, STA_IF

app = picoweb.WebApp('main')

@app.route("/")
def index(req,resp):
    yield from picoweb.start_response(resp)
    yield from resp.awrite("Hello world from picoweb running on the ESP32")
 
app.run(debug=True,host=WLAN(STA_IF).ifconfig()[0],port=80)

Connections

Pin 23 ESP -> SDA MPU Pin 19 ESP -> SCL MPU

MPU AD0 -> - Rail MPU GND -> - Rail MPU Vcc -> + RailArduino MPU 6050 Serial Monitor

ESP GND -> - Rail ESP VEXT -> +Rail

[3/8] Things to look at

  • Can we turn off the MPU with VEXT Control? (ESP Pin 21).
  • Connecting to WiFi - Client Mode
  • Connecting to WiFi - Direct Mode
  • BLE?
  • NTP?
  • How to store the data appropriately - Flash/SqlLite, RTC memory?
  • How to retrieve data over WiFi
  • How to detect battery low
  • WiFiManager