示例#1
0
# Chris@Core: in getting rid of all the redundant code we had to make
# a new file to keep it all together. Now the only thing our devices
# need to import is the new loraAPI, that's in /lib/lora_api.py
import time
from lora_api import loraAPI

# Chris@Core: Here's a couple of dummy data values we want to send
# to the gateway
temp = 33
humi = 66

# Chris@Core: This is how we use the loraAPI we added. We create
# an 'object' using the loraAPI 'class'. When we create this thing
# we give it all the details it will need to know about itself.
node = loraAPI(device_id=1,
               device_name="Node1",
               device_colour="red",
               device_colour_code=0xFF0000)

# Chris@Core: We still want the node to send messages repeatedly
while (True):
    # Chris@Core: The to_gateway() function expects two things:
    # - a "dictionary" of key:value pairs. These are things we're sending
    # to the gateway.
    # - a "list" of things we want back from the Gateway.
    node.to_gateway({
        "temperature": temp,
        "humidity": humi
    }, ["time", "location"])
    # Chris@Core: As usual, after every package we expect an acknowledgement()
    node.acknowledge()
    # Chris@Core: Then we wait before sending another package
import time
from lora_api import loraAPI

node = loraAPI(device_id=2,
               device_name="Node2",
               device_colour="green",
               device_colour_code=0x00FF00)

while (True):
    node.to_gateway({}, ["time"])
    node.acknowledge()
    time.sleep(5)
from lora_api import loraAPI
from machine import Timer

# Create a chronometer to measure elapsed time
# This is used later to ensure we don't wait
# forever for something to happen
chrono = Timer.Chrono()
chrono.start()

# When we are given values for these, store them here
temperature = None
humidity = None

# Make this a gateway on the LoRa network.
gateway = loraAPI(device_name='Gateway',
                  device_colour="blue",
                  device_colour_code=0x0000FF,
                  is_gateway=True)

# Do this forever!
while (True):

    # Receive the next LoRa package
    # It will either be an update from node1, or a request for data from node2
    # After this line, device_id is the number of the node to reply to.
    # If receive_json() worked, we'll have a Python dictionary object with
    # either a dictionary {} or a list [] inside it
    device_id, data = gateway.receive_json()

    if device_id and data:  # Ensure a valid package before trying to dissect it.
        # Same as: if device_is is not None and data is not None: