9 #define RF_MODULE_BUGFIX 0
11 #if RF_MODULE_BUGFIX == 1
12 #warning The firmware compiled with additional code to fix the bug in RF module.
13 static uint8_t receive_bug_fixed=0;
18 uint8_t data_buffer[255];
24 snprintf(data_buffer,255,
"%s\r\n",atcmd);
30 printf(
"%s - no answer from the module.\r\n",atcmd);
34 data_buffer[resp_length] = 0;
35 printf(
"%s\r\n",data_buffer);
38 if ((resp_length==0) || (error==1))
48 if (((ch>=
'A') && (ch<=
'Z')) || ((ch>=
'a') && (ch<=
'z')) || ((ch>=
'0') && (ch<=
'9')))
67 #define USER_INPUT_LENGTH 80
68 static uint8_t user_input[USER_INPUT_LENGTH];
72 printf(
"Current URL saved in EEPROM: %s\r\n",settings->
target_URL);
73 printf(
"New URL (127 characters max!):\r\n");
74 scanf(
"%s",user_input);
77 if (strlen(user_input)>127)
79 printf(
"String too long.\r\n");
93 printf(
"Current server IP saved in EEPROM: %s\r\n",settings->
target_server_IP);
95 printf(
"Enter IP version you want to use, 4 for IPv4 or 6 for IPv6: ");
99 if ((version != 4) && (version != 6))
101 printf(
"Unknown protocol version.\r\n");
106 printf(
"New server IP (%d characters max!):\r\n",
sizeof(settings->
target_server_IP));
107 scanf(
"%s",user_input);
111 printf(
"String too long.\r\n");
119 printf(
"Applying RF module settings for a chosen mode...\r\n");
133 if (error_state != 0)
135 printf(
"Failed to apply settings for a chosen IP version; the module may not work properly.\r\n");
142 printf(
"New port (0 - 65535):\r\n");
152 printf(
"Enter new APN value (63 characters max!),\r\nor enter \"nidd=1\" or \"nidd=0\" to turn NIDD ON/OFF:\r\n");
153 scanf(
"%s",user_input);
156 if (strlen(user_input)>63)
158 printf(
"String too long.\r\n");
162 cmp1 = strcmp(user_input,
"nidd=1");
163 cmp2 = strcmp(user_input,
"nidd=0");
169 printf(
"NIDD APN not set, so NIDD cannot be switched on!\r\n");
173 printf(
"Saving NIDD mode ON...\r\n");
182 printf(
"Saving NIDD mode OFF...\r\n");
186 if ((cmp1!=0) && (cmp2!=0))
189 printf(
"Saving new APN...\r\n");
198 uint8_t temp_ICCID[50];
204 printf(
"Your actual location could be sent to a server. Do you really want to continue (y/n)?\r\n\
205 You can turn on GNSS privacy mode to avoid unplanned location disclosure.\r\n");
206 scanf(
"%s",user_input);
209 if ((user_input[0]==
'y') || (user_input[0]==
'Y'))
218 #if RF_MODULE_BUGFIX == 1
219 receive_bug_fixed = 1;
231 #if RF_MODULE_BUGFIX == 1
232 receive_bug_fixed = 1;
239 printf(
"Entering true direct mode.\r\n\
240 From now on everything you type into this terminal will be transferred to the RF module as is \r\n(and similarly in reverse direction).\r\n\
241 NOTICE: No special commands supported here, for nothing is between you and the RF module.\r\n\
242 YOU NEED TO REBOOT THE BOARD PHYSICALLY TO EXIT THIS MODE.\r\n---\r\n");
245 RCC->AHBENR |= RCC_AHBENR_DMA1EN;
247 NVIC_DisableIRQ(USART3_IRQn);
250 USART3->CR3 |= USART_CR3_DMAR;
251 DMA1_Channel3->CPAR = (uint32_t)(&(USART3->DR));
252 DMA1_Channel3->CMAR = (uint32_t)(&(USART1->DR));
253 DMA1_Channel3->CNDTR = 1;
254 DMA1_Channel3->CCR = DMA_CCR3_MSIZE_0 | DMA_CCR3_PSIZE_0 | DMA_CCR3_CIRC | DMA_CCR3_EN;
257 USART1->CR3 |= USART_CR3_DMAR;
258 DMA1_Channel5->CPAR = (uint32_t)(&(USART1->DR));
259 DMA1_Channel5->CMAR = (uint32_t)(&(USART3->DR));
260 DMA1_Channel5->CNDTR = 1;
261 DMA1_Channel5->CCR = DMA_CCR5_MSIZE_0 | DMA_CCR5_PSIZE_0 | DMA_CCR5_CIRC | DMA_CCR5_EN;
270 printf(
"Entering direct AT-command mode.\r\n\
271 Everything you type in will be passed to RF module directly, except special commands:\r\n\
272 \t\"exit\" - exit direct AT command mode\r\n\
273 \t\"pwrp\" - generate low pulse on PWR_ON pin of a module and wait for startup, useful when using PSM (to wake up the module).\r\n");
278 scanf(
"%s",user_input);
282 if (strcmp(user_input,
"exit")==0)
288 if (strcmp(user_input,
"pwrp")==0)
290 printf(
"Generating pulse on PWR_ON...\r\n");
292 printf(
"Waiting module to start...\r\n");
299 printf(
"Module ready.\r\n");
311 uint32_t *MCU_unique_ID_addr;
314 MCU_unique_ID_addr = MCU_UNIQUE_IDENTIFIER_ADDRESS;
316 printf(
"Board identification data\r\n");
318 printf(
"\r\nSIM card ICCID: %s\r\n",str);
321 printf(
"\r\nRF module IMEI: %s\r\n",str);
323 printf(
"\r\nIMSI:\r\n");
325 printf(
"\r\nRF module model:\r\n");
327 printf(
"\r\nRF module firmware version:\r\n");
330 printf(
"\r\nMCU firmware version:\r\n%s\r\n",FIRMWARE_VERSION);
331 printf(
"\r\nMCU serial number:\r\n0x%X%X%X\r\n",MCU_unique_ID_addr[2],MCU_unique_ID_addr[1],MCU_unique_ID_addr[0]);
347 static uint8_t resp_buffer[512];
357 sscanf(resp_buffer,
"%*s\r\r\n+CSQ: %d",&RSSI);
358 printf(
"RSSI\t= %d dBm (valid range is -111 to -51 dBm)\r\n",(RSSI*2) - 113);
362 printf(
"Unable to execute AT+CSQ\r\n");
367 sscanf(resp_buffer,
"%*s\r\r\n+CESQ: %*d,%*d,%*d,%*d,%d,%d",&RSRQ,&RSRP);
368 printf(
"SNR\t= %d dB\r\nRSRP\t= %d dBm (valid range is -141 to -44 dBm)\r\n",RSRQ,RSRP - 141);
372 printf(
"Unable to execute AT+CESQ\r\n");
380 printf(
"Unable to set up CEREG request mode.\r\n");
386 sscanf(resp_buffer,
"%*s\r\r\n+CEREG: %*d,%*d,\"%*lx\",\"%lx",&CellID);
387 printf(
"Cell ID\t= %d\r\n",CellID);
391 printf(
"Unable to request Cell ID.\r\n");
397 printf(
"Unable to set UCGED request mode.\r\n");
403 sscanf(resp_buffer,
"%*s\r\r\n+RSRP: %*d,%d",&EARFCN);
404 printf(
"EARFCN\t= %d\r\n",EARFCN);
408 printf(
"Unable to request EARFCN.\r\n");
416 uint32_t current_time;
418 uint16_t NIDD_resp_len;
419 static uint8_t NIDD_data[512];
423 printf(
"NIDD method is not enabled. Please first enable NIDD.\r\n");
428 #if RF_MODULE_BUGFIX == 1
429 if (receive_bug_fixed == 0)
431 printf(
"This is the first time in this session that you use the NIDD receive function;\r\n\
432 due to bug in the RF module firmware, in NIDD mode it is necessary to first send something to uplink before it will be possible to receive data.\r\n\
433 For this reason, a telemetry packet will be sent now.\r\n");
435 test_transmit_telemetry(settings);
437 receive_bug_fixed = 1;
441 printf(
"Please enter timeout (seconds): ");
442 scanf(
"%d",&timeout_ms);
449 current_time = start_time;
453 printf(
"Waiting for message via NIDD...\r\n");
455 printf(
"Timeout: %d sec. \r",(timeout_ms - (current_time - start_time))/1000);
457 while ((NIDD_resp_len == 0) && ((current_time - start_time) < timeout_ms))
462 if ((current_time % 1000) == 0)
464 printf(
"Timeout: %d sec. \r",(timeout_ms - (current_time - start_time))/1000);
468 if (NIDD_resp_len == 0)
470 printf(
"No NIDD data received; stopping due to timeout.\r\n");
474 printf(
"NIDD data (%d bytes):\r\n%s\r\n",NIDD_resp_len,NIDD_data);
480 printf(
"Enter telemetry transmission interval in milliseconds (min=1000, max=16000):\r\n");
486 printf(
"Interval too short, corrected to minimum of 1000 ms.\r\n");
492 printf(
"Interval too long, corrected to maximum of 16000 ms.\r\n");
500 printf(
"GNSS privacy mode: 1 - enabled (default), 0 - disabled (use with care):\r\n");
501 scanf(
"%s",user_input);
504 if (user_input[0]==
'0')
518 printf(
"Firmware startup mode: 0 - this menu (default), 1 - logger mode:\r\n");
519 scanf(
"%s",user_input);
523 if (user_input[0]==
'1')
534 printf(
"This setting will take effect after MCU restart.\r\n");
541 int16_t accel_X,accel_Y,accel_Z;
542 uint8_t accel_signature;
543 uint8_t temp_GNSS_string[80];
555 printf(
"On-board sensors data:\r\n\ttemperature:\t%d\r\n\r\n\taccelerometer:\tX = %d\tY = %d\tZ = %d\r\n",Tamb,accel_X,accel_Y,accel_Z);
557 if (accel_signature==0x33)
559 printf(
"\tAccelerometer signature value is correct.\r\n\r\n");
563 printf(
"\tAccelerometer signature value is wrong.\r\n\r\n");
566 printf(
"Testing GNSS option...\r\n");
575 printf(
"GNSS shield is not fitted or does not function properly.\r\n");
579 printf(
"GNSS string:\r\n%s\r\n",temp_GNSS_string);
583 printf(
"Unable to convert NMEA to decimal degrees data;\r\nthis is probably because GNSS fix is not achieved yet.\r\n");
587 printf(
"Parsed data:\r\n\tlatitude = %.6f\r\n\tlongitude = %.6f\r\n",latitude,longitude);
604 uint16_t error_state;
606 uint16_t cmd_resp_length;
610 printf(
"Applying default values to settings (%d bytes)...\r\n",
sizeof(
device_setup_data_t));
622 printf(
"Saving data...\r\n");
624 printf(
"EEPROM data structure initialized.\r\n");
626 printf(
"Checking the RF module activity...\r\n");
631 printf(
"Attempt %d...\r\n",k+1);
634 if (cmd_resp_length==0)
636 printf(
"No answer.\r\n");
640 while ((k<3) && (cmd_resp_length==0));
642 if (cmd_resp_length==0)
644 printf(
"RF module does not seem to be active, running startup algorithm...");
648 printf(
"Configuring the module...\r\n");
653 printf(
"Rebooting module...\r\n");
656 printf(
"error_state = %d\r\n",error_state);
664 while ((k<5) && (res == 0))
679 printf(
"Rebooting module...\r\n");
682 printf(
"error_state = %d\r\n",error_state);
687 printf(
"Rebooting module...\r\n");
690 printf(
"error_state = %d\r\n",error_state);
693 printf(
"Rebooting module...\r\n");
696 printf(
"error_state = %d\r\n",error_state);
699 printf(
"Rebooting module...\r\n");
702 printf(
"error_state = %d\r\n",error_state);
706 printf(
"Rebooting module...\r\n");
709 printf(
"error_state = %d\r\n",error_state);
712 printf(
"error_state = %d\r\n",error_state);
716 printf(
"Accelerometer signature is wrong.\r\n");
721 printf(
"Accelerometer OK.\r\n");
725 printf(
"Firmware compiled with DS1721 support instead of TMP75.\r\n");
727 printf(
"DS1721 initialized.\r\n");
733 if ((Tamb<15) || (Tamb>35))
735 printf(
"The temperature is out of the sane range (%d deg. C); the temperature sensor is possibly not functional.\r\n",Tamb);
740 printf(
"Temperature sensor OK (%d deg. C measured).\r\n",Tamb);
743 print_identification_data(settings);
745 printf(
"\r\n--------------------------------------------------------------------------------\r\n");
749 printf(
"CONFIG_OK=0\r\n");
753 printf(
"CONFIG_OK=1\r\n");
756 printf(
"\r\n--------------------------------------------------------------------------------\r\n");
764 #define MAX_JSON_LENGTH 200
765 #define MAX_COAP_MSG_LEN (MAX_JSON_LENGTH + MAX_URI_LENGTH + 50)
766 uint8_t wait_coap_server_response(uint32_t wait_time)
768 uint32_t start_time,t;
769 uint8_t rx_socket_ID;
770 uint16_t rx_data_length;
772 uint8_t *coap_data_start_ptr;
773 uint8_t CoAP_msg[MAX_COAP_MSG_LEN];
785 printf(
"%d \r",(wait_time/1000) - ((t - start_time)/1000));
792 printf(
"No response from the server, reception terminated by timeout.\r\n");
802 CoAP_msg[rx_data_length] = 0;
805 if (coap_data_start_ptr != NULL)
807 printf(
"Data: %s\r\n",coap_data_start_ptr);
811 printf(
"No data in response.\r\n");
824 #define USE_SERVER_BUG_WORKAROUND 1
828 static uint8_t res_url[80];
829 static uint8_t CoAP_msg[MAX_COAP_MSG_LEN];
830 uint16_t CoAP_msg_len;
837 printf(
"The device is in NIDD mode. CoAP observe function cannot be used, because NIDD does not use CoAP.\r\n");
842 printf(
"Please enter the URL of a resource to be observed:\r\n");
847 printf(
"Please enter the maximum time to wait for server responses (seconds):\r\n");
848 scanf(
"%i",&wait_time);
857 printf(
"Unable to assemble CoAP request; please check the URL; in particular, it must start with '/'.\r\n");
861 printf(
"CoAP OBSERVE message:\r\n");
862 for (k=0; k<CoAP_msg_len; k++)
864 printf(
"%02X ",CoAP_msg[k]);
872 printf(
"Unable to create a UDP socket.\r\n");
878 printf(
"Faled to send CoAP message to server.\r\n");
882 printf(
"CoAP message with an OBSERVE option was sent.\r\n");
884 #if USE_SERVER_BUG_WORKAROUND != 1
885 printf(
"Waiting for the initial server response...\r\n");
887 if (wait_coap_server_response(wait_time) == 0)
893 #warning The firmware is compiled with the server bug workaround included. Notice that this version will not work correctly with a standard-compliant server.
896 printf(
"Waiting for the resource change notification...\r\n");
898 wait_coap_server_response(wait_time);
904 {set_target_URL,
"set the URL of the resource JSON data will be transmitted to"},
905 {set_server_IP,
"set the IP address"},
906 {set_server_port,
"set the port"},
907 {set_NIDD_APN,
"set an APN for NIDD access or turn NIDD mode ON or OFF"},
908 {test_transmit_telemetry,
"force send telemetry packet"},
909 {wait_NIDD_URC,
"wait for incoming NIDD data during specified timeout (and then exit)"},
910 {test_CoAP_observe_function,
"test CoAP OBSERVE function"},
911 {direct_AT_mode,
"enter direct AT-command mode"},
912 {true_direct_mode,
"enter true direct mode to access the RF module\r\n\t\tCAUTION: to exit this mode you will have to reboot the board physically"},
913 {print_identification_data,
"show board identification data (serial numbers, firmware versions, etc.)"},
914 {print_network_info,
"show network information"},
915 {set_telemetry_interval,
"set telemetry transmission interval"},
916 {set_GNSS_privacy,
"set GNSS privacy mode\r\n\t\t(hide actual location data when transmitting on server)"},
917 {set_startup_mode,
"set firmware startup mode (setup or logger)"},
918 {test_sensors,
"read on-board sensors and try to acquire GNSS data"},
919 {perform_system_reset,
"reboot MCU"},
920 {perform_RF_module_reset,
"reboot RF module"},
921 {
perform_initial_setup,
"factory setup & test\r\n\t\t(do not use this unless you really know what you want)"},