sht30_collect.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. rain_cnt = 0;
  187. }
  188. else {
  189. rain_cnt++;
  190. //雨控消抖,1分钟
  191. rt_kprintf("rain_cnt:%d-%d\n",rain_cnt,Read_Rain_Voltage());
  192. if(rain_cnt==60&&Read_Rain_Voltage()==0)
  193. {
  194. rain_cnt = 0;
  195. device_status.rain_status = 0;
  196. }
  197. }
  198. rt_thread_mdelay(10);
  199. }
  200. }
  201. /*读取电流值*/
  202. int Read_Real_Current(void)
  203. {
  204. rt_adc_device_t adc_dev; /*ADC设备句柄*/
  205. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  206. if (adc_dev == RT_NULL)
  207. {
  208. rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  209. return RT_ERROR;// adc 采样值不可能为负值,返回负值表示出错
  210. }
  211. HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  212. rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_1);
  213. //rt_adc_enable(adc_dev, 17);
  214. adc1_value = filter_Mid(rt_adc_read(adc_dev, ADC_DEV_CHANNLE_1));
  215. in_adc_17 = filter_Mid(rt_adc_read(adc_dev, 17));
  216. /* 关闭通道 */
  217. rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_1);
  218. //rt_adc_disable(adc_dev, 17);
  219. //rt_kprintf("read in adc:ADC17:%d-ADC1:%d\n",in_adc_17,adc1_value);
  220. return (int)((ADC_VOL_BASE * (adc1_value*1.0)/(in_adc_17*1.0))*100000);
  221. }
  222. /*读取电压值*/
  223. int Read_Real_Voltage(void)
  224. {
  225. rt_adc_device_t adc_dev; /*ADC设备句柄*/
  226. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  227. if (adc_dev == RT_NULL)
  228. {
  229. rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  230. return RT_ERROR;// adc 采样值不可能为负值,返回负值表示出错
  231. }
  232. HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  233. rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_0);
  234. adc0_value = rt_adc_read(adc_dev, ADC_DEV_CHANNLE_0);
  235. /* 关闭通道 */
  236. rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_0);
  237. return (adc0_value * REFER_VOLTAGE/CONVERT_BITS*12+20);
  238. }
  239. /*读取保险丝的电压值*/
  240. int Read_Fuse_Voltage(void)
  241. {
  242. rt_adc_device_t adc_dev; /*ADC设备句柄*/
  243. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  244. if (adc_dev == RT_NULL)
  245. {
  246. rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  247. return RT_ERROR;// adc 采样值不可能为负值,返回负值表示出错
  248. }
  249. HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  250. rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_5);
  251. adc13_value = rt_adc_read(adc_dev, ADC_DEV_CHANNLE_5);
  252. /* 关闭通道 */
  253. rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_5);
  254. return (adc13_value * REFER_VOLTAGE/CONVERT_BITS*12+8);
  255. }
  256. /*读取雨控*/
  257. int Read_Rain_Voltage(void)
  258. {
  259. // rt_adc_device_t adc_dev; /*ADC设备句柄*/
  260. // adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  261. // if (adc_dev == RT_NULL)
  262. // {
  263. // rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  264. // return RT_ERROR;// adc 采样值不可能为负值,返回负值表示出错
  265. // }
  266. // rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_3);
  267. // adc11_light = rt_adc_read(adc_dev,ADC_DEV_CHANNLE_3);
  268. // rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_3);
  269. // HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  270. // rt_kprintf("read rain value:%d\n",adc11_light);
  271. // return adc11_light;
  272. rt_pin_mode(RAIN_PIN,PIN_MODE_INPUT);
  273. return rt_pin_read(RAIN_PIN);
  274. }
  275. /*读取光控*/
  276. int Read_Light_Voltage(void)
  277. {
  278. rt_adc_device_t adc_dev; /*ADC设备句柄*/
  279. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  280. if (adc_dev == RT_NULL)
  281. {
  282. rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  283. return RT_ERROR;// adc 采样值不可能为负值,返回负值表示出错
  284. }
  285. rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_2);
  286. adc10_rain = rt_adc_read(adc_dev,ADC_DEV_CHANNLE_2);
  287. rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_2);
  288. HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  289. rt_kprintf("read light value:%d\n",adc10_rain);
  290. return adc10_rain;
  291. }
  292. /**
  293. * Rv: 热敏电阻当前阻值
  294. * B: 热敏电阻参数B值
  295. * Revise: 校正温度
  296. * BasicRx: 热敏电阻25度时电阻(标称电阻数值)
  297. * 返回: 摄氏度
  298. */
  299. int TempCalculate(float Rv,int B,int BasicRx)
  300. {
  301. float Rx;
  302. Rx = Rv * 1.0 / BasicRx;
  303. Rx = log(Rx);
  304. Rx = Rx / B;
  305. Rx = 1/(Rx+0.003354);
  306. //Rx = Rx + 0.003356;
  307. //Rx = 1 / Rx;
  308. Rx = Rx - 273.13;
  309. return (int)(Rx+0.5);
  310. }
  311. /*
  312. * 中位值平均滤波法
  313. * */
  314. unsigned int filter_Mid(int adc_return)
  315. {
  316. unsigned char count,i,j;
  317. unsigned int value_buf[N],temp=0;
  318. int sum = 0;
  319. for(count = 0;count<N;count++)
  320. {
  321. value_buf[count] = adc_return;
  322. //rt_kprintf("value_buf:%d-%d\n",value_buf[count],count);
  323. rt_thread_delay(10);
  324. }
  325. for(j=0;j<N-1;j++)
  326. {
  327. for(i=0;i<N-j-1;i++)
  328. {
  329. if(value_buf[i]>value_buf[i+1])
  330. {
  331. temp = value_buf[i];
  332. value_buf[i] = value_buf[i+1];
  333. value_buf[i+1] = temp;
  334. }
  335. }
  336. }
  337. for(count = 1;count < N - 1;count++)
  338. {
  339. sum+= value_buf[count];
  340. }
  341. return (int)(sum/(N-2));
  342. }
  343. /*
  344. 限幅消抖滤波法
  345. A、方法:
  346. 相当于“限幅滤波法”+“消抖滤波法”
  347. 先限幅,后消抖
  348. B、优点:
  349. 继承了“限幅”和“消抖”的优点
  350. 改进了“消抖滤波法”中的某些缺陷,避免将干扰值导入系统
  351. C、缺点:
  352. 对于快速变化的参数不宜
  353. */
  354. unsigned int filter_voltage(void)
  355. {
  356. uint8_t data_cnt = 0;
  357. int value = 0,new_value = 0;
  358. new_value = Read_Real_Voltage();
  359. while(value!=new_value)
  360. {
  361. data_cnt++;
  362. if(data_cnt>=N) return new_value;
  363. rt_thread_delay(10);
  364. new_value = Read_Real_Voltage();
  365. }
  366. return value;
  367. }
  368. /*读取加热仓温度*/
  369. int Read_Ntc_Voltage(void)
  370. {
  371. rt_adc_device_t adc_dev; /*ADC设备句柄*/
  372. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  373. if (adc_dev == RT_NULL)
  374. {
  375. rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  376. return RT_ERROR;// adc 采样值不可能为负值,返回负值表示出错
  377. }
  378. rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_4);
  379. adc12_ntc = filter_Mid(rt_adc_read(adc_dev,ADC_DEV_CHANNLE_4));
  380. rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_4);
  381. HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  382. return adc12_ntc;
  383. }
  384. /* 创建线程 */
  385. int sht30_collect(void)
  386. {
  387. //UART_485_INIT();
  388. /* 创建线程*/
  389. tid1 = rt_thread_create("sht30_collect_thread",sht30_collect_thread_entry, RT_NULL,THREAD_STACK_SIZE,THREAD_PRIORITY, THREAD_TIMESLICE);
  390. tid2 = rt_thread_create("Power Check thread", Power_ReadCheck, RT_NULL, P_THREAD_STACK_SIZE, P_THREAD_PRIORITY, P_THREAD_TIMESCLICE);
  391. /* 如果获得线程控制块,启动这个线程 */
  392. if (tid1 != RT_NULL)
  393. rt_thread_startup(tid1);
  394. if (tid2 != RT_NULL)
  395. rt_thread_startup(tid2);
  396. return 0;
  397. }
  398. //控制台打印传感器信息
  399. void Msh_DisplaySensor(void)
  400. {
  401. rt_kprintf("温度:%d\n",tem_value);
  402. rt_kprintf("湿度:%d\n",hum_value);
  403. rt_kprintf("电压:%s\n",device_status.voltage);
  404. rt_kprintf("电流:%s\n",device_status.power);
  405. }
  406. MSH_CMD_EXPORT(Msh_DisplaySensor,Msh_DisplaySensor);
  407. MSH_CMD_EXPORT(Read_Rain_Voltage,Read_Rain_Voltage);
  408. MSH_CMD_EXPORT(Read_Light_Voltage,Read_Light_Voltage);