Exemplo n.º 1
0
def setup():
    print('Program is starting ... ')
    print('Press Ctrl-C to exit.')
    lora.init()

    #boxNum = box
    #print('Mailbox Number: ' boxNum)

    info('send', 'STATUS')
    packet = bytearray(config.OP_STATUS_SIZE)
    packet[0] = config.OP_STATUS
    packet[1:9] = crypto.get_checksum(packet)
    packet[9] = 1
    packet[10] = 1
    packet[11] = 1
    packet[12] = 1

    i = 0
    for x in os.environ['GATE_PUB_KEYS'].split(','):
        if i == 0:
            i += 1
        else:
            lora.send(packet, i)
            info('send', 'ACK {}'.format(lora.packet_str(packet)))
            i += 1
Exemplo n.º 2
0
def loop():
    try:
        packet = lora.recv()
        if not packet: return
        info('loop', 'recv packet {}'.format(lora.packet_str(packet)))

        base_name = '{}_'.format(get_channel())
        data = []
        op = packet[0]
        size = len(packet)
        if op == config.OP_ACK:
            data.append({'bn': base_name, 'n': 'Flag', 'u': 'Flag', 'v': packet[9]})
            data.append({'n': 'Lock', 'u': 'Lock', 'v': packet[10]})
            data.append({'n': 'Package', 'u': 'Package', 'v': packet[11]})
            data.append({'n': 'Power', 'u': 'Power', 'v': packet[12]})
            data.append({'n': 'Error', 'u': 'Error', 'v': packet[13]})

            box_checksum = packet[1:9]
            info('loop', 'box checksum {}'.format(lora.packet_str(box_checksum)))
        elif op == config.OP_CONNECT:
            data.append({'bn': base_name, 'n': 'Flag', 'u': 'Flag', 'v': packet[0]})
            data.append({'n': 'Lock', 'u': 'Lock', 'v': packet[1]})
            data.append({'n': 'Package', 'u': 'Package', 'v': packet[2]})
            data.append({'n': 'Power', 'u': 'Power', 'v': packet[3]})
            data.append({'n': 'Error', 'u': 'Error', 'v': packet[4]})

            box_key = packet[5:]
            info('loop', 'box key {}'.format(lora.packet_str(box_key)))
        elif op == config.OP_STATUS:
            data.append({'bn': base_name, 'n': 'Flag', 'u': 'Flag', 'v': packet[9]})
            data.append({'n': 'Lock', 'u': 'Lock', 'v': packet[10]})
            data.append({'n': 'Package', 'u': 'Package', 'v': packet[11]})
            data.append({'n': 'Power', 'u': 'Power', 'v': packet[12]})

            box_checksum = packet[1:9]
            info('loop', 'box checksum {}'.format(lora.packet_str(box_checksum)))
        else:
            raise Exception('invalid OP packet {}'.format(op))

        if len(data) > 1:
            info('send', 'ACK')
            packet = bytearray(config.OP_ACK_SIZE)
            packet[0] = config.OP_ACK
            packet[1:9] = crypto.get_checksum(packet)
            packet[9] = 1
            packet[10] = 1
            packet[11] = 1
            packet[12] = 100
            packet[13] = config.E_OK

            lora.send(packet)
            info('send', 'ACK {}'.format(lora.packet_str(packet)))

            msg = Message(get_topic(), data)
            info('loop', 'message {}'.format(msg))
            publish(msg)
    except Exception as ex:
        error('loop', str(ex))
    finally:
        time.sleep(0)
Exemplo n.º 3
0
def setup(box):
    print('Program is starting ... ')
    print('Press Ctrl-C to exit.')
    lora.init()

    print('Mailbox Number: ' + str(box))

    info('send', 'STATUS')
    packet = bytearray(config.OP_STATUS_SIZE)
    packet[0] = config.OP_STATUS
    packet[1:9] = crypto.get_checksum(packet)
    packet[9] = 1
    packet[10] = 1
    packet[11] = 1
    packet[12] = 1

    lora.send(packet)
    info('send', 'ACK {}'.format(lora.packet_str(packet)))
Exemplo n.º 4
0
import time
from network import LoRa
import machine

loraactive = False

if loraactive:
    LoRa = lora.init()
if not loraactive:
    wifi.set_connect()

sensor.defsensor()
print("sensor")
while True:

    distance = sensor.getdistance()
    if not loraactive:
        wifi.send(distance)
        print("wifi")

    else:
        lora.send(distance)
        print("LoRa")
    print("sleeping")
    machine.sleep(10000, True)
    print("waking up")
# power usage sleep 40 10s, active 70 3s
#30,76923076923077 + 233,3333333333333 /13
#20,31558185404339mAh
#60h
Exemplo n.º 5
0
vPanel.measure(vBiasP.voltage)
vPyra.measure(vBiasP.voltage)
vBatt.measure(-vBiasB.voltage)
# Turn OpAmps off
powerPin(0)

# Save measurement's timestamp
timestamp = rtc.getDatetime()

# Compose the message
msg = '{:2.1f},{:3.1f},{:2.3f},{:2.3f},{:2.3f},{:2.3f},{:2.3f}'.format(hih.temperature, hih.humidity, vPanel.voltage, vPyra.voltage, vBatt.voltage, vBiasP.voltage, vBiasB.voltage)

# Send the message to the LoRa gateway
if enableLoRa:
    print('Sending data...')
    lora.send(msg)

# Log data to a file stored in SD card
if logData:
    try:
        print('Saving data to SD card...')
        # SD instantiation
        sd = SD()
        mount(sd, '/sd')
        # Filename pattern: aaaammdd.csv
        with open('/sd/' + timestamp.split(' ')[0].replace('-', '') + '.csv', 'a') as datafile:
            datafile.write( timestamp + ',' + msg + '\n' )
            datafile.close()
        unmount('/sd')
    except:
        print('Error while saving data')
Exemplo n.º 6
0
def send(index):
    packet = packets[index]
    print('TEST: sending packet', packet)
    lora.send(packet)
Exemplo n.º 7
0
def sendToServerLora(dataString):
    print(str(dataString))
    data = constructDataString(dataString)
    lora.init()
    lora.send(data)