/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "lmic.h"
#include "debug.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
// application router ID (LSBF) < ------- IMPORTANT
//static const u1_t APPEUI[8] = { 0xCA, 0x93, 0x03, 0xD0, 0x7E, 0xD5, 0xB3, 0x70 };
// unique device ID (LSBF) < ------- IMPORTANT
//static const u1_t DEVEUI[8] = { 0x38, 0x9B, 0xB1, 0x17, 0x60, 0xF2, 0x43, 0x00 };
// device-specific AES key (derived from device EUI)
//static const u1_t DEVKEY[16] = { 0x08, 0x2C, 0xB4, 0x55, 0xEA, 0xDB, 0xA8, 0xFF, 0x0F, 0x34, 0x7A, 0x69, 0x14, 0x53, 0xC6, 0x48 };
// ABP
// bluepill
static const u4_t DEVADDR = 0x26021008;
static const u1_t NWKSKEY[16] = { 0xE9, 0x10, 0xB1, 0xB3, 0xEC, 0xC4, 0x5C, 0x6B, 0x74, 0x73, 0x13, 0xE8, 0xB4, 0xF2, 0x3D, 0xCA };
static const u1_t APPSKEY[16] = { 0x66, 0xF4, 0x21, 0xE2, 0x69, 0xE5, 0x4B, 0x22, 0x31, 0xE5, 0x89, 0xFF, 0xDA, 0x28, 0x6D, 0x68 };
// ABP
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI1_Init(void);
static void MX_TIM4_Init(void);
static void MX_USART3_UART_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
// provide application router ID (8 bytes, LSBF)
void os_getArtEui (u1_t* buf) {
// memcpy(buf, APPEUI, 8);
}
// provide device ID (8 bytes, LSBF)
void os_getDevEui (u1_t* buf) {
//memcpy(buf, DEVEUI, 8);
}
// provide device key (16 bytes)
void os_getDevKey (u1_t* buf) {
//memcpy(buf, DEVKEY, 16);
}
void initsensor(){
// Here you init your sensors
}
u2_t readsensor(){
u2_t value = 0xDF; /// read from evrything ...make your own sensor
return value;
}
static osjob_t reportjob;
// report sensor value every minute
static void reportfunc (osjob_t* j) {
// read sensor
u2_t val = readsensor();
debug_val("val = ", val);
// prepare and schedule data for transmission
LMIC.frame[0] = val << 8;
LMIC.frame[1] = val;
LMIC_setTxData2(1, LMIC.frame, 2, 0); // (port 1, 2 bytes, unconfirmed)
// reschedule job in 60 seconds
os_setTimedCallback(j, os_getTime()+sec2osticks(10), reportfunc);
}
void initfunc (osjob_t* j) {
// intialize sensor hardware
initsensor();
// reset MAC state
LMIC_reset();
// start joining
//LMIC_startJoining();
// init done - onEvent() callback will be invoked...
// ABP
uint8_t appskey[sizeof(APPSKEY)];
uint8_t nwkskey[sizeof(NWKSKEY)];
memcpy(appskey, APPSKEY, sizeof(APPSKEY));
memcpy(nwkskey, NWKSKEY, sizeof(NWKSKEY));
LMIC_setSession (0x1, DEVADDR, nwkskey, appskey);
// Disable link check validation
LMIC_setLinkCheckMode(0);
// TTN uses SF9 for its RX2 window.
LMIC.dn2Dr = DR_SF9;
// Set data rate and transmit power for uplink (note: txpow seems to be ignored by the library)
LMIC_setDrTxpow(DR_SF7,14);
os_setTimedCallback(j, os_getTime(), reportfunc);
// ABP
}
//////////////////////////////////////////////////
// LMIC EVENT CALLBACK
//////////////////////////////////////////////////
void onEvent (ev_t ev) {
debug_event(ev);
switch(ev) {
// network joined, session established
case EV_JOINING:
debug_str("try joining\r\n");
break;
case EV_JOINED:
debug_led(1);
// kick-off periodic sensor job
reportfunc(&reportjob);
break;
case EV_JOIN_FAILED:
debug_str("join failed\r\n");
break;
case EV_SCAN_TIMEOUT:
debug_str("EV_SCAN_TIMEOUT\r\n");
break;
case EV_BEACON_FOUND:
debug_str("EV_BEACON_FOUND\r\n");
break;
case EV_BEACON_MISSED:
debug_str("EV_BEACON_MISSED\r\n");
break;
case EV_BEACON_TRACKED:
debug_str("EV_BEACON_TRACKED\r\n");
break;
case EV_RFU1:
debug_str("EV_RFU1\r\n");
break;
case EV_REJOIN_FAILED:
debug_str("EV_REJOIN_FAILED\r\n");
break;
case EV_TXCOMPLETE:
debug_str("EV_TXCOMPLETE (includes waiting for RX windows)\r\n");
if (LMIC.txrxFlags & TXRX_ACK)
debug_str("Received ack\r\n");
if (LMIC.dataLen) {
debug_str("Received ");
debug_str(LMIC.dataLen);
debug_str(" bytes of payload\r\n");
}
break;
case EV_LOST_TSYNC:
debug_str("EV_LOST_TSYNC\r\n");
break;
case EV_RESET:
debug_str("EV_RESET\r\n");
break;
case EV_RXCOMPLETE:
// data received in ping slot
debug_str("EV_RXCOMPLETE\r\n");
break;
case EV_LINK_DEAD:
debug_str("EV_LINK_DEAD\r\n");
break;
case EV_LINK_ALIVE:
debug_str("EV_LINK_ALIVE\r\n");
break;
default:
debug_str("Unknown event\r\n");
break;
}
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SPI1_Init();
MX_TIM4_Init();
MX_USART3_UART_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start_IT(&htim4); // <----------- change to your setup
__HAL_SPI_ENABLE(&hspi1); // <----------- change to your setup
osjob_t initjob;
// initialize runtime env
os_init();
// initialize debug library
debug_init();
// setup initial job
os_setCallback(&initjob, initfunc);
// execute scheduled jobs and events
os_runloop();
// (not reached)
return 0;
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}