def main():
    start_time = utime.ticks_ms()
    # Turn LED for the duration of the program
    led = Pin(LED_PIN, Pin.OUT, value=1)
    # Show some information on the onboard OLED
    reset = Pin(SSD1306_SCL_RST, Pin.OUT, value=1)
    i2c_oled = I2C(
        scl=Pin(SSD1306_SCL, Pin.IN, Pin.PULL_UP),
        sda=Pin(SSD1306_SDA, Pin.IN, Pin.PULL_UP),
        freq=SSD1306_FREQ
    )
    oled = ssd1306.SSD1306_I2C(64, 32, i2c_oled)
    # BME280 temperature, pressure and humidity readings
    data = b""
    try:
        i2c_bme = I2C(scl=Pin(BME280_SCL), sda=Pin(BME280_SDA), freq=BME280_FREQ)
        bme = bme280.BME280(i2c=i2c_bme)
        temperature, pressure, humidity = bme.read_compensated_data()
    except (OSError, ValueError, NameError) as e:
        print(e)
        data += b"\x00\x00\x00"
        oled.text(ubinascii.hexlify(data), 0, 16)
        oled.text("ERROR!", 0, 24)
    else:
        print("Measurements:", temperature, "c", pressure, "Pa", humidity, "%")
        # Manipulate sensor values so that they occupy 1 byte each
        # Note how a +128 offset is used for the temperature (c) reading to
        # maintain 1 byte signed
        TEMP_OFFSET = const(128)
        data += round(temperature + TEMP_OFFSET).to_bytes(1, "big")
        data += round(pressure / 1000).to_bytes(1, "big")
        data += round(humidity).to_bytes(1, "big")
        # LoRaWAN / TTN send
        lora = uLoRa(
            cs=LORA_CS,
            sck=LORA_SCK,
            mosi=LORA_MOSI,
            miso=LORA_MISO,
            irq=LORA_IRQ,
            rst=LORA_RST,
            ttn_config=TTN_CONFIG,
            datarate=LORA_DATARATE,
            fport=LORA_FPORT
        )
        print("Sending packet...", lora.frame_counter, ubinascii.hexlify(data))
        lora.send_data(data, len(data), lora.frame_counter)
        print(len(data), "bytes sent!")
        lora.frame_counter += 1
        oled.text(ubinascii.hexlify(data), 0, 16)
        oled.text("SENT!", 0, 24)
    finally:
        oled.show()
        utime.sleep_ms(PROGRAM_WAIT_MS)
        led.off()
        deepsleep(PROGRAM_LOOP_MS - (utime.ticks_ms() - start_time))
Beispiel #2
0
#Enter the Data for the TTN access here
DEVADDR = bytearray([INSERT DATA HERE])
NWKEY = bytearray([INSERT DATA HERE])
APP = bytearray([INSERT DATA HERE])
#Configure your contry accodtingly for Europe you may use ="EU"
TTN_CONFIG = TTN(DEVADDR, NWKEY, APP, country="EU")
FPORT = 1


#Initialize the RFM95
lora = uLoRa(
    cs=LORA_CS,
    sck=LORA_SCK,
    mosi=LORA_MOSI,
    miso=LORA_MISO,
    irq=LORA_IRQ,
    rst=LORA_RST,
    ttn_config=TTN_CONFIG,
    datarate=LORA_DATARATE,
    fport=FPORT
)

#OneWire setup
dat = machine.Pin(16)

# create the onewire object
ds = ds18x20.DS18X20(onewire.OneWire(dat))

#At this point the hardware is ready to go
# We will now scan for attached 1Wire devices