diff --git a/examples/LTE modem/README.md b/examples/LTE modem/README.md index c2d1e4c7..3918988a 100644 --- a/examples/LTE modem/README.md +++ b/examples/LTE modem/README.md @@ -44,6 +44,7 @@ Data frame will send to MQTT every 5 seconds. 2. **Open Serial Studio and configure MQTT**: - Set the **Host**: 127.0.0.1 + - Set the **Port**: 1883 - Set the **Topic**: lte - Set the **Mode**: Subscriber - Set the **Keep Alive**: 600 @@ -57,3 +58,20 @@ Data frame will send to MQTT every 5 seconds. 4. Use **Project editor** to configure dashboard. ![Project Editor](doc/project_editor.png) + + +## UDP Socket +Solution with UDP Socket looks much simpler than MQTT. + +Run in terminal: +`python lte_udp.py` + +### Serial Studio Configuration for UDP Socket + + - Set the **DEVICE SETUP**: I/O Interface: Network Socket + - Set the **Socket type**: UDP + - Set the **Remote address**: 127.0.0.1 + - Set the **Local port**: 5005 + - Set the **Ignore data delimeters**: True + - Click **Connect** +![Serial Studio Quick Plot](doc/udp.png) diff --git a/examples/LTE modem/doc/udp.png b/examples/LTE modem/doc/udp.png new file mode 100644 index 00000000..1b822ff3 Binary files /dev/null and b/examples/LTE modem/doc/udp.png differ diff --git a/examples/LTE modem/lte_udp.py b/examples/LTE modem/lte_udp.py new file mode 100644 index 00000000..cf11ea19 --- /dev/null +++ b/examples/LTE modem/lte_udp.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python + +import requests +import time +import datetime +import xml.etree.ElementTree as ET +import re +import socket + +# -------------------------- CONFIGURATION -------------------------------- + +url_api = 'http://192.168.9.1/api/device/signal' # Address modem API +cycle_time = 5 # Pause between data frame +udp_ip = '127.0.0.1' # UDP target IP +udp_port = 5005 # UDP target port + +# ------------------------------------------------------------------------- + +def get_value(marker): + string = tree.find(marker).text + value = re.search(r'(\-|)(\d+)(\.?)(\d*)', string).group(0) + # print('string=', string, ' value=', value) + return value + +sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + +while True: + xml_data = requests.get(url_api).text + tree = ET.XML(xml_data) + + cell = str(get_value('cell_id')) + rsrq = int(float(get_value('rsrq'))) + rsrp = int(get_value('rsrp')) + rssi = int(get_value('rssi')) + sinr = int(get_value('sinr')) + + pci = int(get_value('pci')) + mode = int(get_value('mode')) + ulbandwidth = int(get_value('ulbandwidth')) + dlbandwidth = int(get_value('dlbandwidth')) + band = int(get_value('band')) + ulfrequency = int(get_value('ulfrequency')) + dlfrequency = int(get_value('dlfrequency')) + + print(f'{datetime.datetime.now().strftime("%H-%M-%S")} CELL={cell} RSRQ={rsrq} RSRP={rsrp} RSSI={rssi} SINR={sinr}') + data_frame = f'{rsrq},{rsrp},{rssi},{sinr},{cell}' + # print(frame) + sock.sendto(data_frame.encode('utf-8'), (udp_ip, udp_port)) + + time.sleep(cycle_time) diff --git a/examples/README.md b/examples/README.md index 5e8f77fc..dd958f96 100644 --- a/examples/README.md +++ b/examples/README.md @@ -15,9 +15,9 @@ Some examples also include Serial Studio project files (`*.json`) to simplify th - **Screenshot**: Example view in Serial Studio. ### 2. LTE modem -- **Description**: This example reads data of signal from LTE modem and transmits it over MQTT. +- **Description**: This example reads data of signal from LTE modem and transmits it over MQTT or UDP Socket. - **Contents**: - - **lte_mqtt.py**: Python script for parsing data of signal from LTE modem API and send it over MQTT. + - **lte_mqtt.py**: Python script for parsing data of signal from LTE modem API and send it over MQTT or UDP Socket. - **README.md**: Setup and usage instructions. - **Screenshot**: Example view in Serial Studio.