|
| 1 | + |
| 2 | +''' |
| 3 | +Copyright (c) 2020, Pycom Limited. |
| 4 | +This software is licensed under the GNU GPL version 3 or any |
| 5 | +later version, with permitted additional terms. For more information |
| 6 | +see the Pycom Licence v1.0 document supplied with this file, or |
| 7 | +available at https://www.pycom.io/opensource/licensing |
| 8 | +
|
| 9 | +This is the main.py file for Pybytes |
| 10 | +This code and included libraries are intended for users wishing to fully |
| 11 | +customise pybytes. It is the same code that is included in the Pybytes firmware |
| 12 | +If you're planning to use the Pybytes firmware, please check out the |
| 13 | +examples in the examples directory which are much easier to use. |
| 14 | +If you make changes to any of the libraries in the lib directory, |
| 15 | +you only need to upload the changed files if using the Pybytes firmware. |
| 16 | +The other libraries will be loaded from the built-in code. |
| 17 | +If you make changes above "Please put your USER code below this line" while |
| 18 | +using a Pybytes enabled firmware, you need to disable auto-start. |
| 19 | +You can disable auto-start by setting "pybytes_autostart": false in |
| 20 | +pybytes_project.json or pybytes_config.json. |
| 21 | +If using the Pybytes firmware, the configuration is already loaded and this |
| 22 | +cannot be deactivated. However if you disable auto-start, you can modify the |
| 23 | +configuration before connecting to Pybytes manually using pybytes.connect() |
| 24 | +''' |
| 25 | + |
| 26 | +# Load configuration, migrate to pybytes_config.json if necessary |
| 27 | +if 'pybytes_config' not in globals().keys(): |
| 28 | + try: |
| 29 | + from pybytes_config import PybytesConfig |
| 30 | + except: |
| 31 | + from _pybytes_config import PybytesConfig |
| 32 | + try: |
| 33 | + from pybytes import Pybytes |
| 34 | + except: |
| 35 | + from _pybytes import Pybytes |
| 36 | + |
| 37 | + pybytes_config = PybytesConfig().read_config() |
| 38 | + |
| 39 | +if (not pybytes_config.get('pybytes_autostart', True)) and pybytes_config.get('cfg_msg') is not None: |
| 40 | + print(pybytes_config.get('cfg_msg')) |
| 41 | + print("Not starting Pybytes as auto-start is disabled") |
| 42 | + |
| 43 | +else: |
| 44 | + # Load Pybytes if it is not already loaded |
| 45 | + if 'pybytes' not in globals().keys(): |
| 46 | + pybytes = Pybytes(pybytes_config, pybytes_config.get('cfg_msg') is None, True) |
| 47 | + |
| 48 | + # Please put your USER code below this line |
| 49 | + |
| 50 | + # SEND SIGNAL |
| 51 | + # You can currently send Strings, Int32, Float32 and Tuples to pybytes using this method. |
| 52 | + # pybytes.send_signal(signalNumber, value) |
| 53 | + |
| 54 | + # SEND SENSOR DATA THROUGH SIGNALS |
| 55 | + # # If you use a Pysense, some libraries are necessary to access its sensors |
| 56 | + # # you can find them here: https://github.com/pycom/pycom-libraries |
| 57 | + # |
| 58 | + # # Include the libraries in the lib folder then import the ones you want to use here: |
| 59 | + # from SI7006A20 import SI7006A20 |
| 60 | + # si = SI7006A20() |
| 61 | + # from LTR329ALS01 import LTR329ALS01 |
| 62 | + # ltr = LTR329ALS01() |
| 63 | + # |
| 64 | + # # Import what is necessary to create a thread |
| 65 | + # import _thread |
| 66 | + # from time import sleep |
| 67 | + # from machine import Pin |
| 68 | + # |
| 69 | + # # Define your thread's behaviour, here it's a loop sending sensors data every 10 seconds |
| 70 | + # def send_env_data(): |
| 71 | + # while (pybytes): |
| 72 | + # pybytes.send_signal(1, si.humidity()) |
| 73 | + # pybytes.send_signal(2, si.temperature()) |
| 74 | + # pybytes.send_signal(3, ltr.light()); |
| 75 | + # sleep(10) |
| 76 | + # |
| 77 | + # # Start your thread |
| 78 | + # _thread.start_new_thread(send_env_data, ()) |
| 79 | + |
| 80 | + # SET THE BATTERY LEVEL |
| 81 | + # pybytes.send_battery_level(23) |
| 82 | + |
| 83 | + # SEND DIGITAL VALUE |
| 84 | + # pybytes.send_digital_pin_value(False, 12, Pin.PULL_UP) |
| 85 | + |
| 86 | + # SEND ANALOG VALUE |
| 87 | + # pybytes.send_analog_pin_value(False, 13) |
| 88 | + |
| 89 | + # REGISTER PERIODICAL DIGIAL VALUE SEND |
| 90 | + # pybytes.register_periodical_digital_pin_publish(False, PIN_NUMBER, Pin.PULL_UP, INTERVAL_SECONDS) |
| 91 | + |
| 92 | + # REGISTER PERIODICAL ANALOG VALUE SEND |
| 93 | + # pybytes.register_periodical_analog_pin_publish(False, PIN_NUMBER, INTERVAL_SECONDS) |
| 94 | + |
| 95 | + # CUSTOM METHOD EXAMPLE |
| 96 | + # def custom_print(params): |
| 97 | + # print("Custom method called") |
| 98 | + # return [255, 20] |
| 99 | + # pybytes.add_custom_method(0, custom_print) |
0 commit comments