this code is using for tests
Dependencies: pulga-lorawan-drv Si1133 BME280
Diff: main.cpp
- Revision:
- 62:e2116ce452eb
- Parent:
- 61:169e75494337
diff -r 169e75494337 -r e2116ce452eb main.cpp --- a/main.cpp Fri Dec 04 18:33:43 2020 +0000 +++ b/main.cpp Mon Mar 22 16:39:31 2021 +0000 @@ -36,12 +36,17 @@ // If longer messages are used, these buffers must be changed accordingly. //uint8_t tx_buffer[1000]; uint8_t rx_buffer[30]; -uint8_t tx_buffer[1000] = {0xd8, 0x60, 0x84, 0x43, 0xa1, 0x01, 0x0a, 0xa1, 0x05, 0x4c, 0x13, 0x28, 0x18, 0x7b, 0x01, 0xa1, 0x26, 0xca, 0xf7, 0x8e, 0x55, 0x51, 0x58, 0x6f, 0xa8, 0x00, 0x82, 0x64, 0xcf, 0xb0, 0x5e, 0x3d, 0x48, 0x7e, 0x30, 0x2f, 0x4c, 0xc3, 0x37, 0x6b, 0x4d, 0xdb, 0xb6, 0xa9, 0x1b, 0xc9, 0x4c, 0xcc, 0x8e, 0x92, 0x39, 0x8e, 0x4f, 0xe9, 0x3d, 0xb0, 0xa7, 0x48, 0x00, 0xfe, 0x38, 0xa7, 0xcd, 0x57, 0x7e, 0x04, 0x65, 0xb8, 0x2b, 0xf8, 0xc3, 0xbe, 0x33, 0x17, 0xf1, 0xe1, 0xfa, 0x88, 0x7d, 0x3b, 0xd4, 0xa0, 0xce, 0xed, 0x84, 0xed, 0x83, 0xdc, 0x57, 0xcf, 0xe8, 0x5b, 0x7e, 0xc0, 0x5c, 0x6b, 0xbd, 0xa1, 0xdb, 0x67, 0x5d, 0xae, 0xc4, 0x1f, 0x6d, 0x98, 0xf0, 0x97, 0xb6, 0x7a, 0x06, 0x55, 0x97, 0xe6, 0x11, 0x07, 0x52, 0x89, 0x73, 0x65, 0x28, 0x7c, 0xc8, 0x69, 0x39, 0x69, 0xde, 0x12, 0x71, 0x87, 0x43, 0xda, 0xd5, 0x11, 0xbf, 0x81, 0x83, 0x56, 0xa2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0xed, 0x33, 0xa0, 0x40}; + +auto timenow = chrono::system_clock::to_time_t(chrono::system_clock::now()); + +#define wait_time 0 + /* * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing */ #define TX_TIMER 10000 -#define DR DR_4 +#define DR DR_0 /** * Maximum number of events for the event queue. @@ -108,6 +113,83 @@ mbed::DigitalOut _actuated_led(P1_14,1); int msg_seq_number; + +///////////////////////////////////////////////////////////////////////// +//Samira +//define the channel +///////////////////////////////////////////////////////////////////////// + + +static loramac_channel_t testchannel[] = { + {0, {916800000, 0, {(DR_5 << 4) | DR_0}, 1}}, + {1, {917000000, 0, {(DR_5 << 4) | DR_0}, 1}}, + {2, {917200000, 0, {(DR_5 << 4) | DR_0}, 1}}, + {3, {917400000, 0, {(DR_5 << 4) | DR_0}, 0}}, + {4, {917600000, 0, {(DR_5 << 4) | DR_0}, 0}}, + {5, {917800000, 0, {(DR_5 << 4) | DR_0}, 0}}, + {6, {918000000, 0, {(DR_5 << 4) | DR_0}, 0}}, + {7, {918200000, 0, {(DR_5 << 4) | DR_0}, 0}} +}; + +/* +To set the channel - execute the following code before calling lorawan.connect() +*/ + +void LoRa_SetChannelPlan() { + lorawan_channelplan_t channelPlan {}; + loramac_channel_t channels[8]; + channelPlan.channels = (loramac_channel_t*) channels; + + //only activate the first three channels, up and downlink . + channels[0] = testchannel[0]; + channels[1] = testchannel[1]; + channels[2] = testchannel[2]; + + channelPlan.nb_channels = 3; + + //remove previously set channel plan + if(lorawan.remove_channel_plan() == LORAWAN_STATUS_OK) { + printf("[+] Removing old channels okay\n"); + } else { + printf("[-] Removing old channels failed\n"); + + } + if (lorawan.set_channel_plan(channelPlan ) == LORAWAN_STATUS_OK) { + printf("[+] Setting TTN channels okay\n"); + } else { + printf("[-] Failed to set TTN channels! Debug return code.\n"); + } +} + + +/* +To print the channel After the CONNECTED event in the callback in the callback handler +*/ +void LoRa_PrintChannels() { + /* print list of all channel frequencies */ + lorawan_channelplan_t channelPlan {}; + // static loramac_channel_t channelbuf[10]; + // channelPlan.channels = channelbuf; + if (lorawan.get_channel_plan(channelPlan) == LORAWAN_STATUS_OK) { + for (uint8_t i = 0; i < channelPlan.nb_channels; i++) { + loramac_channel_t chan = channelPlan.channels[i]; + printf("CHAN %d ID %d FREQ %lu RX1FREQ %lu Band %d DR min %d max %d\n", + (int) i, (int) chan.id, chan.ch_param.frequency, + chan.ch_param.rx1_frequency, (int) chan.ch_param.band, + (int) chan.ch_param.dr_range.fields.min, + (int) chan.ch_param.dr_range.fields.max); + } + } else { + printf("COULD NOT GET CHANNEL PLAN\n"); + } +} + +///////////////////////////////////////////////////////////////////////// +///////////////////////////////////////////////////////////////////////// + + + + int main(void) { // setup tracing @@ -162,8 +244,12 @@ + //samira - to set a channel plan + //LoRa_SetChannelPlan(); + retcode = lorawan.connect(); + if (retcode == LORAWAN_STATUS_OK || retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { @@ -175,7 +261,43 @@ printf("\r\n Connection - In Progress ...\r\n"); _actuated_led =0; + + /* set_time(1256729737); // Set RTC time to Wed, 28 Oct 2009 11:35:37 + + + time_t seconds = time(NULL); + + //printf("Time as seconds since January 1, 1970 = %u\n", (unsigned int)seconds); + + //printf("Time as a basic string = %s", ctime(&seconds)); + + char time_buffer[32]; + strftime(time_buffer, 32, "%I:%M %p\n", localtime(&seconds)); + printf("Time as a custom formatted string = %s", time_buffer); + + + */ +/* testing how wait works. it is for seconds + //samira-time + timenow = chrono::system_clock::to_time_t(chrono::system_clock::now()); + + cout << ctime(&timenow) << endl; + wait (1); + timenow = chrono::system_clock::to_time_t(chrono::system_clock::now()); + cout << "wait was 1"<<ctime(&timenow) << endl; + wait (20); + timenow = chrono::system_clock::to_time_t(chrono::system_clock::now()); + cout << "wait was 2"<<ctime(&timenow) << endl; + wait (30); + timenow = chrono::system_clock::to_time_t(chrono::system_clock::now()); + cout << "wait was 3"<<ctime(&timenow) << endl; + wait (40); + timenow = chrono::system_clock::to_time_t(chrono::system_clock::now()); + cout << "wait was 4"<<ctime(&timenow) << endl; + */ // make your event queue dispatching events forever + + ev_queue.dispatch_forever(); return 0; @@ -196,6 +318,7 @@ */ static void send_message() { + printf("------------------------------------------\n"); uint16_t packet_len; int16_t retcode; int32_t sensor_value; @@ -206,9 +329,10 @@ // packet_len = sprintf((char *) tx_buffer, "Ø`C¡¡L({¡&Ê÷UQXo¨dÏ°^=H~0/LÃ7kMÛ¶©ÉLÌ9Oé=°§Hþ8§ÍW~e¸+øþ3ñáú};Ô ÎííÜWÏè[~À\k½¡Ûg]®Ämð¶zUæRse(|Èi9iÞqCÚÕ¿V¢8P3Æ*Cë¿<É|¦Uí3 @", msg_seq_number); - // packet_len = sprintf((char *) tx_buffer, "This is message with seq_number = %d \n", msg_seq_number); + //packet_len = sprintf((char *) tx_buffer, "This is message with seq_number = %d \n", msg_seq_number); + packet_len = sprintf((char *) tx_buffer, "P"); - packet_len = 162; + // packet_len = 162; printf("packet_len = %d\n", packet_len); @@ -216,12 +340,12 @@ // std::cout<<tx_buffer<<endl; print_lora_tx_buffer_hex(packet_len); - //samira-time + //////samira-time auto timenow = chrono::system_clock::to_time_t(chrono::system_clock::now()); cout << ctime(&timenow) << endl; - + ///// retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, MSG_UNCONFIRMED_FLAG); @@ -239,9 +363,9 @@ return; } - printf("\r\n %d bytes scheduled for transmission \r\n", retcode); - // memset(tx_buffer, 0, sizeof(tx_buffer)); - + printf("\r %d bytes scheduled for transmission \r\n", retcode); + memset(tx_buffer, 0, sizeof(tx_buffer)); + printf("------------------------------------------\n"); } @@ -277,7 +401,16 @@ case CONNECTED: printf("\r\n Connection - Successful \r\n"); if (MBED_CONF_LORA_DUTY_CYCLE_ON) { + + + send_message(); + + // LoRa_PrintChannels(); + + + + } else { ev_queue.call_every(TX_TIMER, send_message); } @@ -288,10 +421,18 @@ printf("\r\n Disconnected Successfully \r\n"); break; case TX_DONE: - printf("\r\n Message Sent to Network Server \r\n"); + //samira-time wait for x minutes + timenow = chrono::system_clock::to_time_t(chrono::system_clock::now()); + cout << "time = "<<ctime(&timenow) << endl; + printf("\r Message Sent to Network Server \r\n"); + printf("\r device waits for %d minutes before sending next messages \r\n", wait_time); + wait(wait_time);//samira-time wait for x seconds + timenow = chrono::system_clock::to_time_t(chrono::system_clock::now()); + cout << "wait time is finished time = "<<ctime(&timenow) << endl; + + //printf("\r\n Message Sent to Network Server \r\n"); if (MBED_CONF_LORA_DUTY_CYCLE_ON) { - send_message(); - wait(1);//samira-time + send_message(); } break; case TX_TIMEOUT: