Enthernet Code
Enthernet Code
DIIDevHeads IoT Integration Server
Created by Enthernet Code on 7/11/2024 in #edge-networking
How can I achieve reliable data transfer over LoRa using MicroPython on Pycom GPy?
from machine import Pin, ADC
from network import LoRa
import socket
import time

# Initialize soil moisture sensor
adc = ADC(0)
moisture_pin = adc.channel(pin='P13')

# Initialize LoRa
lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.EU868)
lora_sock = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
lora_sock.setblocking(False)

# LoRa OTAA credentials
dev_eui = '********'
app_eui = '*******'
app_key = '*******'

# Join the network
lora.join(activation=LoRa.OTAA, auth=(dev_eui, app_eui, app_key), timeout=0)

# Wait until the module has joined the network
while not lora.has_joined():
time.sleep(2.5)
print('Not joined yet...')

print('Joined the network!')

# Function to read soil moisture
def read_soil_moisture():
moisture = moisture_pin()
print('Soil moisture:', moisture)
return moisture

while True:
# Read sensor data
soil_moisture = read_soil_moisture()

# Send data via LoRa
lora_sock.send(bytes([soil_moisture]))
print('Data sent:', soil_moisture)

# Sleep for a while
time.sleep(60)
from machine import Pin, ADC
from network import LoRa
import socket
import time

# Initialize soil moisture sensor
adc = ADC(0)
moisture_pin = adc.channel(pin='P13')

# Initialize LoRa
lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.EU868)
lora_sock = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
lora_sock.setblocking(False)

# LoRa OTAA credentials
dev_eui = '********'
app_eui = '*******'
app_key = '*******'

# Join the network
lora.join(activation=LoRa.OTAA, auth=(dev_eui, app_eui, app_key), timeout=0)

# Wait until the module has joined the network
while not lora.has_joined():
time.sleep(2.5)
print('Not joined yet...')

print('Joined the network!')

# Function to read soil moisture
def read_soil_moisture():
moisture = moisture_pin()
print('Soil moisture:', moisture)
return moisture

while True:
# Read sensor data
soil_moisture = read_soil_moisture()

# Send data via LoRa
lora_sock.send(bytes([soil_moisture]))
print('Data sent:', soil_moisture)

# Sleep for a while
time.sleep(60)
6 replies