Close

Port the Code Over to Python

A project log for LoRa + Neural Network Security System

Spot trespassers with a neural network and transmit basic results via LoRa

capt-flatus-oflahertyCapt. Flatus O'Flaherty ☠ 12/07/2018 at 13:030 Comments

Since the neural network module using Python 3 i thought it would be a good idea to get the LoRa transmiiter hat on the Raspberry Pi, the Dragonino, to also be controlled using this version of Python. Fortunately, people have already done this and it's well documented here: https://github.com/mayeranalytics/pySX127x/issues/21

However, there are a couple of extra steps that are skipped, so I'll write out the whole procedure here:

1. Remove the SD card from the RPi and insert it into a suitable PC.

2. Copy and paste the config.txt file from the /boot folder to your desktop folder.

3. Change the permissions using chmod 777 in command line, or whatever is convenient, and edit the file by adding:

dtoverlay=spi0-cs,cs0_pin=25 

to the very top.

4. Save, and paste back onto the SD card into boot again. This is the only way to quickly and easily edit this file!

5. Download the Python files from here: https://github.com/mayeranalytics/pySX127x , extract, and open up the 'board_config.py' in a text editor.

6. Use the following values in board_config:

    DIO0 = 4
    DIO1 = 23
    DIO2 = 24
    DIO3 = 21
    LED = 18
    SWITCH = 7

...... Oh, I nearly forgot, if you're in Europe, we use 868 (ish) Mhz so where it says: 'low_band = true' in board_config, this needs to be changed to 'false'. It's pretty self explanatory if you read the comments next to it.

7. Find the 'constants.py' file and edit it by adding the following, being careful to use exactly 4 spaces to be compatible with Python formatting:

    @add_lookup
    class SPI_BAUD_RATE:
        MAX_SPEED_HZ = 5000
    @add_lookup
    class SPI_MODE:
        SPI_MODE = 0b01

 8. Find the LoRa.py file and find the line:

spi = BOARD.SpiDev()

insert these lines right underneath it:

spi.max_speed_hz = SPI_BAUD_RATE.MAX_SPEED_HZ
spi.mode = SPI_MODE.SPI_MODE

 9. Open a terminal and CD to the directory that contains the 'tx_beacon.py' file eg

cd /home/pi/Desktop/dragonino/psySX127x-master/

 10. Where '869' is the frequency in MHz and '7' is the spreading factor, run the beacon program using:

python tx_beacon.py -f 869 -s 7

 11.  Tune the Arduino to 8690E5 and you should see something like this in the serial console:

Received packet ' ' with RSSI -33
Received packet ' ' with RSSI -23
Received packet ' ' with RSSI -33
Received packet ' ' with RSSI -26
Received packet ' ' with RSSI -25

 It's working!

12. If you want to see something a bit more meaningful, open 'tx_beacon.py' with text editor and, near line 65, find:

self.write_payload([0x0f])

Change this to:

self.write_payload([0x57,0x68,0x61,0x74,0x20,0x74,0x68,0x65,0x20,0x66,0x75,0x63,0x6B,0x21])

WARNING: This will transmit obscene language over the airways so don't let your granny see it!

Discussions