Updates
This commit is contained in:
parent
7ef2d2ecfb
commit
23074cbf43
1
.gitignore
vendored
1
.gitignore
vendored
@ -58,3 +58,4 @@ docs/_build/
|
|||||||
# PyBuilder
|
# PyBuilder
|
||||||
target/
|
target/
|
||||||
|
|
||||||
|
bin
|
||||||
|
15
Makefile
15
Makefile
@ -1,15 +1,20 @@
|
|||||||
# Makefile for the board
|
NAME=esp32-20220117-v1.18.bin
|
||||||
|
|
||||||
NAME=esp32-20180222-v1.9.3-347-g6e675c1b.bin
|
all: flash upload console
|
||||||
|
|
||||||
all: flash console
|
bin/${NAME}:
|
||||||
|
mkdir -p bin
|
||||||
|
wget https://micropython.org/resources/firmware/${NAME} -O bin/${NAME}
|
||||||
|
|
||||||
flash:
|
flash: bin/${NAME}
|
||||||
esptool.py -p /dev/ttyUSB0 -b 921600 erase_flash
|
|
||||||
esptool.py -p /dev/ttyUSB0 -b 921600 write_flash --flash_mode dio 0x1000 bin/${NAME}
|
esptool.py -p /dev/ttyUSB0 -b 921600 write_flash --flash_mode dio 0x1000 bin/${NAME}
|
||||||
sleep 5
|
sleep 5
|
||||||
ampy -p /dev/ttyUSB0 put ssd1306.py
|
ampy -p /dev/ttyUSB0 put ssd1306.py
|
||||||
|
|
||||||
|
upload:
|
||||||
|
ampy -p /dev/ttyUSB0 put boot.py
|
||||||
|
ampy -p /dev/ttyUSB0 put main.py
|
||||||
|
|
||||||
console:
|
console:
|
||||||
echo "Ctrl-A + Ctrl-Q to close Picocom"
|
echo "Ctrl-A + Ctrl-Q to close Picocom"
|
||||||
picocom -b115200 /dev/ttyUSB0
|
picocom -b115200 /dev/ttyUSB0
|
||||||
|
132
README.md
132
README.md
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
## Getting started
|
## Getting started
|
||||||
|
|
||||||
This howto assumes Ubuntu 16.04 is used on the computer and several tools have been installed:
|
This howto assumes Ubuntu 18.04+ is used on the computer and several tools have been installed:
|
||||||
|
|
||||||
```
|
```
|
||||||
sudo apt install picocom make python3-pip
|
sudo apt install picocom make python3-pip
|
||||||
@ -17,6 +17,36 @@ cd micropython-skeleton
|
|||||||
make
|
make
|
||||||
```
|
```
|
||||||
|
|
||||||
|
In order to access the serial port you likely need to add yourself to the
|
||||||
|
`dialout` group after which you need to
|
||||||
|
log out from your desktop session and log in again
|
||||||
|
|
||||||
|
```
|
||||||
|
sudo gpasswd -a $USER dialout
|
||||||
|
```
|
||||||
|
|
||||||
|
**If you're using one of the computers prepared for the workshop all the steps
|
||||||
|
above have been performed already for you**
|
||||||
|
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
To open serial to the microcontroller:
|
||||||
|
|
||||||
|
```
|
||||||
|
make console
|
||||||
|
```
|
||||||
|
|
||||||
|
To upload `main.py`:
|
||||||
|
|
||||||
|
```
|
||||||
|
make upload
|
||||||
|
```
|
||||||
|
|
||||||
|
**Note that serial connection is used by both so you can't upload scripts while
|
||||||
|
serial console is open in another window**
|
||||||
|
|
||||||
|
|
||||||
## Blinking LED-s
|
## Blinking LED-s
|
||||||
|
|
||||||
First let's some LED-s blinking.
|
First let's some LED-s blinking.
|
||||||
@ -59,7 +89,8 @@ Tasks:
|
|||||||
## Button presses
|
## Button presses
|
||||||
|
|
||||||
On the board there is button labelled "Boot", this is hooked up to pin 2.
|
On the board there is button labelled "Boot", this is hooked up to pin 2.
|
||||||
By default there is a resistor which pulls the voltage on the pin to 3.3v, but when button is pressed the pin is shorted to ground so the voltage goes to 0v.
|
By default there is a resistor which pulls the voltage on the pin to 3.3v,
|
||||||
|
but when button is pressed the pin is shorted to ground so the voltage goes to 0v.
|
||||||
Most modern solutions use interrupts to detect voltage change on the pin:
|
Most modern solutions use interrupts to detect voltage change on the pin:
|
||||||
|
|
||||||
```
|
```
|
||||||
@ -90,27 +121,41 @@ Tasks:
|
|||||||
|
|
||||||
## Driving OLED screens
|
## Driving OLED screens
|
||||||
|
|
||||||
Let's get some pixels on the screen.
|
Let's get some pixels on the screen!
|
||||||
There's 128x64 pixels monochrome OLED screen connected via I2C bus on the pins 4 and 5.
|
There's 128x64 pixels monochrome OLED screen connected via I2C bus on the pins 4 and 5.
|
||||||
|
|
||||||
```
|
```
|
||||||
from machine import Pin, I2C
|
from machine import Pin, SoftI2C
|
||||||
from ssd1306 import SSD1306_I2C
|
from ssd1306 import SSD1306_I2C
|
||||||
|
|
||||||
i2c = I2C(-1, Pin(4),Pin(5), freq=400000) # Bitbanged I2C bus
|
i2c = SoftI2C(Pin(4),Pin(5), freq=400000) # Bitbanged I2C bus
|
||||||
oled = SSD1306_I2C(128, 64, i2c)
|
oled = SSD1306_I2C(128, 64, i2c)
|
||||||
oled.invert(0) # White text on black background
|
oled.invert(0) # White text on black background
|
||||||
oled.contrast(255) # Maximum contrast
|
oled.contrast(255) # Maximum contrast
|
||||||
|
|
||||||
oled.fill(0)
|
oled.fill(0)
|
||||||
name = "Lauri"
|
name = "Hello MicroPython!"
|
||||||
oled.text("Hi %s" % name, 10, 10)
|
oled.text(" %s" % name, 10, 10)
|
||||||
oled.show()
|
oled.show()
|
||||||
```
|
```
|
||||||
|
|
||||||
Tasks:
|
Tasks:
|
||||||
|
|
||||||
1. When button is pressed show a corresponding message on the screen - lights turned on/off or the name of the color shown
|
1. What IP address was assigned to the board by the wireless network?
|
||||||
|
Hint: Check out `wlan.ifconfig()`
|
||||||
|
2. Show button press counter on the screen
|
||||||
|
3. Show on the screen which color is the RGB LED shining
|
||||||
|
|
||||||
|
## Clock synchronization
|
||||||
|
|
||||||
|
Most boards to do not have battery backed time keep track of the time.
|
||||||
|
Once you have the microcontroller connected to the Internet you can query
|
||||||
|
time from one of the NTP servers.
|
||||||
|
|
||||||
|
The Internet connectivity is established using `boot.py` script that is
|
||||||
|
already included in this repo. It by default connects to `k-space.ee legacy`
|
||||||
|
wireless network.
|
||||||
|
|
||||||
|
|
||||||
## Temperature & humidity
|
## Temperature & humidity
|
||||||
|
|
||||||
@ -122,7 +167,6 @@ Next let's hook up DHT11 sensor to the board and measure the temperature.
|
|||||||
|
|
||||||
Some code to get you going:
|
Some code to get you going:
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
from time import sleep
|
from time import sleep
|
||||||
from machine import Pin
|
from machine import Pin
|
||||||
@ -150,19 +194,21 @@ Tasks:
|
|||||||
|
|
||||||
In this case HC-SR04 is hooked up:
|
In this case HC-SR04 is hooked up:
|
||||||
|
|
||||||
* Trigger is connected to pin 25
|
* Trigger is connected to pin 12
|
||||||
* Echo is connected to pin 26
|
* Echo is connected to pin 14
|
||||||
* GND pins are connected
|
* GND pins are connected
|
||||||
* Sonar's Vcc is connected to 3.3V on the board
|
* Sonar's Vcc is connected to 3.3V on the board
|
||||||
|
|
||||||
|
This is exactly the case with the sumorobots. Feel free to try it out!
|
||||||
|
|
||||||
Code to measure distance:
|
Code to measure distance:
|
||||||
|
|
||||||
```
|
```
|
||||||
from time import sleep_us, sleep_ms
|
from time import sleep_us, sleep_ms
|
||||||
from machine import Pin, time_pulse_us
|
from machine import Pin, time_pulse_us
|
||||||
|
|
||||||
trigger = Pin(25, Pin.OUT)
|
trigger = Pin(12, Pin.OUT)
|
||||||
echo = Pin(26, Pin.IN)
|
echo = Pin(14, Pin.IN)
|
||||||
|
|
||||||
def measure():
|
def measure():
|
||||||
trigger.value(0)
|
trigger.value(0)
|
||||||
@ -183,66 +229,14 @@ while True:
|
|||||||
Tasks:
|
Tasks:
|
||||||
|
|
||||||
1. Get distance shown on OLED display
|
1. Get distance shown on OLED display
|
||||||
|
2. How were the constants for converting `duration` to `distance` figured out?
|
||||||
|
|
||||||
## Connecting to internet
|
|
||||||
|
|
||||||
Exit the serial console by pressing Ctrl-A and then Ctrl-Q.
|
|
||||||
Upload module to handle WebSockets and return to Python prompt:
|
|
||||||
|
|
||||||
```
|
|
||||||
ampy -p /dev/ttyUSB0 put uwebsockets.py
|
|
||||||
ampy -p /dev/ttyUSB0 put boot.py # Script that connects to itcollege network
|
|
||||||
make console
|
|
||||||
```
|
|
||||||
|
|
||||||
Press EN button on the board to reset the board.
|
|
||||||
|
|
||||||
Paste following:
|
|
||||||
|
|
||||||
```
|
|
||||||
import sys
|
|
||||||
import uwebsockets
|
|
||||||
from machine import Pin
|
|
||||||
|
|
||||||
Pin(12, Pin.OUT).value(1)
|
|
||||||
Pin(13, Pin.OUT).value(1)
|
|
||||||
led_blue = Pin(15, Pin.OUT)
|
|
||||||
|
|
||||||
channel = "living-room-of-lauri"
|
|
||||||
uri = "ws://iot.koodur.com:80/ws/" + channel
|
|
||||||
print("Connecting to:", uri)
|
|
||||||
conn = uwebsockets.connect(uri)
|
|
||||||
conn.send("alive")
|
|
||||||
|
|
||||||
turned_off = False
|
|
||||||
|
|
||||||
while True:
|
|
||||||
print("Reading message...")
|
|
||||||
fin, opcode, data = conn.read_frame()
|
|
||||||
if data == "toggle":
|
|
||||||
turned_off = not turned_off
|
|
||||||
led_blue.value(turned_off)
|
|
||||||
else:
|
|
||||||
print("Got unknown command:", data)
|
|
||||||
```
|
|
||||||
|
|
||||||
Using web browser navigate [here](http://iot.koodur.com/demo2.html#living-room-of-lauri)
|
|
||||||
|
|
||||||
1. Move to another channel to prevent flipping lights in my living room
|
|
||||||
2. Improve the code so the "Boot" button and button in the web interface both work simultaneously
|
|
||||||
3. Download the HTML file and add buttons to select different colors, adjust Python code to handle new commands
|
|
||||||
4. Add code to send sensor readings to the webserver
|
|
||||||
|
|
||||||
|
|
||||||
## Summary
|
## Summary
|
||||||
|
|
||||||
ESP32 microcontroller with MicroPython is a really cheap way to get started with the IoT stuff. See more detailed information [here](https://lauri.xn--vsandi-pxa.com/2017/06/espressif.html).
|
ESP32 microcontroller with MicroPython is a really cheap way to get started
|
||||||
|
with the IoT stuff. See more detailed information
|
||||||
Some more tricks to try:
|
[here](https://lauri.xn--vsandi-pxa.com/2017/06/espressif.html).
|
||||||
|
|
||||||
* Add dimming of LED-s with PWM
|
|
||||||
* Add [colorpicker](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/color)
|
|
||||||
|
|
||||||
Other interesting projects with ESP8266 and ESP32 microcontrollers:
|
Other interesting projects with ESP8266 and ESP32 microcontrollers:
|
||||||
|
|
||||||
|
Binary file not shown.
10
boot.py
10
boot.py
@ -1,5 +1,13 @@
|
|||||||
|
# Give time to cancel this boot script
|
||||||
|
import time
|
||||||
|
print("Press Ctrl-C to stop boot script...")
|
||||||
|
time.sleep(2)
|
||||||
|
|
||||||
# Connect to wireless network as client
|
# Connect to wireless network as client
|
||||||
import network
|
import network
|
||||||
wlan = network.WLAN(network.STA_IF)
|
wlan = network.WLAN(network.STA_IF)
|
||||||
wlan.active(True)
|
wlan.active(True)
|
||||||
wlan.connect("k-space.ee guest")
|
wlan.connect("k-space.ee legacy")
|
||||||
|
|
||||||
|
while not wlan.isconnected():
|
||||||
|
pass
|
||||||
|
48
main.py
48
main.py
@ -1,18 +1,36 @@
|
|||||||
from time import sleep_ms
|
import ntptime
|
||||||
from machine import Pin, I2C
|
ntptime.settime()
|
||||||
from ssd1306 import SSD1306_I2C
|
|
||||||
|
|
||||||
i2c = I2C(-1, Pin(4),Pin(5),freq=400000) # Bitbanged I2C bus
|
from flipdisc import DisplayBuffer, Font
|
||||||
assert 60 in i2c.scan(), "No OLED display detected!"
|
from time import sleep, localtime
|
||||||
oled = SSD1306_I2C(128, 64, i2c)
|
from machine import UART
|
||||||
buf = "wubba lubba dub dub "
|
|
||||||
oled.invert(0) # White text on black background
|
|
||||||
oled.contrast(255) # Maximum contrast
|
|
||||||
j = 0
|
|
||||||
|
|
||||||
|
TIMEZONE = 3
|
||||||
|
weekdays = "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
|
||||||
|
|
||||||
|
port = UART(2, baudrate=4800, tx=17, rx=16)
|
||||||
|
disp = DisplayBuffer(0x06, 128, 16)
|
||||||
while True:
|
while True:
|
||||||
oled.fill(0)
|
for j in range(0,10):
|
||||||
oled.text(buf[j%len(buf):]+buf, 10, 10)
|
year, month, day, hour, minute, second, dow, _ = localtime()
|
||||||
oled.show()
|
hour = (hour + TIMEZONE) % 24
|
||||||
sleep_ms(20)
|
i = " %02d:%02d:%02d" % (hour, minute, second)
|
||||||
j += 1
|
j = " %04d-%02d-%02d %s" % (year, month, day, weekdays[dow])
|
||||||
|
disp.put_text(i.encode("ascii"),0, 0, Font.F6)
|
||||||
|
disp.put_text(j.encode("ascii"),0, 15, Font.F6)
|
||||||
|
buf = disp.finalize_buffer()
|
||||||
|
port.write(buf)
|
||||||
|
sleep(1)
|
||||||
|
for j in range(0, 2):
|
||||||
|
disp.put_text(b"MicroPython",0, 0, Font.F13_F)
|
||||||
|
buf = disp.finalize_buffer()
|
||||||
|
port.write(buf)
|
||||||
|
sleep(2)
|
||||||
|
disp.put_text(b"... is awesome",0, 0, Font.F13_F)
|
||||||
|
buf = disp.finalize_buffer()
|
||||||
|
port.write(buf)
|
||||||
|
sleep(2)
|
||||||
|
|
||||||
|
port.close()
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user