|
| 1 | +# Copyright (c) 2020, Digi International, Inc. |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to deal |
| 5 | +# in the Software without restriction, including without limitation the rights |
| 6 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +# copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in |
| 11 | +# all copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +# SOFTWARE. |
| 20 | + |
| 21 | +from hdc1080 import HDC1080 |
| 22 | +from machine import I2C |
| 23 | +import time |
| 24 | +import xbee |
| 25 | + |
| 26 | +# Constants |
| 27 | +SLEEP_TIME_MS = 8000 # 8 seconds. |
| 28 | +AWAKE_TIME_MS = 2000 # 2 seconds. |
| 29 | + |
| 30 | +MSG_ON = "ON" |
| 31 | +MSG_OFF = "OFF" |
| 32 | +MSG_SEPARATOR = "@@@" |
| 33 | +MSG_AWAKE = "AWAKE" |
| 34 | + |
| 35 | +print(" +------------------------------------+") |
| 36 | +print(" | XBee MicroPython End to End Sample |") |
| 37 | +print(" +------------------------------------+\n") |
| 38 | + |
| 39 | +# Instantiate the HDC1080 peripheral. |
| 40 | +sensor = HDC1080(I2C(1)) |
| 41 | + |
| 42 | +# Instantiate the XBee device. |
| 43 | +xb = xbee.XBee() |
| 44 | + |
| 45 | +# Configure sleep mode to be managed by MicroPython. |
| 46 | +xb.atcmd("SM", 0x06) |
| 47 | + |
| 48 | +sleep_time = SLEEP_TIME_MS |
| 49 | + |
| 50 | +# Start reading temperature and humidity measures. |
| 51 | +while True: |
| 52 | + # Notify the gateway that the XBee is awake. |
| 53 | + xbee.transmit(xbee.ADDR_COORDINATOR, MSG_AWAKE) |
| 54 | + # Wait during the configured time for incoming messages. |
| 55 | + start_time_ms = time.ticks_ms() |
| 56 | + while time.ticks_diff(time.ticks_ms(), start_time_ms) < AWAKE_TIME_MS: |
| 57 | + incoming = xbee.receive() |
| 58 | + if incoming is not None and incoming["sender_nwk"] == 0 and not incoming["broadcast"]: |
| 59 | + try: |
| 60 | + payload = int(incoming["payload"].decode()) |
| 61 | + except ValueError: |
| 62 | + continue |
| 63 | + if payload < 0: |
| 64 | + continue |
| 65 | + # Update the sleep time. |
| 66 | + sleep_time = payload * 1000 |
| 67 | + print("Temperature/humidity service stopped.\n" if sleep_time == 0 |
| 68 | + else "Changed sleep time to %d s.\n" % payload) |
| 69 | + |
| 70 | + if sleep_time > 0: |
| 71 | + # Read the temperature and humidity. |
| 72 | + temp_celsius = sensor.read_temperature(True) |
| 73 | + humidity_hr = sensor.read_humidity() |
| 74 | + |
| 75 | + # Print values in the console. |
| 76 | + print("- Temperature: %s C" % round(temp_celsius, 1)) |
| 77 | + print("- Humidity: %s %%\n" % round(humidity_hr, 1)) |
| 78 | + |
| 79 | + # Create the message to send with the temperature and humidity. |
| 80 | + tempHum = "{0}{1}{2}".format( |
| 81 | + round(temp_celsius, 1), |
| 82 | + MSG_SEPARATOR, |
| 83 | + round(humidity_hr, 1) |
| 84 | + ) |
| 85 | + |
| 86 | + # Send values to the gateway (coordinator). |
| 87 | + try: |
| 88 | + xbee.transmit(xbee.ADDR_COORDINATOR, tempHum) |
| 89 | + except OSError as e: |
| 90 | + print("Could not transmit data: " + str(e)) |
| 91 | + |
| 92 | + # Sleep for the configured timeout. |
| 93 | + xb.sleep_now(sleep_time) |
0 commit comments