Skip to content

Instantly share code, notes, and snippets.

@marioboncz
Created December 27, 2019 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marioboncz/241caa9e93714589940156009441b9ca to your computer and use it in GitHub Desktop.
Save marioboncz/241caa9e93714589940156009441b9ca to your computer and use it in GitHub Desktop.
#include "mbed.h"
#include "NetworkInterface.h"
#include "CellularInterface.h"
#include "CellularDevice.h"
#include "TCPSocket.h"
#include "common_functions.h"
#include "CellularNonIPSocket.h"
#include "CellularDevice.h"
#include "CellularLog.h"
#include "ThisThread.h"
#include "stats_report.h"
#include "mbed_error.h"
#include "SeggerRTT/SEGGER_RTT.h"
#include "retarget_segger_rtt.h"
#define MBED_TRACE_MAX_LEVEL TRACE_LEVEL_DEBUG
#define MBED_CONF_MBED_TRACE_ENABLE 1
#include "mbed-trace/mbed_trace.h"
#define MBED_CONF_RETARGET_STDIO_ALT 1
#define MBED_CONF_IOTSHIELD_CATM1_RESET P0_14
#define MBED_CONF_IOTSHIELD_CATM1_PWRKEY P0_15
#define RAK8212_ITRACKER_LED P0_12
#define UDP 0
#define TCP 1
#define NONIP 2
DigitalOut led1(RAK8212_ITRACKER_LED);
#define SLEEP_TIME 1000 // (msec)
#define PRINT_AFTER_N_LOOPS 20
NetworkInterface* nInterface;
nsapi_error_t do_connect()
{
nsapi_error_t retcode = NSAPI_ERROR_OK;
uint8_t retry_counter = 0;
printf("do_connect function \n");
while (nInterface->get_connection_status() != NSAPI_STATUS_GLOBAL_UP) {
printf(" nInterface->connect: before\n");
retcode = nInterface->connect();
printf(" do_connect after\n");
if (retcode == NSAPI_ERROR_AUTH_FAILURE) {
printf("\n\nAuthentication Failure. Exiting application\n");
} else if (retcode == NSAPI_ERROR_OK) {
printf("\n\nConnection Established.\n");
} else if (retry_counter > 3) {
printf("\n\nFatal connection failure: %d\n", retcode);
} else {
printf("\n\nCouldn't connect: %d, will retry\n", retcode);
retry_counter++;
continue;
}
break;
}
printf("\n\n do_connect end\n");
return retcode;
}
static void print_trace_to_segger(const char* value) {
printf(value);
}
// main() runs in its own thread in the OS
int main()
{
mbed_trace_init();
mbed_trace_print_function_set(print_trace_to_segger);
mbed_trace_config_set(TRACE_ACTIVE_LEVEL_ALL);
printf("v16 \r\n");
//// V1
CellularContext * context = CellularContext::get_default_instance();
CellularDevice* device = CellularDevice::get_default_instance(); //context->get_device(); //CellularDevice::get_default_instance();
nInterface = (NetworkInterface*) context;
// MBED_ASSERT(nInterface);
//printf("get_connection_status %d", nInterface->get_connection_status());
//printf("get_mac_address %s", nInterface->get_mac_address());
//printf("get_ip_address %s", nInterface->get_ip_address());
do_connect();
printf("after connect");
int count = 0;
while (true) {
// Blink LED and wait 0.5 seconds
// led1 = !led1;
printf("loop1 \n");
rtos::ThisThread::sleep_for(SLEEP_TIME);
if ((0 == count) || (PRINT_AFTER_N_LOOPS == count)) {
// Following the main thread wait, report on the current system status
// sys_state.report_state();
count = 0;
fflush(stdout);
}
++count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment