Sending random values to TTN

Hello there,

I am currently working on a project that sends MQ-7 (Gas sensor) values to TTN using a dual channel gateway built from a dragino Lora Hat and a Raspberry pi3. While im waiting for my sensor to arrive from post i decided to write a program that will send random values that acts like my sensor to the TTN since it will be good for my project Blog. Unfortunately my program doesn’t seem to be working. I belive evrything in the program is correct but the device wont join TTN. There could be something wrong with my program but I don’t know what :frowning:

from network import LoRa
import socket
import time
import ubinascii
import struct
import random
import time
from random import seed
from random import randint

COLOUR_BLACK = 0x000000
COLOUR_RED   = 0xFF0000
COLOUR_GREEN = 0x00FF00

pycom.heartbeat(False)
pycom.rgbled(COLOUR_BLACK)

# Initialise LoRa in LORAWAN mode.
# Please pick the region that matches where you are using the device:
# Asia = LoRa.AS923
# Australia = LoRa.AU915
# Europe = LoRa.EU868
# United States = LoRa.US915
lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.EU868)

print("DevEUI: " + ubinascii.hexlify(lora.mac()).decode('utf-8').upper())

# create an OTAA authentication parameters

app_eui = ubinascii.unhexlify('xxxxxxxxxxxxxx')
app_key = ubinascii.unhexlify('xxxxxxxxxxxxxxxxxxxxxxxxxxxx')

# join a network using OTAA (Over the Air Activation)
lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key), timeout=0)

while not lora.has_joined():
    print('Not yet joined...')
pycom.rgbled(COLOUR_RED)
    time.sleep(3)
                                    #line.40
print("Joined network")
pycom.rgbled(COLOUR_GREEN)

# create socket to be used for LoRa communication
s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
# configure data rate
s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
# make the socket blocking
# (waits for the data to be sent and for the 2 receive windows to expire)
s.setblocking(True)

#define which port with the socket bind
s.bind(2)

#send some data
def send_level():

    # seed random number generator
    seed(1)
    # generate some integers
    for _ in range(10000000): #Fake sensor sends values till 10000000 to act as
                              # a infinite loop
                              
    value = randint(1, 12800) #12,800 is the highest level of CO 

    print('Sending CO level: {}'.format(value))
    uplink_message = bytes([value])
    s.send(uplink_message)

    time.sleep(5) # 5 second delay
    
s.setblocking(False)
# get any data received...
data = s.recv(64)
print(data)

I recieve some errors such as…

Traceback (most recent call last):
File “”, line 40
IndentationError: unexpected indent

But when i check the line i cant seem to spot the error.
Any help or pointers will be much appriciated :slightly_smiling_face:

This is not a compliant LoRaWAN gateway which should be disconnected from the TTN network immediately as it causes huge disruption to any nodes in the area that are expecting to be heard by a compliant gateway.

This is likely to be the root cause of your problem - your device isn’t able to communicate properly with your Dual Channel Packet Forwarder.

  1. Please format your post so we can read it. Your original mix of bold/capitals/normal text and lack of indentation makes it impossible to find any issues.
  2. Are you aware Python uses indentation as part of the syntax? The ‘pycom.rgbled’ at the start of the line terminates the loop above it. The next line with indentation confuses python as it should start at the first character of the line as well.
  3. As mentioned by @descartes, you don’t have a LoRaWAN gateway, your hardware is disruptive and should not be used in TTN. Even once you solved your syntax issues this combination of a pycom node and incompatible packet forwarder won’t work.

Additionally, sending even the tinest of uplinks every 5 seconds is an egregious breach of the TTN Fair Use Policy and is most likely illegal (as in law, as in Police) in your jurisdiction.

Fortunately the code doesn’t actually call the send function and double fortunately the function won’t repeat 10000000 times due to indention errors.

If they were working, you’d never see a downlink as that code would never be reached.