The goal of this lab is to read real data with LoRaWAN device and send it to TTN, while also retrieving your data from TTN using MQTT. You will accomplish the stable display and storage of actual measurement data on the TTN platform. The team project’s ultimate objective is to attain a comparable outcome.
By completing the fifth training lab, you will be able to:
Please do the following before the Friday lab section:
App: Integrations: MQTT: Generate Key. Copy it to a safe place now for further use.
TTN cannot share the key you generated after you exit that page, so if you lose
it, you have to make another key.
Please install a recent version of Python. You can find Python packages in the “Files” section of the Python 3.11.2 Page. Be sure to check the box to install pip as well. On Windows, please also check the box to install the py launcher and to add Python to PATH. If you have taken CS 1110, that setup should work.
Python is run from a command line in our case. On MacOS, search for the “Terminal” application,
and on Windows, search for “PowerShell.” Throughout this lab, certain symbols
placed before commands will notate
MacOS Terminal ($
), Windows PowerShell (PS >
), and Python interactive-mode (>>>
)command lines.
If you copy commands, do not copy the leading symbols ($
, PS >
, >>>
, etc.).
On MacOS, use
$ python3
to start an interactive Python session.
From PowerShell, use
PS > py
to start an interactive Python session.
In Python, you can use the quit()
function to quit.
>>> quit()
On MacOS, Control-D is the interactive-mode quit keyboard shortcut, and on Windows, Control-Z then enter quits.
You can also put Python statements into a file. Python essentially reads
and executes your file line by line. You can instruct Python to run a file
myPythonCode.py
with the following command.
$ python3 myPythonCode.py
PS > python myPythonCode.py
The above command assumes that you are in the same directory (folder) as your
Python file. If you are not, you can use the cd
command to change
directory. For example, if your Python file was in Documents/sp23/MAE4220
,
you could use
cd Documents/sp23/MAE4220
to go to that directory. Spaces and special characters are sometimes challenging
to use on the command line, but using tab to auto-complete can help. For example,
if I have a folder called Test Folder
, if I hit tab when I have typed
cd Tes
MacOS will autocomplete it to
cd Test\ Folder/
and Windows will autocomplete it to
cd '.\Test Folder\'
Python has a package manager called pip. Many packages are available in the PyPi repository, but we will just use one this week.
pip3 install paho-mqtt==1.6.1
In some cases, pip3 or pip doesn’t appear as a command, so you can try commands like
python3 -m pip install paho-mqtt==1.6.1
or
python -m pip install paho-mqtt==1.6.1
or
py -m pip install paho-mqtt==1.6.1
In lab 2, you made a small set of libraries to read temperature data. In lab 3, you practiced loading different kinds of values into packets and decoding. In this lab, you will merge the two. Please set up your Feather and decoder to send and decode thermistor and digital sensor temperature measurements.
To read your temperature sensors, use your library code. You should copy
your TemperatureSensor.h and TemperatureSensor.cpp files from your Lab 2
sketch folder to your Lab 5 sketch folder. Review your Lab 2 sketch as
necessary to see what #include
files and class instantiations
(like PANE103395_VD thermistor;
) are necessary to use your library.
Change struct pkt_fmt
to include a few floats to hold temperature
data.
Update your packet with a reading of
(float) thermistor.getTemperature()
before calling sendBuffer
.
There is not a great need to send a double
(twice as much data)
over LoRaWAN because neither sensor is very precise.
Your lab 4 does not have to work in order for you to save data and meet the lab 5 objective. You simply have to be able to send and decode data.
You can modify a simple MQTT script in Python using a module such as Paho MQTT to subscribe to the updates that your app creates. This is especially useful if you want to do further processing on your data. The tool can give json, which is easy to work with on Python on Linux, macOS, and Windows. There is an example given below.
Here’s an example script in Python that receives data via MQTT and saves the data to a file. You may download it here.
#!/usr/bin/env python3
# from https://stackoverflow.com/questions/54292179/saving-mqtt-data-from-subscribe-topic-on-a-text-file
import paho.mqtt.client as mqttClient
import time
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to broker")
global Connected #Use global variable
Connected = True #Signal connection
else:
print("Connection failed")
def on_message(client, userdata, message):
print("")
print("Message received: " + str(message.payload))
with open('myData.txt','a+') as f: # You can change the data file name here
f.write(str(message.payload)[2:-1]+"\n")
Connected = False #global variable for the state of the connection
broker_address= "nam1.cloud.thethings.network" #host
port = 1883 #Broker port
user = "yourAppHere@ttn" #<-- Put your TTN V3 app here #Connection username
password = "" #<-- Put your TTN V3 API key in quotes #Connection password
client = mqttClient.Client("Python") #create new instance
client.username_pw_set(user, password=password) #set username and password
client.on_connect= on_connect #attach function to callback
client.on_message= on_message #attach function to callback
client.connect(broker_address,port,60) #connect
client.subscribe(f"v3/{user}/devices/+/up") #subscribe
client.loop_forever() #then keep listening forever
Fill in your app name and MQTT API key. When you run
the Python script,
Python will subscribe to your app’s MQTT feed and append messages to
myData.txt
. You may change myData.txt to another file name.
You will make a plot of such data in Lab 6.
Please let paho run for a while and submit your data file as a .txt file to Canvas as a deliverable.