How can I achieve reliable data transfer over LoRa using MicroPython on Pycom GPy?

@Middleware & OS I'm developing a smart agriculture system using a Pycom GPy, which features an ESP32 microcontroller with LTE and LoRa capabilities. The system is running MicroPython, and I'm using a soil moisture sensor to monitor soil conditions. My goal is to read soil moisture data and send it over LoRa to a remote gateway. I've written the following code to initialize the soil moisture sensor and set up LoRa communication, but I'm encountering errors, I'm unable to get valid readings from the soil moisture sensor, and the LoRa transmission does not seem to be working. ValueError: invalid pin for ADC Not joined yet... I have verified the sensor connection and checked the pin configuration, ensured that the LoRa credentials (DevEUI, AppEUI, AppKey) are correct and match those provided by the network, cross checked the LoRaWAN regional settings and ensured the frequency is set correctly, verified the LoRa antenna is properly connected. But still getting the errors. How can I resolve these issues to successfully read data from the soil moisture sensor and transmit it via LoRa using MicroPython on the Pycom GPy? Please provide any necessary configuration changes or code corrections. Here's my code
Solution:
Hello guys, after a while of debugging and checking through I was able to fix the ValueError: invalid pin for ADC on my code, it turns out I was required to use p15 intead of p13 and for the Not joined yet... error it was due to tge mistake I made in imputing my Appkey which i have corrected now. This is My. Code ```python from machine import Pin, ADC from network import LoRa...
Jump to solution
4 Replies
Enthernet Code
Enthernet Code6mo ago
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)
James Johnson
James Johnson6mo ago
i think it's an connection problem with device
Solution
Enthernet Code
Enthernet Code6mo ago
Hello guys, after a while of debugging and checking through I was able to fix the ValueError: invalid pin for ADC on my code, it turns out I was required to use p15 intead of p13 and for the Not joined yet... error it was due to tge mistake I made in imputing my Appkey which i have corrected now. This is My. Code
from machine import Pin, ADC
from network import LoRa
import socket
import time

# Initialize soil moisture sensor
adc = ADC() # Corrected ADC initialization
moisture_pin = adc.channel(attn=ADC.ATTN_11DB, pin='P15') # Corrected pin configuration

# 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 = '********'
DevEUI
app_eui = '********'
AppEUI
app_key = '********'
AppKey

# 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.voltage() # Use voltage method to get the sensor reading
print('Soil moisture:', moisture)
return moisture

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

# Prepare data for transmission
data = [int(soil_moisture / 4)]

# Send data via LoRa
lora_sock.send(data)
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() # Corrected ADC initialization
moisture_pin = adc.channel(attn=ADC.ATTN_11DB, pin='P15') # Corrected pin configuration

# 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 = '********'
DevEUI
app_eui = '********'
AppEUI
app_key = '********'
AppKey

# 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.voltage() # Use voltage method to get the sensor reading
print('Soil moisture:', moisture)
return moisture

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

# Prepare data for transmission
data = [int(soil_moisture / 4)]

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

# Sleep for a while
time.sleep(60)
Enthernet Code
Enthernet Code6mo ago
But am now having issues sending the data as it is not reflecting anything @everyone @Middleware & OS
Want results from more Discord servers?
Add your server