joining OTAA and then sending location data
-
Hi
I have been able to join my lop4 to TTN using OTAA manually.
Now i want to upload the main.py to the board so that when it boots, it should be able to connect to the TTN automatically and then send the pytrack location data. How can i accomplish this ?
-
@abhishek2101 That's good. besides that, make yourself familiar with ftp (file transfer protocol), who's only purpose is to transfer files. That avoids any copying errros.
-
@robert-hh got it working !!
since i was cutting and pasting the code, there were some truncation problems which were causing the errors.
-
@abhishek2101 Since you have importedthat already in main.py, the second one is not executed. You have to remove that "import mylora" first from main.py
-
@robert-hh nothing happens
attached screenshot
-
@abhishek2101 There is a difference. When you run the code from pymakr, it is freshly uploaded. If you call it from main.py fro in the terminal window of pymakr, you run the local copy on the device.
What happens, if in the REPL prompt, you enter the command:
import mylora
-
@robert-hh here is the code, dont see any problem with line 5
the same code when i run (using pymakr plugin in atom) without uploading is able to run and join the TTN.
from network import LoRa import socket import time import ubinascii # 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.IN865) # create an OTAA authentication parameters app_eui = ubinascii.unhexlify('xxxxxxx') app_key = ubinascii.unhexlify('xxxxxxx') # join a network using OTAA (Over the Air Activation) lora.join(activation=LoRa.OTAA, auth=(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 yet joined...') # create a LoRa socket s = socket.socket(socket.AF_LORA, socket.SOCK_RAW) # set the LoRaWAN 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) # send some data s.send(bytes([0x01, 0x02, 0x03])) # make the socket non-blocking # (because if there's no data received it will block forever...) s.setblocking(False) # get any data received (if any...) data = s.recv(64) print(data)```
-
@abhishek2101 What I see:
main.py starts and calls mylora.py, and that runs into an error at line 5. So check your script.
-
@robert-hh it does not work from REPL prompt also. I have also tried reseting from the button.
screenshot attached.
-
@abhishek2101 You can also look into files using Chrome or the linux file manager, by connecting to:
ftp://<lopy_ip_address>
e.g.
ftp://192.168.4.1
-
@abhishek2101 Does it work, if you call:
import mylora
from the REPL prompt?
if yes, and this statement is inside main.py, do not reset from pymakr, instead push the reset button on the Lopy4.
Pymakr may do a safe boot, which prevents boot.py and main.py from running.
Using my firmware package, you can check the content of main.py and boot.py from the REPL command line by:from upysh import * cat("main.py") cat("boot.py")
-
@robert-hh unfortunately, that is not working for me.
-
@abhishek2101 Add a statement:
import mylora
to main.py
-
@robert-hh i have put the following code in mylora.py and uploaded the same to the board using pymakr plugin for atom. When I upload I see a directory structure created (image attached) under flash directory. Now, if I want this code to run everytime the module is powered on what should I do ?
from network import LoRa import socket import time import ubinascii # 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.IN865) # create an OTAA authentication parameters app_eui = ubinascii.unhexlify('xxxxxxxx') app_key = ubinascii.unhexlify('xxxxxxxxxx') # join a network using OTAA (Over the Air Activation) lora.join(activation=LoRa.OTAA, auth=(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 yet joined...') # create a LoRa socket s = socket.socket(socket.AF_LORA, socket.SOCK_RAW) # set the LoRaWAN 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) # send some data s.send(bytes([0x01, 0x02, 0x03])) # make the socket non-blocking # (because if there's no data received it will block forever...) s.setblocking(False) # get any data received (if any...) data = s.recv(64) print(data)
-
@abhishek2101 There is always a main.py. The question is, whether it is the right one. You could try to give your file a different name, like otaa_test.py, and start it form the REPL prompt with:
import otaa_test
Thatis anyhow the preferred method - putting you own logic in separate files and start that from main.py with import xxxxx
-
@robert-hh I am using the upload feature of pymakr plugin in atom to upload the main.py file. After this when I ftp into the board I can see main.py inside the flash directory.
-
@abhishek2101 when using ftp, did you send the main.py to /flash/main.py on the device?
Also, using the package I made, there is an simple files editor embedded, which you can call from the REPL prompt in a terminal window like Putty or Picocom or screen. And some file tools too.
For the editor, enter:from pye import pye pye("main.py")
Documentations is at https://github.com/robert-hh/Micropython-Editor
For the file tools, enter:from upysh enter *
then you have something like ls, cat, .. at the REPL prompt, and a man command for an overview of all supported commands.
-
@robert-hh I am using the package you made.
-
@abhishek2101 About region IN865. Dou you use the standard firmware form Pycom or the IN865 package which I had assembled. Only in the latter case, IN865 is supported.
-
@abhishek2101 You're using an invalid region in Line 12. Apart from that, the code should work (tested it) and the heartbeat is should be blinking as well. Could you change the region and try again? You can find the available options here.