Difference between revisions of "MicroPython"
TimGremalm (talk | contribs) |
TimGremalm (talk | contribs) |
||
| (7 intermediate revisions by the same user not shown) | |||
| Line 21: | Line 21: | ||
[http://micropython.org/download#esp8266 http://micropython.org/download#esp8266] | [http://micropython.org/download#esp8266 http://micropython.org/download#esp8266] | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
wget http://micropython.org/resources/firmware/esp8266- | wget http://micropython.org/resources/firmware/esp8266-20190125-v1.10.bin | ||
pip install esptool | pip install esptool | ||
esptool.py --port /dev/ttyUSB0 write_flash 0 esp8266- | esptool.py --port /dev/ttyUSB0 write_flash 0 esp8266-20190125-v1.10.bin | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 35: | Line 35: | ||
[http://docs.micropython.org/en/v1.8.7/esp8266/esp8266/tutorial/network_basics.html http://docs.micropython.org/en/v1.8.7/esp8266/esp8266/tutorial/network_basics.html] | [http://docs.micropython.org/en/v1.8.7/esp8266/esp8266/tutorial/network_basics.html http://docs.micropython.org/en/v1.8.7/esp8266/esp8266/tutorial/network_basics.html] | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="python"> | ||
import network | import network | ||
ap_if = network.WLAN(network.AP_IF) | ap_if = network.WLAN(network.AP_IF) | ||
ap_if.disconnect() | ap_if.disconnect() | ||
| Line 47: | Line 46: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="python"> | ||
sta_if.ifconfig() | sta_if.ifconfig() | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=HTTP GET= | =HTTP GET= | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="python"> | ||
import urequests | import urequests | ||
response = urequests.get("http://tim.gremalm.se/hello") | response = urequests.get("http://tim.gremalm.se/hello") | ||
| Line 59: | Line 58: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=Connect through WEBrepl (websocket)= | |||
[https://learn.adafruit.com/micropython-basics-esp8266-webrepl/access-webrepl https://learn.adafruit.com/micropython-basics-esp8266-webrepl/access-webrepl] | |||
<syntaxhighlight lang="python"> | |||
import webrepl_setup | |||
</syntaxhighlight> | |||
Use HTTP and not HTTPS when connecting to the websocket. HTTPS-version doesn't support connecting to HTTP websocket due to mixed content. | |||
[http://micropython.org/webrepl/ http://micropython.org/webrepl/] | |||
=GPIO= | |||
[https://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/pins.html https://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/pins.html] | |||
<syntaxhighlight lang="python"> | |||
from machine import Pin | |||
button = Pin(0) | |||
button = Pin(0, Pin.IN, Pin.PULL_UP) | |||
pin.value() | |||
</syntaxhighlight> | |||
[https://github.com/nodemcu/nodemcu-devkit] | |||
[[File:NodeMCUPinout.png]] | |||
=WS2812= | =WS2812= | ||
[http://docs.micropython.org/en/v1.8.2/esp8266/esp8266/tutorial/neopixel.html http://docs.micropython.org/en/v1.8.2/esp8266/esp8266/tutorial/neopixel.html] | [http://docs.micropython.org/en/v1.8.2/esp8266/esp8266/tutorial/neopixel.html http://docs.micropython.org/en/v1.8.2/esp8266/esp8266/tutorial/neopixel.html] | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="python"> | ||
from machine import Pin | from machine import Pin | ||
from neopixel import NeoPixel | from neopixel import NeoPixel | ||
| Line 76: | Line 96: | ||
=Color conversion with HSL2GRB= | =Color conversion with HSL2GRB= | ||
[https://gist.github.com/dries007/26a2f36770c28500b912b0ecf1276129 https://gist.github.com/dries007/26a2f36770c28500b912b0ecf1276129] | [https://gist.github.com/dries007/26a2f36770c28500b912b0ecf1276129 https://gist.github.com/dries007/26a2f36770c28500b912b0ecf1276129] | ||
Makes it easier to work with colors. Send in argument hue, and get out RGB | |||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
def hsv_to_rgb(h, s, v): | def hsv_to_rgb(h, s, v): | ||
| Line 100: | Line 121: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
= | =Servo= | ||
Download and upload to ESP's flash: | |||
<syntaxhighlight lang=" | https://github.com/TimGremalm/Folkrace187/blob/master/servo.py | ||
<syntaxhighlight lang="python"> | |||
servoA = servo.Servo(machine.Pin(14)) | |||
servoA.write_angle(90) | |||
time.sleep(1) | |||
servoA.write_angle(110) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
=File system on flash= | =File system on flash= | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="python"> | ||
import os | import os | ||
os.listdir() | os.listdir() | ||
os.remove('filename') | os.remove('filename') | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=ampy= | =ampy= | ||
| Line 132: | Line 144: | ||
a file utility tool for MicroPython on ESP8266 | a file utility tool for MicroPython on ESP8266 | ||
Latest revision as of 15:01, 7 April 2019
MicroPython
a software implementation of the Python 3 programming language, written in C, that is optimized to run on a microcontroller
https://docs.micropython.org/en/latest/pyboard/
NodeMCU Devkit
- ESP8266 a microcontroller from Espressif
- Integrated WiFi
- 80MHz clock speed
Install
Windows/Mac CP2102 virtual com port driver
If you're running Windows or Mac you may have to install the virtual com port driver.
https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers
Firmware
http://docs.micropython.org/en/v1.8.2/esp8266/esp8266/tutorial/intro.html#deploying-the-firmware
http://micropython.org/download#esp8266 <syntaxhighlight lang="bash"> wget http://micropython.org/resources/firmware/esp8266-20190125-v1.10.bin pip install esptool esptool.py --port /dev/ttyUSB0 write_flash 0 esp8266-20190125-v1.10.bin </syntaxhighlight>
Connect through serial
<syntaxhighlight lang="bash"> screen /dev/ttyUSB0 115200 </syntaxhighlight> Stäng screen: Ctrl+A, k, y
Connect to WiFi
http://docs.micropython.org/en/v1.8.7/esp8266/esp8266/tutorial/network_basics.html
<syntaxhighlight lang="python"> import network ap_if = network.WLAN(network.AP_IF) ap_if.disconnect()
sta_if = network.WLAN(network.STA_IF) sta_if.active(True)
sta_if.connect('SSID', 'password') </syntaxhighlight>
<syntaxhighlight lang="python"> sta_if.ifconfig() </syntaxhighlight>
HTTP GET
<syntaxhighlight lang="python"> import urequests response = urequests.get("http://tim.gremalm.se/hello") response.text response.close() </syntaxhighlight>
Connect through WEBrepl (websocket)
https://learn.adafruit.com/micropython-basics-esp8266-webrepl/access-webrepl
<syntaxhighlight lang="python"> import webrepl_setup </syntaxhighlight>
Use HTTP and not HTTPS when connecting to the websocket. HTTPS-version doesn't support connecting to HTTP websocket due to mixed content. http://micropython.org/webrepl/
GPIO
https://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/pins.html
<syntaxhighlight lang="python">
from machine import Pin
button = Pin(0)
button = Pin(0, Pin.IN, Pin.PULL_UP)
pin.value()
</syntaxhighlight>
[1]
WS2812
http://docs.micropython.org/en/v1.8.2/esp8266/esp8266/tutorial/neopixel.html <syntaxhighlight lang="python"> from machine import Pin from neopixel import NeoPixel
np = NeoPixel(Pin(2), 3)
np[0] = (255, 0, 0) np[1] = (0, 128, 0) np[2] = (0, 0, 64) np.write() </syntaxhighlight>
Color conversion with HSL2GRB
https://gist.github.com/dries007/26a2f36770c28500b912b0ecf1276129 Makes it easier to work with colors. Send in argument hue, and get out RGB <syntaxhighlight lang="python"> def hsv_to_rgb(h, s, v): if s == 0.0: return v, v, v i = int(h*6.0) f = (h*6.0) - i p = v*(1.0 - s) q = v*(1.0 - s*f) t = v*(1.0 - s*(1.0-f)) i = i%6 if i == 0: return v, t, p if i == 1: return q, v, p if i == 2: return p, v, t if i == 3: return p, q, v if i == 4: return t, p, v if i == 5: return v, p, q </syntaxhighlight>
Servo
Download and upload to ESP's flash: https://github.com/TimGremalm/Folkrace187/blob/master/servo.py
<syntaxhighlight lang="python"> servoA = servo.Servo(machine.Pin(14)) servoA.write_angle(90) time.sleep(1) servoA.write_angle(110)
</syntaxhighlight>
File system on flash
<syntaxhighlight lang="python"> import os os.listdir() os.remove('filename') </syntaxhighlight>
ampy
https://github.com/adafruit/ampy
a file utility tool for MicroPython on ESP8266
