#include #include #include "LORA.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/queue.h" #include "driver/uart.h" #include "esp_log.h" #include "y_ringbuf.h" #include #include "driver/gpio.h" #include "freertos/timers.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_check.h" #include "esp_sleep.h" #include "soc/uart_pins.h" #include "driver/uart.h" #include "driver/gpio.h" #include "driver/rtc_io.h" #include "sdkconfig.h" #include "../../main/user_sleep.h" #include "esp_sleep.h" #include "SPIFFS.h" QueueHandle_t lora_receiveQueue; SemaphoreHandle_t debug_Mutex; extern struct RINGBUF_st; RINGBUF_st *lora_ringbuf; uint8_t rssi; uart_status_t lora_uart_status = uart_idle; //声明一个TimerHandle_t类型的变量,用于存储定时器句柄 TimerHandle_t lora_uart_Timerout; bool is_change_chanel = false; static const char *LOG_TAG = "LORA"; //static char lora_rx_buf[32] = {}; uint8_t rawData_16[1024]; extern QueueHandle_t lora_data_queue; uint8_t rec_rssi; static const int LORA_BUF_SIZE = 1024; static bool half_packet_flag =false; static QueueHandle_t lora_queue; LORA_CFG_T lora_cfg_data; uint8_t lora_rssi_data = 0; uint8_t lora_software_version = 0; bool is_uart_wake = false; //static int lora_analytic_data(const uint8_t* data_buf,int len); static int lora_set_receive(const uint8_t* data,size_t len); static int send_lora_queue(const uint8_t* data_buf,int len); static esp_err_t uart_wakeup_config(void); // #if USER_QIXIN //添加的初始化代码 TaskHandle_t lora_uart_tx_handle; QueueHandle_t lora_uart_Queue; //串口接受队列 TimerHandle_t lora_cmd_Timerout; //初始化命令超时函数 QueueHandle_t lora_dealhandle; //开始处理逻辑的数据 //沃进初始化命令配置 lora_cmd_t lora_cmd[]= { #if 0 { {0x03,0x26,0x01,0x8E},4,"",lora_cmd_success_Hander,lora_cmd_timerout_Hander},/*测试AT*/ { {0x03,0x01,0x15,0x27},4,"",lora_cmd_success_Hander,lora_cmd_timerout_Hander},/*恢复默认设置*/ { {0x03,0x26,0x00,0x89},4,"",lora_cmd_success_Hander,lora_cmd_timerout_Hander},/*设置工作频率*/ //{ {0x03,0x26,0x01},"",lora_cmd_success_Hander,lora_cmd_timerout_Hander},/*设置波特率115200*/ #else { .cmd_mode = 0x01, .lora_send_cmd_handler = lora_set_config_mode,.success_hanlder =lora_cmd_success_Hander,.timeout_hanlder=lora_cmd_timerout_Hander}, /*进入配置模式*/ { .cmd_mode = LORA_CHANENL,.lora_send_cmd_handler = lora_set_channel,.success_hanlder =lora_cmd_success_Hander,.timeout_hanlder=lora_cmd_timerout_Hander},/*设置信道模式*/ { .cmd_mode = 0x06, .lora_send_cmd_handler = lora_set_bps,.success_hanlder =lora_cmd_success_Hander,.timeout_hanlder=lora_cmd_timerout_Hander}, /*设置波特率*/ //设置设备ID 地址 0xFFFFFFFF { .cmd_mode = 0x00000001, .lora_send_cmd_handler = lora_set_device_id,.success_hanlder =lora_cmd_success_Hander,.timeout_hanlder=lora_cmd_timerout_Hander}, //设置目标地址 0xFFFFFFFF 所有都可以接收到 { .cmd_mode = 0xFFFFFFFF, .lora_send_cmd_handler = lora_set_dst_device_id,.success_hanlder =lora_cmd_success_Hander,.timeout_hanlder=lora_cmd_timerout_Hander}, { .cmd_mode = 0x07, .lora_send_cmd_handler = lora_get_rssi,.success_hanlder =lora_cmd_success_Hander,.timeout_hanlder=lora_cmd_timerout_Hander}, /*rssi*/ //{ .cmd_mode = 0x01, .lora_send_cmd_handler = lora_set_mode,.success_hanlder =lora_cmd_success_Hander,.timeout_hanlder=lora_cmd_timerout_Hander}, /*设置透传模式*/ { .cmd_mode = 0x00, .lora_send_cmd_handler = lora_set_config_mode,.success_hanlder =lora_cmd_success_Hander,.timeout_hanlder=lora_cmd_timerout_Hander}, /*退出配置模式*/ #endif }; // lora_cmd_t lora_cmd_rssi[]= // { // #if 0 // { {0x03,0x26,0x01,0x8E},4,"",lora_cmd_success_Hander,lora_cmd_timerout_Hander},/*测试AT*/ // { {0x03,0x01,0x15,0x27},4,"",lora_cmd_success_Hander,lora_cmd_timerout_Hander},/*恢复默认设置*/ // { {0x03,0x26,0x00,0x89},4,"",lora_cmd_success_Hander,lora_cmd_timerout_Hander},/*设置工作频率*/ // //{ {0x03,0x26,0x01},"",lora_cmd_success_Hander,lora_cmd_timerout_Hander},/*设置波特率115200*/ // #else // { .cmd_mode = 0x01, .lora_send_cmd_handler = lora_set_config_mode,.success_hanlder =lora_cmd_success_Hander,.timeout_hanlder=lora_cmd_timerout_Hander}, /*进入配置模式*/ // { .cmd_mode = 0x07, .lora_send_cmd_handler = lora_get_rssi,.success_hanlder =lora_cmd_success_Hander,.timeout_hanlder=lora_cmd_timerout_Hander}, /*rssi*/ // //{ .cmd_mode = 0x01, .lora_send_cmd_handler = lora_set_mode,.success_hanlder =lora_cmd_success_Hander,.timeout_hanlder=lora_cmd_timerout_Hander}, /*设置透传模式*/ // { .cmd_mode = 0x00, .lora_send_cmd_handler = lora_set_config_mode,.success_hanlder =lora_cmd_success_Hander,.timeout_hanlder=lora_cmd_timerout_Hander}, /*退出配置模式*/ // #endif // }; //初始化结构体 Lora_t lora = { .lora_mode = Lora_Cmd_Mode, .lora_cmd = lora_cmd, .cmd_index = 0, .timerout =false, .cmd_type = lora_cmd_system_type, }; /********************************************************************************* * function : lora_cmd_success_Hander * Description : lora AT命令配置回复成功 * Input : * Output : * Author : 祁鑫 Data : 2023 7.18 **********************************************************************************/ void lora_cmd_success_Hander(unsigned char *cmd,unsigned char *cmd_rsp,int len) { ESP_LOGE(LOG_TAG,"%s,cmd = %s,cmd rsp =%s len = %d\r\n",__FUNCTION__,cmd,cmd_rsp,len); } /********************************************************************************* * function : lora_sendData * Description : lora AT命令配置回复超时或失败 * Input : * Output : * Author : 祁鑫 Data : 2023 7.18 **********************************************************************************/ void lora_cmd_timerout_Hander(unsigned char *cmd,unsigned char *cmd_rsp,int len) { printf("%s,cmd = %s,cmd rsp =%s len = %d\r\n",__FUNCTION__,cmd,cmd_rsp,len); } /********************************************************************************* * function : lora_sendData * Description : lora 发送数据函数 * Input : * Output : * Author : 祁鑫 Data : 2023 7.18 **********************************************************************************/ int lora_sendData(unsigned char* data,int len) { //const int len = strlen(data); int txBytes = 0; int offset = 0; #if 1 #define MAX_LEN 64//50*2 #define TIME_SET 50 #else #define MAX_LEN 200 #define TIME_SET 30 #endif offset=0; while(len>0) { if(len>MAX_LEN) { #if 0 send_lora_data(result+offset,MAX_LEN); #else txBytes = uart_write_bytes(LORA_UART, data+offset, MAX_LEN); vTaskDelay(TIME_SET / portTICK_PERIOD_MS); #endif offset=offset+MAX_LEN; len=len-MAX_LEN; }else{ #if 0 send_lora_data(result+offset,len); #else txBytes = uart_write_bytes(LORA_UART, data+offset, len); vTaskDelay(TIME_SET / portTICK_PERIOD_MS); #endif len=0; //Serial.println("send end packet."); } } #if 0 //可以发送的 const int txBytes = uart_write_bytes(LORA_UART, data, len); #endif //printf("Write %d bytes\r\n", txBytes); return txBytes; } /********************************************************************************* * function : lora_send_index_init_cmd * Description : lora 发送初始化指定索引位置命令函数 * Input : * Output : * Author : 祁鑫 Data : 2023 7.18 **********************************************************************************/ void lora_send_index_init_cmd(uint8_t index) { if(index >=(sizeof(lora_cmd)/sizeof(lora_cmd[0]))) return; if(lora_cmd[index].lora_send_cmd_handler != NULL) { lora_cmd[index].lora_send_cmd_handler( lora_cmd[index].cmd_mode); } } int find_value(const unsigned char *hex_array, size_t array_size, unsigned char target_value) { for (size_t i = 0; i < array_size; i++) { if (hex_array[i] == target_value) { return (int)i; } } return -1; } /********************************************************************************* * function : lora_cmd_timerout_Callback * Description : lora AT命令配置回复超时定时器 * Input : * Output : * Author : 祁鑫 Data : 2023 7.18 **********************************************************************************/ void lora_cmd_timerout_Callback(TimerHandle_t xTimer) { lora.timerout = true; printf("lora_cmd_timerout_Callback\r\n"); } void lora_init_cmd_handler(unsigned char * data,void *param) { #if VJ_LORA_ENABLE uint16_t len = *(uint16_t*)param; if((find_value(data,len,LORA_SUCCESS)!= -1) ||(find_value(data,len,0x07)!= -1)) //if(strncmp((char*)data,(char *)lora_cmd[lora.cmd_index].cmd_rsp,strlen((char *)lora_cmd[lora.cmd_index].cmd_rsp)) == 0 ) { //printf("cmd is ok %d\r\n",lora.cmd_index); if(lora.cmd_index==(sizeof(lora_cmd)/sizeof(lora_cmd[0]) -1)) { xTimerStop(lora_cmd_Timerout,0); lora.lora_mode = Lora_UART_Mode; gpio_set_level(LORA_SET_PIN, 1); // if(is_change_chanel == true) // { // is_change_chanel = false; // } //uart_set_baudrate(LORA_UART,115200); printf("lora at all cmd init ok \r\n"); #if 0 extern void user_i2s_init(void); user_i2s_init(); #else //ota_init(); void lora_send_test_handler(); //xTaskCreate(lora_send_test_handler, "lora_send_test_task", 1024*4, NULL, configMAX_PRIORITIES, NULL); #endif }else { xTimerStop(lora_cmd_Timerout,0); lora.cmd_index +=1; #if 0 lora_sendData((unsigned char*)lora_cmd[lora.cmd_index].cmd,4); #else lora_send_index_init_cmd(lora.cmd_index); #endif xTimerStart(lora_cmd_Timerout, 0); } }else if(find_value(data,len,LORA_FAIL)!= -1) { printf("cmd is fail\r\n"); if(lora.timerout == true) { lora.timerout =false; } if(lora.retry_times>=MAX_RETRY_NUM) { lora.retry_times = 0; //xTimerStop(lora_cmd_Timerout,0); printf("cmd is running retry_times max contiune next cmd\r\n"); return; } lora.retry_times+=1; } #endif // xTimerStart(lora_cmd_Timerout, 30); // if(xTimerIsTimerActive(lora_cmd_Timerout) != pdFALSE) // { // xTimerReset(lora_cmd_Timerout,0); // //printf("receiving\r\n"); // //printf("timer is running"); // } } /********************************************************************************* * function : dymatic_change_chanel * Description : lora AT命令配置动态切换信道 * Input : * Output : * Author : 祁鑫 Data : 2023 10.23 **********************************************************************************/ void dymatic_change_chanel(uint8_t chanel) { // ESP_LOGW(LOG_TAG,"切换chanel = %d ",chanel); lora.cmd_index = 0; lora.lora_mode = Lora_Cmd_Mode; //设置进入命令模式 lora_cmd[1].cmd_mode = chanel; //设置信道 lora_send_index_init_cmd(lora.cmd_index); // xTimerStart(lora_cmd_Timerout, 0); } /********************************************************************************* * function : dymatic_change_device_id * Description : lora AT命令配置动态修改设备ID * Input : * Output : * Author : 祁鑫 Data : 2023 10.23 **********************************************************************************/ void dymatic_change_device_id(uint32_t id) { for(int i = 0;i>8)&0xff; lorabuf[4]=(id>>16)&0xff; lorabuf[5]=(id>>24)&0xff; #else temp = (ID>>0)&0xff; lorabuf[2]=temp; temp = (ID>>8)&0xff; lorabuf[3]=temp; temp = (ID>>16)&0xff; lorabuf[4]=temp; temp = (ID>>24)&0xff; lorabuf[5]=temp; #endif lorabuf[6]=crc8(lorabuf,6,0x55,0x07); lora_sendData(lorabuf,sizeof(lorabuf)); #endif } /********************************************************************************* * function : lora_set_dst_device_id * Description : lora AT命令配置LORA目标设备ID * Input : 4个字节数据 * Output : * Author : 祁鑫 Data : 2023 10.23 **********************************************************************************/ void lora_set_dst_device_id(uint32_t id) { printf("%s id = %ld\r\n",__FUNCTION__,id); #if 1 uint8_t lorabuf[7]; uint8_t temp = 0x00; uint32_t ID = id; lorabuf[0]=0x06; lorabuf[1]=0x0E; #if 0 lorabuf[2]=id&0xff; lorabuf[3]=(id>>8)&0xff; lorabuf[4]=(id>>16)&0xff; lorabuf[5]=(id>>24)&0xff; #else temp = (ID>>0)&0xff; lorabuf[2]=temp; temp = (ID>>8)&0xff; lorabuf[3]=temp; temp = (ID>>16)&0xff; lorabuf[4]=temp; temp = (ID>>24)&0xff; lorabuf[5]=temp; #endif lorabuf[6]=crc8(lorabuf,6,0x55,0x07); lora_sendData(lorabuf,sizeof(lorabuf)); #endif } uint8_t set_lora(uint8_t new_channel,uint8_t eflag) { uint8_t ret = 0; uint8_t data[50] = {0}; int user_size = 0; lora.lora_mode = Lora_Cmd_Mode; gpio_set_level(LORA_SET_PIN, 0); lora_set_config_mode(0x01); vTaskDelay(100/ portTICK_PERIOD_MS); user_size = y_ringbuf_get_used_size(lora_ringbuf); y_ringbuf_read_clear(lora_ringbuf, data, user_size); if(user_size!=0) { printf("rsp len = %d\r\n",user_size); for(int i = 0;ierr\n\n\n"); lora_set_channel(LORA_CHANENL); vTaskDelay(100/ portTICK_PERIOD_MS); user_size = y_ringbuf_get_used_size(lora_ringbuf); y_ringbuf_read_clear(lora_ringbuf, data, user_size); } if(user_size!=0 ) { printf("lora_set_channel len = %d\r\n",user_size); for(int i = 0;i 0) { // 启动定时器,第二个参数为定时器超时时间,设置为0表示立即启动 if(lora_uart_status == uart_idle) { lora_uart_status = uart_receving; xTimerStart(lora_uart_Timerout, 0); //printf("first rev\r\n"); }else if(lora_uart_status == uart_receving) { if(xTimerIsTimerActive(lora_uart_Timerout) != pdFALSE) { xTimerReset(lora_uart_Timerout,0); //printf("receiving\r\n"); //printf("timer is running"); }else{ //printf("timer is run ok"); } //portTICK_PERIOD_MS if(event.size <= uart_fifo_size) { lora_uart_status = uart_receving; } } receive_len = event.size; int len = uart_read_bytes(LORA_UART, data, event.size,/*uart_timerout_times / portTICK_PERIOD_MS*/portMAX_DELAY); receive_len = len; y_ringbuf_write(lora_ringbuf, data, receive_len); } // xSemaphoreGive( debug_Mutex ); break; //Event of HW FIFO overflow detected case UART_FIFO_OVF: printf("UART_FIFO_OVF\r\n"); // If fifo overflow happened, you should consider adding flow control for your application. // The ISR has already reset the rx FIFO, // As an example, we directly flush the rx buffer here in order to read more data. uart_flush_input(LORA_UART); xQueueReset(lora_uart_Queue); break; //Event of UART ring buffer full case UART_BUFFER_FULL: printf("UART_BUFFER_FULL\r\n"); // If buffer full happened, you should consider encreasing your buffer size // As an example, we directly flush the rx buffer here in order to read more data. uart_flush_input(LORA_UART); xQueueReset(lora_uart_Queue); break; //Event of UART RX break detected case UART_BREAK: // printf("UART_BREAK\r\n"); break; //Event of UART parity check error case UART_PARITY_ERR: printf("UART_PARITY_ERR\r\n"); break; //Event of UART frame error case UART_FRAME_ERR: printf("UART_FRAME_ERR\r\n"); break; //UART_PATTERN_DET case UART_PATTERN_DET: printf("UART_PATTERN_DET\r\n"); break; // Event of waking up by UART case UART_WAKEUP: printf("uart wakeup\r\n"); if(xTimerIsTimerActive(lora_uart_Timerout) != pdFALSE) { printf("uart timer run\r\n"); xTimerReset(lora_uart_Timerout,0); }else { printf("uart timer not run\r\n"); } break; //Others default: printf("other error\r\n"); break; } } } #endif free(data); //free(cmd); } void lora_receive_callback_handler() { int length = 0; uint8_t* data = (uint8_t*) malloc(buffer_size+1); //int length = 0; // uint8_t* result = (uint8_t*) malloc(buffer_size+1); int user_size = 0; // #include "../../main/yc_protocol.h" // extern YC_DATA_T yc_data; while (1) { // 从队列中接收数据 if (xQueueReceive(lora_receiveQueue, &length, portMAX_DELAY) == pdPASS){ user_size = y_ringbuf_get_used_size(lora_ringbuf); if(lora.lora_mode == Lora_Cmd_Mode) { //y_ringbuf_read_clear(lora_ringbuf, data, user_size); //printf("user_size = %d\r\n",user_size); //lora 命令模式处理 // if(lora.cmd_type == lora_cmd_system_type) // { // for(int i = 0;irssi = %02x----,",data[i+2]); // if(data[i+2] >100) // rssi = 100; // else // rssi = data[i+2]; // } // } // #if 1 // lora_init_cmd_handler(data,&user_size); // #else // //发送回复显示 // lora_sendData(data,user_size); // #endif // } }else if(lora.lora_mode == Lora_UART_Mode) { // //lora 透传模式处理 // for(int i = 0;i Lora_UART_Mode %.*s\n", user_size, data); //发送消息处理 #if 0 xQueueSend(lora_dealhandle, &length, portMAX_DELAY); #else if (xQueueSend(lora_dealhandle, &length, portMAX_DELAY) == pdPASS) { // 消息发送成功 // ESP_LOGW(LOG_TAG,"send ok"); } else { // 消息发送失败,可能是消息队列已满 ESP_LOGE(LOG_TAG,"send fail"); } } } } free(data); } #if VJ_LORA_ENABLE /**沃进方案校验和计算法 * @funtion:crc8多项式冗余校验 * @param 1:pData,计算数据源地址 * @param 2:dataLen,计算数据源长度 * @param 3:initialValue,crc结果初值 * @param 4:polynomial,多项式 * * @return :校验结果 */ uint8_t crc8( uint8_t *pData, uint16_t dataLen, uint8_t initialValue, uint8_t polynomial ) { uint8_t i; uint8_t crc; crc = initialValue; while (dataLen --) { crc ^= *pData ++; for( i = 0; i < 8; i++ ) { if(crc & 0x80) { crc <<= 1; // shift left once crc ^= polynomial; // XOR with polynomial } else { crc <<= 1; // shift left once } } } return crc; } //=0,在配置模式下,退出配置模式,非配置模式下,认为是透传数据 //=1,进入配置模式,此时可以配置其他命令 void VJ_Lora_set_cfg_mode(uint8_t mode) { uint8_t cmd[4]={0}; cmd[0]=0x03; cmd[1]=0x26; cmd[2]=mode; cmd[3]=crc8(cmd,3,0x55,0x07); printf("cmd[3] = %02x\r\n",cmd[3]); lora_sendData(cmd,sizeof(cmd)); } /**沃进方案设置无线信道********************************************** 范围:0~31 具体对应的频率与无线频段范围和信道间隔带宽的设置相关。 比如信道间隔 1MHz,无线频段范围为 433MHz 频段时, =0,对应 433MHz =1,对应 434MHz … =31,对应 464MHz 无线频段范围为 868MHz 频段时, =0,对应 868MHz =1,对应 869MHz … =31,对应 899MHz 默认为 0 信道 设置立即生效,支持掉电保存 ******************************************************************/ int vjlora_set_Wireless_Channel(uint8_t Channel) { int ret = 0; VJ_Lora_set_cfg_mode(1); //ret=vjlora_set_by_serial(0x01,Channel); VJ_Lora_set_cfg_mode(0); return ret; } #endif #if 0 /********************************************************************************* * function : lora_uart_is_normal * Description : 判断通信是否正常 * Input : boatu * Output : * Author : 祁鑫 Data : 2023 7.10 **********************************************************************************/ bool lora_uart_is_normal(int Baud) { uint8_t i = 0, times = 0,length = 0; #if 1 String cmd = "AH"; String cmd_rsp = "OK"; loraSerial.begin(Baud,SERIAL_8N1,LORA_RX,LORA_TX); // 启动串口通讯,波特率设置为115200,并配置对应端口 TX48 RX45 bool is = lora_send_cmd(cmd,cmd_rsp); if(is) { return true; }else{ return false; } #else loraSerial.begin(Baud,SERIAL_8N1,LORA_RX,LORA_TX); // 启动串口通讯,波特率设置为115200,并配置对应端口 TX48 RX45 //loraSerial.println("AH");/* code */ while (1) { times++; //while(loraSerial.read() >0){} //loraSerial.flush(); loraSerial.println("AH");/* code */ //delay(200); memset(receive_buf,0x00,sizeof(receive_buf)); i=0; while (loraSerial.available()>0) { //if(loraSerial.available() == 4) { //for(i = 0;i<4;i++) receive_buf[i]= loraSerial.read(); //i++; delay(100); i++; //break; } } #if 1 Serial.println("receive data = "); for(int j = 0;j<20;j++) Serial.println(receive_buf[j],HEX); Serial.println("receive data ok"); #endif if(receive_buf[0]=='O' && receive_buf[1]=='K') { //Serial.print("uart init ok\r\n"); return true; break; } //if(receive_buf.find("OK")) // { // //Serial.print("uart init ok\r\n"); // return true; // break; // } else { //Serial.print("uart init fail\r\n"); if(times>AT_MAX_RETRY_TIMES) { return false; break; } delay(200); } } #endif } /********************************************************************************* * function : lora_send_cmd * Description : 发送AT字符串命令并判断返回字符串 * Input : * Output : * Author : 祁鑫 Data : 2023 7.10 **********************************************************************************/ bool lora_send_cmd(String cmd,String cmd_rsp) { uint8_t i = 0, times = 0; bool is = false; String read_str; #if 1 while (1) { times++; loraSerial.println(cmd);/* code */ //delay(200); memset(receive_buf,0x00,sizeof(receive_buf)); i=0; while (loraSerial.available()>0) { Serial.println(loraSerial.available()); #if 1 delay(20); read_str = loraSerial.readString(); #else receive_buf[i] = loraSerial.read(); #endif i++; } #if 0 Serial.println("receive data = "); Serial.println(read_str); //Serial.println(receive_buf[1],HEX); #endif //if(read_str.equals(cmd_rsp)) if(read_str.startsWith(cmd_rsp) || read_str.startsWith("OK")) { //Serial.println("cmd rsp is ok"); is = true; break; }else{ if(times>AT_MAX_RETRY_TIMES) { //Serial.println("cmd rsp is fail"); is = false; break; } delay(20); } } if(is) { return true; }else { return false; } #endif } /********************************************************************************* * function : lora_set_Baud * Description : 设置波特率 * Input : baud 1-8 * Output : X 取值 1-8,可设置波特率为 1200bps、 2400bps、 4800bps、 9600bps、 19200bps、 38400bps、 57600bps 和 115200bps。 出厂默认为 9600bps。 * Author : 祁鑫 Data : 2023 7.10 **********************************************************************************/ bool lora_set_Baud(int baud) { //char baute[6] = {0}; bool is = false; String baute = LORA_CMD_AH_BAUDRATE; String baute_rsp = LORA_CMD_AH_BAUDRATE_RSP; if(baud == 1){ baute.concat("1"); baute_rsp.concat("1"); is = lora_send_cmd(baute,baute_rsp); }else if(baud == 2) { baute.concat("2"); baute_rsp.concat("2"); is = lora_send_cmd(baute,baute_rsp); }else if(baud == 3) { baute.concat("3"); baute_rsp.concat("3"); is = lora_send_cmd(baute,baute_rsp); } else if(baud == 4) { baute.concat("4"); baute_rsp.concat("4"); is = lora_send_cmd(baute,baute_rsp); } else if(baud == 5) { baute.concat("5"); baute_rsp.concat("5"); is = lora_send_cmd(baute,baute_rsp); } else if(baud == 6) { baute.concat("6"); baute_rsp.concat("6"); is = lora_send_cmd(baute,baute_rsp); } else if(baud == 7) { baute.concat("7"); baute_rsp.concat("7"); is = lora_send_cmd(baute,baute_rsp); } else if(baud == 8) { baute.concat("8"); baute_rsp.concat("8"); is = lora_send_cmd(baute,baute_rsp); } if(is) { return true; }else { return false; } #if 0 switch (baud) { case 1 : baute.concat("1"); baute_rsp.concat("1"); is = lora_send_cmd(baute,baute_rsp); break; case 2 : baute.concat("2"); baute_rsp.concat("2"); is = lora_send_cmd(baute,baute_rsp); break; case 3 : baute.concat("3"); baute_rsp.concat("3"); is = lora_send_cmd(baute,baute_rsp); break; case 4 : baute.concat("4"); baute_rsp.concat("4"); is = lora_send_cmd(baute,baute_rsp); break; case 5 : baute.concat("5"); baute_rsp.concat("5"); is = lora_send_cmd(baute,baute_rsp); break; case 6 : baute.concat("6"); baute_rsp.concat("6"); is = lora_send_cmd(baute,baute_rsp); break; case 7 : baute.concat("7"); baute_rsp.concat("7"); is = lora_send_cmd(baute,baute_rsp); break; case 8 : baute.concat("8"); baute_rsp.concat("8"); is = lora_send_cmd(baute,baute_rsp); break; default: break; } #endif } /********************************************************************************* * function : lora_set_default * Description : 设置默认参数 * Input : * Output : 模组出厂设置参数为: 串口波特率 9600bps, 1 停止位, 8 数据位,无奇偶校验; 通信频率: 433.92MHz 发射功率: P8( Max) =21dBm LoRa 参数: SF=9、 BW=125KHz、 CR=4/5 模组 ID: 0000 当前数据包 RSSI 状态: OFF * Author : 祁鑫 Data : 2023 7.18 **********************************************************************************/ bool lora_set_default() { bool is = false; String cmd = "AH+D"; String cmd_rsp = "OK+DEFAULT"; is = lora_send_cmd(cmd,cmd_rsp); if(is) { Serial.println("lora_set_default ok"); return true; }else { Serial.println("lora_set_default fail"); return false; } } /********************************************************************************* * function : lora_set_work_freq * Description : 更改射频中心频率(通信频率或工作频率) * Input : baud 1-8 * Output : * Author : 祁鑫 Data : 2023 7.18 **********************************************************************************/ bool lora_set_work_freq(String freq) { bool is = false; String cmd = "AH+C"; String cmd_rsp = "OK+C"; cmd.concat(freq); cmd_rsp.concat(freq); is = lora_send_cmd(cmd,cmd_rsp); if(is) { Serial.println("lora_set_work_freq ok"); return true; }else { Serial.println("lora_set_work_freq fail"); return false; } } /********************************************************************************* * function : lora_set_send_power * Description : 设置发射功率 * Input : baud 1-8 * Output : X 取值 1-8,出厂默认设置为 8,发射功率最大。发射功率等级设置 为 1,发射功率最小。 X 值 1 2 3 4 5 6 7 8 对应模块发射功率 -1 2 5 8 11 14 17 21, 发射功率单位 dBm * Author : 祁鑫 Data : 2023 7.10 **********************************************************************************/ bool lora_set_send_power(int baud) { bool is = false; String cmd = "AH+P"; String cmd_rsp = "OK+P"; if(baud == 1){ cmd.concat("1"); cmd_rsp.concat("1"); is = lora_send_cmd(cmd,cmd_rsp); }else if(baud == 2) { cmd.concat("2"); cmd_rsp.concat("2"); is = lora_send_cmd(cmd,cmd_rsp); }else if(baud == 3) { cmd.concat("3"); cmd_rsp.concat("3"); is = lora_send_cmd(cmd,cmd_rsp); } else if(baud == 4) { cmd.concat("4"); cmd_rsp.concat("4"); is = lora_send_cmd(cmd,cmd_rsp); } else if(baud == 5) { cmd.concat("5"); cmd_rsp.concat("5"); is = lora_send_cmd(cmd,cmd_rsp); } else if(baud == 6) { cmd.concat("6"); cmd_rsp.concat("6"); is = lora_send_cmd(cmd,cmd_rsp); } else if(baud == 7) { cmd.concat("7"); cmd_rsp.concat("7"); is = lora_send_cmd(cmd,cmd_rsp); } else if(baud == 8) { cmd.concat("8"); cmd_rsp.concat("8"); is = lora_send_cmd(cmd,cmd_rsp); } if(is) { return true; }else { return false; } } /********************************************************************************* * function : lora_set_sf * Description : 设置 Lora 扩频因子(SF)值 * Input : baud 1-8 * Output : X 取值 1-8, 分别对应 SF5、 SF6、 SF7、 SF8、 SF9、 SF10、 SF11、 SF12 * Author : 祁鑫 Data : 2023 7.10 **********************************************************************************/ bool lora_set_sf(int baud) { bool is = false; String cmd = "AH+S"; String cmd_rsp = "OK+S"; if(baud == 1){ cmd.concat("1"); cmd_rsp.concat("1"); is = lora_send_cmd(cmd,cmd_rsp); }else if(baud == 2) { cmd.concat("2"); cmd_rsp.concat("2"); is = lora_send_cmd(cmd,cmd_rsp); }else if(baud == 3) { cmd.concat("3"); cmd_rsp.concat("3"); is = lora_send_cmd(cmd,cmd_rsp); } else if(baud == 4) { cmd.concat("4"); cmd_rsp.concat("4"); is = lora_send_cmd(cmd,cmd_rsp); } else if(baud == 5) { cmd.concat("5"); cmd_rsp.concat("5"); is = lora_send_cmd(cmd,cmd_rsp); } else if(baud == 6) { cmd.concat("6"); cmd_rsp.concat("6"); is = lora_send_cmd(cmd,cmd_rsp); } else if(baud == 7) { cmd.concat("7"); cmd_rsp.concat("7"); is = lora_send_cmd(cmd,cmd_rsp); } else if(baud == 8) { cmd.concat("8"); cmd_rsp.concat("8"); is = lora_send_cmd(cmd,cmd_rsp); } if(is) { return true; }else { return false; } } /********************************************************************************* * function : lora_set_bw * Description : 设置 Lora 带宽(BW) * Input : * Output : X 取值 0-9, 分别对应 7.81 kHz、 10.42 kHz、 15.63 kHz、 20.83 kHz、 31.25 kHz、 41.67 kHz、 62.50 kHz、 125 kHz、 250 kHz、 500 kHz。 * Author : 祁鑫 Data : 2023 7.10 **********************************************************************************/ bool lora_set_bw(int baud) { bool is = false; String cmd = "AH+W"; String cmd_rsp = "OK+W"; if(baud == 1){ cmd.concat("1"); cmd_rsp.concat("1"); is = lora_send_cmd(cmd,cmd_rsp); }else if(baud == 2) { cmd.concat("2"); cmd_rsp.concat("2"); is = lora_send_cmd(cmd,cmd_rsp); }else if(baud == 3) { cmd.concat("3"); cmd_rsp.concat("3"); is = lora_send_cmd(cmd,cmd_rsp); } else if(baud == 4) { cmd.concat("4"); cmd_rsp.concat("4"); is = lora_send_cmd(cmd,cmd_rsp); } else if(baud == 5) { cmd.concat("5"); cmd_rsp.concat("5"); is = lora_send_cmd(cmd,cmd_rsp); } else if(baud == 6) { cmd.concat("6"); cmd_rsp.concat("6"); is = lora_send_cmd(cmd,cmd_rsp); } else if(baud == 7) { cmd.concat("7"); cmd_rsp.concat("7"); is = lora_send_cmd(cmd,cmd_rsp); } else if(baud == 8) { cmd.concat("8"); cmd_rsp.concat("8"); is = lora_send_cmd(cmd,cmd_rsp); } else if(baud == 9) { cmd.concat("9"); cmd_rsp.concat("9"); is = lora_send_cmd(cmd,cmd_rsp); } if(is) { return true; }else { return false; } } /********************************************************************************* * function : lora_set_net_id * Description : 设置模组网络 ID * Input : X 为 0-9, 可以设置 0000-9999 * Output : * Author : 祁鑫 Data : 2023 7.18 **********************************************************************************/ bool lora_set_net_id(String id) { bool is = false; String cmd = "AH+I"; String cmd_rsp = "OK+I"; cmd.concat(id); cmd_rsp.concat(id); is = lora_send_cmd(cmd,cmd_rsp); if(is) { Serial.println("lora_set_net_id ok"); return true; }else { Serial.println("lora_set_net_id fail"); return false; } } /********************************************************************************* * function : lora_set_rssi * Description : 设置当前数据包 RSSI 状态 * Input : X=0 RSSI 状态为 OFF, X=1 RSSI 状态为 0N * Output : * Author : 祁鑫 Data : 2023 7.18 **********************************************************************************/ bool lora_set_rssi(int rssi) { bool is = false; String cmd = "AH+Q"; String cmd_rsp = "OK+Q"; if(rssi == 0){ cmd.concat("0"); cmd_rsp.concat("0"); is = lora_send_cmd(cmd,cmd_rsp); }else if(rssi == 1) { cmd.concat("1"); cmd_rsp.concat("1"); is = lora_send_cmd(cmd,cmd_rsp); } if(is) { if(rssi == 0){ Serial.println("lora_set_rssi off ok"); }else if(rssi == 1){ Serial.println("lora_set_rssi on ok"); } return true; }else { if(rssi == 0){ Serial.println("lora_set_rssi off fail"); }else if(rssi == 1){ Serial.println("lora_set_rssi on fail"); } return false; } } #endif void lora_set_config_mode(uint8_t mode) { //printf("%s\r\n",__FUNCTION__); #if 1 uint8_t lorabuf[4]; lorabuf[0]=0x03; lorabuf[1]=0x26; lorabuf[2]=mode; lorabuf[3]=crc8(lorabuf,3,0x55,0x07); lora_sendData(lorabuf,sizeof(lorabuf)); #endif } void lora_set_into_config_mode() { #if 1 uint8_t lorabuf[4]; lorabuf[0]=0x03; lorabuf[1]=0x26; lorabuf[2]=0x01; lorabuf[3]=crc8(lorabuf,3,0x55,0x07); lora_sendData(lorabuf,sizeof(lorabuf)); #endif } void lora_set_exit_config_mode() { #if 1 uint8_t lorabuf[4]; lorabuf[0]=0x03; lorabuf[1]=0x00; lorabuf[2]=0x01; lorabuf[3]=crc8(lorabuf,3,0x55,0x07); lora_sendData(lorabuf,sizeof(lorabuf)); #endif } void lora_set_channel(uint8_t channel) { ESP_LOGW(LOG_TAG," ------>%s channel = %d\r\n",__FUNCTION__,channel); #if 1 uint8_t lorabuf[4]; lorabuf[0]=0x03; lorabuf[1]=0x01; lorabuf[2]=channel; lorabuf[3]=crc8(lorabuf,3,0x55,0x07); lora_sendData(lorabuf,sizeof(lorabuf)); #endif } /**设置无线波特率************************************************************ 范围:0~6 =0, 预留 =1, 预留 =2, 对应 1220bps(默认) =3, 对应 2440bps =4, 对应 5000bps =5, 对应 12500bps =6, 对应 37500bps =其他,无效 设置重启生效,支持掉电保存 越低的无线波特率可以获得更 远的通讯距离 ***************************************************************************/ void lora_set_bps(uint8_t bps) { //printf("%s\r\n",__FUNCTION__); #if 1 uint8_t lorabuf[4]; lorabuf[0]=0x03; lorabuf[1]=0x04; lorabuf[2]=bps; lorabuf[3]=crc8(lorabuf,3,0x55,0x07); lora_sendData(lorabuf,sizeof(lorabuf)); #endif } /*设置透传模式*/ void lora_set_mode(uint8_t mode) { printf("%s\r\n",__FUNCTION__); #if 1 uint8_t lorabuf[4]; lorabuf[0]=0x03; lorabuf[1]=0x08; lorabuf[2]=mode; lorabuf[3]=crc8(lorabuf,3,0x55,0x07); lora_sendData(lorabuf,sizeof(lorabuf)); #endif } /*出厂默认配置回复*/ void lora_factory_config() { printf("%s\r\n",__FUNCTION__); #if 1 uint8_t lorabuf[4]; lorabuf[0]=0x03; lorabuf[1]=0x21; lorabuf[2]=0x01; lorabuf[3]=crc8(lorabuf,3,0x55,0x07); lora_sendData(lorabuf,sizeof(lorabuf)); #endif } void lora_software_reset() { #if 0 uint8_t lorabuf[8]; lorabuf[0]=0x03; lorabuf[1]=0x22; lorabuf[2]=0x01; lorabuf[3]=crc8(lorabuf,3,0x55,0x07); lorabuf[4]=0x00; loraSerial.write((char *)lorabuf,4); Serial.write((char *)lorabuf,4); //delay(100); while(1) { if(lora_packetComplete==true) { lora_packetComplete =false; #if 0 printf("len =%d\r\n",lora_get_len); for(int i = 0;i 0) { // 启动定时器,第二个参数为定时器超时时间,设置为0表示立即启动 if(lora_uart_status == uart_idle) { lora_uart_status = uart_receving; xTimerStart(lora_uart_Timerout, 0); //printf("first rev\r\n"); }else if(lora_uart_status == uart_receving) { if(xTimerIsTimerActive(lora_uart_Timerout) != pdFALSE) { xTimerReset(lora_uart_Timerout,0); //printf("receiving\r\n"); //printf("timer is running"); }else{ //printf("timer is run ok"); } //portTICK_PERIOD_MS if(event.size <= uart_fifo_size) { lora_uart_status = uart_receving; } } receive_len = event.size; int len = uart_read_bytes(UART_NUM_1, dtmp, event.size,/*uart_timerout_times / portTICK_PERIOD_MS*/portMAX_DELAY); for(int i = 0;i