Skip to main content

START with this before doing ABP http://skovholm.com/node/13

If you want to use the LMIC and the cubemx you just have to change some lines in the main.c

Thanks to Piotr Siwy to inspire me.

Here you find the file set /sites/default/files/bluepill_ABP.zip

if you just want to change the OTAA example from the video the follow these few steps.

You have to set the keys in

Then you have to put in some lines in the initfunc

And you have to move up readsensor and the reportfunc

 

job done ...it works very good on a bluepill ....

If you want to copy paste ...you can do it from code underneath

 


/* USER CODE BEGIN PV */
// application router ID (LSBF) < ------- IMPORTANT
static const u1_t APPEUI[8] = { 0x5F, 0x7D, 0x02, 0xD0, 0x7E, 0xD5, 0xB3, 0x70 };
// unique device ID (LSBF) < ------- IMPORTANT
static const u1_t DEVEUI[8] = { 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15 };
// device-specific AES key (derived from device EUI)
static const u1_t DEVKEY[16] = { 0x17, 0xD1, 0x31, 0x96, 0xDF, 0x02, 0x9C, 0x15, 0x45, 0x0B, 0x5C, 0x50, 0xA0, 0xDD, 0xD9, 0x55 };
// ABP
static const u1_t NWKSKEY[16] = { 0xB9, 0x08, 0xC6, 0x34, 0x13, 0x24, 0x42, 0x31, 0xC1, 0x06, 0x2C, 0x15, 0x5F, 0xA4, 0x7C, 0x99 };
static const u1_t APPSKEY[16] = { 0x61, 0x87, 0x0A, 0x09, 0xCE, 0x1B, 0xA3, 0xC2, 0x92, 0x70, 0x36, 0xDE, 0xA1, 0x58, 0x61, 0xB4 };
static const u4_t DEVADDR = 0x260117A4;
// 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_USART1_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 10 seconds
os_setTimedCallback(j, os_getTime()+sec2osticks(5), 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);
LMIC_setupChannel(0, 868100000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
LMIC_setupChannel(1, 868300000, DR_RANGE_MAP(DR_SF12, DR_SF7B), BAND_CENTI); // g-band
LMIC_setupChannel(2, 868500000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
LMIC_setupChannel(3, 867100000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
LMIC_setupChannel(4, 867300000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
LMIC_setupChannel(5, 867500000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
LMIC_setupChannel(6, 867700000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
LMIC_setupChannel(7, 867900000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
LMIC_setupChannel(8, 868800000, DR_RANGE_MAP(DR_FSK, DR_FSK), BAND_MILLI); // g2-band
// 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_USART1_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 */
}

Tags