• ESP8266 WiFi物联网智能插座—下位机软件实现


    目录

    1、软件架构

    2、开发环境 

    3、软件功能

    4、程序设计

    4.1、初始化

    4.2、主循环状态机

    4.3、初始化模式

    4.4、配置模式

    4.5、运行模式

    4.6、重启模式

    4.7、升级模式

    5、程序功能特点

    5.1、日志管理

    5.2、数据缓存队列


    本篇博文开始讲解下位机插座节点的MCU软件程序是如何实现。

    1、软件架构

    下位机软件架构采用前后台控制系统,使用状态机思维实现程序设计

    2、开发环境 

    开发环境使用Arduino IDE,IDE安装过程可参见:https://handsome-man.blog.csdn.net/article/details/121195905

    智能插座的控制器是ESP8266,需要在IDE中安装该开发包,如下图所示:

    3、软件功能

    下位机软件整功能如下图所示:

    4、程序设计

    4.1、初始化

    节点上电后会执行初始化,初始化程序顺序执行,代码如下所示:

    1. Init_Log();
    2. Log.verboseln("config start!");
    3. Log.verboseln("init IO");
    4. Init_IO();
    5. Log.verboseln("IO OK!");
    6. Log.verboseln("init EEPROM");
    7. Init_EEPROM();
    8. if(Device_VariableInitial(MODE1) == STATUS_SUCCESS)
    9. {
    10. Log.verboseln("EEPROM OK!");
    11. }
    12. else
    13. {
    14. Log.errorln("EEPROM ERROR!");
    15. }
    16. Log.verboseln("init data queue");
    17. Init_queue();
    18. Log.verboseln("data queue OK!");
    19. Log.verboseln("init WiFi and server");
    20. if(Init_WIFI() == STATUS_SUCCESS)
    21. {
    22. Log.verboseln("WiFi and server OK!");
    23. }
    24. else
    25. {
    26. Log.errorln("WiFi and server ERROR!");
    27. }
    28. Log.verboseln("init time");
    29. Init_Time();
    30. Log.verboseln("time OK!");
    31. Log.verboseln("init electrical parameter");
    32. // Init_BL0942(); // 串口初始化时,已经初始化波特率
    33. Log.verboseln("electrical parameter OK!");
    34. Log.verboseln("config end!");
    35. program_state.run_state = INIT_STATE;

    初始化时候有两点需要注意:

    1、节点的日志打印和采集电参数据使用同一路UART,在正式版本软件中,为了避免出现数据错乱的问题,需要将日志打印功能关闭,使#define LOG_OFF 0

    1. /**
    2. ******************************************************************************
    3. ** \brief 初始化log日志模块
    4. **
    5. ** \param 无
    6. **
    7. ** \retval 无
    8. **
    9. ******************************************************************************/
    10. void Init_Log(void)
    11. {
    12. Serial.begin(4800, SERIAL_8N1); // 4800bps 无校验
    13. Serial.println();
    14. Log.setPrefix(printPrefix); // set prefix similar to NLog
    15. Log.setSuffix(printSuffix); // set suffix
    16. Log.begin(LOG_LEVEL_VERBOSE, &Serial);
    17. Log.setShowLevel(false); // Do not show loglevel, we will do this in the prefix
    18. #if LOG_OFF
    19. DeInit_Log();
    20. #endif
    21. }

     2、E2PROM使用ESP8266内置的Flash模拟。默认情况下,每次线烧程序、OTA升级程序,这部分存储的配置并不会覆盖或者更新,只有上位机下发更新配置参数才会修改。如果想线烧程序更改配置,需要先将标志位#define DEVICE_FLAG   0XAA55修改成非0XAA55的其他数值。

    4.2、主循环状态机

    在主循环中使用1ms周期调度维护软件状态机,节点运行有5种状态模式:初始化模式、配置模式、运行模式、重启模式和升级模式。节点默认处于运行模式,代码如下所示:

    1. switch(program_state.run_state)
    2. {
    3. // 初始化模式
    4. case INIT_STATE:
    5. Init_State();
    6. break;
    7. // 配置模式
    8. case CONFIG_STATE:
    9. Config_State();
    10. break;
    11. // 运行模式
    12. case RUN_STATE:
    13. Run_State();
    14. break;
    15. // 重启模式
    16. case RESET_STATE:
    17. Reset_State();
    18. break;
    19. // 升级模式
    20. case UPDATA_STATE:
    21. Updata_State();
    22. break;
    23. }

    4.3、初始化模式

    初始化模式中初始化一些变量数据。

    初始化模式中,有一个机制,第一次连接立刻上传一次数据到服务器,否则就按照默认的60秒周期上报数据,第一次上报数据会很慢。代码如下所示:

    1. /**
    2. ******************************************************************************
    3. ** \brief 初始化状态逻辑
    4. **
    5. ** \param 无
    6. **
    7. ** \retval 无
    8. **
    9. ******************************************************************************/
    10. void Init_State(void)
    11. {
    12. wifi_send_data.device_head = DeviceParamSave.device_head + FUNCTION_ID1;
    13. wifi_send_data.device_id = DeviceParamSave.device_id;
    14. memcpy(&(wifi_send_data.software_version), &(DeviceParamSave.software_version), 15);
    15. memcpy(&(wifi_send_data.hardware_version), &(DeviceParamSave.hardware_version), 15);
    16. memcpy(&(wifi_send_data.release_time), &(DeviceParamSave.release_time), 10);
    17. wifi_send_data.upload_cycle = DeviceParamSave.upload_cycle;
    18. wifi_send_data.sample_cycle = DeviceParamSave.sample_cycle;
    19. program_state.run_state_time = (DeviceParamSave.upload_cycle * 1000); // 第一次连接立刻上传一次数据到服务器
    20. program_state.run_state = RUN_STATE;
    21. }

    4.4、配置模式

    配置模式可接收上位机下发的配置参数,存储到节点E2PROM中。

    配置模式有超时机制,3分钟上位机未下发配置参数,自动跳转到运行模式。

    更新配置参数后,由配置模式切换到重启模式,节电重启。

    代码如下所示:

    1. /**
    2. ******************************************************************************
    3. ** \brief 配置状态逻辑
    4. **
    5. ** \param 无
    6. **
    7. ** \retval 无
    8. **
    9. ******************************************************************************/
    10. void Config_State(void)
    11. {
    12. program_state.config_state_time++;
    13. if(program_state.config_state_time >= CYCLE_TIME_180SEC)
    14. {
    15. LED_OFF;
    16. program_state.config_state_time = 0;
    17. program_state.run_state = RUN_STATE;
    18. Log.warningln("config timeout");
    19. Log.warningln("switch run state");
    20. }
    21. // 处理WiFi接收的数据
    22. if(wifi_receive_flag == true)
    23. {
    24. if(receive_data[0] == DeviceParamSave.device_head + FUNCTION_ID4)
    25. {
    26. memcpy(&wifi_receive_config, receive_data, sizeof(ReceiveConfig_t));
    27. if((wifi_receive_config.device_old_head == (DeviceParamSave.device_head + FUNCTION_ID4)) &&
    28. (wifi_receive_config.device_old_id == (DeviceParamSave.device_id) || (wifi_receive_config.device_old_id == 0XFFFF)))
    29. {
    30. crc_temp = check_crc16((uint8_t *)&wifi_receive_config, wifi_receive_config.device_len - 2);
    31. if(wifi_receive_config.crc == crc_temp)
    32. {
    33. if(wifi_receive_config.device_config_type == 0) // 默认配置
    34. {
    35. Log.verboseln("default setting...");
    36. DeviceParamSave.device_flag = DEVICE_FLAG;
    37. if((wifi_receive_config.device_new_head != 0) && (wifi_receive_config.device_new_head != DeviceParamSave.device_head))
    38. {
    39. DeviceParamSave.device_head = wifi_receive_config.device_new_head;
    40. }
    41. else
    42. {
    43. Log.verboseln("DEVICE_HEAD 0 or invariant");
    44. }
    45. if((wifi_receive_config.device_new_id != 0) && (wifi_receive_config.device_new_id != DeviceParamSave.device_id))
    46. {
    47. DeviceParamSave.device_id = wifi_receive_config.device_new_id;
    48. }
    49. else
    50. {
    51. Log.verboseln("DEVICE_ID 0 or invariant");
    52. }
    53. if((strcmp(wifi_receive_config.software_version, "") != 0) && (strcmp(wifi_receive_config.software_version, DeviceParamSave.software_version) != 0))
    54. {
    55. memcpy(&(DeviceParamSave.software_version), &(wifi_receive_config.software_version), 15);
    56. }
    57. else
    58. {
    59. Log.verboseln("SW_VERSION null or invariant");
    60. }
    61. if((strcmp(wifi_receive_config.hardware_version, "") != 0) && (strcmp(wifi_receive_config.hardware_version, DeviceParamSave.hardware_version) != 0))
    62. {
    63. memcpy(&(DeviceParamSave.hardware_version), &(wifi_receive_config.hardware_version), 15);
    64. }
    65. else
    66. {
    67. Log.verboseln("HW_VERSION null or invariant");
    68. }
    69. if((strcmp(wifi_receive_config.release_time, "") != 0) && (strcmp(wifi_receive_config.release_time, DeviceParamSave.release_time) != 0))
    70. {
    71. memcpy(&(DeviceParamSave.release_time), &(wifi_receive_config.release_time), 10);
    72. }
    73. else
    74. {
    75. Log.verboseln("RELEASE_TIME null or invariant");
    76. }
    77. if((wifi_receive_config.upload_cycle != 0) && (wifi_receive_config.upload_cycle != DeviceParamSave.upload_cycle))
    78. {
    79. DeviceParamSave.upload_cycle = wifi_receive_config.upload_cycle;
    80. }
    81. else
    82. {
    83. Log.verboseln("UPLOAD_CYCLE 0 or invariant");
    84. }
    85. if((wifi_receive_config.sample_cycle != 0) && (wifi_receive_config.sample_cycle != DeviceParamSave.sample_cycle))
    86. {
    87. DeviceParamSave.sample_cycle = wifi_receive_config.sample_cycle;
    88. }
    89. else
    90. {
    91. Log.verboseln("SAMPLE_CYCLE 0 or invariant");
    92. }
    93. if((strcmp(wifi_receive_config.wifi_ssid, "") != 0) && (strcmp(wifi_receive_config.wifi_ssid, DeviceParamSave.wifi_ssid) != 0))
    94. {
    95. memcpy(&(DeviceParamSave.wifi_ssid), &(wifi_receive_config.wifi_ssid), 64);
    96. }
    97. else
    98. {
    99. Log.verboseln("WIFI_SSID null or invariant");
    100. }
    101. if((strcmp(wifi_receive_config.wifi_password, "") != 0) && (strcmp(wifi_receive_config.wifi_password, DeviceParamSave.wifi_password) != 0))
    102. {
    103. memcpy(&(DeviceParamSave.wifi_password), &(wifi_receive_config.wifi_password), 64);
    104. }
    105. else
    106. {
    107. Log.verboseln("WIFI_PASSWORD null or invariant");
    108. }
    109. if((strcmp(wifi_receive_config.server_ip, "") != 0) && (strcmp(wifi_receive_config.server_ip, DeviceParamSave.server_ip) != 0))
    110. {
    111. memcpy(&(DeviceParamSave.server_ip), &(wifi_receive_config.server_ip), 64);
    112. }
    113. else
    114. {
    115. Log.verboseln("SERVER_IP null or invariant");
    116. }
    117. if((wifi_receive_config.server_port != 0) && (wifi_receive_config.server_port != DeviceParamSave.server_port))
    118. {
    119. DeviceParamSave.server_port = wifi_receive_config.server_port;
    120. }
    121. else
    122. {
    123. Log.verboseln("SERVER_PORT 0 or invariant");
    124. }
    125. }
    126. else if(wifi_receive_config.device_config_type == 1) // 恢复出厂设置
    127. {
    128. Log.verboseln("factory data reset...");
    129. DeviceParamSave.device_flag = DEVICE_FLAG;
    130. DeviceParamSave.device_head = DEVICE_HEAD;
    131. DeviceParamSave.device_id = DEVICE_ID;
    132. memcpy(&(DeviceParamSave.software_version), SW_VERSION, strlen(SW_VERSION));
    133. memcpy(&(DeviceParamSave.hardware_version), HW_VERSION, strlen(HW_VERSION));
    134. memcpy(&(DeviceParamSave.release_time), RELEASE_TIME, strlen(RELEASE_TIME));
    135. DeviceParamSave.upload_cycle = UPLOAD_CYCLE;
    136. DeviceParamSave.sample_cycle = SAMPLE_CYCLE;
    137. memcpy(&(DeviceParamSave.wifi_ssid), WIFI_SSID, strlen(WIFI_SSID));
    138. memcpy(&(DeviceParamSave.wifi_password), WIFI_PASSWORD, strlen(WIFI_PASSWORD));
    139. memcpy(&(DeviceParamSave.server_ip), SERVER_IP, strlen(SERVER_IP));
    140. DeviceParamSave.server_port = SERVER_PORT;
    141. }
    142. DeviceParamSave.crc = check_crc16((uint8_t *)&DeviceParamSave, sizeof(DeviceParamSave_t) - 2);
    143. Log.verboseln("DEVICE_HEAD:0X%X", DeviceParamSave.device_head);
    144. Log.verboseln("DEVICE_ID:0X%X", DeviceParamSave.device_id);
    145. Log.verboseln("SW_VERSION:%S", DeviceParamSave.software_version);
    146. Log.verboseln("HW_VERSION:%S", DeviceParamSave.hardware_version);
    147. Log.verboseln("RELEASE_TIME:%S", DeviceParamSave.release_time);
    148. Log.verboseln("UPLOAD_CYCLE:%d", DeviceParamSave.upload_cycle);
    149. Log.verboseln("SAMPLE_CYCLE:%d", DeviceParamSave.sample_cycle);
    150. Log.verboseln("WIFI_SSID:%S", DeviceParamSave.wifi_ssid);
    151. Log.verboseln("WIFI_PASSWORD:%S", DeviceParamSave.wifi_password);
    152. Log.verboseln("SERVER_IP:%S", DeviceParamSave.server_ip);
    153. Log.verboseln("SERVER_PORT:%d", DeviceParamSave.server_port);
    154. if(Device_SaveParam() == STATUS_SUCCESS)
    155. {
    156. // 成功响应
    157. wifi_send_state.state_id = ((wifi_receive_config.device_old_head - DeviceParamSave.device_head) << 4) + STATUS_SUCCESS;
    158. program_state.config_state_time = 0;
    159. program_state.run_state = RESET_STATE; // 配置成功,重启节点
    160. Log.verboseln("config successful");
    161. Log.verboseln("switch reset state");
    162. }
    163. else
    164. {
    165. // 失败响应
    166. wifi_send_state.state_id = ((wifi_receive_config.device_old_head - DeviceParamSave.device_head) << 4) + STATUS_ERROR;
    167. Log.errorln("config fail");
    168. }
    169. }
    170. else
    171. {
    172. // 失败响应
    173. wifi_send_state.state_id = ((wifi_receive_config.device_old_head - DeviceParamSave.device_head) << 4) + STATUS_ERROR;
    174. Log.errorln("verify error");
    175. }
    176. }
    177. else
    178. {
    179. // 失败响应
    180. wifi_send_state.state_id = ((wifi_receive_config.device_old_head - DeviceParamSave.device_head) << 4) + STATUS_ERROR;
    181. Log.errorln("frame error");
    182. }
    183. // WiFi发送响应组包
    184. wifi_send_state.device_head = DeviceParamSave.device_head + FUNCTION_ID2;
    185. wifi_send_state.device_len = sizeof(SendState_t);
    186. wifi_send_state.device_id = DeviceParamSave.device_id;
    187. memcpy(&(wifi_send_state.software_version), &(DeviceParamSave.software_version), 15);
    188. memcpy(&(wifi_send_state.hardware_version), &(DeviceParamSave.hardware_version), 15);
    189. wifi_send_state.crc = check_crc16((uint8_t *)&wifi_send_state, wifi_send_state.device_len - 2);
    190. WIFI_send_data((char *)&wifi_send_state, wifi_send_state.device_len);
    191. }
    192. // 清除数据缓存
    193. memset(receive_data, 0, wifi_receive_config.device_len);
    194. memset((char *)&wifi_send_state, 0, wifi_send_state.device_len);
    195. memset((char *)&wifi_receive_config, 0, wifi_receive_config.device_len);
    196. wifi_receive_flag = false; // 处理完成后,方可接收WiFi新数据
    197. }
    198. }

    4.5、运行模式

    只有在运行模式下,上位机才可以切换到配置模式、重启模式和升级模式,其他模式暂不支持远程控制模式切换。

    运行模式下可周期上报节点数据,以及支持上位机控制继电器开关。

    代码如下所示:

    1. /**
    2. ******************************************************************************
    3. ** \brief 运行状态逻辑
    4. **
    5. ** \param 无
    6. **
    7. ** \retval 无
    8. **
    9. ******************************************************************************/
    10. void Run_State(void)
    11. {
    12. program_state.run_state_time++;
    13. // 处理WiFi接收的数据
    14. if(wifi_receive_flag == true)
    15. {
    16. if(receive_data[0] == DeviceParamSave.device_head + FUNCTION_ID3)
    17. {
    18. memcpy(&wifi_receive_mode_data, receive_data, sizeof(ReceiveData_Mode_t));
    19. if((wifi_receive_mode_data.device_head == (DeviceParamSave.device_head + FUNCTION_ID3)) &&
    20. (wifi_receive_mode_data.device_id == (DeviceParamSave.device_id) || (wifi_receive_mode_data.device_id == 0XFFFF)))
    21. {
    22. crc_temp = check_crc16((uint8_t *)&wifi_receive_mode_data, wifi_receive_mode_data.device_len - 2);
    23. if(wifi_receive_mode_data.crc == crc_temp)
    24. {
    25. if(wifi_receive_mode_data.switch_mode == 0)
    26. {
    27. program_state.run_state = RUN_STATE;
    28. Log.verboseln("keep run state");
    29. }
    30. else if(wifi_receive_mode_data.switch_mode == 1)
    31. {
    32. RELAY_OFF; // 进入配置模式,要断开继电器
    33. program_state.run_state = CONFIG_STATE;
    34. Log.verboseln("switch config state");
    35. }
    36. else if(wifi_receive_mode_data.switch_mode == 2)
    37. {
    38. RELAY_OFF; // 进入升级模式,要断开继电器
    39. program_state.run_state = UPDATA_STATE;
    40. Log.verboseln("switch updata state");
    41. }
    42. else if(wifi_receive_mode_data.switch_mode == 3)
    43. {
    44. RELAY_OFF; // 进入重启模式,要断开继电器
    45. program_state.run_state = RESET_STATE;
    46. Log.verboseln("switch reset state");
    47. }
    48. // 成功响应
    49. wifi_send_state.state_id = ((wifi_receive_mode_data.device_head - DeviceParamSave.device_head) << 4) + STATUS_SUCCESS;
    50. }
    51. else
    52. {
    53. // 失败响应
    54. wifi_send_state.state_id = ((wifi_receive_mode_data.device_head - DeviceParamSave.device_head) << 4) + STATUS_ERROR;
    55. Log.errorln("verify error");
    56. }
    57. }
    58. else
    59. {
    60. // 失败响应
    61. wifi_send_state.state_id = ((wifi_receive_mode_data.device_head - DeviceParamSave.device_head) << 4) + STATUS_ERROR;
    62. Log.errorln("frame error");
    63. }
    64. // WiFi发送响应组包
    65. wifi_send_state.device_head = DeviceParamSave.device_head + FUNCTION_ID2;
    66. wifi_send_state.device_len = sizeof(SendState_t);
    67. wifi_send_state.device_id = DeviceParamSave.device_id;
    68. memcpy(&(wifi_send_state.software_version), &(DeviceParamSave.software_version), 15);
    69. memcpy(&(wifi_send_state.hardware_version), &(DeviceParamSave.hardware_version), 15);
    70. wifi_send_state.crc = check_crc16((uint8_t *)&wifi_send_state, wifi_send_state.device_len - 2);
    71. WIFI_send_data((char *)&wifi_send_state, wifi_send_state.device_len);
    72. }
    73. if(receive_data[0] == DeviceParamSave.device_head + FUNCTION_ID5)
    74. {
    75. memcpy(&wifi_receive_control_data, receive_data, sizeof(ReceiveData_Control_t));
    76. if((wifi_receive_control_data.device_head == (DeviceParamSave.device_head + FUNCTION_ID5)) &&
    77. (wifi_receive_control_data.device_id == (DeviceParamSave.device_id) || (wifi_receive_control_data.device_id == 0XFFFF)))
    78. {
    79. crc_temp = check_crc16((uint8_t *)&wifi_receive_control_data, wifi_receive_control_data.device_len - 2);
    80. if(wifi_receive_control_data.crc == crc_temp)
    81. {
    82. if(wifi_receive_control_data.relay_state == 0)
    83. {
    84. RELAY_OFF;
    85. }
    86. else
    87. {
    88. RELAY_ON;
    89. }
    90. // 成功响应
    91. wifi_send_state.state_id = ((wifi_receive_control_data.device_head - DeviceParamSave.device_head) << 4) + STATUS_SUCCESS;
    92. }
    93. else
    94. {
    95. // 失败响应
    96. wifi_send_state.state_id = ((wifi_receive_control_data.device_head - DeviceParamSave.device_head) << 4) + STATUS_ERROR;
    97. Log.errorln("verify error");
    98. }
    99. }
    100. else
    101. {
    102. // 失败响应
    103. wifi_send_state.state_id = ((wifi_receive_control_data.device_head - DeviceParamSave.device_head) << 4) + STATUS_ERROR;
    104. Log.errorln("frame error");
    105. }
    106. // WiFi发送响应组包
    107. wifi_send_state.device_head = DeviceParamSave.device_head + FUNCTION_ID2;
    108. wifi_send_state.device_len = sizeof(SendState_t);
    109. wifi_send_state.device_id = DeviceParamSave.device_id;
    110. memcpy(&(wifi_send_state.software_version), &(DeviceParamSave.software_version), 15);
    111. memcpy(&(wifi_send_state.hardware_version), &(DeviceParamSave.hardware_version), 15);
    112. wifi_send_state.crc = check_crc16((uint8_t *)&wifi_send_state, wifi_send_state.device_len - 2);
    113. WIFI_send_data((char *)&wifi_send_state, wifi_send_state.device_len);
    114. }
    115. // 清除数据缓存
    116. memset(receive_data, 0, wifi_receive_control_data.device_len);
    117. memset((char *)&wifi_send_state, 0, wifi_send_state.device_len);
    118. memset((char *)&wifi_receive_mode_data, 0, wifi_receive_mode_data.device_len);
    119. memset((char *)&wifi_receive_control_data, 0, wifi_receive_control_data.device_len);
    120. wifi_receive_flag = false; // 处理完成后,方可接收WiFi新数据
    121. }
    122. // 采集电压、电流和电耗,统计设备有效运行时间
    123. // 在逻辑上设定,采样时间要小于等于上传云端时间
    124. // 此项目中采样周期必须设定为1秒
    125. if((program_state.run_state_time % DeviceParamSave.sample_cycle) == 0)
    126. {
    127. Updata_BL0942();
    128. wifi_send_data.voltage = getVoltage(); // 电压
    129. wifi_send_data.current = getCurrent(); // 电流
    130. wifi_send_data.power = getActivePower(); // 功率
    131. wifi_send_data.electricity = getEnergy(); // 电量
    132. if(wifi_send_data.power > 0.5) // 功率大于0.5W,认为有负载
    133. {
    134. run_start_flag = true;
    135. }
    136. else
    137. {
    138. run_start_flag = false;
    139. }
    140. }
    141. // 上传数据到服务器
    142. if(program_state.run_state_time >= (DeviceParamSave.upload_cycle * 1000))
    143. {
    144. program_state.run_state_time = 0; // 上传周期时间要大于采样周期时间
    145. hours = run_time_ms / 3600000;
    146. minutes = (run_time_ms % 3600000) / 60000;
    147. seconds = (run_time_ms % 60000) / 1000;
    148. time_data = String(hours) + '-' + String(minutes) + '-' + String(seconds);
    149. memcpy(&(wifi_send_data.run_time), time_data.c_str(), time_data.length());
    150. for(uint8_t i = time_data.length(); i < 12; i++)
    151. {
    152. wifi_send_data.run_time[i] = 0x00;
    153. }
    154. wifi_send_data.device_len = sizeof(SendData_t);
    155. wifi_send_data.crc = check_crc16((uint8_t *)&wifi_send_data, wifi_send_data.device_len - 2);
    156. WIFI_send_data((char *)&wifi_send_data, wifi_send_data.device_len);
    157. }
    158. }

    4.6、重启模式

    确保缓存区数据都发送出去并且断开WiFi和服务器连接后节点重启。代码如下所示:

    1. /**
    2. ******************************************************************************
    3. ** \brief 重启状态逻辑
    4. **
    5. ** \param 无
    6. **
    7. ** \retval 无
    8. **
    9. ******************************************************************************/
    10. void Reset_State(void)
    11. {
    12. if(tk_queue_empty(&send_dataqueue) == true) // 确保发送缓存区的数据都发送后才可以重启
    13. {
    14. delay(3000); // 重启节点的ACK可能还未发送出去,需要有延时
    15. DeInit_WIFI();
    16. ESP.restart();
    17. }
    18. }

    4.7、升级模式

    当所有发送缓存区的数据都发送完成后,才可以执行升级功能。

    目前升级仅支持局域网升级,升级前节点会发送升级的IP和端口给上位机。

    升级超时时间默认设置为180秒,超时后节点切换到重启模式。

    代码如下所示:

    1. /**
    2. ******************************************************************************
    3. ** \brief 升级状态逻辑
    4. **
    5. ** \param 无
    6. **
    7. ** \retval 无
    8. **
    9. ******************************************************************************/
    10. void Updata_State(void)
    11. {
    12. static bool state_flag = false;
    13. if(Init_OTA() == STATUS_SUCCESS)
    14. {
    15. if(state_flag == false)
    16. {
    17. state_flag = true;
    18. // WiFi发送升级IP和端口
    19. wifi_send_updata.device_head = DeviceParamSave.device_head + FUNCTION_ID6;
    20. wifi_send_updata.device_len = sizeof(SendUpdata_t);
    21. wifi_send_updata.device_id = DeviceParamSave.device_id;
    22. memcpy(&(wifi_send_updata.software_version), &(DeviceParamSave.software_version), 15);
    23. memcpy(&(wifi_send_updata.hardware_version), &(DeviceParamSave.hardware_version), 15);
    24. memcpy(&(wifi_send_updata.updata_ip), ota_ip, strlen(ota_ip));
    25. wifi_send_updata.updata_port = OTA_PORT;
    26. wifi_send_updata.crc = check_crc16((uint8_t *)&wifi_send_updata, wifi_send_updata.device_len - 2);
    27. WIFI_send_data((char *)&wifi_send_updata, wifi_send_updata.device_len);
    28. memset((char *)&wifi_send_updata, 0, wifi_send_updata.device_len);
    29. }
    30. if(tk_queue_empty(&send_dataqueue) == true) // 确保发送缓存区的数据都发送后才可以升级
    31. {
    32. OTA_updata();
    33. }
    34. }
    35. program_state.updata_state_time++;
    36. if(program_state.updata_state_time >= CYCLE_TIME_180SEC)
    37. {
    38. LED_OFF;
    39. program_state.updata_state_time = 0;
    40. program_state.run_state = RESET_STATE;
    41. Log.warningln("updata timeout");
    42. Log.warningln("switch reset state");
    43. }
    44. }

    5、程序功能特点

    5.1、日志管理

    下位机支持日志管理,可自定义串口打印不同等级的日志。

    不过打印日志的串口和驱动BL0942的串口共用一路,所以在发布正式程序时,需要屏蔽日志打印功能。

    日志管理部分代码如下所示:

    1. /**
    2. ******************************************************************************
    3. ** \brief 初始化log日志模块
    4. **
    5. ** \param 无
    6. **
    7. ** \retval 无
    8. **
    9. ******************************************************************************/
    10. void Init_Log(void)
    11. {
    12. Serial.begin(4800, SERIAL_8N1); // 4800bps 无校验
    13. Serial.println();
    14. Log.setPrefix(printPrefix); // set prefix similar to NLog
    15. Log.setSuffix(printSuffix); // set suffix
    16. Log.begin(LOG_LEVEL_VERBOSE, &Serial);
    17. Log.setShowLevel(false); // Do not show loglevel, we will do this in the prefix
    18. #if LOG_OFF
    19. DeInit_Log();
    20. #endif
    21. }

    5.2、数据缓存队列

    发送和接收数据支持FIFO缓存方式写入和读取数据,可自定义缓存区大小。

    本项目中程序基本是顺序结构运行,不存在外部中断和定时任务对数据的干扰,并且发送和接收数据的数据量也不是很大,即使暂不使用FIFO缓存也可以满足使用要求。

    数据缓存部分代码如下所示:

    1. /**
    2. ******************************************************************************
    3. ** \brief 初始化数据缓存
    4. **
    5. ** \param 无
    6. **
    7. ** \retval 无
    8. **
    9. ******************************************************************************/
    10. void Init_queue(void)
    11. {
    12. // 清空缓冲区
    13. memset(send_dataqueue_pool, 0, SEND_DATAQUEUE_POOL_SIZE);
    14. memset(receive_dataqueue_pool, 0, RECEIVE_DATAQUEUE_POOL_SIZE);
    15. memset(serial_receive_dataqueue_pool, 0, SERIAL_RECEIVE_DATAQUEUE_POOL_SIZE);
    16. // 静态方式创建一个循环队列,存满不能再存
    17. tk_queue_init(&send_dataqueue, send_dataqueue_pool, sizeof(send_dataqueue_pool), sizeof(send_dataqueue_pool[0]), false);
    18. tk_queue_init(&receive_dataqueue, receive_dataqueue_pool, sizeof(receive_dataqueue_pool), sizeof(receive_dataqueue_pool[0]), false);
    19. tk_queue_init(&serial_receive_dataqueue, serial_receive_dataqueue_pool, sizeof(serial_receive_dataqueue_pool), sizeof(serial_receive_dataqueue_pool[0]), false);
    20. }
  • 相关阅读:
    Python之pip命令指定安装源和版本
    SpringBoot3数据库集成
    线性代数的本质笔记
    C++ Reference: Standard C++ Library reference: C Library: cctype: isgraph
    .NET 7 来了!!!
    一问读懂CSS中的绝对定位 包含块 子绝父相 偏移量
    《C++程序设计原理与实践》笔记 第18章 向量和数组
    基于Effect的组件设计 | 京东云技术团队
    计算机网络——带你理解走进计算机网络
    Apache Doris 数据建模之 Aggregate Key 模型
  • 原文地址:https://blog.csdn.net/m0_38106923/article/details/130256406