This project is intended to be the logical continue of:
- Google-Cloud-based-IoT-System Tutorial. ( 1 )
- Google Cloud based IoT System - RIOT, MQTTSN, MQTT. ( 2 )
As study project of IoT course at Sapienza - University of Rome. In this section we will build on-top of the cloud based system developed in the previously parts. Now the virtual environmental station, running on RIOT, will publish random value to The Things Network through LoRaWAN, then we develop a bridge between the MQTT broker of TTN and the Google Cloud based infrastructure.The RIOT-OS application will be deployed on IOT-LAB in different nodes to test the system through real-world devices.
DEPENDENCIESIn this project there are few dependencies needed, this can be easly obtained following the official guide linked here:
- RIOT-OS
- Python (2.7 and 3.0+ are needed)
- IOT-LAB account
- The Things Network account
RIOT
virtual
devices
We are going to use pkg_semtech-loramac test provided by RIOT over "/RIOT/tests/pkg_semtech-loramac" that give us a solid base to develop our RIOT application integrated with LoRa. The program is pretty simple and similar to what we have seen in the second tutorial, basically we generate and send random value every 5s through LoRa.
else if (strcmp(argv[1], "station_pub"))
{
uint8_t cnf = LORAMAC_DEFAULT_TX_MODE; /* Default: confirmable*/
uint8_t port = LORAMAC_DEFAULT_TX_PORT; /* Default: 2 */
time_t tm;
srand((unsigned)time(&tm));
char *device;
int random = 0;
semtech_loramac_set_tx_mode(&loramac, cnf);
semtech_loramac_set_tx_port(&loramac, port);
while (1)
{
random = rand() % 5;
printf("Device id extracted: %d\n", random);
switch (random)
{
case 0:
device = "thermometer";
break;
[...]
}
char *data = generate_value(device);
printf("Device %s push data: %s\n", device, data);
printf("========================================\n");
switch (semtech_loramac_send(&loramac,
(uint8_t *)data, strlen(data)))
{
[..]
xtimer_sleep(5);
}
}
The
Things
Network
First of all we need to build an application in TTN.Login with your account or build a new one here, then add a new application, register a device in your TTN application. Remember to save important information as: Device EUI, Application EUI, APP Key, Application ID, Handler Registration that will be essential in the next steps.IoT-LAB
Login to your account or build a new one if you are not confident please read this documentation.We are going to use Sanclay site host, follow this steps:
ssh <username>@saclay.iot-lab.info
iotlab-auth -u <username>
Then start an experiment on BOARD=st-lrwan1:sx1276 for 60 minutes whit name riot_ttn.Remember to save the <output id>
iotlab-experiment submit -n riot_ttn -d 60 -l 1,archi=st-lrwan1:sx1276+site=saclay
Launch this command to see more infos, especially the network_address to the board
iotlab-experiment get -i <output id> -r
{
"network_address": "st-lrwan1-x.saclay.iot-lab.info",
}
Please remember the '-x' number after st-lrwan
Clone RIOT repo on the iot-lab istance and export gcc on path.
git clone https://github.com/RIOT-OS/RIOT.git -b 2019.01-branch
cd RIOT
export PATH=/opt/gcc-arm-none-eabi-7-2018-q2-update/bin:$PATH
Now clone my repository, move main.c to pkg_semtech-loramac and build the application.
git clone https://github.com/SignorAutoma/IoT2020_ProjectHW.git
mv ./IoT2020_ProjectHW/lorawan/main.c ./RIOT/tests/pkg_semtech-loramac
make -C tests/pkg_semtech-loramac clean all
Flash the ST LoRa node with LoRaWAN firmware just built (the x at the end of the script is the same number taken from the network_address).
iotlab-node --update tests/pkg_semtech-loramac/bin/b-l072z-lrwan1/tests_pkg_semtech-loramac.elf -l saclay,st-lrwan1,x
Then access to the RIOT shell running on the node using netcat.
nc st-lrwan1-x 20000
Set devui, appeui, appkey, dr and then join otaa
loramac set deveui ****************
loramac set appeui ****************
loramac set appkey ******************************
loramac set dr 5
loramac join otaa
Finally run polling_sub command
loramac polling_sub
TTN/MQTT
BRIDGE
A simple python program that uses the MQTTbridgeGoogle client developed in my second tutorial and setup the connection to TTN. Basically every message received are forwarded to google through a publish command.
def uplink_callback(msg, client):
data_decoded = base64.b64decode(msg.payload_raw)
data = data_decoded.decode('ascii')
print("Received uplink: ", data)
google_client.publish(mqtt_topic, data, qos=0)
google_client.loop()
return True
handler = ttn.HandlerClient(app_id, access_key)
mqtt_client = handler.data()
mqtt_client.set_uplink_callback(uplink_callback)
mqtt_client.connect()
print("TNN to MQTT connection started.")
while(1):
{}
The payload received is decoded using base64 in order to be readable from humans.Remember to change the config fields as app_id, access_key and all the google_client parameters.
To run follow this simply commands:
cd ttn_mqtt_bridge
pip install ttn
python TTNloraclient.py
Comments