Added basics folder with instructions on how to use the micropython tool chains
parent
10fc924385
commit
38e4b3f759
@ -1 +1,2 @@
|
|||||||
.idea/
|
.idea/
|
||||||
|
/env
|
@ -0,0 +1,55 @@
|
|||||||
|
# The Basics
|
||||||
|
|
||||||
|
The first `boot.py` will be executed when the interpreter starts. This file should contain only code associated with configuration. Then `main.py` will run. It is within this `main.py` that you will place your entry point code.
|
||||||
|
|
||||||
|
## Setting the AMPY_PORT variable.
|
||||||
|
|
||||||
|
All of the commands below specify a serial port in their execution. We can use the `AMPY_PORT` environmental variable in our terminal session to avoid having to specify this port everytime.
|
||||||
|
|
||||||
|
```
|
||||||
|
export AMPY_PORT=/dev/cu.SLAB_USBtoUART
|
||||||
|
```
|
||||||
|
|
||||||
|
## Uploading files with Ampy
|
||||||
|
|
||||||
|
First review the list of commands that the `ampy` tool provides.
|
||||||
|
|
||||||
|
```
|
||||||
|
ampy --help
|
||||||
|
```
|
||||||
|
We can view the contents of the current file system:
|
||||||
|
|
||||||
|
```
|
||||||
|
ampy --port /serial/port ls
|
||||||
|
```
|
||||||
|
|
||||||
|
We can execute a command immediately on a connected board:
|
||||||
|
|
||||||
|
```
|
||||||
|
ampy --port /serial/port run test.py
|
||||||
|
```
|
||||||
|
|
||||||
|
We can also upload the code directly to the board. Notice we are passing the upload path after the file in our current directory. So, this command is placing our test.py file on the board in a file under the root dir named `main.py`.
|
||||||
|
```
|
||||||
|
ampy --port /serial/port run test.py /main.py
|
||||||
|
```
|
||||||
|
|
||||||
|
We can pull the file from the board to our current working directory if we'd like so:
|
||||||
|
|
||||||
|
```
|
||||||
|
ampy --port /serial/port get main.py ./dirp.py
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## Connecting to a micropython board using screen on a Mac
|
||||||
|
|
||||||
|
```
|
||||||
|
screen /dev/cu.SLAB_USBtoUART 115200
|
||||||
|
```
|
||||||
|
To detach from a screen session first press `Ctrl-A` then `Ctrl-D`.
|
||||||
|
|
||||||
|
To resume a detach session use:
|
||||||
|
|
||||||
|
```
|
||||||
|
screen -r
|
||||||
|
```
|
@ -0,0 +1,28 @@
|
|||||||
|
# This file is executed on every boot (including wake-boot from deepsleep)
|
||||||
|
# import esp
|
||||||
|
# esp.osdebug(None)
|
||||||
|
import gc
|
||||||
|
|
||||||
|
# import webrepl
|
||||||
|
# webrepl.start()
|
||||||
|
gc.collect()
|
||||||
|
|
||||||
|
|
||||||
|
def connect():
|
||||||
|
import network
|
||||||
|
|
||||||
|
sta_if = network.WLAN(network.STA_IF)
|
||||||
|
if not sta_if.isconnected():
|
||||||
|
print("connecting to wireless network....")
|
||||||
|
sta_if.active(True)
|
||||||
|
sta_if.connect(b"YOUR_WIRELESS_SSID", b"YOUR_WIRELESS_PASSWORD")
|
||||||
|
while not sta_if.isconnected():
|
||||||
|
pass
|
||||||
|
print("network config:", sta_if.ifconfig())
|
||||||
|
|
||||||
|
# def no_debug():
|
||||||
|
# import esp
|
||||||
|
# esp.osdebug(None)
|
||||||
|
|
||||||
|
|
||||||
|
connect()
|
@ -0,0 +1,11 @@
|
|||||||
|
###########################################################################
|
||||||
|
# Setup code goes below, this is called once at the start of the program: #
|
||||||
|
###########################################################################
|
||||||
|
import utime
|
||||||
|
|
||||||
|
print('Hello world! I can count:')
|
||||||
|
i = 1
|
||||||
|
while True:
|
||||||
|
print(i)
|
||||||
|
i += 1
|
||||||
|
utime.sleep(1) # Delay for 1 second
|
@ -1,16 +0,0 @@
|
|||||||
def connect():
|
|
||||||
import network
|
|
||||||
sta_if = network.WLAN(network.STA_IF)
|
|
||||||
if not sta_if.isconnected():
|
|
||||||
print("connecting to wireless network....")
|
|
||||||
sta_if.active(True)
|
|
||||||
sta_if.connect(b'YOUR_WIRELESS_SSID', b'YOUR_WIRELESS_PASSWORD')
|
|
||||||
while not sta_if.isconnected():
|
|
||||||
pass
|
|
||||||
print('network config:', sta_if.ifconfig())
|
|
||||||
|
|
||||||
#def no_debug():
|
|
||||||
# import esp
|
|
||||||
# esp.osdebug(None)
|
|
||||||
|
|
||||||
connect()
|
|
@ -0,0 +1,3 @@
|
|||||||
|
# MQTT Light Sensor
|
||||||
|
|
||||||
|
This project uses a simple photo resistor to capture data and push the result to an MQTT broker.
|
@ -1,76 +0,0 @@
|
|||||||
import ujson as json
|
|
||||||
import machine
|
|
||||||
import ubinascii
|
|
||||||
import utime
|
|
||||||
from umqtt.simple import MQTTClient
|
|
||||||
|
|
||||||
|
|
||||||
class LightSensor:
|
|
||||||
"""A class for interacting with a photoresistor on a ESP8266. Suggested using 1K Ohm Resistor and 3.3V power
|
|
||||||
source"""
|
|
||||||
|
|
||||||
def __init__(self, pin=0, threshold=200):
|
|
||||||
self.adc = machine.ADC(pin)
|
|
||||||
self.threshold = threshold
|
|
||||||
|
|
||||||
def read_light(self):
|
|
||||||
if self.adc.read() > self.threshold:
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
False
|
|
||||||
|
|
||||||
|
|
||||||
CONFIG = {
|
|
||||||
"broker": "10.0.1.146",
|
|
||||||
"sensor_pin": 0,
|
|
||||||
"client_id": b"esp8266_" + ubinascii.hexlify(machine.unique_id()),
|
|
||||||
"topic": b"light",
|
|
||||||
}
|
|
||||||
|
|
||||||
client = None
|
|
||||||
sensor_pin = None
|
|
||||||
|
|
||||||
|
|
||||||
def setup_pins():
|
|
||||||
global sensor_pin
|
|
||||||
sensor_pin = machine.ADC(CONFIG.get("sensor_pin"))
|
|
||||||
|
|
||||||
|
|
||||||
def load_config():
|
|
||||||
try:
|
|
||||||
with open("/config.json") as f:
|
|
||||||
config = json.loads(f.read())
|
|
||||||
except (OSError, ValueError):
|
|
||||||
print("Couldn't load /config.json")
|
|
||||||
save_config()
|
|
||||||
else:
|
|
||||||
CONFIG.update(config)
|
|
||||||
print("Loaded config from /config.json")
|
|
||||||
|
|
||||||
|
|
||||||
def save_config():
|
|
||||||
try:
|
|
||||||
with open("/config.json", "w") as f:
|
|
||||||
f.write(json.dumps(CONFIG))
|
|
||||||
except OSError:
|
|
||||||
print("Couldn't save /config.json")
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
client = MQTTClient(CONFIG['client_id'], CONFIG['broker'])
|
|
||||||
client.connect()
|
|
||||||
print("Connected to {} using id {}".format(CONFIG['broker'], CONFIG['client_id']))
|
|
||||||
while True:
|
|
||||||
data = sensor_pin.read()
|
|
||||||
payload = json.dumps({"payload": data}).encode('utf-8')
|
|
||||||
client.publish('{}/{}'.format(CONFIG['topic'],
|
|
||||||
CONFIG['client_id']),
|
|
||||||
payload)
|
|
||||||
print('Sensor state: {}'.format(data))
|
|
||||||
utime.sleep(5)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
load_config()
|
|
||||||
setup_pins()
|
|
||||||
main()
|
|
@ -0,0 +1,5 @@
|
|||||||
|
pip-tools
|
||||||
|
esptool
|
||||||
|
adafruit-ampy
|
||||||
|
docopt
|
||||||
|
black
|
@ -0,0 +1,22 @@
|
|||||||
|
#
|
||||||
|
# This file is autogenerated by pip-compile
|
||||||
|
# To update, run:
|
||||||
|
#
|
||||||
|
# pip-compile requirements.in
|
||||||
|
#
|
||||||
|
--trusted-host pypi.python.org
|
||||||
|
|
||||||
|
adafruit-ampy==1.0.7
|
||||||
|
appdirs==1.4.3 # via black
|
||||||
|
attrs==19.1.0 # via black
|
||||||
|
black==18.9b0
|
||||||
|
click==7.0 # via adafruit-ampy, black, pip-tools
|
||||||
|
docopt==0.6.2
|
||||||
|
ecdsa==0.13 # via esptool
|
||||||
|
esptool==2.6
|
||||||
|
pip-tools==3.4.0
|
||||||
|
pyaes==1.6.1 # via esptool
|
||||||
|
pyserial==3.4 # via adafruit-ampy, esptool
|
||||||
|
python-dotenv==0.10.1 # via adafruit-ampy
|
||||||
|
six==1.12.0 # via pip-tools
|
||||||
|
toml==0.10.0 # via black
|
Loading…
Reference in New Issue