hmi.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-05-26 Administrator the first version
  9. */
  10. #include <rtthread.h>
  11. #include "hmi.h"
  12. #include <board.h>
  13. #include <control.h>
  14. #include <rtdevice.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "stdarg.h"
  19. #include "string.h"
  20. #include "sht30_collect.h"
  21. #include "beep.h"
  22. #include "easyflash.h"
  23. #include "sound.h"
  24. #include "ymodem.h"
  25. #define DBG_TAG "hmi.c"
  26. #define DBG_LVL LOG_LVL_INFO
  27. #include <shell.h>
  28. #include "dtu_uart2.h"
  29. #include <fal.h>
  30. #define HMI_THREAD_PRIORITY 5 //线程优先级
  31. #define HMI_THREAD_STACK_SIZE 2048 //线程栈空间大小
  32. #define HMI_THREAD_TIMESLICE 10 //时间片
  33. #define CONTROL_MOTOR_PRIORITY 18
  34. #define CONTROL_MOTOR_STACK_SIZE 2048
  35. #define CONTROL_MOTOR_TIMESLICE 5
  36. static char msg_pool[256];
  37. static char rx_buffer[HMI_UART_RXBUFFER_SIZE + 1];
  38. void list_mem(void);
  39. /**/
  40. Device_Status_typedef device_status;
  41. time_t time(time_t *t);
  42. /* 串口接收消息结构*/
  43. struct rx_msg
  44. {
  45. rt_device_t dev;
  46. rt_size_t size;
  47. };
  48. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /*初始化配置参数*/
  49. ////
  50. static rt_thread_t hmi1 = RT_NULL;
  51. static rt_thread_t ctr_motor = RT_NULL;
  52. rt_device_t serial; /*串口设备句柄*/
  53. /* 消息队列控制块 */
  54. static struct rt_messagequeue rx_mq;//接收数据消息队列
  55. static void HMI_Decode(char *cmd,uint8_t cmd_len);
  56. static rt_err_t uart_input(rt_device_t dev, rt_size_t size);
  57. static int HMI_uart4_init(void)
  58. {
  59. serial = rt_device_find(UART_NAME);
  60. /*修改串口配置参数*/
  61. config.baud_rate = BAUD_RATE_115200; //修改波特率9600
  62. config.data_bits = DATA_BITS_8; //数据位8
  63. config.stop_bits = STOP_BITS_1; //停止位1
  64. config.bufsz = 512; //缓冲区128字节
  65. config.parity = PARITY_NONE; //无奇偶校验
  66. /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
  67. rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
  68. // /* step4:打开串口设备。以中断接收及轮询发送模式打开串口设备 */
  69. //rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
  70. /* 以 DMA 接收及轮询发送模式打开串口设备 */
  71. rt_device_open(serial, RT_DEVICE_FLAG_DMA_RX);
  72. return RT_EOK;
  73. }
  74. /*串口屏写数据*/
  75. void HMI_WRITE(char *fmt, ...)
  76. {
  77. va_list ap;
  78. //末尾需要拼接的3个ff
  79. const char hmi_hex[3]={0xff,0xff,0xff};
  80. static char str[MAX_SIZE];
  81. memset(str,'\0',sizeof(str));
  82. rt_size_t str_length;
  83. va_start(ap,fmt);
  84. str_length = rt_vsnprintf(str, sizeof(str) - 1, fmt, ap);
  85. if (str_length > MAX_SIZE - 1)
  86. str_length = MAX_SIZE - 1;
  87. rt_device_write(serial, 0, str, str_length);
  88. va_end(ap);
  89. rt_device_write(serial, 0,hmi_hex,3);
  90. }
  91. /* 接收数据回调函数 */
  92. static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
  93. {
  94. struct rx_msg msg;
  95. rt_err_t result;
  96. msg.dev = dev;
  97. msg.size = size;
  98. result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
  99. if ( result == -RT_EFULL)
  100. {
  101. /* 消息队列满 */
  102. rt_kprintf("message queue full!\n");
  103. }
  104. return result;
  105. }
  106. /*入口函数*/
  107. static void Hmi_thread_entry(void *parameter)
  108. {
  109. struct rx_msg msg;
  110. rt_err_t result;
  111. rt_uint32_t rx_length;
  112. while(1)
  113. {
  114. rt_memset(&msg, 0, sizeof(msg));
  115. /* 从消息队列中读取消息*/
  116. result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), 50);
  117. if (result == RT_EOK)
  118. {
  119. rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
  120. rx_buffer[rx_length] = '\0';
  121. rt_kprintf("%s\n",rx_buffer);
  122. //\r 对应的hex为0x0d,0x0a,占两个字节,屏幕发送的数据都以\r结尾
  123. HMI_Decode(rx_buffer,rx_length);
  124. }
  125. /*********************************************************************************************************************/
  126. //rt_kprintf("%04d-%02d-%02d %02d:%02d:%02d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  127. //rt_kprintf("workSta.vbat.txt=%c%d.%02d%c\n",hmi_str,voltage/100,voltage%100,hmi_str);
  128. }
  129. }
  130. /*******************************************************************************************/
  131. /***************************** 解析从屏幕收到的数据 begin **********************************/
  132. static void HMI_Decode(char *cmd,uint8_t cmd_len)
  133. {
  134. char *var=NULL;
  135. rt_free(var);
  136. if(device_status.refresh_num==0) device_status.refresh_num = 10;
  137. if(strstr(rx_buffer,"getFullData") != NULL){
  138. time_t now = time(RT_NULL);
  139. static struct tm tm;
  140. gmtime_r(&now, &tm);
  141. char *readinfo = makeJson_ReadInfo();
  142. if(device_status.refresh_num==5)
  143. {
  144. device_status.refresh_num = 15;
  145. DTU_Write("%s",readinfo);
  146. list_mem();
  147. device_status.hmi_fresh = 1;
  148. rt_thread_delay(100);
  149. }
  150. //在解析getinfo中清除超时
  151. if(device_status.refresh_timeout==30)
  152. {
  153. rt_sprintf(device_status.sim,"1");
  154. rt_sprintf(device_status.net,"1");
  155. rt_sprintf(device_status.csq,"0");
  156. device_status.link_sta=0;
  157. device_status.refresh_timeout=0;
  158. }
  159. rt_free(readinfo);
  160. device_status.refresh_num--;
  161. device_status.refresh_timeout++;
  162. HMI_WRITE("uploadSet.iccid.txt=\"%s\"",device_status.iccid);
  163. rt_kprintf("上传平台为:%s\n",device_status.mqttcfg1);
  164. if(strstr(device_status.mqttcfg1,"8.136.98.49")!=NULL)
  165. {
  166. HMI_WRITE("uploadSet.plt.val=%d",RESET);
  167. }
  168. else if (strstr(device_status.mqttcfg1,"120.27.222.26")!=NULL) {
  169. HMI_WRITE("uploadSet.plt.val=%d",SET);
  170. }
  171. //HMI_WRITE("mqttcfg.mqtt1Message.txt=\"%s\"",device_status.mqttcfg1);
  172. HMI_WRITE("uploadSet.t0.txt=\"%s\"",device_status.mqttcfg1);
  173. //更新固件版本号
  174. HMI_WRITE("debug.t3.txt=\"%s\"",device_status.driver);
  175. //更新固件编译日期
  176. HMI_WRITE("debug.t4.txt=\"%s\"",device_status.driver_compile);
  177. //
  178. HMI_WRITE("debug.t5.txt=\"%s\"",device_status.driver_bl);
  179. HMI_WRITE("debug.t7.txt=\"%s\"",device_status.driver_compile);
  180. //更新dtu/rtu版本
  181. HMI_WRITE("debug.t9.txt=\"%s\"",device_status.driver_4g);
  182. //更新时间
  183. HMI_WRITE("menu.rtc.txt=\"%04d-%02d-%02d %02d:%02d:%02d\"",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  184. HMI_WRITE("workSta.rtc.txt=\"%04d-%02d-%02d %02d:%02d:%02d\"",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  185. HMI_WRITE("set.t0.txt=\"%d\"",tm.tm_hour);
  186. HMI_WRITE("set.t1.txt=\"%d\"",tm.tm_min);
  187. HMI_WRITE("set.t2.txt=\"%d\"",tm.tm_sec);
  188. //更新电压
  189. HMI_WRITE("workSta.vbat.txt=\"%s\"",device_status.voltage);
  190. //更新电流
  191. HMI_WRITE("workSta.power.txt=\"%s\"",device_status.power);
  192. //更新温湿度数据
  193. HMI_WRITE("workSta.envHum.val=%d",atoi(device_status.humidity));
  194. HMI_WRITE("workSta.envTem.val=%d",atoi(device_status.tempture));
  195. //更新电池状态
  196. HMI_WRITE("workSta.batStatus.val=%d",device_status.bat_status);
  197. //更新上仓门状态
  198. HMI_WRITE("workSta.upStatus.val=%d",device_status.up_status);
  199. //更新下仓门状态
  200. HMI_WRITE("workSta.downStatus.val=%d",device_status.down_status);
  201. //更新工作状态
  202. HMI_WRITE("workSta.workStatus.val=%d",device_status.work_status);
  203. //更新温控状态
  204. HMI_WRITE("workSta.tempStatus.val=%d",device_status.temp_status);
  205. //更新雨控状态
  206. HMI_WRITE("workSta.rainStatus.val=%d",device_status.rain_status);
  207. //更新光控状态
  208. HMI_WRITE("workSta.sunStatus.val=%d",device_status.sun_status);
  209. //更新有线连接状态
  210. HMI_WRITE("menu.va_net.val=%d",device_status.link_sta);
  211. //更新CSQ
  212. HMI_WRITE("menu.csq_value.txt=\"%s\"",device_status.csq);
  213. HMI_WRITE("menu.va_csq.val=%d",atoi(device_status.csq));
  214. HMI_WRITE("workSta.csq_value.txt=\"%s\"",device_status.csq);
  215. //更新收集时间
  216. HMI_WRITE("sysSet.stime.val=%d",device_status.clt_time);
  217. //更新加热时间
  218. HMI_WRITE("workSta.heatTime.val=%d",device_status.heattime);
  219. HMI_WRITE("sysSet.jtime.val=%d",device_status.heattime);
  220. //
  221. HMI_WRITE("sysSet.vol.val=%d",device_status.vol);
  222. //更新加热时间和温度
  223. HMI_WRITE("workSta.heatTem.val=%d",device_status.heattem);
  224. HMI_WRITE("sysSet.jheat.val=%d",device_status.heatvalue);
  225. //更新加热状态
  226. HMI_WRITE("workSta.heatStatus.val=%d",device_status.heat_status);
  227. //更新熄屏时间
  228. HMI_WRITE("thsp=%d",device_status.thsp);
  229. //更新密码
  230. static char pwd_char[10];
  231. //整型转字符串
  232. itoa(device_status.passWord,pwd_char,10);
  233. HMI_WRITE("inputPasswd.pwdData.txt=\"%s\"",pwd_char);
  234. //更新时间设置中的参数
  235. //更新定时模式
  236. HMI_WRITE("clockSet.timeSwitch.val=%d",device_status.timeSwitch);
  237. //更新光控定时时长
  238. HMI_WRITE("clockSet.sunBar.val=%d",device_status.sunBar);
  239. //更新时控开始
  240. HMI_WRITE("clockSet.startBar.val=%d",device_status.startBar);
  241. //更新时控结束
  242. HMI_WRITE("clockSet.stopBar.val=%d",device_status.stopBar);
  243. //SIM卡状态
  244. HMI_WRITE("workSta.bt0.val=%d",atoi(device_status.sim));
  245. //ID
  246. HMI_WRITE("menu.equipid.txt=\"%s\"",device_status.imei);
  247. HMI_WRITE("workSta.equipid.txt=\"%s\"",device_status.imei);
  248. //数据上传间隔
  249. HMI_WRITE("uploadSet.datt.val=%d",device_status.dattim);
  250. //语音音量
  251. HMI_WRITE("sysSet.vol.val=%d",device_status.vol);
  252. //更新联网状态
  253. if(atoi(device_status.net)==1)
  254. {
  255. HMI_WRITE("menu.net.val=1");
  256. HMI_WRITE("workSta.net.val=1");
  257. }
  258. else {
  259. HMI_WRITE("workSta.net.val=0");
  260. HMI_WRITE("menu.net.val=0");
  261. }
  262. if(atoi(device_status.csq)==0&&atoi(device_status.net)==1)
  263. {
  264. HMI_WRITE("menu.net.val=1");
  265. HMI_WRITE("workSta.net.val=2");
  266. }
  267. /**********************************吸虫塔*********************************/
  268. //更新电压
  269. HMI_WRITE("menu.vbat.txt=\"%sV\"",device_status.voltage);
  270. //更新震动时间
  271. HMI_WRITE("menu.shake_sec.val=%d",device_status.shake_sec);
  272. //更新电流
  273. HMI_WRITE("menu.power.txt=\"%smA\"",device_status.power);
  274. //更新温湿度数据
  275. HMI_WRITE("menu.envHum.val=%d",atoi(device_status.humidity));
  276. HMI_WRITE("menu.envTem.val=%d",atoi(device_status.tempture));
  277. //SIM卡状态
  278. HMI_WRITE("menu.bt0.val=%d",atoi(device_status.sim));
  279. //数据上传间隔
  280. HMI_WRITE("menu.datt.val=%d",device_status.dattim);
  281. /**********************************************************************/
  282. }
  283. else if (strstr(rx_buffer,"getClockData")!=NULL) {
  284. //更新定时模式
  285. HMI_WRITE("clockSet.timeSwitch.val=%d",device_status.timeSwitch);
  286. //更新光控定时时长
  287. HMI_WRITE("clockSet.sunBar.val=%d",device_status.sunBar);
  288. //更新时控开始
  289. HMI_WRITE("clockSet.startBar.val=%d",device_status.startBar);
  290. //更新时控结束
  291. HMI_WRITE("clockSet.stopBar.val=%d",device_status.stopBar);
  292. }
  293. else if (strstr(rx_buffer,"button")!=NULL) {
  294. rt_kprintf("按键!\n");
  295. DTU_Write("button");
  296. }
  297. //拍照
  298. else if(strstr(rx_buffer,"putTest.switchPhoto=1")!=NULL) {
  299. device_status.photo_ws = 1;
  300. }
  301. //清扫
  302. else if (strstr(rx_buffer,"clear")!=NULL) {
  303. rt_kprintf("\n清扫!\n");
  304. if(device_status.version_type==4||device_status.version_type==41)
  305. {
  306. device_status.clear_status=1;
  307. }
  308. else if (device_status.version_type==1) {
  309. turnzp();
  310. }
  311. else if (device_status.version_type==6) {
  312. ctr_JD_Clear();
  313. }
  314. }
  315. //震动
  316. else if (strstr(rx_buffer,"putTest.switchShake=1")!=NULL) {
  317. rt_kprintf("\n震动!\n");
  318. if(device_status.version_type==6)
  319. {
  320. Motor_shake(ON, device_status.shake_sec);
  321. }
  322. else{
  323. ctr_shake(ON,device_status.shake_sec);
  324. }
  325. }
  326. //打开补光
  327. else if (strstr(rx_buffer,"putTest.switchFill=1")!=NULL) {
  328. rt_kprintf("\n打开补光!!!\n");
  329. ctr_led(ON);
  330. }
  331. //关闭补光
  332. else if (strstr(rx_buffer,"putTest.switchFill=0")!=NULL) {
  333. rt_kprintf("\n关闭补光!!!\n");
  334. ctr_led(OFF);
  335. }
  336. //打开灯管
  337. else if (strstr(rx_buffer,"putTest.switchLamp=1")!=NULL)
  338. {
  339. device_status.test_flag=1;
  340. rt_kprintf("Open Lamp!\n");
  341. ctr_lamp(ON,1);
  342. #if LAMP_EN == 1
  343. ctr_lamp(ON,2);
  344. ctr_lamp(ON,3);
  345. #endif
  346. }
  347. //关闭灯管
  348. else if (strstr(rx_buffer,"putTest.switchLamp=0")!=NULL)
  349. {
  350. device_status.test_flag=0;
  351. rt_kprintf("Close Lamp1!\n");
  352. ctr_lamp(OFF,1);
  353. #if LAMP_EN == 1
  354. ctr_lamp(OFF,2);
  355. ctr_lamp(OFF,3);
  356. #endif
  357. }
  358. //关闭加热
  359. else if(strstr(rx_buffer,"putTest.switchHeat=0")!=NULL) {
  360. device_status.heat_status = 0;
  361. ctr_Heat(OFF);
  362. rt_kprintf("Close Heat!\n");
  363. }
  364. //打开加热
  365. else if(strstr(rx_buffer,"putTest.switchHeat=1")!=NULL) {
  366. device_status.heat_status = 1;
  367. ctr_Heat(ON);
  368. rt_kprintf("Open Heat!\n");
  369. }
  370. //打开上仓门
  371. else if(strstr(rx_buffer,"putTest.switchUp=1")!=NULL){
  372. rt_kprintf("Open UP!\n");
  373. ctr_cangmen_up(OFF);
  374. device_status.ctr_up_motor = 1;
  375. }
  376. //关闭上仓门
  377. else if(strstr(rx_buffer,"putTest.switchUp=0")!=NULL){
  378. rt_kprintf("CLOSE UP!\n");
  379. ctr_cangmen_up(OFF);
  380. device_status.ctr_up_motor = 2;
  381. }
  382. //打开下仓门
  383. else if(strstr(rx_buffer,"putTest.switchDown=1")!=NULL){
  384. ctr_cangmen_down(OFF);
  385. device_status.ctr_down_motor = 1;
  386. rt_kprintf("Open down!\n");
  387. }
  388. //关闭下仓门
  389. else if(strstr(rx_buffer,"putTest.switchDown=0")!=NULL){
  390. ctr_cangmen_down(OFF);
  391. device_status.ctr_down_motor = 2;
  392. rt_kprintf("CLOSE down!\n");
  393. }
  394. //定时设置
  395. else if (strstr(rx_buffer,"saveClockSet=")!=NULL) {
  396. char sun_str[3],start_str[3],stop_str[3],timeswitch_str[3];
  397. rt_kprintf("时间设置!\n");
  398. var=strstr(rx_buffer,"=");
  399. var++;
  400. strncpy(sun_str,var,2);
  401. if(atoi(sun_str) > 0 && atoi(sun_str) <12)
  402. {
  403. #if LAMP_EN == 0
  404. device_status.sunBar=atoi(sun_str);
  405. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
  406. #else
  407. rt_kprintf("光控定时设置失败!!!,三光源模式!!\n");
  408. #endif
  409. // time_t now;
  410. // time(&now);
  411. // static struct tm nowstamp;
  412. // nowstamp =*localtime(&now);
  413. // nowstamp.tm_hour = nowstamp.tm_hour - 8;
  414. // static rt_uint32_t nowstamp_num = 0;
  415. // nowstamp_num = mktime(&nowstamp);
  416. // device_status.light_over_tim = device_status.sunBar * 3600 + nowstamp_num;
  417. }
  418. else {
  419. rt_kprintf("参数设置异常(光控时长):%d!!!%d",atoi(sun_str));
  420. }
  421. //时控开始时间
  422. strncpy(start_str,var+2,2);
  423. if(atoi(start_str) >= 0 && atoi(start_str) < 24)
  424. {
  425. device_status.startBar=atoi(start_str);
  426. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
  427. }
  428. else {
  429. rt_kprintf("参数设置异常(开始):%d!!!",atoi(start_str));
  430. }
  431. //
  432. strncpy(stop_str,var+4,2);
  433. if(atoi(stop_str) >= 0&& atoi(stop_str) < 24)
  434. {
  435. device_status.stopBar=atoi(stop_str);
  436. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
  437. }
  438. else {
  439. rt_kprintf("参数设置异常(结束):%d!!!",atoi(stop_str));
  440. }
  441. //
  442. strncpy(timeswitch_str,var+7,2);
  443. if(atoi(timeswitch_str) >= 0 && atoi(timeswitch_str) <= 1)
  444. {
  445. #if LAMP_EN == 0
  446. device_status.timeSwitch=atoi(timeswitch_str);
  447. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  448. #endif
  449. }
  450. else {
  451. rt_kprintf("参数设置异常(模式):%d!!!",atoi(timeswitch_str));
  452. }
  453. MA_save();
  454. rt_free(var);
  455. rt_kprintf("工作时间参数已保存:模式-%d,光控时间-%d,时控开始时间-%d,时控结束时间-%d\n",device_status.timeSwitch,device_status.sunBar,device_status.startBar,device_status.stopBar);
  456. }
  457. else if (strstr(rx_buffer,"saveSystemSet")!=NULL) {
  458. char clt_time_str[3],heattime_str[3],heatvale_str[4],vol_str[4],thsp_str[5];
  459. rt_kprintf("系统设置!\n");
  460. var = strstr(rx_buffer,"=");
  461. var ++;
  462. strncpy(clt_time_str,var,2);
  463. if(atoi(clt_time_str)>0 && atoi(clt_time_str) <= 30)
  464. {
  465. device_status.clt_time = atoi(clt_time_str); //收集时间
  466. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
  467. }
  468. strncpy(heattime_str,var+2,2);
  469. if(atoi(heattime_str)>0 && atoi(heattime_str) <= 30)
  470. {
  471. rt_kprintf("加热时间:%d\n",atoi(heattime_str));
  472. device_status.heattime = atoi(heattime_str); //加热时间
  473. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
  474. }
  475. strncpy(heatvale_str,var+4,3);
  476. if(atoi(heatvale_str) > 0 && atoi(heatvale_str) <= 130)
  477. {
  478. device_status.heatvalue=atoi(heatvale_str);
  479. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  480. }
  481. strncpy(vol_str,var+7,3);
  482. if(atoi(vol_str)>0&&atoi(vol_str)<8)
  483. {
  484. device_status.vol = atoi(vol_str); //音量
  485. DTU_Write("volume=");
  486. }
  487. strncpy(thsp_str,var+10,4);
  488. if(atoi(thsp_str)>0&&atoi(thsp_str)<3600)
  489. {
  490. device_status.thsp = atoi(thsp_str); //熄屏时间
  491. }
  492. else {
  493. rt_kprintf("参数设置失败!!!\n");
  494. }
  495. MA_save();
  496. rt_free(var);
  497. rt_kprintf("系统设置已保存:收集时间-%d,加热时间-%d,加热温度-%d,音量-%d,自动关屏-%d\n",device_status.clt_time,device_status.heattime,device_status.heatvalue,device_status.vol,device_status.thsp);
  498. }
  499. else if (strstr(rx_buffer,"rtc_h=")!=NULL) {
  500. char rtc_h[2];
  501. var = strstr(rx_buffer,"=");
  502. var++;
  503. strncpy(rtc_h,var,2);
  504. if(atoi(rtc_h)>=0 && atoi(rtc_h) <= 24)
  505. {
  506. set_time(atoi(rtc_h), 99, 99);
  507. }
  508. }
  509. else if (strstr(rx_buffer,"rtc_m=")!=NULL) {
  510. char rtc_m[2];
  511. var = strstr(rx_buffer,"=");
  512. var++;
  513. strncpy(rtc_m,var,2);
  514. if(atoi(rtc_m)>=0 && atoi(rtc_m) <= 24)
  515. {
  516. set_time(99, atoi(rtc_m), 99);
  517. }
  518. }
  519. else if (strstr(rx_buffer,"rtc_s=")!=NULL) {
  520. char rtc_s[2];
  521. var = strstr(rx_buffer,"=");
  522. var++;
  523. strncpy(rtc_s,var,2);
  524. if(atoi(rtc_s)>=0 && atoi(rtc_s) <= 24)
  525. {
  526. set_time(99, 99, atoi(rtc_s));
  527. }
  528. }
  529. else if(strstr(rx_buffer,"saveUploadSet=")!=NULL)
  530. {
  531. static char dattim_str[3];
  532. rt_kprintf("上传间隔设置!\n");
  533. var = strstr(rx_buffer,"=");
  534. var ++;
  535. strncpy(dattim_str,var,2);
  536. if(atoi(dattim_str)>=1&&atoi(dattim_str)<=120)
  537. {
  538. device_status.dattim =atoi(dattim_str); //数据上传间隔
  539. ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim));
  540. MA_save();
  541. }
  542. else {
  543. rt_kprintf("设置失败!!!\n");
  544. }
  545. rt_free(var);
  546. rt_kprintf("数据上传设置:%d\n",device_status.dattim);
  547. }
  548. //光控测试
  549. else if(strstr(rx_buffer,"putTest.switchSun=1")!=NULL)
  550. {
  551. rt_kprintf("光控测试!!!");
  552. ctr_lamp(ON,1);
  553. device_status.sun_test = 1;
  554. }
  555. else if (strstr(rx_buffer,"shakesec=")!=NULL)
  556. {
  557. static char shakesec_str[4];
  558. rt_kprintf("震动时间设置!\n");
  559. var = strstr(rx_buffer,"=");
  560. var ++;
  561. strncpy(shakesec_str,var,3);
  562. if(atoi(shakesec_str)>=1&&atoi(shakesec_str)<=1000)
  563. {
  564. device_status.shake_sec =atoi(shakesec_str); //
  565. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec));
  566. MA_save();
  567. }
  568. else {
  569. rt_kprintf("设置失败!!!\n");
  570. }
  571. rt_free(var);
  572. rt_kprintf("震动时间设置:%d\n",device_status.shake_sec);
  573. }
  574. //切换大数据平台
  575. else if (strstr(rx_buffer,"8.136.98.49")!=NULL)
  576. {
  577. rt_sprintf(device_status.mqttcfg1, "8.136.98.49");
  578. ef_set_env_blob("platform", &device_status.mqttcfg1, sizeof(device_status.mqttcfg1));
  579. //给RTU发送切换指令
  580. DTU_Write("%s",makeJson_platform("8.136.98.49"));
  581. rt_free(makeJson_platform("8.136.98.49"));
  582. rt_thread_delay(2000);
  583. MA_save();
  584. }
  585. else if (strstr(rx_buffer,"120.27.222.26")!=NULL)
  586. {
  587. rt_sprintf(device_status.mqttcfg1, "120.27.222.26");
  588. ef_set_env_blob("platform", &device_status.mqttcfg1, sizeof(device_status.mqttcfg1));
  589. //给RTU发送切换指令
  590. DTU_Write("%s",makeJson_platform("120.27.222.26"));
  591. rt_free(makeJson_platform("120.27.222.26"));
  592. rt_thread_delay(2000);
  593. MA_save();
  594. }
  595. //恢复出厂设置电池
  596. else if (strstr(rx_buffer,"resetdcDefault")!=NULL)
  597. {
  598. ResetParamDefault_DC();
  599. MA_save();
  600. }
  601. //恢复出厂设置-交流
  602. else if (strstr(rx_buffer,"resetDefault")!=NULL)
  603. {
  604. ResetParamDefault();
  605. MA_save();
  606. }
  607. //获取SIM卡信息
  608. else if(strstr(rx_buffer,"getSim")!=NULL)
  609. {
  610. //http://sim.openluat.com/api/wechat/openapidoc
  611. DTU_Write("getsiminfo");
  612. }
  613. else if (strstr(rx_buffer,"DebugEnable")!=NULL) {
  614. Debug_log_ON();
  615. }
  616. else if (strstr(rx_buffer,"DebugDisable")!=NULL) {
  617. Debug_log_OFF();
  618. }
  619. //1.0
  620. else if (strstr(rx_buffer,"SwversionV1")!=NULL)
  621. {
  622. if(device_status.version_type != 1)
  623. {
  624. device_status.version_type = 1;
  625. sprintf(device_status.driver, "%s",VERSION_ZP);
  626. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  627. MA_save();
  628. }
  629. else {
  630. }
  631. }
  632. //高空
  633. else if (strstr(rx_buffer,"SwversionV11")!=NULL)
  634. {
  635. if(device_status.version_type != 11)
  636. {
  637. device_status.version_type = 11;
  638. sprintf(device_status.driver, "%s",VERSION_ZP);
  639. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  640. MA_save();
  641. }
  642. else {
  643. }
  644. }
  645. //4.0
  646. else if (strstr(rx_buffer,"SwversionV41")!=NULL) {
  647. if(device_status.version_type != 41)
  648. {
  649. device_status.version_type = 41;
  650. sprintf(device_status.driver, "%s",VERSION_HK);
  651. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  652. MA_save();
  653. }
  654. else {
  655. }
  656. }
  657. //水稻
  658. else if (strstr(rx_buffer,"SwversionV4")!=NULL) {
  659. if(device_status.version_type != 4)
  660. {
  661. device_status.version_type = 4;
  662. sprintf(device_status.driver, "%s",VERSION_HK_2000W);
  663. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  664. MA_save();
  665. }
  666. else {
  667. }
  668. }
  669. //5.0测报灯
  670. else if (strstr(rx_buffer,"SwversionV5")!=NULL) {
  671. if(device_status.version_type!=5)
  672. {
  673. device_status.version_type = 5;
  674. sprintf(device_status.driver, "%s",VERSION_V5);
  675. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  676. MA_save();
  677. }
  678. }
  679. //吸虫塔
  680. else if (strstr(rx_buffer,"SwversionV6")!=NULL) {
  681. if(device_status.version_type != 6)
  682. {
  683. device_status.version_type = 6;
  684. sprintf(device_status.driver, "%s",VERSION_XCT);
  685. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  686. MA_save();
  687. }
  688. else {
  689. }
  690. }
  691. else if(strstr(rx_buffer,"DisplayPower")!=RT_NULL)
  692. {
  693. HMI_WRITE("sysTest.sysTest_power.txt=\"%s\"",device_status.power);
  694. // HMI_WRITE("sysTest.switchHeat.val=%d",device_status.heat_status);
  695. }
  696. //立即上传
  697. else if(strstr(rx_buffer,"uploaddata")!=RT_NULL)
  698. {
  699. UpLoad_Data();
  700. }
  701. else if (strstr(rx_buffer,"checkupdate")!=NULL)
  702. {
  703. time_t now = time(RT_NULL);
  704. static struct tm tm;
  705. gmtime_r(&now, &tm);
  706. HMI_WRITE("hardcheck.tip_rtc.txt=\"%04d-%02d-%02d %02d:%02d:%02d\"",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  707. HMI_WRITE("hardcheck.p0.pic=%d",70);
  708. //RTC时间判断,如果当前年份小于2020,提示需要设置时间
  709. if(tm.tm_year+1900<2020)
  710. {
  711. rt_kprintf("时间未设置:%04d-%02d-%02d %02d:%02d:%02d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  712. HMI_WRITE("hardcheck.tip_rtc.txt=%04d-%02d-%02d %02d:%02d:%02d",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  713. }
  714. }
  715. //进入bootloader模式
  716. else if (strstr(rx_buffer,"bootloader")!=NULL) {
  717. device_status.stop_in_bootloader = 1;
  718. ef_set_env_blob("stop_in_bootloader", &device_status.stop_in_bootloader, sizeof(device_status.stop_in_bootloader));
  719. rt_hw_cpu_reset();
  720. }
  721. //软件重启
  722. else if (strstr(rx_buffer,"reboot")!=NULL) {
  723. rt_hw_cpu_reset();
  724. }
  725. //屏幕上触发远程升级
  726. else if (strstr(rx_buffer,"hmi_update")!=NULL) {
  727. DTU_Write("hmi_update");
  728. device_status.update_flag = 1;
  729. ef_set_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag));
  730. rt_thread_delay(2000);
  731. rt_hw_cpu_reset();
  732. }
  733. }
  734. //入口函数-电机控制及
  735. static void Ctr_Mortor_entry(void* parameter)
  736. {
  737. //rt_uint32_t limit_current = 3000; //推杆电击限制电流1A
  738. rt_uint32_t rtc_err_cnt = 0;
  739. static rt_uint16_t power_current = 0;
  740. static rt_uint16_t num_up_open = 200; //20s
  741. static rt_uint16_t num_up_close = 200; //
  742. static rt_uint16_t num_down_open = 200; //
  743. static rt_uint16_t num_down_close = 200; //
  744. static rt_uint16_t photo_led_sec = 450; //45S
  745. while(1)
  746. {
  747. time_t time_reset12V;
  748. time(&time_reset12V);
  749. static struct tm alarm_reset12V;
  750. alarm_reset12V =*localtime(&time_reset12V);
  751. if((alarm_reset12V.tm_hour)==5&&(alarm_reset12V.tm_min==59)&&(alarm_reset12V.tm_sec==55))
  752. {
  753. ctr_RTU_DTU(OFF);
  754. rt_thread_delay(1000*10);
  755. ctr_RTU_DTU(ON);
  756. }
  757. else if((alarm_reset12V.tm_hour)==0&&(alarm_reset12V.tm_min==0)&&(alarm_reset12V.tm_sec==0))
  758. {
  759. rtc_err_cnt ++;
  760. if(rtc_err_cnt==300000)
  761. {
  762. ctr_RTU_DTU(OFF);
  763. rt_thread_delay(1000*10);
  764. ctr_RTU_DTU(ON);
  765. rt_hw_cpu_reset();
  766. }
  767. }
  768. if(device_status.ctr_up_motor==1)
  769. {
  770. //打开上仓门
  771. ctr_cangmen_up(OPEN);
  772. num_up_open--;
  773. //如果未工作且上仓门电机电流大于1A,立即停止,记录到Flash,上报预警
  774. //2022-03-14-add:&&atoi(device_status.power)<8000
  775. // if(atoi(device_status.power)>limit_current&&atoi(device_status.power)<8000)
  776. // {
  777. // ctr_cangmen_up(OFF);
  778. // num_up_open = 200;
  779. // rt_kprintf("*************************上仓门电机打开堵转!!!**********************%d\n",atoi(device_status.power));
  780. // }
  781. if (num_up_open == 0)
  782. {
  783. ctr_cangmen_up(OFF);
  784. num_up_open = 200;
  785. }
  786. }
  787. else if(device_status.ctr_up_motor==2)
  788. {
  789. ctr_cangmen_up(CLOSE);
  790. num_up_close --;
  791. //2022-03-14:&&atoi(device_status.power)<8000
  792. // if(atoi(device_status.power)>limit_current&&atoi(device_status.power)<8000)
  793. // {
  794. // ctr_cangmen_up(OFF);
  795. // num_up_close = 200;
  796. // rt_kprintf("************************上仓门电机关闭堵转!!!**********************%d\n",atoi(device_status.power));
  797. // }
  798. if (num_up_close == 0)
  799. {
  800. ctr_cangmen_up(OFF);
  801. num_up_close= 200;
  802. }
  803. }
  804. if (device_status.ctr_down_motor == 1)
  805. {
  806. if(num_down_open==200)
  807. {
  808. power_current = atoi(device_status.power);
  809. }
  810. ctr_cangmen_down(OPEN);
  811. num_down_open--;
  812. // if(atoi(device_status.power)>limit_current)
  813. // {
  814. // ctr_cangmen_down(OFF);
  815. // num_down_open = 200;
  816. // rt_kprintf("*************************下仓门电机打开堵转!!!**********************%d\n",atoi(device_status.power));
  817. // }
  818. //如果关闭仓门1S后检测不到电流增大,则判定为异常
  819. // if (atoi(device_status.power)<(power_current+100)&&num_down_open==190&&device_status.work_status==1) {
  820. // DTU_Write("%s",makeJson_Warn("Down Motor OPEN ERR","device_Down_TG"));
  821. // rt_free(makeJson_Warn("Down Motor OPEN ERR","device_Down_TG"));
  822. // }
  823. if (num_down_open == 0)
  824. {
  825. ctr_cangmen_down(OFF);
  826. num_down_open = 200;
  827. }
  828. }
  829. else if (device_status.ctr_down_motor == 2)
  830. {
  831. if(num_down_close==200)
  832. {
  833. power_current = atoi(device_status.power);
  834. }
  835. ctr_cangmen_down(CLOSE);
  836. num_down_close--;
  837. //2022-03-14-add:&&atoi(device_status.power)<8000
  838. // if(atoi(device_status.power)>limit_current&&atoi(device_status.power)<8000)
  839. // {
  840. // ctr_cangmen_down(OFF);
  841. // num_down_close = 200;
  842. // rt_kprintf("*************************下仓门电机关闭堵转!!!**********************%d\n",atoi(device_status.power));
  843. // }
  844. //如果关闭仓门1S后检测不到电流增大,则判定为异常
  845. // if (atoi(device_status.power)<(power_current+100)&&num_down_close==190&&device_status.work_status==1) {
  846. // DTU_Write("%s",makeJson_Warn("Down Motor CLOSE ERR","device_Down_TG"));
  847. // rt_free(makeJson_Warn("Down Motor CLOSE ERR","device_Down_TG"));
  848. // }
  849. if (num_down_close == 0)
  850. {
  851. ctr_cangmen_down(OFF);
  852. num_down_close = 200;
  853. }
  854. }
  855. if(device_status.sun_test==1)
  856. {
  857. SunTest();
  858. }
  859. if(device_status.photo_ws==1)
  860. {
  861. //打开补光灯
  862. if(photo_led_sec==100)
  863. {
  864. ctr_led(ON);
  865. DTU_Write("takephoto.mp3");
  866. //延时
  867. rt_thread_delay(1000);
  868. rt_kprintf("\n测试拍照!\n");
  869. Hk_Take_io();
  870. }
  871. photo_led_sec--;
  872. if(photo_led_sec==0)
  873. {
  874. ctr_led(OFF);
  875. photo_led_sec = 100;
  876. device_status.photo_ws = 0;
  877. HMI_WRITE("sysTest.switchPhoto.val=0");
  878. HMI_WRITE("sysTest.switchFill.val=0");
  879. }
  880. }
  881. rt_thread_delay(100);
  882. //rt_kprintf("action num is:%d%d%d",num_up_open,num_up_close,num_down_open,num_down_close,num_clear);
  883. }
  884. }
  885. /*屏幕写数据*/
  886. int Hmi_Fresh(void)
  887. {
  888. //初始化串口4
  889. rt_err_t ret = RT_EOK;
  890. /* 初始化消息队列 */
  891. rt_mq_init(&rx_mq, "rx_mq",
  892. msg_pool, /* 存放消息的缓冲区 */
  893. sizeof(struct rx_msg), /* 一条消息的最大长度 */
  894. sizeof(msg_pool), /* 存放消息的缓冲区大小 */
  895. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  896. /* 设置接收回调函数 */
  897. rt_device_set_rx_indicate(serial, uart_input);
  898. /* 创建线程*/
  899. hmi1 = rt_thread_create("Hmi_Fresh_thread",
  900. Hmi_thread_entry,
  901. RT_NULL,
  902. HMI_THREAD_STACK_SIZE,
  903. HMI_THREAD_PRIORITY,
  904. HMI_THREAD_TIMESLICE);
  905. /*部分控制的线程*/
  906. ctr_motor = rt_thread_create("Ctr motor",
  907. Ctr_Mortor_entry,
  908. RT_NULL,
  909. CONTROL_MOTOR_STACK_SIZE,
  910. CONTROL_MOTOR_PRIORITY,
  911. CONTROL_MOTOR_TIMESLICE);
  912. if(ctr_motor != RT_NULL)
  913. {
  914. rt_thread_startup(ctr_motor);
  915. }
  916. /* 如果获得线程控制块,启动这个线程 */
  917. if (hmi1 != RT_NULL)
  918. {
  919. rt_thread_startup(hmi1);
  920. }
  921. else
  922. {
  923. ret = RT_ERROR;
  924. }
  925. return ret;
  926. }
  927. void Check_ParamConfig(void)
  928. {
  929. time_t now = time(RT_NULL);
  930. static struct tm tm;
  931. gmtime_r(&now, &tm);
  932. if(easyflash_init()==EF_NO_ERR) //初始化成功
  933. {
  934. ef_get_env_blob("reboot_time", &device_status.rebootcnt, sizeof(device_status.rebootcnt), NULL); //读出reboot_time的值
  935. ef_get_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag), NULL); //读出updateflag的值
  936. ef_get_env_blob("iSave", &device_status.iSave, sizeof(device_status.iSave), NULL); //判断是否用户配置了程序
  937. device_status.rebootcnt++;
  938. ef_set_env_blob("reboot_time", &device_status.rebootcnt, sizeof(device_status.rebootcnt));
  939. rt_kprintf("REBOOT_TIME:%d-%d-%d\n",device_status.rebootcnt,device_status.iSave,device_status.update_flag);
  940. }
  941. else {
  942. rt_kprintf("easyflash init ERRPR!!!\n");
  943. }
  944. //保存默认屏幕配置
  945. if(device_status.iSave!=FLASH_FLAG)
  946. {
  947. device_status.iSave=FLASH_FLAG;
  948. device_status.clt_time = 10; //收集时间
  949. device_status.heattime = 10; //加热时间
  950. device_status.heatvalue=100;//加热温度
  951. device_status.thsp = 300; //默认熄屏时间5分钟
  952. device_status.sunBar = 9; //光控定时时长
  953. device_status.startBar = 20; //定时开始时间
  954. device_status.stopBar = 0; //定时结束时间
  955. device_status.tph = 70; //高温保护阈值
  956. device_status.tpl = 5; //低温保护阈值
  957. device_status.shake_ws = 1; //震动开关,默认开
  958. device_status.shake_sec = 1; //震动时间,s
  959. device_status.vol = 7; //音量
  960. device_status.timeSwitch = 0; //工作模式:光控
  961. device_status.passWord = 8888; //屏幕密码
  962. device_status.dattim = 20; //数据上传间隔
  963. device_status.ds = 1; //设备状态
  964. device_status.version_type = 41; //版本类型
  965. device_status.update_flag = 0;
  966. rt_sprintf(device_status.tempture, "26");
  967. rt_sprintf(device_status.humidity, "31");
  968. rt_sprintf(device_status.mqttcfg1, "8.136.98.49");
  969. ef_set_env_blob("iSave", &device_status.iSave, sizeof(device_status.iSave)); //
  970. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  971. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
  972. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
  973. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
  974. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  975. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
  976. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
  977. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  978. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
  979. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
  980. ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关
  981. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间
  982. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  983. ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord));
  984. ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
  985. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  986. ef_set_env_blob("env_t",&device_status.tempture,sizeof(device_status.tempture)); //温度
  987. ef_set_env_blob("env_h",&device_status.humidity,sizeof(device_status.humidity)); //湿度
  988. ef_set_env_blob("platform",&device_status.mqttcfg1,sizeof(device_status.mqttcfg1)); //上传平台
  989. if(device_status.version_type==1)
  990. {
  991. sprintf(device_status.driver, "%s",VERSION_ZP);
  992. }
  993. else if (device_status.version_type==4) {
  994. sprintf(device_status.driver, "%s",VERSION_HK);
  995. }
  996. rt_kprintf("SOFTWARE VERSION:%s\n",device_status.driver);
  997. rt_kprintf("set flash paramconfis:\n");
  998. rt_kprintf("stime:%d\n",device_status.iSave);
  999. rt_kprintf("stime:%d\n",device_status.clt_time);
  1000. rt_kprintf("startBar:%d\n",device_status.startBar);
  1001. rt_kprintf("stopBar:%d\n",device_status.stopBar);
  1002. rt_kprintf("timeSwitch:%d\n",device_status.timeSwitch);
  1003. rt_kprintf("sunBar:%d\n",device_status.sunBar);
  1004. rt_kprintf("jtime:%d\n",device_status.heattime);
  1005. rt_kprintf("jheat:%d\n",device_status.heatvalue);
  1006. rt_kprintf("thsp:%d\n", device_status.thsp);
  1007. rt_kprintf("密码:%d\n",device_status.passWord);
  1008. }//从easyflash读取参数配置
  1009. else if (device_status.iSave==FLASH_FLAG) {
  1010. ef_get_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim),NULL); //数据上传间隔
  1011. ef_get_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time), NULL); //收集时间
  1012. ef_get_env_blob("startBar", &device_status.startBar,sizeof(device_status.startBar), NULL); //开始时间
  1013. ef_get_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar), NULL); //结束时间
  1014. ef_get_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch), NULL); //模式
  1015. ef_get_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar), NULL); //光控时间
  1016. ef_get_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime), NULL); //加热时间
  1017. ef_get_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue), NULL); //加热温度
  1018. ef_get_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws), NULL); //震动开关
  1019. ef_get_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec), NULL); //震动时间
  1020. ef_get_env_blob("tph", &device_status.tph, sizeof(device_status.tph), NULL); //高温保护
  1021. ef_get_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl), NULL); //低温保护
  1022. ef_get_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim), NULL); //数据上传间隔
  1023. ef_get_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp), NULL); //熄屏时间
  1024. ef_get_env_blob("passWord",&device_status.passWord,sizeof(device_status.passWord),NULL); //密码
  1025. ef_get_env_blob("imei",&device_status.imei,sizeof(device_status.imei),NULL); //IMEI
  1026. ef_get_env_blob("iccid",&device_status.iccid,sizeof(device_status.iccid),NULL); //ICCID
  1027. ef_get_env_blob("lat",&device_status.lat,sizeof(device_status.lat),NULL);
  1028. ef_get_env_blob("lng",&device_status.lng,sizeof(device_status.lng),NULL);
  1029. ef_get_env_blob("ds",&device_status.ds,sizeof(device_status.ds),NULL); //开关机状态
  1030. ef_get_env_blob("env_t",&device_status.tempture,sizeof(device_status.tempture),NULL); //环境温度
  1031. ef_get_env_blob("env_h",&device_status.humidity,sizeof(device_status.humidity),NULL); //环境湿度
  1032. ef_get_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type),NULL); //版本类型
  1033. ef_get_env_blob("driver_bl",&device_status.driver_bl,sizeof(device_status.driver_bl),NULL); //BL版本号 进BL的时候存储到FLASH
  1034. ef_get_env_blob("platform",&device_status.mqttcfg1,sizeof(device_status.mqttcfg1),NULL); //BL版本号 进BL的时候存储到FLASH
  1035. device_status.light_over_tim = device_status.sunBar * 3600; //光控计数
  1036. device_status.work_done = 0;
  1037. //工作完成标志位
  1038. if(device_status.dattim==0)
  1039. {
  1040. device_status.dattim = 20;
  1041. }
  1042. if(device_status.version_type==0)
  1043. {
  1044. app_partion_backup(); //固件备份至df_area
  1045. device_status.version_type = 41;
  1046. rt_sprintf(device_status.driver, "%s",VERSION_HK);
  1047. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type));
  1048. }
  1049. else if(device_status.version_type==1)
  1050. {
  1051. rt_sprintf(device_status.driver, "%s",VERSION_ZP);
  1052. }
  1053. else if (device_status.version_type==11)
  1054. {
  1055. rt_sprintf(device_status.driver, "%s",VERSION_GK);
  1056. }
  1057. else if (device_status.version_type==4)
  1058. {
  1059. rt_sprintf(device_status.driver, "%s",VERSION_HK_2000W);
  1060. }
  1061. else if (device_status.version_type==41) {
  1062. rt_sprintf(device_status.driver, "%s",VERSION_HK);
  1063. }
  1064. else if (device_status.version_type==5) {
  1065. rt_sprintf(device_status.driver, "%s",VERSION_V5);
  1066. }
  1067. else if (device_status.version_type==6)
  1068. {
  1069. rt_pin_mode(HK_5000W_P, PIN_MODE_OUTPUT);
  1070. rt_pin_write(HK_5000W_P,ON);
  1071. rt_sprintf(device_status.driver, "%s",VERSION_XCT);
  1072. }
  1073. if(device_status.iccid[0]!=8)
  1074. {
  1075. rt_sprintf(device_status.iccid,"87654321000000000000");
  1076. }
  1077. #if LAMP_EN == 1
  1078. device_status.sunBar = 9;
  1079. device_status.timeSwitch = 0;
  1080. #endif
  1081. rt_kprintf("SOFTWARE VERSION:%s-imei:%s--iccid:%s\n",device_status.driver,device_status.imei,device_status.iccid);
  1082. rt_kprintf("read flash paramconfis:%d-%s-%s\r\n",device_status.version_type,device_status.driver_bl,device_status.driver_compile);
  1083. rt_kprintf("开机次数:%d\n",device_status.rebootcnt);
  1084. rt_kprintf("收集时间:%d\n",device_status.clt_time);
  1085. rt_kprintf("定时开始时间:%d\n",device_status.startBar);
  1086. rt_kprintf("定时结束时间:%d\n",device_status.stopBar);
  1087. rt_kprintf("timeSwitch:%d\n",device_status.timeSwitch);
  1088. rt_kprintf("光控定时时长:%d\n",device_status.sunBar);
  1089. rt_kprintf("收集时间:%d\n",device_status.heattime);
  1090. rt_kprintf("加热温度:%d\n",device_status.heatvalue);
  1091. rt_kprintf("震动开关:%d\n",device_status.shake_ws);
  1092. rt_kprintf("震动时间:%d\n",device_status.shake_sec);
  1093. rt_kprintf("高温保护:%d\n",device_status.tph);
  1094. rt_kprintf("低温保护:%d\n",device_status.tpl);
  1095. rt_kprintf("thsp:%d\n", device_status.thsp);
  1096. rt_kprintf("密码:%d\n",device_status.passWord);
  1097. rt_kprintf("数据上传间隔:%d\n",device_status.dattim);
  1098. rt_kprintf("温湿度数据:%d-%d\n",atoi(device_status.tempture),atoi(device_status.humidity));
  1099. rt_kprintf("上传平台:%s\n",device_status.mqttcfg1);
  1100. }
  1101. rt_kprintf("当前时间:%04d-%02d-%02d %02d:%02d:%02d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  1102. //RTC时间判断,如果当前年份小于2020,提示需要设置时间,切换为光控
  1103. if(device_status.update_flag==1)
  1104. {
  1105. device_status.update_flag = 0;
  1106. ef_set_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag));
  1107. ymodem_ota(1,0);
  1108. }
  1109. else {
  1110. device_status.update_flag = 0;
  1111. //初始化联网状态
  1112. rt_sprintf(device_status.net,"1");
  1113. rt_sprintf(device_status.csq,"0");
  1114. rt_sprintf(device_status.sim,"1");
  1115. ctr_router(ON);
  1116. Hmi_Fresh();
  1117. Control_clear();
  1118. dtu_data_play();
  1119. sht30_collect();
  1120. #if SENSOR_EN==1
  1121. mb_master_samlpe();
  1122. #endif
  1123. uint16_t connect_num=0;
  1124. //等待联网,
  1125. while(atoi(device_status.net)==1)
  1126. {
  1127. char *readinfo = makeJson_ReadInfo();
  1128. DTU_Write("%s",readinfo);
  1129. rt_free(readinfo);
  1130. rt_thread_delay(1000);
  1131. connect_num++;
  1132. rt_kprintf("联网中......%d\n",connect_num);
  1133. //超时时间为3分钟
  1134. if(connect_num==180)
  1135. {
  1136. device_status.timeSwitch = 0;
  1137. rt_kprintf("Time not set:%04d-%02d-%02d %02d:%02d:%02d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  1138. set_date(2020, 1, 1);
  1139. set_time(0, 0, 0);
  1140. break;
  1141. }
  1142. if(tm.tm_year>=2021)
  1143. break;
  1144. }
  1145. if(device_status.version_type==6) device_status.timeSwitch = 1;
  1146. DTU_Write("%s",makeJson_Warn("turnon", "device_stm32"));
  1147. }
  1148. }
  1149. void ResetParamDefault(void)
  1150. {
  1151. device_status.clt_time = 10; //收集时间
  1152. device_status.dattim = 20; //数据上传间隔
  1153. device_status.startBar = 20; //定时开始时间
  1154. device_status.stopBar = 5; //定时结束时间
  1155. device_status.heattime = 10; //加热时间
  1156. device_status.shake_ws = 1; //针对开关
  1157. device_status.shake_sec = 1; //震动时间1-100ms
  1158. device_status.sunBar = 9; //光控定时时长
  1159. device_status.tph = 70; //高温保护阈值
  1160. device_status.tpl = 5; //低温保护阈值
  1161. device_status.timeSwitch = 0; //定时模式:光控
  1162. device_status.heatvalue=100; //加热温度
  1163. device_status.ds = 1; //设备状态开机
  1164. device_status.thsp = 300;
  1165. device_status.passWord = 8888;
  1166. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  1167. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
  1168. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
  1169. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
  1170. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  1171. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
  1172. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
  1173. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  1174. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
  1175. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
  1176. ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关
  1177. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间
  1178. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  1179. ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord));
  1180. DTU_Write("reset.mp3");
  1181. }
  1182. void ResetParamDefault_DC(void)
  1183. {
  1184. device_status.clt_time = 10; //收集时间
  1185. device_status.dattim = 20; //数据上传间隔
  1186. device_status.startBar = 20; //定时开始时间
  1187. device_status.stopBar = 0; //定时结束时间
  1188. device_status.heattime = 10; //加热时间
  1189. device_status.shake_ws = 1; //针对开关
  1190. device_status.shake_sec = 1; //震动时间1-100ms
  1191. device_status.sunBar = 4; //光控定时时长
  1192. device_status.tph = 70; //高温保护阈值
  1193. device_status.tpl = 5; //低温保护阈值
  1194. device_status.timeSwitch = 0; //定时模式
  1195. device_status.heatvalue=100;
  1196. device_status.ds = 1; //设备状态开机
  1197. device_status.thsp = 300;
  1198. device_status.passWord = 8888;
  1199. device_status.timeSwitch = 0;
  1200. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  1201. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
  1202. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
  1203. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
  1204. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  1205. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
  1206. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
  1207. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  1208. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
  1209. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
  1210. ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关
  1211. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间
  1212. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  1213. ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord));
  1214. DTU_Write("reset.mp3");
  1215. }
  1216. //固件备份
  1217. void app_partion_backup(void)
  1218. {
  1219. rt_kprintf("固件恢复!!!\n");
  1220. const char *dst_part_name = "df_area";
  1221. const char *src_part_name = "app";
  1222. const struct fal_partition *dst_part;
  1223. const struct fal_partition *src_part;
  1224. dst_part = fal_partition_find(dst_part_name);
  1225. src_part = fal_partition_find(src_part_name);
  1226. if (dst_part == RT_NULL || src_part == RT_NULL)
  1227. {
  1228. if (dst_part == RT_NULL)
  1229. rt_kprintf("%s partition is not exist!\n", dst_part_name);
  1230. if (src_part == RT_NULL)
  1231. rt_kprintf("%s partition is not exist!\n", src_part_name);
  1232. }
  1233. else
  1234. {
  1235. rt_kprintf("Clone %s partition to %s partition:\n", src_part_name, dst_part_name);
  1236. if (fal_partition_erase(dst_part, 0, dst_part->len) >= 0)
  1237. {
  1238. int clone_pos = 0;
  1239. int clone_len = 0, clone_tol_len;
  1240. rt_uint8_t *buf = rt_malloc(4096);
  1241. if (dst_part->len < src_part->len)
  1242. clone_tol_len = dst_part->len;
  1243. else
  1244. clone_tol_len = src_part->len;
  1245. while ((clone_pos < clone_tol_len) && (buf != RT_NULL))
  1246. {
  1247. clone_len = fal_partition_read(src_part, clone_pos, buf, 4096);
  1248. if (clone_len < 0)
  1249. {
  1250. rt_kprintf("\nread %s partition failed, clone stop!\n", src_part_name);
  1251. break;
  1252. }
  1253. if (fal_partition_write(dst_part, clone_pos, buf, clone_len) < 0)
  1254. {
  1255. rt_kprintf("\nwrite %s partition failed, clone stop!\n", dst_part_name);
  1256. break;
  1257. }
  1258. HMI_WRITE("#");
  1259. rt_kprintf("#");
  1260. clone_pos += clone_len;
  1261. }
  1262. if (clone_pos >= clone_tol_len)
  1263. {
  1264. rt_kprintf("\nClone partition success, total %d bytes!\n", clone_tol_len);
  1265. }
  1266. else
  1267. {
  1268. rt_kprintf("Clone partition failed!");
  1269. }
  1270. if (buf)
  1271. rt_free(buf);
  1272. }
  1273. }
  1274. }
  1275. void Debug_log_ON(void)
  1276. {
  1277. rt_console_set_device("uart4");
  1278. finsh_set_device("uart4");
  1279. }
  1280. void Debug_log_OFF(void)
  1281. {
  1282. rt_console_set_device("uart1");
  1283. finsh_set_device("uart1");
  1284. rt_mq_init(&rx_mq, "rx_mq",
  1285. msg_pool, /* 存放消息的缓冲区 */
  1286. sizeof(struct rx_msg), /* 一条消息的最大长度 */
  1287. sizeof(msg_pool), /* 存放消息的缓冲区大小 */
  1288. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  1289. rt_device_set_rx_indicate(serial, uart_input);
  1290. hmi1 = rt_thread_create("Hmi_Fresh_thread",
  1291. Hmi_thread_entry,
  1292. RT_NULL,
  1293. HMI_THREAD_STACK_SIZE,
  1294. HMI_THREAD_PRIORITY,
  1295. HMI_THREAD_TIMESLICE);
  1296. if (hmi1 != RT_NULL)
  1297. {
  1298. rt_thread_startup(hmi1);
  1299. }
  1300. }
  1301. MSH_CMD_EXPORT(Debug_log_ON,Debug_log_ON);
  1302. MSH_CMD_EXPORT(Debug_log_OFF,Debug_log_OFF);
  1303. //
  1304. INIT_BOARD_EXPORT(HMI_uart4_init);