Skip to content

Commit

Permalink
Moved packet parsing logic to the radio class and increase mqtt packe…
Browse files Browse the repository at this point in the history
…t size to 1000 bytes
  • Loading branch information
4m1g0 committed Jan 6, 2020
1 parent 8b62f6e commit 2ce80eb
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 107 deletions.
98 changes: 7 additions & 91 deletions FossaGroundStation/FossaGroundStation.ino
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
@dev_4m1g0 https://twitter.com/dev_4m1g0
@g4lile0 https://twitter.com/G4lile0
====================================================
IMPORTANT:
- Change libraries/PubSubClient/src/PubSubClient.h
#define MQTT_MAX_PACKET_SIZE 1000
**************************************************************************/

#if defined(ARDUINO) && ARDUINO >= 100
Expand All @@ -69,7 +74,7 @@

ConfigManager configManager;
MQTT_Client mqtt(configManager);
Radio radio(configManager);
Radio radio(configManager, mqtt);

const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 0; // 3600; // 3600 for Spain
Expand Down Expand Up @@ -266,96 +271,7 @@ void loop() {
radio.enableInterrupt();
}

uint8_t *respOptData; // Warning: this needs to be freed
size_t respLen = 0;
uint8_t functionId = 0;
uint8_t error = radio.listen(respOptData, respLen, functionId);
if (!error)
processReceivedFrame(functionId, respOptData, respLen);

delete[] respOptData;
respOptData = nullptr;
}

void processReceivedFrame(uint8_t functionId, uint8_t *respOptData, size_t respLen) {
switch(functionId) {
case RESP_PONG:
Serial.println(F("Pong!"));
mqtt.sendPong();
break;

case RESP_SYSTEM_INFO:
Serial.println(F("System info:"));

Serial.print(F("batteryChargingVoltage = "));
status.sysInfo.batteryChargingVoltage = FCP_Get_Battery_Charging_Voltage(respOptData);
Serial.println(FCP_Get_Battery_Charging_Voltage(respOptData));

Serial.print(F("batteryChargingCurrent = "));
status.sysInfo.batteryChargingCurrent = (FCP_Get_Battery_Charging_Current(respOptData), 4);
Serial.println(FCP_Get_Battery_Charging_Current(respOptData), 4);

Serial.print(F("batteryVoltage = "));
status.sysInfo.batteryVoltage=FCP_Get_Battery_Voltage(respOptData);
Serial.println(FCP_Get_Battery_Voltage(respOptData));

Serial.print(F("solarCellAVoltage = "));
status.sysInfo.solarCellAVoltage= FCP_Get_Solar_Cell_Voltage(0, respOptData);
Serial.println(FCP_Get_Solar_Cell_Voltage(0, respOptData));

Serial.print(F("solarCellBVoltage = "));
status.sysInfo.solarCellBVoltage= FCP_Get_Solar_Cell_Voltage(1, respOptData);
Serial.println(FCP_Get_Solar_Cell_Voltage(1, respOptData));

Serial.print(F("solarCellCVoltage = "));
status.sysInfo.solarCellCVoltage= FCP_Get_Solar_Cell_Voltage(2, respOptData);
Serial.println(FCP_Get_Solar_Cell_Voltage(2, respOptData));

Serial.print(F("batteryTemperature = "));
status.sysInfo.batteryTemperature=FCP_Get_Battery_Temperature(respOptData);
Serial.println(FCP_Get_Battery_Temperature(respOptData));

Serial.print(F("boardTemperature = "));
status.sysInfo.boardTemperature=FCP_Get_Board_Temperature(respOptData);
Serial.println(FCP_Get_Board_Temperature(respOptData));

Serial.print(F("mcuTemperature = "));
status.sysInfo.mcuTemperature =FCP_Get_MCU_Temperature(respOptData);
Serial.println(FCP_Get_MCU_Temperature(respOptData));

Serial.print(F("resetCounter = "));
status.sysInfo.resetCounter=FCP_Get_Reset_Counter(respOptData);
Serial.println(FCP_Get_Reset_Counter(respOptData));

Serial.print(F("powerConfig = 0b"));
status.sysInfo.powerConfig=FCP_Get_Power_Configuration(respOptData);
Serial.println(FCP_Get_Power_Configuration(respOptData), BIN);

mqtt.sendSystemInfo();
break;

case RESP_LAST_PACKET_INFO:
Serial.println(F("Last packet info:"));

Serial.print(F("SNR = "));
Serial.print(respOptData[0] / 4.0);
Serial.println(F(" dB"));

Serial.print(F("RSSI = "));
Serial.print(respOptData[1] / -2.0);
Serial.println(F(" dBm"));
break;

case RESP_REPEATED_MESSAGE:
Serial.println(F("Got repeated message:"));
Serial.println((char*)respOptData);
mqtt.sendMessage((char*)respOptData,respLen);
break;

default:
Serial.println(F("Unknown function ID!"));
break;
}
radio.listen();
}

void switchTestmode() {
Expand Down
13 changes: 7 additions & 6 deletions FossaGroundStation/src/Mqtt/MQTT_Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ void MQTT_Client::reconnect() {
Serial.println ("If this is taking more than expected, connect to the config panel on the ip: " + WiFi.localIP().toString() + " to review the MQTT connection credentials.");
if (connect(clientId.c_str(), configManager.getMqttUser(), configManager.getMqttPass(), buildTopic(topicStatus).c_str(), 2, false, "0")) {
Serial.println("connected");
subscribeToAll();
sendWelcome();
}
else {
Expand Down Expand Up @@ -98,7 +99,7 @@ void MQTT_Client::sendWelcome() {
publish(buildTopic(topicWelcome).c_str(), buffer,n );
}

void MQTT_Client::sendSystemInfo(void) {
void MQTT_Client::sendSystemInfo() {
time_t now;
time(&now);
const size_t capacity = JSON_ARRAY_SIZE(2) + JSON_OBJECT_SIZE(19);
Expand Down Expand Up @@ -130,7 +131,7 @@ void MQTT_Client::sendSystemInfo(void) {
publish(buildTopic(topicSysInfo).c_str(), buffer,n );
}

void MQTT_Client::sendPong(void) {
void MQTT_Client::sendPong() {
time_t now;
time(&now);
const size_t capacity = JSON_ARRAY_SIZE(2) + JSON_OBJECT_SIZE(7);
Expand All @@ -149,7 +150,7 @@ void MQTT_Client::sendPong(void) {
serializeJson(doc, buffer);
size_t n = serializeJson(doc, buffer);

publish(buildTopic(topicPong).c_str(), buffer,n );
publish(buildTopic(topicPong).c_str(), buffer, n);
}

void MQTT_Client::sendMessage(char* frame, size_t respLen) {
Expand Down Expand Up @@ -212,10 +213,10 @@ void MQTT_Client::sendMessage(char* frame, size_t respLen) {
}
}

void MQTT_Client::sendRawPacket(void) {
void MQTT_Client::sendRawPacket(String packet) {
time_t now;
time(&now);
const size_t capacity = JSON_ARRAY_SIZE(2) + JSON_OBJECT_SIZE(7);
const size_t capacity = JSON_ARRAY_SIZE(2) + JSON_OBJECT_SIZE(10);
DynamicJsonDocument doc(capacity);
doc["station"] = configManager.getThingName();
JsonArray station_location = doc.createNestedArray("station_location");
Expand All @@ -225,7 +226,7 @@ void MQTT_Client::sendRawPacket(void) {
doc["snr"] = status.lastPacketInfo.snr;
doc["frequency_error"] = status.lastPacketInfo.frequencyerror;
doc["unix_GS_time"] = now;
doc["pong"] = 1;
doc["data"] = packet.c_str();
serializeJson(doc, Serial);
char buffer[256];
serializeJson(doc, buffer);
Expand Down
2 changes: 1 addition & 1 deletion FossaGroundStation/src/Mqtt/MQTT_Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class MQTT_Client : public PubSubClient {
void sendSystemInfo();
void sendPong();
void sendMessage(char* frame, size_t respLen);
void sendRawPacket();
void sendRawPacket(String packet);

protected:
#ifdef SECURE_MQTT
Expand Down
103 changes: 98 additions & 5 deletions FossaGroundStation/src/Radio/Radio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "Radio.h"
#include "../Comms/Comms.h"
#include <base64.h>

char callsign[] = "FOSSASAT-1";
bool received = false;
Expand All @@ -35,8 +36,9 @@ bool eInterrupt = true;
#define SYNC_WORD_7X 0xFF // sync word when using SX127x
#define SYNC_WORD_6X 0x0F0F // SX126x

Radio::Radio(ConfigManager& x)
Radio::Radio(ConfigManager& x, MQTT_Client& mqtt)
: configManager(x)
, mqtt(mqtt)
{

}
Expand Down Expand Up @@ -204,7 +206,7 @@ void Radio::requestRetransmit(char* data) {
}
}

uint8_t Radio::listen(uint8_t *&respOptData, size_t &respLen, uint8_t &functionId){
uint8_t Radio::listen() {
// check if the flag is set (received interruption)
if(!received)
return 1;
Expand All @@ -216,7 +218,7 @@ uint8_t Radio::listen(uint8_t *&respOptData, size_t &respLen, uint8_t &function
// reset flag
received = false;

respLen = 0;
size_t respLen = 0;
uint8_t* respFrame = 0;
int state = 0;
// read received data
Expand All @@ -240,12 +242,12 @@ uint8_t Radio::listen(uint8_t *&respOptData, size_t &respLen, uint8_t &function
}

// get function ID
functionId = FCP_Get_FunctionID(callsign, respFrame, respLen);
uint8_t functionId = FCP_Get_FunctionID(callsign, respFrame, respLen);
Serial.print(F("Function ID: 0x"));
Serial.println(functionId, HEX);

// check optional data
respOptData = nullptr;
uint8_t *respOptData = nullptr;
uint8_t respOptDataLen = FCP_Get_OptData_Length(callsign, respFrame, respLen);
Serial.print(F("Optional data ("));
Serial.print(respOptDataLen);
Expand All @@ -257,6 +259,11 @@ uint8_t Radio::listen(uint8_t *&respOptData, size_t &respLen, uint8_t &function
PRINT_BUFF(respFrame, respLen);
}

String encoded = base64::encode(respFrame, respLen);
mqtt.sendRawPacket(encoded);

delete[] respFrame;

struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
Expand Down Expand Up @@ -302,6 +309,11 @@ uint8_t Radio::listen(uint8_t *&respOptData, size_t &respLen, uint8_t &function
// enable interrupt service routine
enableInterrupt();

if (state == ERR_NONE) {
processReceivedFrame(functionId, respOptData, respLen);
}
delete[] respOptData;

if (state == ERR_NONE) {
return 0;
} else if (state == ERR_CRC_MISMATCH) {
Expand All @@ -315,3 +327,84 @@ uint8_t Radio::listen(uint8_t *&respOptData, size_t &respLen, uint8_t &function
return 3;
}
}

void Radio::processReceivedFrame(uint8_t functionId, uint8_t *respOptData, size_t respLen) {
switch(functionId) {
case RESP_PONG:
Serial.println(F("Pong!"));
mqtt.sendPong();
break;

case RESP_SYSTEM_INFO:
Serial.println(F("System info:"));

Serial.print(F("batteryChargingVoltage = "));
status.sysInfo.batteryChargingVoltage = FCP_Get_Battery_Charging_Voltage(respOptData);
Serial.println(FCP_Get_Battery_Charging_Voltage(respOptData));

Serial.print(F("batteryChargingCurrent = "));
status.sysInfo.batteryChargingCurrent = (FCP_Get_Battery_Charging_Current(respOptData), 4);
Serial.println(FCP_Get_Battery_Charging_Current(respOptData), 4);

Serial.print(F("batteryVoltage = "));
status.sysInfo.batteryVoltage=FCP_Get_Battery_Voltage(respOptData);
Serial.println(FCP_Get_Battery_Voltage(respOptData));

Serial.print(F("solarCellAVoltage = "));
status.sysInfo.solarCellAVoltage= FCP_Get_Solar_Cell_Voltage(0, respOptData);
Serial.println(FCP_Get_Solar_Cell_Voltage(0, respOptData));

Serial.print(F("solarCellBVoltage = "));
status.sysInfo.solarCellBVoltage= FCP_Get_Solar_Cell_Voltage(1, respOptData);
Serial.println(FCP_Get_Solar_Cell_Voltage(1, respOptData));

Serial.print(F("solarCellCVoltage = "));
status.sysInfo.solarCellCVoltage= FCP_Get_Solar_Cell_Voltage(2, respOptData);
Serial.println(FCP_Get_Solar_Cell_Voltage(2, respOptData));

Serial.print(F("batteryTemperature = "));
status.sysInfo.batteryTemperature=FCP_Get_Battery_Temperature(respOptData);
Serial.println(FCP_Get_Battery_Temperature(respOptData));

Serial.print(F("boardTemperature = "));
status.sysInfo.boardTemperature=FCP_Get_Board_Temperature(respOptData);
Serial.println(FCP_Get_Board_Temperature(respOptData));

Serial.print(F("mcuTemperature = "));
status.sysInfo.mcuTemperature =FCP_Get_MCU_Temperature(respOptData);
Serial.println(FCP_Get_MCU_Temperature(respOptData));

Serial.print(F("resetCounter = "));
status.sysInfo.resetCounter=FCP_Get_Reset_Counter(respOptData);
Serial.println(FCP_Get_Reset_Counter(respOptData));

Serial.print(F("powerConfig = 0b"));
status.sysInfo.powerConfig=FCP_Get_Power_Configuration(respOptData);
Serial.println(FCP_Get_Power_Configuration(respOptData), BIN);

mqtt.sendSystemInfo();
break;

case RESP_LAST_PACKET_INFO:
Serial.println(F("Last packet info:"));

Serial.print(F("SNR = "));
Serial.print(respOptData[0] / 4.0);
Serial.println(F(" dB"));

Serial.print(F("RSSI = "));
Serial.print(respOptData[1] / -2.0);
Serial.println(F(" dBm"));
break;

case RESP_REPEATED_MESSAGE:
Serial.println(F("Got repeated message:"));
Serial.println((char*)respOptData);
mqtt.sendMessage((char*)respOptData,respLen);
break;

default:
Serial.println(F("Unknown function ID!"));
break;
}
}
7 changes: 5 additions & 2 deletions FossaGroundStation/src/Radio/Radio.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,28 @@
#include <RadioLib.h>
#include "../ConfigManager/ConfigManager.h"
#include "../Status.h"
#include "../Mqtt/MQTT_Client.h"

extern Status status;

class Radio {
public:
Radio(ConfigManager& configManager);
Radio(ConfigManager& x, MQTT_Client& mqtt);
void init();
void sendPing();
void requestInfo();
void requestPacketInfo();
void requestRetransmit(char* data);
void enableInterrupt();
void disableInterrupt();
uint8_t listen(uint8_t *&respOptData, size_t &respLen, uint8_t &functionId);
uint8_t listen();
bool isReady() { return ready; }

private:
PhysicalLayer* lora;
ConfigManager& configManager;
MQTT_Client& mqtt;
void processReceivedFrame(uint8_t functionId, uint8_t *respOptData, size_t respLen);

static void setFlag();
int sendFrame(uint8_t functionId, const char* data = "");
Expand Down
2 changes: 1 addition & 1 deletion FossaGroundStation/src/Status.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct PacketInfo {
};

struct Status {
const uint32_t version = 2001060; // version year month day release
const uint32_t version = 2001061; // version year month day release
bool mqtt_connected = false;
SysInfo sysInfo;
PacketInfo lastPacketInfo;
Expand Down
2 changes: 1 addition & 1 deletion lib/pubsubclient/src/PubSubClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

// MQTT_MAX_PACKET_SIZE : Maximum packet size
#ifndef MQTT_MAX_PACKET_SIZE
#define MQTT_MAX_PACKET_SIZE 128
#define MQTT_MAX_PACKET_SIZE 1000
#endif

// MQTT_KEEPALIVE : keepAlive interval in Seconds
Expand Down
Loading

0 comments on commit 2ce80eb

Please sign in to comment.