Added sonar example, fixed URL-s

This commit is contained in:
Lauri Võsandi 2018-02-27 11:44:08 +02:00
parent ae8299c4db
commit 6564761f68
2 changed files with 70 additions and 15 deletions

View File

@ -13,7 +13,3 @@ flash:
console:
echo "Ctrl-A + Ctrl-Q to close Picocom"
picocom -b115200 /dev/ttyUSB0
dep:
sudo apt install python3-pip
sudo pip3 install adafruit-ampy

View File

@ -2,14 +2,23 @@
## Getting started
MicroPython project skeleton
This howto assumes Ubuntu 16.04 is used on the computer and several tools have been installed:
```
git clone http://git.k-space.ee/lauri/micropython-skeleton
sudo apt install picocom make python3-pip
sudo pip3 install esptool.py adafruit-ampy
```
Clone the Git repository and run makefiles:
```
git clone https://github.com/k-space-ee/micropython-skeleton
cd micropython-skeleton
make
```
## Blinking LED-s
First let's some LED-s blinking.
Press Ctrl-E for paste mode, otherwise spaces get mangled.
Press Ctrl-Shift-V for pasting.
@ -47,7 +56,7 @@ Tasks:
# Button presses
## Button presses
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.
@ -79,7 +88,7 @@ Tasks:
# Driving OLED screens
## Driving OLED screens
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.
@ -107,6 +116,13 @@ Tasks:
Next let's hook up DHT11 sensor to the board and measure the temperature.
* Sensor's Vcc is connected to 3.3v on the board
* Ground pins (GND) are connected
* Sensor's data pin is connected to board pin 4
Some code to get you going:
```
from time import sleep
from machine import Pin
@ -115,20 +131,60 @@ from dht import DHT11
d = DHT11(Pin(4))
try:
d.measure()
d.measure()
except OSError:
print("Sensor not connected")
print("Sensor not connected")
else:
print("Temperature %sC" % d.temperature())
print("Humidity %s%%" % d.humidity())
print("Temperature %sC" % d.temperature())
print("Humidity %s%%" % d.humidity())
finally:
sleep(1)
sleep(1)
```
Tasks:
1. Get temperature and humidity displayed on the screen
## Distance with sonar
In this case HC-SR04 is hooked up:
* Trigger is connected to pin 25
* Echo is connected to pin 26
* GND pins are connected
* Sonar's Vcc is connected to 3.3V on the board
Code to measure distance:
```
from time import sleep_us, sleep_ms
from machine import Pin, time_pulse_us
trigger = Pin(25, Pin.OUT)
echo = Pin(26, Pin.IN)
def measure():
trigger.value(0)
sleep_us(5)
trigger.value(1)
sleep_us(10)
trigger.value(0)
duration = time_pulse_us(echo, 1, 29000)
distance = (duration / 2.0) / 29
return distance
while True:
print("Distance is: %s cm" % measure())
sleep_ms(200)
```
Tasks:
1. Get distance shown on OLED display
## Connecting to internet
Exit the serial console by pressing Ctrl-A and then Ctrl-Q.
@ -176,9 +232,10 @@ Using web browser navigate [here](http://iot.koodur.com/demo2.html#living-room-o
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).
@ -190,4 +247,6 @@ Some more tricks to try:
Other interesting projects with ESP8266 and ESP32 microcontrollers:
* [Nixie clock](https://github.com/k-space-ee/nixiesp12) with ESP8266
* [Sumorobot](http://robot.itcollege.ee/sumorobot/2017/08/25/sumesp-prototype/) with ESP32
* [Sumorobot](http://robot.itcollege.ee/sumorobot/2017/08/25/sumesp-prototype/) with ESP32
Come and visit [k-space.ee](https://k-space.ee) on Wednesdays 18:00 for more of MicroPython