Skip to content

Commit

Permalink
Merge pull request #74 from SQ2CPA/main
Browse files Browse the repository at this point in the history
Received packets list
  • Loading branch information
richonguzman authored Apr 13, 2024
2 parents 8e505df + 7147316 commit 0695e66
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 8 deletions.
40 changes: 35 additions & 5 deletions data_embed/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@
class="navbar bg-body-secondary shadow-sm border-bottom sticky-top navbar-expand-lg"
>
<div class="container">
<a class="navbar-brand"
<a class="navbar-brand" href=""
>CA2RXU's LoRa</a
>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarColor01" aria-controls="navbarColor01" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarColor01">
<ul class="navbar-nav me-auto">
<li class="nav-item">
<a class="nav-link" href="/">
Configuration
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/update">
Update <small>OTA</small>
Expand All @@ -40,19 +45,44 @@
<a class="dropdown-item" href="#" id="reboot">Reboot</a>
</div>
</li>
<!-- <li class="nav-item">
<a class="nav-link" href="#" id="received-packets">
<li class="nav-item">
<a class="nav-link" href="/received-packets">
Received packets
</a>
</li> -->
</li>
</ul>
<div class="d-flex">
<button class="btn btn-success my-2 my-sm-0" type="submit">Save</button>
</div>
</div>
</div>
</nav>
<div class="container">

<div class="container d-none" id="received-packets">
<div class="row my-5 d-flex align-items-top">
<div class="col-12">
<h3>Last 20 received packets list</h3>
<table class="table mt-4">
<thead>
<tr>
<th scope="col">Time*</th>
<th scope="col">Frame</th>
<th scope="col">RSSI</th>
<th scope="col">SNR</th>
</tr>
</thead>
<tbody>

</tbody>
</table>
<span>List refresh automatically every 15 seconds.</span><br>
<small>* - For now time is measured from board boot up.</small>
</div>
</div>
<hr />
</div>

<div class="container" id="configuration">
<main>
<div class="col-10 my-5 mx-auto">
<div class="row my-5 d-flex align-items-top">
Expand Down
57 changes: 56 additions & 1 deletion data_embed/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,59 @@ form.addEventListener("submit", async (event) => {
setTimeout(checkConnection, 2000);
});

fetchSettings();
fetchSettings();

function loadReceivedPackets(packets) {
if (packets) {
document.querySelector('#received-packets tbody').innerHTML = '';

const container = document.querySelector("#received-packets tbody");

container.innerHTML = '';

const date = new Date();

packets.forEach((packet) => {
const element = document.createElement("tr");

date.setTime(packet.millis);

const p = date.toUTCString().split(' ')

element.innerHTML = `
<td>${p[p.length-2]}</td>
<td>${packet.packet}</td>
<td>${packet.RSSI}</td>
<td>${packet.SNR}</td>
`;

container.appendChild(element);
})
}

setTimeout(fetchReceivedPackets, 15000);
}

function fetchReceivedPackets() {
fetch("/received-packets.json")
.then((response) => response.json())
.then((packets) => {
loadReceivedPackets(packets);
})
.catch((err) => {
console.error(err);

console.error(`Failed to load received packets`);
});
}

document.querySelector('a[href="/received-packets"]').addEventListener('click', function (e) {
e.preventDefault();

document.getElementById('received-packets').classList.remove('d-none');
document.getElementById('configuration').classList.add('d-none');

document.querySelector('button[type=submit]').remove();

fetchReceivedPackets();
})
2 changes: 2 additions & 0 deletions src/LoRa_APRS_iGate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ std::vector<String> lastHeardStation_temp;
std::vector<String> packetBuffer;
std::vector<String> packetBuffer_temp;

std::vector<ReceivedPacket> receivedPackets;

String firstLine, secondLine, thirdLine, fourthLine, fifthLine, sixthLine, seventhLine, iGateBeaconPacket, iGateLoRaBeaconPacket;

void setup() {
Expand Down
17 changes: 17 additions & 0 deletions src/lora_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

extern Configuration Config;

extern std::vector<ReceivedPacket> receivedPackets;

bool transmissionFlag = true;
bool ignorePacket = false;
bool operationDone = true;
Expand Down Expand Up @@ -169,6 +171,21 @@ namespace LoRa_Utils {
freqError = radio.getFrequencyError();
Utils::println("<--- LoRa Packet Rx : " + loraPacket);
Utils::println("(RSSI:" + String(rssi) + " / SNR:" + String(snr) + " / FreqErr:" + String(freqError) + ")");

if (!Config.lowPowerMode) {
ReceivedPacket receivedPacket;
receivedPacket.millis = millis();
receivedPacket.packet = loraPacket.substring(3);
receivedPacket.RSSI = rssi;
receivedPacket.SNR = snr;

if (receivedPackets.size() >= 20) {
receivedPackets.erase(receivedPackets.begin());
}

receivedPackets.push_back(receivedPacket);
}

if (Config.syslog.active && WiFi.status() == WL_CONNECTED) {
SYSLOG_Utils::log("Rx", loraPacket, rssi, snr, freqError);
}
Expand Down
7 changes: 7 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@

#include <Arduino.h>

class ReceivedPacket {
public:
long millis;
String packet;
int RSSI;
float SNR;
};

namespace Utils {

Expand Down
26 changes: 24 additions & 2 deletions src/web_utils.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#include <ArduinoJson.h>
#include "configuration.h"
#include "ota_utils.h"
#include "web_utils.h"
#include "utils.h"

extern Configuration Config;
extern uint32_t lastBeaconTx;
extern Configuration Config;

extern uint32_t lastBeaconTx;
extern std::vector<ReceivedPacket> receivedPackets;

extern const char web_index_html[] asm("_binary_data_embed_index_html_gz_start");
extern const char web_index_html_end[] asm("_binary_data_embed_index_html_gz_end");
Expand Down Expand Up @@ -57,6 +61,23 @@ namespace WEB_Utils {
request->send(200, "application/json", fileContent);
}

void handleReceivedPackets(AsyncWebServerRequest *request) {
StaticJsonDocument<1536> data;

for (int i = 0; i < receivedPackets.size(); i++) {
data[i]["millis"] = receivedPackets[i].millis;
data[i]["packet"] = receivedPackets[i].packet;
data[i]["RSSI"] = receivedPackets[i].RSSI;
data[i]["SNR"] = receivedPackets[i].SNR;
}

String buffer;

serializeJson(data, buffer);

request->send(200, "application/json", buffer);
}

void handleWriteConfiguration(AsyncWebServerRequest *request) {
Serial.println("Got new config from www");

Expand Down Expand Up @@ -214,6 +235,7 @@ namespace WEB_Utils {
void setup() {
server.on("/", HTTP_GET, handleHome);
server.on("/status", HTTP_GET, handleStatus);
server.on("/received-packets.json", HTTP_GET, handleReceivedPackets);
server.on("/configuration.json", HTTP_GET, handleReadConfiguration);
server.on("/configuration.json", HTTP_POST, handleWriteConfiguration);
server.on("/action", HTTP_POST, handleAction);
Expand Down

0 comments on commit 0695e66

Please sign in to comment.