From ba4a397e5cd38dd65d0aef9ed864b55fe2eb401d Mon Sep 17 00:00:00 2001 From: Peter Hart Date: Fri, 28 Aug 2020 23:37:54 -0400 Subject: [PATCH] wlan, ntp, time/ip display --- boot.py | 43 +++++++++++++++++++++++++++++++++++++ main.py | 22 ++++++++++++++++--- notes.org | 64 ++++++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 116 insertions(+), 13 deletions(-) create mode 100644 boot.py diff --git a/boot.py b/boot.py new file mode 100644 index 0000000..ea1e65c --- /dev/null +++ b/boot.py @@ -0,0 +1,43 @@ +# This file is executed on every boot (including wake-boot from deepsleep) +#import esp +#esp.osdebug(None) +#import webrepl +#webrepl.start() + +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=hostname) + +iphone = b'Hart\xe2\x80\x99s iPhone' +home = b"Hart's" + +ff=wlan.scan() +connecting=true +for i in ff: + print(i) + +def matches(nws, sid): + for i in nws: + if i[0] == sid: + return True + return False + +if matches(ff,iphone): + print('connecting to iphone') + wlan.connect(iphone,'rg3t3jpht2aah') +elif matches(ff,home): + print('connecting to home') + wlan.connect(home,'D187wn644GeH') +else: + print('not connecting') + connecting = false +while connecting and not wlan.isconnected(): + machine.idle() +print('WLAN connection succeeded') + +import ntptime +ntptime.settime() diff --git a/main.py b/main.py index 9ca3aa0..506b667 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,8 @@ -from machine import I2C, Pin +from machine import I2C, Pin, Timer, RTC +from network import STA_IF, WLAN import ssd1306 import time +import utime from mpu6050 import accel screen = {} @@ -21,13 +23,27 @@ mpu['sda'] = Pin(23, Pin.OUT, Pin.PULL_UP) mpu['i2c'] = I2C(scl=mpu['scl'], sda=mpu['sda']) accelerometer = accel(mpu['i2c']) +def date_time(): + (y,mo,d,wd,h,mi,s,*v) = RTC().datetime() + (y,mo,d,h,mi,s,wd,yd) = utime.localtime(utime.mktime((y,mo,d,h,mi,s,wd,0))-4*3600) + return ('%02d/%02d/%02d' % (mo,d,y-2000), '%02d:%02d:%02d' % (h,mi,s)) + +def ip(): + return WLAN(STA_IF).ifconfig()[0] + def output_positions(arg): oled.fill(0) oled.text('X: %(AcX)+05d' % arg, 5, 5) oled.text('Y: %(AcY)+05d' % arg, 5, 15) oled.text('Z: %(AcZ)+05d' % arg, 5, 25) + (d,t) = date_time() + oled.text(d,5,35) + oled.text(t,5,45) + oled.text(ip(),5,55) oled.show() -for i in range(0,240): +def run_cycle(): output_positions(accelerometer.get_values()) - time.sleep_ms(250) + +timer = Timer(0) +timer.init(period=1000, mode=Timer.PERIODIC, callback=lambda t:run_cycle()) diff --git a/notes.org b/notes.org index 495becf..d756fce 100644 --- a/notes.org +++ b/notes.org @@ -43,6 +43,50 @@ a.get_values() #+END_SRC +* Turn off MPU (through vext, also turns off screen) +#+BEGIN_SRC python +from machine import Pin + +vext = Pin(21, Pin.OUT) +vext.value(0) # Turns ON Vext +vext.value(1) # Turns OFF Vext +#+END_SRC + +* Wifi Connection Example, outbound requests. + +#+BEGIN_SRC python +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() +#+END_SRC + +* Getting a Unique ID for this instance + +#+BEGIN_SRC python +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]) +#+END_SRC + +This can be used to append to e.g. the DHCP hostname to make it likely +to be unique. + +* NTP + +#+BEGIN_SRC python +import ntptime +ntptime.settime() +import machine +rtc = machine.RTC() +rtc.datetime() +#+END_SRC + * Connections Pin 23 ESP -> SDA MPU @@ -50,20 +94,20 @@ Pin 19 ESP -> SCL MPU MPU AD0 -> - Rail MPU GND -> - Rail -MPU Vcc -> + Rail +MPU Vcc -> + RailArduino MPU 6050 Serial Monitor ESP GND -> - Rail ESP VEXT -> +Rail -* Things to look at +* [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 + - [X] Can we turn off the MPU with VEXT Control? (ESP Pin 21). + - [X] Connecting to WiFi - Client Mode + - [ ] Connecting to WiFi - Direct Mode + - [ ] BLE? + - [X] NTP? + - [ ] How to store the data appropriately - Flash/SqlLite, RTC memory? + - [ ] How to retrieve data over WiFi + - [ ] How to detect battery low - [[https://github.com/tayfunulu/WiFiManager][WiFiManager]]