bme680.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. // ***************************************************************************
  2. // Port of BMP680 module for ESP8266 with nodeMCU
  3. //
  4. // Written by Lukas Voborsky, @voborsky
  5. // ***************************************************************************
  6. // #define NODE_DEBUG
  7. #include "module.h"
  8. #include "lauxlib.h"
  9. #include "platform.h"
  10. #include <math.h>
  11. #include "bme680_defs.h"
  12. #define DEFAULT_HEATER_DUR 100
  13. #define DEFAULT_HEATER_TEMP 300
  14. #define DEFAULT_AMBIENT_TEMP 23
  15. static const uint32_t bme680_i2c_id = BME680_CHIP_ID_ADDR;
  16. static uint8_t bme680_i2c_addr = BME680_I2C_ADDR_PRIMARY;
  17. os_timer_t bme680_timer; // timer for forced mode readout
  18. int lua_connected_readout_ref; // callback when readout is ready
  19. static struct bme680_calib_data bme680_data;
  20. static uint8_t bme680_mode = 0; // stores oversampling settings
  21. static uint8 os_temp = 0;
  22. static uint8 os_pres = 0;
  23. static uint8 os_hum = 0; // stores humidity oversampling settings
  24. static uint16_t heatr_dur;
  25. static int8_t amb_temp = 23; //DEFAULT_AMBIENT_TEMP;
  26. static uint32_t bme680_h = 0;
  27. static double bme680_hc = 1.0;
  28. // return 0 if good
  29. static int r8u_n(uint8_t reg, int n, uint8_t *buff) {
  30. int i;
  31. platform_i2c_send_start(bme680_i2c_id);
  32. platform_i2c_send_address(bme680_i2c_id, bme680_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  33. platform_i2c_send_byte(bme680_i2c_id, reg);
  34. // platform_i2c_send_stop(bme680_i2c_id); // doco says not needed
  35. platform_i2c_send_start(bme680_i2c_id);
  36. platform_i2c_send_address(bme680_i2c_id, bme680_i2c_addr, PLATFORM_I2C_DIRECTION_RECEIVER);
  37. while (n-- > 0)
  38. *buff++ = platform_i2c_recv_byte(bme680_i2c_id, n > 0);
  39. platform_i2c_send_stop(bme680_i2c_id);
  40. return 0;
  41. }
  42. static uint8_t w8u(uint8_t reg, uint8_t val) {
  43. platform_i2c_send_start(bme680_i2c_id);
  44. platform_i2c_send_address(bme680_i2c_id, bme680_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  45. platform_i2c_send_byte(bme680_i2c_id, reg);
  46. platform_i2c_send_byte(bme680_i2c_id, val);
  47. platform_i2c_send_stop(bme680_i2c_id);
  48. }
  49. static uint8_t r8u(uint8_t reg) {
  50. uint8_t ret[1];
  51. r8u_n(reg, 1, ret);
  52. return ret[0];
  53. }
  54. /* This part of code is coming from the original bme680.c driver by Bosch.
  55. * Copyright (C) 2017 - 2018 Bosch Sensortec GmbH
  56. *
  57. * Redistribution and use in source and binary forms, with or without
  58. * modification, are permitted provided that the following conditions are met:
  59. *
  60. * Redistributions of source code must retain the above copyright
  61. * notice, this list of conditions and the following disclaimer.
  62. *
  63. * Redistributions in binary form must reproduce the above copyright
  64. * notice, this list of conditions and the following disclaimer in the
  65. * documentation and/or other materials provided with the distribution.
  66. *
  67. * Neither the name of the copyright holder nor the names of the
  68. * contributors may be used to endorse or promote products derived from
  69. * this software without specific prior written permission.
  70. *
  71. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  72. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  73. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  74. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  75. * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
  76. * OR CONTRIBUTORS BE LIABLE FOR ANY
  77. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  78. * OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,
  79. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  80. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  81. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  82. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  83. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  84. * ANY WAY OUT OF THE USE OF THIS
  85. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  86. *
  87. * The information provided is believed to be accurate and reliable.
  88. * The copyright holder assumes no responsibility
  89. * for the consequences of use
  90. * of such information nor for any infringement of patents or
  91. * other rights of third parties which may result from its use.
  92. * No license is granted by implication or otherwise under any patent or
  93. * patent rights of the copyright holder.
  94. */
  95. /**static variables */
  96. /**Look up table for the possible gas range values */
  97. uint32_t lookupTable1[16] = { UINT32_C(2147483647), UINT32_C(2147483647), UINT32_C(2147483647), UINT32_C(2147483647),
  98. UINT32_C(2147483647), UINT32_C(2126008810), UINT32_C(2147483647), UINT32_C(2130303777), UINT32_C(2147483647),
  99. UINT32_C(2147483647), UINT32_C(2143188679), UINT32_C(2136746228), UINT32_C(2147483647), UINT32_C(2126008810),
  100. UINT32_C(2147483647), UINT32_C(2147483647) };
  101. /**Look up table for the possible gas range values */
  102. uint32_t lookupTable2[16] = { UINT32_C(4096000000), UINT32_C(2048000000), UINT32_C(1024000000), UINT32_C(512000000),
  103. UINT32_C(255744255), UINT32_C(127110228), UINT32_C(64000000), UINT32_C(32258064), UINT32_C(16016016), UINT32_C(
  104. 8000000), UINT32_C(4000000), UINT32_C(2000000), UINT32_C(1000000), UINT32_C(500000), UINT32_C(250000),
  105. UINT32_C(125000) };
  106. static uint8_t calc_heater_res(uint16_t temp)
  107. {
  108. uint8_t heatr_res;
  109. int32_t var1;
  110. int32_t var2;
  111. int32_t var3;
  112. int32_t var4;
  113. int32_t var5;
  114. int32_t heatr_res_x100;
  115. if (temp < 200) /* Cap temperature */
  116. temp = 200;
  117. else if (temp > 400)
  118. temp = 400;
  119. var1 = (((int32_t) amb_temp * bme680_data.par_gh3) / 1000) * 256;
  120. var2 = (bme680_data.par_gh1 + 784) * (((((bme680_data.par_gh2 + 154009) * temp * 5) / 100) + 3276800) / 10);
  121. var3 = var1 + (var2 / 2);
  122. var4 = (var3 / (bme680_data.res_heat_range + 4));
  123. var5 = (131 * bme680_data.res_heat_val) + 65536;
  124. heatr_res_x100 = (int32_t) (((var4 / var5) - 250) * 34);
  125. heatr_res = (uint8_t) ((heatr_res_x100 + 50) / 100);
  126. return heatr_res;
  127. }
  128. static uint8_t calc_heater_dur(uint16_t dur)
  129. {
  130. uint8_t factor = 0;
  131. uint8_t durval;
  132. if (dur >= 0xfc0) {
  133. durval = 0xff; /* Max duration*/
  134. } else {
  135. while (dur > 0x3F) {
  136. dur = dur / 4;
  137. factor += 1;
  138. }
  139. durval = (uint8_t) (dur + (factor * 64));
  140. }
  141. return durval;
  142. }
  143. static int16_t calc_temperature(uint32_t temp_adc)
  144. {
  145. int64_t var1;
  146. int64_t var2;
  147. int64_t var3;
  148. int16_t calc_temp;
  149. var1 = ((int32_t) temp_adc / 8) - ((int32_t) bme680_data.par_t1 * 2);
  150. var2 = (var1 * (int32_t) bme680_data.par_t2) / 2048;
  151. var3 = ((var1 / 2) * (var1 / 2)) / 4096;
  152. var3 = ((var3) * ((int32_t) bme680_data.par_t3 * 16)) / 16384;
  153. bme680_data.t_fine = (int32_t) (var2 + var3);
  154. calc_temp = (int16_t) (((bme680_data.t_fine * 5) + 128) / 256);
  155. return calc_temp;
  156. }
  157. static uint32_t calc_pressure(uint32_t pres_adc)
  158. {
  159. int32_t var1;
  160. int32_t var2;
  161. int32_t var3;
  162. int32_t calc_pres;
  163. var1 = (((int32_t) bme680_data.t_fine) / 2) - 64000;
  164. var2 = ((var1 / 4) * (var1 / 4)) / 2048;
  165. var2 = ((var2) * (int32_t) bme680_data.par_p6) / 4;
  166. var2 = var2 + ((var1 * (int32_t) bme680_data.par_p5) * 2);
  167. var2 = (var2 / 4) + ((int32_t) bme680_data.par_p4 * 65536);
  168. var1 = ((var1 / 4) * (var1 / 4)) / 8192;
  169. var1 = (((var1) * ((int32_t) bme680_data.par_p3 * 32)) / 8) + (((int32_t) bme680_data.par_p2 * var1) / 2);
  170. var1 = var1 / 262144;
  171. var1 = ((32768 + var1) * (int32_t) bme680_data.par_p1) / 32768;
  172. calc_pres = (int32_t) (1048576 - pres_adc);
  173. calc_pres = (int32_t) ((calc_pres - (var2 / 4096)) * (3125));
  174. calc_pres = ((calc_pres / var1) * 2);
  175. var1 = ((int32_t) bme680_data.par_p9 * (int32_t) (((calc_pres / 8) * (calc_pres / 8)) / 8192)) / 4096;
  176. var2 = ((int32_t) (calc_pres / 4) * (int32_t) bme680_data.par_p8) / 8192;
  177. var3 = ((int32_t) (calc_pres / 256) * (int32_t) (calc_pres / 256) * (int32_t) (calc_pres / 256)
  178. * (int32_t) bme680_data.par_p10) / 131072;
  179. calc_pres = (int32_t) (calc_pres) + ((var1 + var2 + var3 + ((int32_t) bme680_data.par_p7 * 128)) / 16);
  180. return (uint32_t) calc_pres;
  181. }
  182. static uint32_t calc_humidity(uint16_t hum_adc)
  183. {
  184. int32_t var1;
  185. int32_t var2;
  186. int32_t var3;
  187. int32_t var4;
  188. int32_t var5;
  189. int32_t var6;
  190. int32_t temp_scaled;
  191. int32_t calc_hum;
  192. temp_scaled = (((int32_t) bme680_data.t_fine * 5) + 128) / 256;
  193. var1 = (int32_t) (hum_adc - ((int32_t) ((int32_t) bme680_data.par_h1 * 16)))
  194. - (((temp_scaled * (int32_t) bme680_data.par_h3) / ((int32_t) 100)) / 2);
  195. var2 = ((int32_t) bme680_data.par_h2
  196. * (((temp_scaled * (int32_t) bme680_data.par_h4) / ((int32_t) 100))
  197. + (((temp_scaled * ((temp_scaled * (int32_t) bme680_data.par_h5) / ((int32_t) 100))) / 64)
  198. / ((int32_t) 100)) + (int32_t) (1 * 16384))) / 1024;
  199. var3 = var1 * var2;
  200. var4 = (int32_t) bme680_data.par_h6 * 128;
  201. var4 = ((var4) + ((temp_scaled * (int32_t) bme680_data.par_h7) / ((int32_t) 100))) / 16;
  202. var5 = ((var3 / 16384) * (var3 / 16384)) / 1024;
  203. var6 = (var4 * var5) / 2;
  204. calc_hum = (((var3 + var6) / 1024) * ((int32_t) 1000)) / 4096;
  205. if (calc_hum > 100000) /* Cap at 100%rH */
  206. calc_hum = 100000;
  207. else if (calc_hum < 0)
  208. calc_hum = 0;
  209. return (uint32_t) calc_hum;
  210. }
  211. static uint32_t calc_gas_resistance(uint16_t gas_res_adc, uint8_t gas_range)
  212. {
  213. int64_t var1;
  214. uint64_t var2;
  215. int64_t var3;
  216. uint32_t calc_gas_res;
  217. var1 = (int64_t) ((1340 + (5 * (int64_t) bme680_data.range_sw_err)) * ((int64_t) lookupTable1[gas_range])) / 65536;
  218. var2 = (((int64_t) ((int64_t) gas_res_adc * 32768) - (int64_t) (16777216)) + var1);
  219. var3 = (((int64_t) lookupTable2[gas_range] * (int64_t) var1) / 512);
  220. calc_gas_res = (uint32_t) ((var3 + ((int64_t) var2 / 2)) / (int64_t) var2);
  221. return calc_gas_res;
  222. }
  223. uint16_t calc_dur()
  224. {
  225. uint32_t tph_dur; /* Calculate in us */
  226. /* TPH measurement duration */
  227. tph_dur = ((uint32_t) (os_temp + os_pres + os_hum) * UINT32_C(1963));
  228. tph_dur += UINT32_C(477 * 4); /* TPH switching duration */
  229. tph_dur += UINT32_C(477 * 5); /* Gas measurement duration */
  230. tph_dur += UINT32_C(500); /* Get it to the closest whole number.*/
  231. tph_dur /= UINT32_C(1000); /* Convert to ms */
  232. tph_dur += UINT32_C(1); /* Wake up duration of 1ms */
  233. NODE_DBG("tpc_dur: %d\n", tph_dur);
  234. /* The remaining time should be used for heating */
  235. return heatr_dur + (uint16_t) tph_dur;
  236. }
  237. /* This part of code is coming from the original bme680.c driver by Bosch.
  238. * END */
  239. static double ln(double x) {
  240. double y = (x-1)/(x+1);
  241. double y2 = y*y;
  242. double r = 0;
  243. for (int8_t i=33; i>0; i-=2) { //we've got the power
  244. r = 1.0/(double)i + y2 * r;
  245. }
  246. return 2*y*r;
  247. }
  248. static double bme280_qfe2qnh(int32_t qfe, int32_t h) {
  249. double hc;
  250. if (bme680_h == h) {
  251. hc = bme680_hc;
  252. } else {
  253. hc = pow((double)(1.0 - 2.25577e-5 * h), (double)(-5.25588));
  254. bme680_hc = hc; bme680_h = h;
  255. }
  256. double qnh = (double)qfe * hc;
  257. return qnh;
  258. }
  259. static int bme680_lua_setup(lua_State* L) {
  260. uint8_t ack;
  261. bme680_i2c_addr = BME680_I2C_ADDR_PRIMARY;
  262. platform_i2c_send_start(bme680_i2c_id);
  263. ack = platform_i2c_send_address(bme680_i2c_id, bme680_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  264. platform_i2c_send_stop(bme680_i2c_id);
  265. if (!ack) {
  266. NODE_DBG("No ACK on address: %x\n", bme680_i2c_addr);
  267. bme680_i2c_addr = BME680_I2C_ADDR_SECONDARY;
  268. platform_i2c_send_start(bme680_i2c_id);
  269. ack = platform_i2c_send_address(bme680_i2c_id, bme680_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  270. platform_i2c_send_stop(bme680_i2c_id);
  271. if (!ack) {
  272. NODE_DBG("No ACK on address: %x\n", bme680_i2c_addr);
  273. return 0;
  274. }
  275. }
  276. uint8_t chipid = r8u(BME680_CHIP_ID_ADDR);
  277. NODE_DBG("chip_id: %x\n", chipid);
  278. #define r16uLE_buf(reg) (uint16_t)(((uint16_t)reg[1] << 8) | (uint16_t)reg[0])
  279. #define r16sLE_buf(reg) (int16_t)(r16uLE_buf(reg))
  280. uint8_t buff[BME680_COEFF_SIZE], *reg;
  281. r8u_n(BME680_COEFF_ADDR1, BME680_COEFF_ADDR1_LEN, buff);
  282. r8u_n(BME680_COEFF_ADDR2, BME680_COEFF_ADDR2_LEN, &buff[BME680_COEFF_ADDR1_LEN]);
  283. reg = buff + 1;
  284. bme680_data.par_t2 = r16sLE_buf(reg); reg+=2; // #define BME680_T3_REG (3)
  285. bme680_data.par_t3 = (int8_t) reg[0]; reg+=2; // #define BME680_P1_LSB_REG (5)
  286. bme680_data.par_p1 = r16uLE_buf(reg); reg+=2; // #define BME680_P2_LSB_REG (7)
  287. bme680_data.par_p2 = r16sLE_buf(reg); reg+=2; // #define BME680_P3_REG (9)
  288. bme680_data.par_p3 = (int8_t) reg[0]; reg+=2; // #define BME680_P4_LSB_REG (11)
  289. bme680_data.par_p4 = r16sLE_buf(reg); reg+=2; // #define BME680_P5_LSB_REG (13)
  290. bme680_data.par_p5 = r16sLE_buf(reg); reg+=2; // #define BME680_P7_REG (15)
  291. bme680_data.par_p7 = (int8_t) reg[0]; reg++; // #define BME680_P6_REG (16)
  292. bme680_data.par_p6 = (int8_t) reg[0]; reg+=3; // #define BME680_P8_LSB_REG (19)
  293. bme680_data.par_p8 = r16sLE_buf(reg); reg+=2; // #define BME680_P9_LSB_REG (21)
  294. bme680_data.par_p9 = r16sLE_buf(reg); reg+=2; // #define BME680_P10_REG (23)
  295. bme680_data.par_p10 = (int8_t) reg[0]; reg+=2; // #define BME680_H2_MSB_REG (25)
  296. bme680_data.par_h2 = (uint16_t) (((uint16_t) reg[0] << BME680_HUM_REG_SHIFT_VAL)
  297. | ((reg[1]) >> BME680_HUM_REG_SHIFT_VAL)); reg++; // #define BME680_H1_LSB_REG (26)
  298. bme680_data.par_h1 = (uint16_t) (((uint16_t) reg[1] << BME680_HUM_REG_SHIFT_VAL)
  299. | (reg[0] & BME680_BIT_H1_DATA_MSK)); reg+=2; // #define BME680_H3_REG (28)
  300. bme680_data.par_h3 = (int8_t) reg[0]; reg++; // #define BME680_H4_REG (29)
  301. bme680_data.par_h4 = (int8_t) reg[0]; reg++; // #define BME680_H5_REG (30)
  302. bme680_data.par_h5 = (int8_t) reg[0]; reg++; // #define BME680_H6_REG (31)
  303. bme680_data.par_h6 = (uint8_t) reg[0]; reg++; // #define BME680_H7_REG (32)
  304. bme680_data.par_h7 = (int8_t) reg[0]; reg++; // #define BME680_T1_LSB_REG (33)
  305. bme680_data.par_t1 = r16uLE_buf(reg); reg+=2; // #define BME680_GH2_LSB_REG (35)
  306. bme680_data.par_gh2 = r16sLE_buf(reg); reg+=2; // #define BME680_GH1_REG (37)
  307. bme680_data.par_gh1 = reg[0]; reg++; // #define BME680_GH3_REG (38)
  308. bme680_data.par_gh3 = reg[0];
  309. #undef r16uLE_buf
  310. #undef r16sLE_buf
  311. /* Other coefficients */
  312. bme680_data.res_heat_range = ((r8u(BME680_ADDR_RES_HEAT_RANGE_ADDR) & BME680_RHRANGE_MSK) / 16);
  313. bme680_data.res_heat_val = (int8_t) r8u(BME680_ADDR_RES_HEAT_VAL_ADDR);
  314. bme680_data.range_sw_err = ((int8_t) r8u(BME680_ADDR_RANGE_SW_ERR_ADDR) & (int8_t) BME680_RSERROR_MSK) / 16;
  315. NODE_DBG("par_T: %d\t%d\t%d\n", bme680_data.par_t1, bme680_data.par_t2, bme680_data.par_t3);
  316. NODE_DBG("par_P: %d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", bme680_data.par_p1, bme680_data.par_p2, bme680_data.par_p3, bme680_data.par_p4, bme680_data.par_p5, bme680_data.par_p6, bme680_data.par_p7, bme680_data.par_p8, bme680_data.par_p9, bme680_data.par_p10);
  317. NODE_DBG("par_H: %d\t%d\t%d\t%d\t%d\t%d\t%d\n", bme680_data.par_h1, bme680_data.par_h2, bme680_data.par_h3, bme680_data.par_h4, bme680_data.par_h5, bme680_data.par_h6, bme680_data.par_h7);
  318. NODE_DBG("par_GH: %d\t%d\t%d\n", bme680_data.par_gh1, bme680_data.par_gh2, bme680_data.par_gh3);
  319. NODE_DBG("res_heat_range, res_heat_val, range_sw_err: %d\t%d\t%d\n", bme680_data.res_heat_range, bme680_data.res_heat_val, bme680_data.range_sw_err);
  320. uint8_t full_init = !lua_isnumber(L, 7)?1:lua_tointeger(L, 7); // 7-th parameter: init the chip too
  321. if (full_init) {
  322. uint8_t filter;
  323. uint8_t const bit3 = 0b111;
  324. uint8_t const bit2 = 0b11;
  325. //bme680.setup([temp_oss, press_oss, humi_oss, heater_temp, heater_duration, IIR_filter])
  326. os_temp = (!lua_isnumber(L, 1)?BME680_OS_2X:(luaL_checkinteger(L, 1)&bit3)); // 1-st parameter: temperature oversampling
  327. os_pres = (!lua_isnumber(L, 2)?BME680_OS_16X:(luaL_checkinteger(L, 2)&bit3)); // 2-nd parameter: pressure oversampling
  328. os_hum = (!lua_isnumber(L, 3))?BME680_OS_1X:(luaL_checkinteger(L, 3)&bit3);
  329. bme680_mode = BME680_SLEEP_MODE | (os_pres << 2) | (os_temp << 5);
  330. os_hum = os_hum; // 3-rd parameter: humidity oversampling
  331. filter = ((!lua_isnumber(L, 6)?BME680_FILTER_SIZE_31:(luaL_checkinteger(L, 6)&bit3)) << 2); // 6-th parameter: IIR filter
  332. NODE_DBG("mode: %x\nhumidity oss: %x\nconfig: %x\n", bme680_mode, os_hum, filter);
  333. heatr_dur = (!lua_isnumber(L, 5)?DEFAULT_HEATER_DUR:(luaL_checkinteger(L, 5))); // 5-th parameter: heater duration
  334. w8u(BME680_GAS_WAIT0_ADDR, calc_heater_dur(heatr_dur));
  335. w8u(BME680_RES_HEAT0_ADDR, calc_heater_res((!lua_isnumber(L, 4)?DEFAULT_HEATER_TEMP:(luaL_checkinteger(L, 4))))); // 4-th parameter: heater temperature
  336. w8u(BME680_CONF_ODR_FILT_ADDR, BME680_SET_BITS_POS_0(r8u(BME680_CONF_ODR_FILT_ADDR), BME680_FILTER, filter)); // #define BME680_CONF_ODR_FILT_ADDR UINT8_C(0x75)
  337. // set heater on
  338. w8u(BME680_CONF_HEAT_CTRL_ADDR, BME680_SET_BITS_POS_0(r8u(BME680_CONF_HEAT_CTRL_ADDR), BME680_HCTRL, 1));
  339. w8u(BME680_CONF_T_P_MODE_ADDR, bme680_mode);
  340. w8u(BME680_CONF_OS_H_ADDR, BME680_SET_BITS_POS_0(r8u(BME680_CONF_OS_H_ADDR), BME680_OSH, os_hum));
  341. w8u(BME680_CONF_ODR_RUN_GAS_NBC_ADDR, 1 << 4 | 0 & bit3);
  342. }
  343. lua_pushinteger(L, 1);
  344. return 1;
  345. }
  346. static void bme280_readoutdone (void *arg)
  347. {
  348. NODE_DBG("timer out\n");
  349. lua_State *L = lua_getstate();
  350. lua_rawgeti (L, LUA_REGISTRYINDEX, lua_connected_readout_ref);
  351. lua_call (L, 0, 0);
  352. luaL_unref (L, LUA_REGISTRYINDEX, lua_connected_readout_ref);
  353. os_timer_disarm (&bme680_timer);
  354. }
  355. static int bme680_lua_startreadout(lua_State* L) {
  356. uint32_t delay;
  357. if (lua_isnumber(L, 1)) {
  358. delay = luaL_checkinteger(L, 1);
  359. if (!delay) {delay = calc_dur();} // if delay is 0 then set the default delay
  360. }
  361. if (!lua_isnoneornil(L, 2)) {
  362. lua_pushvalue(L, 2);
  363. lua_connected_readout_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  364. } else {
  365. lua_connected_readout_ref = LUA_NOREF;
  366. }
  367. w8u(BME680_CONF_OS_H_ADDR, os_hum);
  368. w8u(BME680_CONF_T_P_MODE_ADDR, (bme680_mode & 0xFC) | BME680_FORCED_MODE);
  369. NODE_DBG("control old: %x, control: %x, delay: %d\n", bme680_mode, (bme680_mode & 0xFC) | BME680_FORCED_MODE, delay);
  370. if (lua_connected_readout_ref != LUA_NOREF) {
  371. NODE_DBG("timer armed\n");
  372. os_timer_disarm (&bme680_timer);
  373. os_timer_setfn (&bme680_timer, (os_timer_func_t *)bme280_readoutdone, L);
  374. os_timer_arm (&bme680_timer, delay, 0); // trigger callback when readout is ready
  375. }
  376. return 0;
  377. }
  378. // Return nothing on failure
  379. // Return T, QFE, H if no altitude given
  380. // Return T, QFE, H, QNH if altitude given
  381. static int bme680_lua_read(lua_State* L) {
  382. uint8_t buff[BME680_FIELD_LENGTH] = { 0 };
  383. uint8_t gas_range;
  384. uint32_t adc_temp;
  385. uint32_t adc_pres;
  386. uint16_t adc_hum;
  387. uint16_t adc_gas_res;
  388. uint8_t status;
  389. uint32_t qfe;
  390. uint8_t calc_qnh = lua_isnumber(L, 1);
  391. r8u_n(BME680_FIELD0_ADDR, BME680_FIELD_LENGTH, buff);
  392. status = buff[0] & BME680_NEW_DATA_MSK;
  393. /* read the raw data from the sensor */
  394. adc_pres = (uint32_t) (((uint32_t) buff[2] * 4096) | ((uint32_t) buff[3] * 16) | ((uint32_t) buff[4] / 16));
  395. adc_temp = (uint32_t) (((uint32_t) buff[5] * 4096) | ((uint32_t) buff[6] * 16) | ((uint32_t) buff[7] / 16));
  396. adc_hum = (uint16_t) (((uint32_t) buff[8] * 256) | (uint32_t) buff[9]);
  397. adc_gas_res = (uint16_t) ((uint32_t) buff[13] * 4 | (((uint32_t) buff[14]) / 64));
  398. gas_range = buff[14] & BME680_GAS_RANGE_MSK;
  399. status |= buff[14] & BME680_GASM_VALID_MSK;
  400. status |= buff[14] & BME680_HEAT_STAB_MSK;
  401. NODE_DBG("status, new_data, gas_range, gasm_valid: 0x%x, 0x%x, 0x%x, 0x%x\n", status, status & BME680_NEW_DATA_MSK, buff[14] & BME680_GAS_RANGE_MSK, buff[14] & BME680_GASM_VALID_MSK);
  402. if (!(status & BME680_NEW_DATA_MSK)) {
  403. return 0;
  404. }
  405. int16_t temp = calc_temperature(adc_temp);
  406. amb_temp = temp / 100;
  407. lua_pushinteger(L, temp);
  408. qfe = calc_pressure(adc_pres);
  409. lua_pushinteger(L, qfe);
  410. lua_pushinteger(L, calc_humidity(adc_hum));
  411. lua_pushinteger(L, calc_gas_resistance(adc_gas_res, gas_range));
  412. if (calc_qnh) { // have altitude
  413. int32_t h = luaL_checkinteger(L, 1);
  414. double qnh = bme280_qfe2qnh(qfe, h);
  415. lua_pushinteger(L, (int32_t)(qnh + 0.5));
  416. return 5;
  417. }
  418. return 4;
  419. }
  420. static int bme680_lua_qfe2qnh(lua_State* L) {
  421. if (!lua_isnumber(L, 2)) {
  422. return luaL_error(L, "wrong arg range");
  423. }
  424. int32_t qfe = luaL_checkinteger(L, 1);
  425. int32_t h = luaL_checkinteger(L, 2);
  426. double qnh = bme280_qfe2qnh(qfe, h);
  427. lua_pushinteger(L, (int32_t)(qnh + 0.5));
  428. return 1;
  429. }
  430. static int bme680_lua_altitude(lua_State* L) {
  431. if (!lua_isnumber(L, 2)) {
  432. return luaL_error(L, "wrong arg range");
  433. }
  434. int32_t P = luaL_checkinteger(L, 1);
  435. int32_t qnh = luaL_checkinteger(L, 2);
  436. double h = (1.0 - pow((double)P/(double)qnh, 1.0/5.25588)) / 2.25577e-5 * 100.0;
  437. lua_pushinteger(L, (int32_t)(h + (((h<0)?-1:(h>0)) * 0.5)));
  438. return 1;
  439. }
  440. static int bme680_lua_dewpoint(lua_State* L) {
  441. if (!lua_isnumber(L, 2)) {
  442. return luaL_error(L, "wrong arg range");
  443. }
  444. double H = luaL_checkinteger(L, 1)/100000.0;
  445. double T = luaL_checkinteger(L, 2)/100.0;
  446. const double c243 = 243.5;
  447. const double c17 = 17.67;
  448. double c = ln(H) + ((c17 * T) / (c243 + T));
  449. double d = (c243 * c)/(c17 - c) * 100.0;
  450. lua_pushinteger(L, (int32_t)(d + (((d<0)?-1:(d>0)) * 0.5)));
  451. return 1;
  452. }
  453. LROT_BEGIN(bme680)
  454. LROT_FUNCENTRY( setup, bme680_lua_setup )
  455. LROT_FUNCENTRY( startreadout, bme680_lua_startreadout )
  456. LROT_FUNCENTRY( qfe2qnh, bme680_lua_qfe2qnh )
  457. LROT_FUNCENTRY( altitude, bme680_lua_altitude )
  458. LROT_FUNCENTRY( dewpoint, bme680_lua_dewpoint )
  459. LROT_FUNCENTRY( read, bme680_lua_read )
  460. LROT_END( bme680, NULL, 0 )
  461. NODEMCU_MODULE(BME680, "bme680", bme680, NULL);