Skip to content

Commit

Permalink
update compile-able example
Browse files Browse the repository at this point in the history
  • Loading branch information
aldwinhermanudin committed Dec 8, 2023
1 parent 325fd68 commit a93b4cf
Show file tree
Hide file tree
Showing 25 changed files with 6,138 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@
[submodule "src/mbedos/test-deep-sleep/stm32customtargets"]
path = src/mbedos/test-deep-sleep/stm32customtargets
url = [email protected]:teapotlaboratories/stm32customtargets.git

[submodule "src/mbedos/test-bme688/mbed-os"]
path = src/mbedos/test-bme688/mbed-os
url = [email protected]:teapotlaboratories/mbed-os.git
[submodule "src/mbedos/test-bme688/stm32customtargets"]
path = src/mbedos/test-bme688/stm32customtargets
url = [email protected]:teapotlaboratories/stm32customtargets.git
206 changes: 206 additions & 0 deletions src/mbedos/test-bme688/Communication/LoRa.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
#include "LoRa.hpp"






static EventQueue LoRaQueue(MAX_NUMBER_OF_EVENTS *EVENTS_EVENT_SIZE);
using namespace events;
static STM32WL_LoRaRadio radioLoRa;



LoRa::LoRa() : _lorawan(radioLoRa), LoRaInternalThread(osPriorityNormal,OS_STACK_SIZE,nullptr,"LoRaThread")
{
}

void LoRa::start()
{
LoRaInternalThread.start(callback(this,&LoRa::_internalThread));
}

void LoRa::loraHandler(lorawan_event_t event)
{
switch (event) {
case CONNECTED:
printf("Connected to TTN\n\r");
break;
case DISCONNECTED:
LoRaQueue.break_dispatch();
printf("\r\n Disconnected Successfully \r\n");
break;
case TX_DONE:
printf("\r\n Message Sent to Network Server \r\n");
break;
case TX_TIMEOUT:
case TX_ERROR:
case TX_CRYPTO_ERROR:
case TX_SCHEDULING_ERROR:
printf("\r\n Transmission Error - EventCode = %d \r\n", event);
break;
case RX_DONE:
printf("\r\n Received message from Network Server \r\n");
this->receiveMessage();
break;
case RX_TIMEOUT:
case RX_ERROR:
printf("\r\n Error in reception - Code = %d \r\n", event);
break;
case JOIN_FAILURE:
printf("\r\n OTAA Failed - Check Keys \r\n");
break;
case UPLINK_REQUIRED:
printf("\r\n Uplink required by NS \r\n");
if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
this->sendMessage();
}
break;
default:
MBED_ASSERT("Unknown Event");
}
}


void LoRa::_internalThread()
{
setup_trace();

ThisThread::sleep_for(1000ms);
if (_lorawan.initialize(&LoRaQueue) != LORAWAN_STATUS_OK) {
printf("\r\n LoRa initialization failed! \r\n");
}

// prepare application callbacks
callbacks.events = mbed::callback(this,&LoRa::loraHandler);
_lorawan.add_app_callbacks(&callbacks);

// Set number of retries in case of CONFIRMED messages
if (_lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER)
!= LORAWAN_STATUS_OK) {
printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n");
}
printf("\r\n CONFIRMED message retries : %d \r\n",
CONFIRMED_MSG_RETRY_COUNTER);

// Enable adaptive data rate
if (_lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) {
printf("\r\n enable_adaptive_datarate failed! \r\n");
}

printf("\r\n Adaptive data rate (ADR) - Enabled \r\n");
retcode = _lorawan.connect();

if (retcode == LORAWAN_STATUS_OK ||
retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
} else {
printf("\r\n Connection error, code = %d \r\n", retcode);
}

printf("\r\n Connection - In Progress ...\r\n");

LoRaQueue.dispatch_forever();
}


void LoRa::sendMessage()
{

printf("send something!\n\r");
printf("Temperature:%2.1f *C\n\r",data.temperature);
printf("Humidity:%2.1f\n\r",data.humidity);
printf("CO2:%2.1f\n\r",data.CO2);
printf("CO2 Accuracy:%d\n\r",data.CO2Accuracy);
printf("IAQ:%2.1f\n\r",data.IAQ);
printf("IAQ Accuracy:%d",data.IAQAccuracy);
uint16_t packet_len;
int16_t retcode;
int32_t sensor_value=0;
uint8_t tx_buffer[30];

packet_len = sprintf((char *) tx_buffer, "%2.2f %2.2f %d %2.2f %2.2f",
data.temperature,data.humidity,data.IAQAccuracy,data.IAQ,data.CO2);


retcode = _lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len,
MSG_UNCONFIRMED_FLAG);

if (retcode < 0) {
retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n")
: printf("\r\n send() - Error code %d \r\n", retcode);

if (retcode == LORAWAN_STATUS_WOULD_BLOCK) {
//retry in 3 seconds
if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
LoRaQueue.call_in(3000ms, callback(this,&LoRa::sendMessage));
}
}
return;
}

printf("\r\n %d bytes scheduled for transmission \r\n", retcode);
memset(tx_buffer, 0, sizeof(tx_buffer));
}

void LoRa::receiveMessage()
{
printf("Decode etc\n\r");
}

void LoRa::sendGarbage()
{
data.temperature = 1;
data.humidity = 1;
data.CO2 = 1;
data.CO2Accuracy = 1;
data.IAQ = 1;
data.IAQAccuracy = 4;
printf("send something!\n\r");
printf("Temperature:%2.1f *C\n\r",data.temperature);
printf("Humidity:%2.1f\n\r",data.humidity);
printf("CO2:%2.1f\n\r",data.CO2);
printf("CO2 Accuracy:%d\n\r",data.CO2Accuracy);
printf("IAQ:%2.1f\n\r",data.IAQ);
printf("IAQ Accuracy:%d",data.IAQAccuracy);
uint16_t packet_len;
int16_t retcode;
int32_t sensor_value=0;
uint8_t tx_buffer[30];

packet_len = sprintf((char *) tx_buffer, "%2.2f %2.2f %d %2.2f %2.2f",
data.temperature,data.humidity,data.IAQAccuracy,data.IAQ,data.CO2);


retcode = _lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len,
MSG_UNCONFIRMED_FLAG);

if (retcode < 0) {
retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n")
: printf("\r\n send() - Error code %d \r\n", retcode);

if (retcode == LORAWAN_STATUS_WOULD_BLOCK) {
//retry in 3 seconds
if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
LoRaQueue.call_in(3000ms, callback(this,&LoRa::sendMessage));
}
}
return;
}

printf("\r\n %d bytes scheduled for transmission \r\n", retcode);
memset(tx_buffer, 0, sizeof(tx_buffer));
}

void LoRa::updateData(dataContainer newestData)
{
data = newestData;
}

void LoRa::sendData(dataContainer newestData)
{
LoRaQueue.call(callback(this,&LoRa::updateData),newestData);
LoRaQueue.call(callback(this,&LoRa::sendMessage));
}



47 changes: 47 additions & 0 deletions src/mbedos/test-bme688/Communication/LoRa.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifndef __LORA__H
#define __LORA__H

#include "BME688.hpp"
#include "mbed.h"
#include "lorawan/LoRaWANInterface.h"
#include "lorawan/system/lorawan_data_structures.h"
#include "events/EventQueue.h"
#include "trace_helper.h"
#include "lorawan/LoRaRadio.h"
#include "STM32WL_LoRaRadio.h"
#define MAX_NUMBER_OF_EVENTS 10
#define CONFIRMED_MSG_RETRY_COUNTER 3



class LoRa{

public:
LoRa();
void start();
void sendData(dataContainer newestData);
void updateData(dataContainer newestData);
void sendGarbage();
private:
dataContainer data;
void sendMessage();
void receiveMessage();
void loraHandler(lorawan_event_t event);
void _internalThread();
LoRaWANInterface _lorawan;
lorawan_app_callbacks_t callbacks;
Thread LoRaInternalThread;
lorawan_status_t retcode;
};











#endif
40 changes: 40 additions & 0 deletions src/mbedos/test-bme688/SensorFusion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "SensorFusion.hpp"

static EventQueue fusionQueue(5 *EVENTS_EVENT_SIZE);
SensorFusion::SensorFusion() : _iaqSensor(), _lora(),_fusionThread(osPriorityAboveNormal,OS_STACK_SIZE,nullptr,"SensorThread")
{

}




void SensorFusion::start()
{
_lora.start();
_iaqSensor.initialise();
_iaqSensor.doMeasurements();
_fusionThread.start(callback(this,&SensorFusion::_fusionThreadWorker));
fusionQueue.call_every(15s,callback(this,&SensorFusion::checkDataAndPost));
}


void SensorFusion::checkDataAndPost()
{
printf("Test\n\r");
if(_iaqSensor.isNewDataAvailable())
{
_lora.sendData(_iaqSensor.returnLatest());
}
else
{
//Replace with Error
_lora.sendGarbage();
}
}


void SensorFusion::_fusionThreadWorker()
{
fusionQueue.dispatch_forever();
}
29 changes: 29 additions & 0 deletions src/mbedos/test-bme688/SensorFusion.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef __SENSOR__FUSION__H
#define __SENSOR__FUSION__H
#include "LoRa.hpp"
#include "BME688.hpp"





class SensorFusion{

public:
SensorFusion();
void checkDataAndPost();
void start();
private:
BME688 _iaqSensor;
LoRa _lora;
Thread _fusionThread;
void _fusionThreadWorker();



};




#endif
Loading

0 comments on commit a93b4cf

Please sign in to comment.