sht30_collect.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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-23 Administrator the first version
  9. */
  10. #include <rtthread.h>
  11. #include <board.h>
  12. #include <sht3x.h>
  13. #include "sht30_collect.h"
  14. #include <string.h>
  15. #include <stdio.h>
  16. #include "control.h"
  17. #include "math.h"
  18. //温湿度
  19. #define THREAD_PRIORITY 21 //线程优先级
  20. #define THREAD_STACK_SIZE 2048 //栈大小
  21. #define THREAD_TIMESLICE 5 //时间片
  22. //电压,电流
  23. #define P_THREAD_PRIORITY 13 //线程优先级
  24. #define P_THREAD_STACK_SIZE 1024 //栈大小
  25. #define P_THREAD_TIMESCLICE 5 //时间片
  26. //ADC
  27. #define ADC_DEV_NAME "adc1" /*ADC设备名称*/
  28. #define ADC_DEV_CHANNLE_0 0 /*ADC通道*///电流
  29. #define ADC_DEV_CHANNLE_1 1 /*ADC通道*///电压
  30. #define ADC_DEV_CHANNLE_2 10 /*雨控*/
  31. #define ADC_DEV_CHANNLE_3 11 /*光控*/
  32. #define ADC_DEV_CHANNLE_4 12 /*加热仓温度*/
  33. #define ADC_DEV_CHANNLE_5 13 /*保险丝之后的电压*/
  34. #define REFER_VOLTAGE 330 /* 参考电压 3.3V,数据精度乘以100保留2位小数*/
  35. #define CONVERT_BITS (1<<12) /* 转换位数为12位 */
  36. static char send_buf[64];
  37. //#define serial_printf(x,...) {sprintf(send_buf,x,##__VA_ARGS__);rt_kprintf(send_buf);}
  38. extern Device_Status_typedef device_status;
  39. rt_device_t serial_485; /*串口设备句柄*/
  40. struct serial_configure uart_config_485 = RT_SERIAL_CONFIG_DEFAULT; /*初始化配置参数*/
  41. rt_uint32_t adc0_value,adc1_value,in_adc_17,adc13_value;
  42. rt_uint32_t adc10_rain,adc11_light,adc12_ntc;
  43. int8_t hum_value,tem_value;
  44. static rt_thread_t tid1 = RT_NULL;
  45. static rt_thread_t tid2 = RT_NULL;
  46. ADC_HandleTypeDef hadc1;
  47. /*串口初始化*/
  48. int UART_485_INIT(void)
  49. {
  50. serial_485 = rt_device_find(UART_485);
  51. /*修改串口配置参数*/
  52. uart_config_485.baud_rate = BAUD_RATE_9600; //修改波特率9600
  53. uart_config_485.data_bits = DATA_BITS_8; //数据位8
  54. uart_config_485.stop_bits = STOP_BITS_1; //停止位1
  55. uart_config_485.bufsz = 512; //缓冲区1k
  56. uart_config_485.parity = PARITY_NONE; //奇校验
  57. /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
  58. rt_device_control(serial_485, RT_DEVICE_CTRL_CONFIG, &uart_config_485);
  59. /* step4:打开串口设备。以DMA接收及轮询发送模式打开串口设备 */
  60. rt_device_open(serial_485, RT_DEVICE_FLAG_INT_RX);
  61. return 0;
  62. }
  63. /* 入口函数 */
  64. static void sht30_collect_thread_entry(void *parameter)
  65. {
  66. sht3x_device_t sht3x_device;
  67. static int checklightnum = 0;
  68. static int checknightnum = 0;
  69. sht3x_device = sht3x_init("i2c1", 0x44);
  70. //此处需要短暂的延时,否则会出现连续读取失败的问题
  71. rt_thread_mdelay(150);
  72. while (1)
  73. {
  74. if(sht3x_device)
  75. {
  76. if(RT_EOK == sht3x_read_singleshot(sht3x_device))
  77. {
  78. hum_value = sht3x_device->humidity;
  79. tem_value = sht3x_device->temperature;
  80. if(hum_value!=0)
  81. {
  82. rt_sprintf(device_status.humidity,"%d.%d",(int)sht3x_device->humidity, (uint)(sht3x_device->humidity * 10) % 10);
  83. rt_sprintf(device_status.tempture,"%d.%d",(int)sht3x_device->temperature, (uint)(sht3x_device->temperature * 10) % 10);
  84. }
  85. else {
  86. rt_sprintf(device_status.humidity,"30.0");
  87. rt_sprintf(device_status.tempture,"25.0");
  88. }
  89. if(tem_value>=device_status.tph||tem_value<=device_status.tpl)
  90. {
  91. device_status.temp_status = 1;
  92. }
  93. else if(tem_value>=(device_status.tpl+5)&&tem_value<=(device_status.tph-10)) {
  94. device_status.temp_status = 0;
  95. }
  96. }
  97. else {
  98. sht3x_softreset(sht3x_device);
  99. hum_value = 30;
  100. tem_value = 25;
  101. }
  102. // rt_kprintf("sht30 humidity : %d.%d ", (int)sht3x_device->humidity, (int)(sht3x_device->humidity * 10) % 10);
  103. // rt_kprintf("temperature: %d.%d\n", (int)sht3x_device->temperature, (int)(sht3x_device->temperature * 10) % 10);
  104. }
  105. else
  106. {
  107. hum_value = 30;
  108. tem_value = 25;
  109. rt_kprintf("read sht3x fail.\r\n");
  110. //重新初始化
  111. sht3x_deinit(sht3x_device);
  112. sht3x_device = sht3x_init("i2c1", 0x44);
  113. rt_thread_mdelay(150);
  114. }
  115. //rt_kprintf("the voltage is:%d.%02d\n",Read_Real_Voltage()/100,Read_Real_Voltage()%100);
  116. //sprintf(send_buf,"%.2f",(Read_Real_Current()/100.0*2.5)-20.0);
  117. //device_status.power = send_buf;
  118. //rt_kprintf(send_buf);
  119. if(Read_Light_Voltage()>500)
  120. {
  121. checklightnum++;
  122. if(checklightnum==40)
  123. {
  124. device_status.sun_status = 1;
  125. checklightnum =0;
  126. }
  127. }
  128. else {
  129. checknightnum ++;
  130. if(checknightnum==40)
  131. {
  132. device_status.sun_status = 0;
  133. checknightnum = 0;
  134. }
  135. }
  136. rt_kprintf("光控计数:%d-%d\n",checklightnum,checknightnum);
  137. rt_thread_mdelay(5000);
  138. }
  139. }
  140. static void Power_ReadCheck(void *parameter)
  141. {
  142. static char voltage_tmp[10];
  143. static int rain_cnt = 0;
  144. static int RV;
  145. while(1)
  146. {
  147. sprintf(send_buf,"%.2f",Read_Real_Current()/100.0*2.5);
  148. device_status.power = send_buf;
  149. if (Read_Real_Voltage()/100<12) {
  150. //关闭外设
  151. ctr_cangmen_down(OFF);
  152. ctr_cangmen_up(OFF);
  153. ctr_Heat(OFF);
  154. ctr_lamp(OFF);
  155. ctr_led(OFF);
  156. ctr_router(OFF);
  157. }
  158. if(Read_Real_Voltage()/100<21)
  159. {
  160. device_status.bat_status = 1; //欠压
  161. }
  162. else {
  163. device_status.bat_status = 0; //解除欠压
  164. ctr_router(ON); //打开路由器供电
  165. ctr_photo(ON); //打开相机供电
  166. }
  167. sprintf(voltage_tmp,"%d.%02d",filter_voltage()/100,filter_voltage()%100);
  168. device_status.voltage = voltage_tmp;
  169. sprintf(device_status.fuse_voltage, "%d.%02d",Read_Fuse_Voltage()/100,Read_Fuse_Voltage()%100);
  170. RV = 10000 * (4096 - Read_Ntc_Voltage()) / Read_Ntc_Voltage();
  171. if(TempCalculate(RV, 3950, 10000)-TempCalculate(RV, 3950, 10000)>10)
  172. {
  173. rt_kprintf("temp value err!!!!");
  174. }
  175. else {
  176. device_status.heattem = TempCalculate(RV, 3950, 10000);
  177. if(device_status.heattem>160)
  178. {
  179. device_status.heattem = 25;
  180. }
  181. }
  182. if(Read_Rain_Voltage()==1)
  183. {
  184. device_status.rain_status = 1;
  185. ctr_lamp(OFF);
  186. device_status.ctr_up_motor =1; //打开上仓门
  187. device_status.ctr_down_motor =1; //打开下仓门
  188. rain_cnt = 0;
  189. }
  190. else {
  191. rain_cnt++;
  192. //雨控消抖,1分钟
  193. rt_kprintf("rain_cnt:%d-%d\n",rain_cnt,Read_Rain_Voltage());
  194. if(rain_cnt==60&&Read_Rain_Voltage()==0)
  195. {
  196. rain_cnt = 0;
  197. device_status.rain_status = 0;
  198. }
  199. }
  200. rt_thread_mdelay(10);
  201. }
  202. }
  203. /*读取电流值*/
  204. int Read_Real_Current(void)
  205. {
  206. rt_adc_device_t adc_dev; /*ADC设备句柄*/
  207. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  208. if (adc_dev == RT_NULL)
  209. {
  210. rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  211. return RT_ERROR;// adc 采样值不可能为负值,返回负值表示出错
  212. }
  213. HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  214. rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_1);
  215. //rt_adc_enable(adc_dev, 17);
  216. adc1_value = filter_Mid(rt_adc_read(adc_dev, ADC_DEV_CHANNLE_1));
  217. in_adc_17 = filter_Mid(rt_adc_read(adc_dev, 17));
  218. /* 关闭通道 */
  219. rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_1);
  220. //rt_adc_disable(adc_dev, 17);
  221. //rt_kprintf("read in adc:ADC17:%d-ADC1:%d\n",in_adc_17,adc1_value);
  222. return (int)((ADC_VOL_BASE * (adc1_value*1.0)/(in_adc_17*1.0))*100000);
  223. }
  224. /*读取电压值*/
  225. int Read_Real_Voltage(void)
  226. {
  227. rt_adc_device_t adc_dev; /*ADC设备句柄*/
  228. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  229. if (adc_dev == RT_NULL)
  230. {
  231. rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  232. return RT_ERROR;// adc 采样值不可能为负值,返回负值表示出错
  233. }
  234. HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  235. rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_0);
  236. adc0_value = rt_adc_read(adc_dev, ADC_DEV_CHANNLE_0);
  237. /* 关闭通道 */
  238. rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_0);
  239. return (adc0_value * REFER_VOLTAGE/CONVERT_BITS*12);
  240. }
  241. /*读取保险丝的电压值*/
  242. int Read_Fuse_Voltage(void)
  243. {
  244. rt_adc_device_t adc_dev; /*ADC设备句柄*/
  245. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  246. if (adc_dev == RT_NULL)
  247. {
  248. rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  249. return RT_ERROR;// adc 采样值不可能为负值,返回负值表示出错
  250. }
  251. HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  252. rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_5);
  253. adc13_value = rt_adc_read(adc_dev, ADC_DEV_CHANNLE_5);
  254. /* 关闭通道 */
  255. rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_5);
  256. return (adc13_value * REFER_VOLTAGE/CONVERT_BITS*12+8);
  257. }
  258. /*读取雨控*/
  259. int Read_Rain_Voltage(void)
  260. {
  261. // rt_adc_device_t adc_dev; /*ADC设备句柄*/
  262. // adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  263. // if (adc_dev == RT_NULL)
  264. // {
  265. // rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  266. // return RT_ERROR;// adc 采样值不可能为负值,返回负值表示出错
  267. // }
  268. // rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_3);
  269. // adc11_light = rt_adc_read(adc_dev,ADC_DEV_CHANNLE_3);
  270. // rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_3);
  271. // HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  272. // rt_kprintf("read rain value:%d\n",adc11_light);
  273. // return adc11_light;
  274. rt_pin_mode(RAIN_PIN,PIN_MODE_INPUT);
  275. return rt_pin_read(RAIN_PIN);
  276. }
  277. /*读取光控*/
  278. int Read_Light_Voltage(void)
  279. {
  280. rt_adc_device_t adc_dev; /*ADC设备句柄*/
  281. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  282. if (adc_dev == RT_NULL)
  283. {
  284. rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  285. return RT_ERROR;// adc 采样值不可能为负值,返回负值表示出错
  286. }
  287. rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_2);
  288. adc10_rain = rt_adc_read(adc_dev,ADC_DEV_CHANNLE_2);
  289. rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_2);
  290. HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  291. rt_kprintf("read light value:%d\n",adc10_rain);
  292. return adc10_rain;
  293. }
  294. /**
  295. * Rv: 热敏电阻当前阻值
  296. * B: 热敏电阻参数B值
  297. * Revise: 校正温度
  298. * BasicRx: 热敏电阻25度时电阻(标称电阻数值)
  299. * 返回: 摄氏度
  300. */
  301. int TempCalculate(float Rv,int B,int BasicRx)
  302. {
  303. float Rx;
  304. Rx = Rv * 1.0 / BasicRx;
  305. Rx = log(Rx);
  306. Rx = Rx / B;
  307. Rx = 1/(Rx+0.003354);
  308. //Rx = Rx + 0.003356;
  309. //Rx = 1 / Rx;
  310. Rx = Rx - 273.13;
  311. return (int)(Rx+0.5);
  312. }
  313. /*
  314. * 中位值平均滤波法
  315. * */
  316. unsigned int filter_Mid(int adc_return)
  317. {
  318. unsigned char count,i,j;
  319. unsigned int value_buf[N],temp=0;
  320. int sum = 0;
  321. for(count = 0;count<N;count++)
  322. {
  323. value_buf[count] = adc_return;
  324. //rt_kprintf("value_buf:%d-%d\n",value_buf[count],count);
  325. rt_thread_delay(10);
  326. }
  327. for(j=0;j<N-1;j++)
  328. {
  329. for(i=0;i<N-j-1;i++)
  330. {
  331. if(value_buf[i]>value_buf[i+1])
  332. {
  333. temp = value_buf[i];
  334. value_buf[i] = value_buf[i+1];
  335. value_buf[i+1] = temp;
  336. }
  337. }
  338. }
  339. for(count = 1;count < N - 1;count++)
  340. {
  341. sum+= value_buf[count];
  342. }
  343. return (int)(sum/(N-2));
  344. }
  345. /*
  346. 限幅消抖滤波法
  347. A、方法:
  348. 相当于“限幅滤波法”+“消抖滤波法”
  349. 先限幅,后消抖
  350. B、优点:
  351. 继承了“限幅”和“消抖”的优点
  352. 改进了“消抖滤波法”中的某些缺陷,避免将干扰值导入系统
  353. C、缺点:
  354. 对于快速变化的参数不宜
  355. */
  356. unsigned int filter_voltage(void)
  357. {
  358. uint8_t data_cnt = 0;
  359. int value = 0,new_value = 0;
  360. new_value = Read_Real_Voltage();
  361. while(value!=new_value)
  362. {
  363. data_cnt++;
  364. if(data_cnt>=N) return new_value;
  365. rt_thread_delay(10);
  366. new_value = Read_Real_Voltage();
  367. }
  368. return value;
  369. }
  370. /*读取加热仓温度*/
  371. int Read_Ntc_Voltage(void)
  372. {
  373. rt_adc_device_t adc_dev; /*ADC设备句柄*/
  374. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  375. if (adc_dev == RT_NULL)
  376. {
  377. rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  378. return RT_ERROR;// adc 采样值不可能为负值,返回负值表示出错
  379. }
  380. rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_4);
  381. adc12_ntc = filter_Mid(rt_adc_read(adc_dev,ADC_DEV_CHANNLE_4));
  382. rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_4);
  383. HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  384. return adc12_ntc;
  385. }
  386. /* 创建线程 */
  387. int sht30_collect(void)
  388. {
  389. //UART_485_INIT();
  390. /* 创建线程*/
  391. tid1 = rt_thread_create("sht30_collect_thread",sht30_collect_thread_entry, RT_NULL,THREAD_STACK_SIZE,THREAD_PRIORITY, THREAD_TIMESLICE);
  392. tid2 = rt_thread_create("Power Check thread", Power_ReadCheck, RT_NULL, P_THREAD_STACK_SIZE, P_THREAD_PRIORITY, P_THREAD_TIMESCLICE);
  393. /* 如果获得线程控制块,启动这个线程 */
  394. if (tid1 != RT_NULL)
  395. rt_thread_startup(tid1);
  396. if (tid2 != RT_NULL)
  397. rt_thread_startup(tid2);
  398. return 0;
  399. }
  400. //控制台打印传感器信息
  401. void Msh_DisplaySensor(void)
  402. {
  403. rt_kprintf("温度:%d\n",tem_value);
  404. rt_kprintf("湿度:%d\n",hum_value);
  405. rt_kprintf("电压:%s\n",device_status.voltage);
  406. rt_kprintf("电流:%s\n",device_status.power);
  407. }
  408. MSH_CMD_EXPORT(Msh_DisplaySensor,Msh_DisplaySensor);
  409. MSH_CMD_EXPORT(Read_Rain_Voltage,Read_Rain_Voltage);
  410. MSH_CMD_EXPORT(Read_Light_Voltage,Read_Light_Voltage);