From 38e4b3f759ec9ba36c0849735a0852e4bfc3aa15 Mon Sep 17 00:00:00 2001 From: androiddrew Date: Tue, 5 Mar 2019 20:44:06 -0500 Subject: [PATCH] Added basics folder with instructions on how to use the micropython tool chains --- .gitignore | 3 +- README.md | 2 + basics/README.md | 55 ++++++++++++++++++ basics/boot.py | 28 +++++++++ basics/test.py | 11 ++++ boot.py | 16 ------ light_sensor/README.md | 3 + light_sensor/main.py | 2 +- {umqtt => light_sensor/umqtt}/simple.py | 0 main.py | 76 ------------------------- requirements.in | 5 ++ requirements.txt | 22 +++++++ 12 files changed, 129 insertions(+), 94 deletions(-) create mode 100644 basics/README.md create mode 100644 basics/boot.py create mode 100644 basics/test.py delete mode 100644 boot.py create mode 100644 light_sensor/README.md rename {umqtt => light_sensor/umqtt}/simple.py (100%) delete mode 100644 main.py create mode 100644 requirements.in create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore index 62c8935..11d7604 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.idea/ \ No newline at end of file +.idea/ +/env \ No newline at end of file diff --git a/README.md b/README.md index 3c71c83..8e9ed8e 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,8 @@ ampy --help ampy -p /dev/tty.SLAB_USBtoUART -b 115200 ls ``` +See the basic directory within this project for more detailed usage of the ampy package. + ## MQTT on Hassio Assuming you have [Hassio installed](https://www.home-assistant.io/getting-started/) (on a raspberry Pi3 for example). You will find the [official MQTT broker](https://www.home-assistant.io/docs/mqtt/broker/) documentation lacking in that small amount of detail that will get you up and running. This section is supposed to help take you that final mile. diff --git a/basics/README.md b/basics/README.md new file mode 100644 index 0000000..f39cc28 --- /dev/null +++ b/basics/README.md @@ -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 +``` diff --git a/basics/boot.py b/basics/boot.py new file mode 100644 index 0000000..df56c8f --- /dev/null +++ b/basics/boot.py @@ -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() diff --git a/basics/test.py b/basics/test.py new file mode 100644 index 0000000..e430e40 --- /dev/null +++ b/basics/test.py @@ -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 diff --git a/boot.py b/boot.py deleted file mode 100644 index d7aca5c..0000000 --- a/boot.py +++ /dev/null @@ -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() \ No newline at end of file diff --git a/light_sensor/README.md b/light_sensor/README.md new file mode 100644 index 0000000..817d71c --- /dev/null +++ b/light_sensor/README.md @@ -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. \ No newline at end of file diff --git a/light_sensor/main.py b/light_sensor/main.py index 9e68b96..e23b345 100644 --- a/light_sensor/main.py +++ b/light_sensor/main.py @@ -2,7 +2,7 @@ import ujson as json import machine import ubinascii import utime -from umqtt.simple import MQTTClient +from light_sensor.umqtt.simple import MQTTClient class LightSensor: diff --git a/umqtt/simple.py b/light_sensor/umqtt/simple.py similarity index 100% rename from umqtt/simple.py rename to light_sensor/umqtt/simple.py diff --git a/main.py b/main.py deleted file mode 100644 index 9e68b96..0000000 --- a/main.py +++ /dev/null @@ -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() diff --git a/requirements.in b/requirements.in new file mode 100644 index 0000000..96890b1 --- /dev/null +++ b/requirements.in @@ -0,0 +1,5 @@ +pip-tools +esptool +adafruit-ampy +docopt +black \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7c1d232 --- /dev/null +++ b/requirements.txt @@ -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