sht30_collect.c 13 KB

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