bme280.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. // ***************************************************************************
  2. // BMP280 module for ESP8266 with nodeMCU
  3. //
  4. // Written by Lukas Voborsky, @voborsky
  5. //
  6. // MIT license, http://opensource.org/licenses/MIT
  7. // ***************************************************************************
  8. //#define NODE_DEBUG
  9. #include "module.h"
  10. #include "lauxlib.h"
  11. #include "platform.h"
  12. #include "c_math.h"
  13. /****************************************************/
  14. /**\name registers definition */
  15. /***************************************************/
  16. #define BME280_REGISTER_CONTROL (0xF4)
  17. #define BME280_REGISTER_CONTROL_HUM (0xF2)
  18. #define BME280_REGISTER_CONFIG (0xF5)
  19. #define BME280_REGISTER_CHIPID (0xD0)
  20. #define BME280_REGISTER_VERSION (0xD1)
  21. #define BME280_REGISTER_SOFTRESET (0xE0)
  22. #define BME280_REGISTER_CAL26 (0xE1)
  23. #define BME280_REGISTER_PRESS (0xF7) // 0xF7-0xF9
  24. #define BME280_REGISTER_TEMP (0xFA) // 0xFA-0xFC
  25. #define BME280_REGISTER_HUM (0xFD) // 0xFD-0xFE
  26. #define BME280_REGISTER_DIG_T (0x88) // 0x88-0x8D ( 6)
  27. #define BME280_REGISTER_DIG_P (0x8E) // 0x8E-0x9F (18)
  28. #define BME280_REGISTER_DIG_H1 (0xA1) // 0xA1 ( 1)
  29. #define BME280_REGISTER_DIG_H2 (0xE1) // 0xE1-0xE7 ( 7)
  30. /****************************************************/
  31. /**\name I2C ADDRESS DEFINITIONS */
  32. /***************************************************/
  33. #define BME280_I2C_ADDRESS1 (0x76)
  34. #define BME280_I2C_ADDRESS2 (0x77)
  35. /****************************************************/
  36. /**\name POWER MODE DEFINITIONS */
  37. /***************************************************/
  38. /* Sensor Specific constants */
  39. #define BME280_SLEEP_MODE (0x00)
  40. #define BME280_FORCED_MODE (0x01)
  41. #define BME280_NORMAL_MODE (0x03)
  42. #define BME280_SOFT_RESET_CODE (0xB6)
  43. /****************************************************/
  44. /**\name OVER SAMPLING DEFINITIONS */
  45. /***************************************************/
  46. #define BME280_OVERSAMP_1X (0x01)
  47. #define BME280_OVERSAMP_2X (0x02)
  48. #define BME280_OVERSAMP_4X (0x03)
  49. #define BME280_OVERSAMP_8X (0x04)
  50. #define BME280_OVERSAMP_16X (0x05)
  51. /****************************************************/
  52. /**\name STANDBY TIME DEFINITIONS */
  53. /***************************************************/
  54. #define BME280_STANDBY_TIME_1_MS (0x00)
  55. #define BME280_STANDBY_TIME_63_MS (0x01)
  56. #define BME280_STANDBY_TIME_125_MS (0x02)
  57. #define BME280_STANDBY_TIME_250_MS (0x03)
  58. #define BME280_STANDBY_TIME_500_MS (0x04)
  59. #define BME280_STANDBY_TIME_1000_MS (0x05)
  60. #define BME280_STANDBY_TIME_10_MS (0x06)
  61. #define BME280_STANDBY_TIME_20_MS (0x07)
  62. /****************************************************/
  63. /**\name FILTER DEFINITIONS */
  64. /***************************************************/
  65. #define BME280_FILTER_COEFF_OFF (0x00)
  66. #define BME280_FILTER_COEFF_2 (0x01)
  67. #define BME280_FILTER_COEFF_4 (0x02)
  68. #define BME280_FILTER_COEFF_8 (0x03)
  69. #define BME280_FILTER_COEFF_16 (0x04)
  70. /****************************************************/
  71. /**\data type definition */
  72. /***************************************************/
  73. #define BME280_S32_t int32_t
  74. #define BME280_U32_t uint32_t
  75. #define BME280_S64_t int64_t
  76. #define BME280_SAMPLING_DELAY 113 //maximum measurement time in ms for maximum oversampling for all measures = 1.25 + 2.3*16 + 2.3*16 + 0.575 + 2.3*16 + 0.575 ms
  77. // #define r16s(reg) ((int16_t)r16u(reg))
  78. // #define r16sLE(reg) ((int16_t)r16uLE(reg))
  79. // #define bme280_adc_P(void) r24u(BME280_REGISTER_PRESS)
  80. // #define bme280_adc_T(void) r24u(BME280_REGISTER_TEMP)
  81. // #define bme280_adc_H(void) r16u(BME280_REGISTER_HUM)
  82. static const uint32_t bme280_i2c_id = 0;
  83. static uint8_t bme280_i2c_addr = BME280_I2C_ADDRESS1;
  84. static uint8_t bme280_isbme = 0; // 1 if the chip is BME280, 0 for BMP280
  85. static uint8_t bme280_mode = 0; // stores oversampling settings
  86. static uint8_t bme280_ossh = 0; // stores humidity oversampling settings
  87. os_timer_t bme280_timer; // timer for forced mode readout
  88. int lua_connected_readout_ref; // callback when readout is ready
  89. static struct {
  90. uint16_t dig_T1;
  91. int16_t dig_T2;
  92. int16_t dig_T3;
  93. uint16_t dig_P1;
  94. int16_t dig_P2;
  95. int16_t dig_P3;
  96. int16_t dig_P4;
  97. int16_t dig_P5;
  98. int16_t dig_P6;
  99. int16_t dig_P7;
  100. int16_t dig_P8;
  101. int16_t dig_P9;
  102. uint8_t dig_H1;
  103. int16_t dig_H2;
  104. uint8_t dig_H3;
  105. int16_t dig_H4;
  106. int16_t dig_H5;
  107. int8_t dig_H6;
  108. } bme280_data;
  109. static BME280_S32_t bme280_t_fine;
  110. static uint32_t bme280_h = 0;
  111. static double bme280_hc = 1.0;
  112. // return 0 if good
  113. static int r8u_n(uint8_t reg, int n, uint8_t *buf) {
  114. int i;
  115. platform_i2c_send_start(bme280_i2c_id);
  116. platform_i2c_send_address(bme280_i2c_id, bme280_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  117. platform_i2c_send_byte(bme280_i2c_id, reg);
  118. // platform_i2c_send_stop(bme280_i2c_id); // doco says not needed
  119. platform_i2c_send_start(bme280_i2c_id);
  120. platform_i2c_send_address(bme280_i2c_id, bme280_i2c_addr, PLATFORM_I2C_DIRECTION_RECEIVER);
  121. while (n-- > 0)
  122. *buf++ = platform_i2c_recv_byte(bme280_i2c_id, n > 0);
  123. platform_i2c_send_stop(bme280_i2c_id);
  124. return 0;
  125. }
  126. static uint8_t w8u(uint8_t reg, uint8_t val) {
  127. platform_i2c_send_start(bme280_i2c_id);
  128. platform_i2c_send_address(bme280_i2c_id, bme280_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  129. platform_i2c_send_byte(bme280_i2c_id, reg);
  130. platform_i2c_send_byte(bme280_i2c_id, val);
  131. platform_i2c_send_stop(bme280_i2c_id);
  132. }
  133. static uint8_t r8u(uint8_t reg) {
  134. uint8_t ret[1];
  135. r8u_n(reg, 1, ret);
  136. return ret[0];
  137. }
  138. // Returns temperature in DegC, resolution is 0.01 DegC. Output value of “5123” equals 51.23 DegC.
  139. // t_fine carries fine temperature as global value
  140. static BME280_S32_t bme280_compensate_T(BME280_S32_t adc_T) {
  141. BME280_S32_t var1, var2, T;
  142. var1 = ((((adc_T>>3) - ((BME280_S32_t)bme280_data.dig_T1<<1))) * ((BME280_S32_t)bme280_data.dig_T2)) >> 11;
  143. var2 = (((((adc_T>>4) - ((BME280_S32_t)bme280_data.dig_T1)) * ((adc_T>>4) - ((BME280_S32_t)bme280_data.dig_T1))) >> 12) *
  144. ((BME280_S32_t)bme280_data.dig_T3)) >> 14;
  145. bme280_t_fine = var1 + var2;
  146. T = (bme280_t_fine * 5 + 128) >> 8;
  147. return T;
  148. }
  149. // Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24 integer bits and 8 fractional bits).
  150. // Output value of “24674867” represents 24674867/256 = 96386.2 Pa = 963.862 hPa
  151. static BME280_U32_t bme280_compensate_P(BME280_S32_t adc_P) {
  152. BME280_S64_t var1, var2, p;
  153. var1 = ((BME280_S64_t)bme280_t_fine) - 128000;
  154. var2 = var1 * var1 * (BME280_S64_t)bme280_data.dig_P6;
  155. var2 = var2 + ((var1*(BME280_S64_t)bme280_data.dig_P5)<<17);
  156. var2 = var2 + (((BME280_S64_t)bme280_data.dig_P4)<<35);
  157. var1 = ((var1 * var1 * (BME280_S64_t)bme280_data.dig_P3)>>8) + ((var1 * (BME280_S64_t)bme280_data.dig_P2)<<12);
  158. var1 = (((((BME280_S64_t)1)<<47)+var1))*((BME280_S64_t)bme280_data.dig_P1)>>33;
  159. if (var1 == 0) {
  160. return 0; // avoid exception caused by division by zero
  161. }
  162. p = 1048576-adc_P;
  163. p = (((p<<31)-var2)*3125)/var1;
  164. var1 = (((BME280_S64_t)bme280_data.dig_P9) * (p>>13) * (p>>13)) >> 25;
  165. var2 = (((BME280_S64_t)bme280_data.dig_P8) * p) >> 19;
  166. p = ((p + var1 + var2) >> 8) + (((BME280_S64_t)bme280_data.dig_P7)<<4);
  167. p = (p * 10) >> 8;
  168. return (BME280_U32_t)p;
  169. }
  170. // Returns humidity in %RH as unsigned 32 bit integer in Q22.10 format (22 integer and 10 fractional bits).
  171. // Output value of “47445” represents 47445/1024 = 46.333 %RH
  172. static BME280_U32_t bme280_compensate_H(BME280_S32_t adc_H) {
  173. BME280_S32_t v_x1_u32r;
  174. v_x1_u32r = (bme280_t_fine - ((BME280_S32_t)76800));
  175. v_x1_u32r = (((((adc_H << 14) - (((BME280_S32_t)bme280_data.dig_H4) << 20) - (((BME280_S32_t)bme280_data.dig_H5) * v_x1_u32r)) +
  176. ((BME280_S32_t)16384)) >> 15) * (((((((v_x1_u32r * ((BME280_S32_t)bme280_data.dig_H6)) >> 10) * (((v_x1_u32r *
  177. ((BME280_S32_t)bme280_data.dig_H3)) >> 11) + ((BME280_S32_t)32768))) >> 10) + ((BME280_S32_t)2097152)) *
  178. ((BME280_S32_t)bme280_data.dig_H2) + 8192) >> 14));
  179. v_x1_u32r = (v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) * ((BME280_S32_t)bme280_data.dig_H1)) >> 4));
  180. v_x1_u32r = (v_x1_u32r < 0 ? 0 : v_x1_u32r);
  181. v_x1_u32r = (v_x1_u32r > 419430400 ? 419430400 : v_x1_u32r);
  182. v_x1_u32r = v_x1_u32r>>12;
  183. return (BME280_U32_t)((v_x1_u32r * 1000)>>10);
  184. }
  185. static double ln(double x) {
  186. double y = (x-1)/(x+1);
  187. double y2 = y*y;
  188. double r = 0;
  189. for (int8_t i=33; i>0; i-=2) { //we've got the power
  190. r = 1.0/(double)i + y2 * r;
  191. }
  192. return 2*y*r;
  193. }
  194. static double bme280_qfe2qnh(int32_t qfe, int32_t h) {
  195. double hc;
  196. if (bme280_h == h) {
  197. hc = bme280_hc;
  198. } else {
  199. hc = pow((double)(1.0 - 2.25577e-5 * h), (double)(-5.25588));
  200. bme280_hc = hc; bme280_h = h;
  201. }
  202. double qnh = (double)qfe * hc;
  203. return qnh;
  204. }
  205. static int bme280_lua_init(lua_State* L) {
  206. uint8_t sda;
  207. uint8_t scl;
  208. uint8_t config;
  209. uint8_t ack;
  210. uint8_t full_init;
  211. uint8_t const bit3 = 0b111;
  212. uint8_t const bit2 = 0b11;
  213. if (!lua_isnumber(L, 1) || !lua_isnumber(L, 2)) {
  214. return luaL_error(L, "wrong arg range");
  215. }
  216. sda = luaL_checkinteger(L, 1);
  217. scl = luaL_checkinteger(L, 2);
  218. bme280_mode = (!lua_isnumber(L, 6)?BME280_NORMAL_MODE:(luaL_checkinteger(L, 6)&bit2)) // 6-th parameter: power mode
  219. | ((!lua_isnumber(L, 4)?BME280_OVERSAMP_16X:(luaL_checkinteger(L, 4)&bit3)) << 2) // 4-th parameter: pressure oversampling
  220. | ((!lua_isnumber(L, 3)?BME280_OVERSAMP_16X:(luaL_checkinteger(L, 3)&bit3)) << 5); // 3-rd parameter: temperature oversampling
  221. bme280_ossh = (!lua_isnumber(L, 5))?BME280_OVERSAMP_16X:(luaL_checkinteger(L, 5)&bit3); // 5-th parameter: humidity oversampling
  222. config = ((!lua_isnumber(L, 7)?BME280_STANDBY_TIME_20_MS:(luaL_checkinteger(L, 7)&bit3))<< 5) // 7-th parameter: inactive duration in normal mode
  223. | ((!lua_isnumber(L, 8)?BME280_FILTER_COEFF_16:(luaL_checkinteger(L, 8)&bit3)) << 2); // 8-th parameter: IIR filter
  224. full_init = !lua_isnumber(L, 9)?1:lua_tointeger(L, 9); // 9-th parameter: init the chip too
  225. NODE_DBG("mode: %x\nhumidity oss: %x\nconfig: %x\n", bme280_mode, bme280_ossh, config);
  226. platform_i2c_setup(bme280_i2c_id, sda, scl, PLATFORM_I2C_SPEED_SLOW);
  227. bme280_i2c_addr = BME280_I2C_ADDRESS1;
  228. platform_i2c_send_start(bme280_i2c_id);
  229. ack = platform_i2c_send_address(bme280_i2c_id, bme280_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  230. platform_i2c_send_stop(bme280_i2c_id);
  231. if (!ack) {
  232. NODE_DBG("No ACK on address: %x\n", bme280_i2c_addr);
  233. bme280_i2c_addr = BME280_I2C_ADDRESS2;
  234. platform_i2c_send_start(bme280_i2c_id);
  235. ack = platform_i2c_send_address(bme280_i2c_id, bme280_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  236. platform_i2c_send_stop(bme280_i2c_id);
  237. if (!ack) {
  238. NODE_DBG("No ACK on address: %x\n", bme280_i2c_addr);
  239. return 0;
  240. }
  241. }
  242. uint8_t chipid = r8u(BME280_REGISTER_CHIPID);
  243. NODE_DBG("chip_id: %x\n", chipid);
  244. bme280_isbme = (chipid == 0x60);
  245. #define r16uLE_buf(reg) (uint16_t)((reg[1] << 8) | reg[0])
  246. #define r16sLE_buf(reg) (int16_t)(r16uLE_buf(reg))
  247. uint8_t buf[18], *reg;
  248. r8u_n(BME280_REGISTER_DIG_T, 6, buf);
  249. reg = buf;
  250. bme280_data.dig_T1 = r16uLE_buf(reg); reg+=2;
  251. bme280_data.dig_T2 = r16sLE_buf(reg); reg+=2;
  252. bme280_data.dig_T3 = r16sLE_buf(reg);
  253. //NODE_DBG("dig_T: %d\t%d\t%d\n", bme280_data.dig_T1, bme280_data.dig_T2, bme280_data.dig_T3);
  254. r8u_n(BME280_REGISTER_DIG_P, 18, buf);
  255. reg = buf;
  256. bme280_data.dig_P1 = r16uLE_buf(reg); reg+=2;
  257. bme280_data.dig_P2 = r16sLE_buf(reg); reg+=2;
  258. bme280_data.dig_P3 = r16sLE_buf(reg); reg+=2;
  259. bme280_data.dig_P4 = r16sLE_buf(reg); reg+=2;
  260. bme280_data.dig_P5 = r16sLE_buf(reg); reg+=2;
  261. bme280_data.dig_P6 = r16sLE_buf(reg); reg+=2;
  262. bme280_data.dig_P7 = r16sLE_buf(reg); reg+=2;
  263. bme280_data.dig_P8 = r16sLE_buf(reg); reg+=2;
  264. bme280_data.dig_P9 = r16sLE_buf(reg);
  265. // NODE_DBG("dig_P: %d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", bme280_data.dig_P1, bme280_data.dig_P2, bme280_data.dig_P3, bme280_data.dig_P4, bme280_data.dig_P5, bme280_data.dig_P6, bme280_data.dig_P7, bme280_data.dig_P8, bme280_data.dig_P9);
  266. if (full_init) w8u(BME280_REGISTER_CONFIG, config);
  267. if (bme280_isbme) {
  268. bme280_data.dig_H1 = r8u(BME280_REGISTER_DIG_H1);
  269. r8u_n(BME280_REGISTER_DIG_H2, 7, buf);
  270. reg = buf;
  271. bme280_data.dig_H2 = r16sLE_buf(reg); reg+=2;
  272. bme280_data.dig_H3 = reg[0]; reg++;
  273. bme280_data.dig_H4 = (int16_t)reg[0] << 4 | (reg[1] & 0x0F); reg+=1; // H4[11:4 3:0] = 0xE4[7:0] 0xE5[3:0] 12-bit signed
  274. bme280_data.dig_H5 = (int16_t)reg[1] << 4 | (reg[0] >> 4); reg+=2; // H5[11:4 3:0] = 0xE6[7:0] 0xE5[7:4] 12-bit signed
  275. bme280_data.dig_H6 = (int8_t)reg[0];
  276. // NODE_DBG("dig_H: %d\t%d\t%d\t%d\t%d\t%d\n", bme280_data.dig_H1, bme280_data.dig_H2, bme280_data.dig_H3, bme280_data.dig_H4, bme280_data.dig_H5, bme280_data.dig_H6);
  277. if (full_init) w8u(BME280_REGISTER_CONTROL_HUM, bme280_ossh);
  278. lua_pushinteger(L, 2);
  279. } else {
  280. lua_pushinteger(L, 1);
  281. }
  282. #undef r16uLE_buf
  283. #undef r16sLE_buf
  284. if (full_init) w8u(BME280_REGISTER_CONTROL, bme280_mode);
  285. return 1;
  286. }
  287. static void bme280_readoutdone (void *arg)
  288. {
  289. NODE_DBG("timer out\n");
  290. lua_State *L = lua_getstate();
  291. lua_rawgeti (L, LUA_REGISTRYINDEX, lua_connected_readout_ref);
  292. lua_call (L, 0, 0);
  293. luaL_unref (L, LUA_REGISTRYINDEX, lua_connected_readout_ref);
  294. os_timer_disarm (&bme280_timer);
  295. }
  296. static int bme280_lua_startreadout(lua_State* L) {
  297. uint32_t delay;
  298. if (lua_isnumber(L, 1)) {
  299. delay = luaL_checkinteger(L, 1);
  300. if (!delay) {delay = BME280_SAMPLING_DELAY;} // if delay is 0 then set the default delay
  301. }
  302. if (!lua_isnoneornil(L, 2)) {
  303. lua_pushvalue(L, 2);
  304. lua_connected_readout_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  305. } else {
  306. lua_connected_readout_ref = LUA_NOREF;
  307. }
  308. w8u(BME280_REGISTER_CONTROL_HUM, bme280_ossh);
  309. w8u(BME280_REGISTER_CONTROL, (bme280_mode & 0xFC) | BME280_FORCED_MODE);
  310. NODE_DBG("control old: %x, control: %x, delay: %d\n", bme280_mode, (bme280_mode & 0xFC) | BME280_FORCED_MODE, delay);
  311. if (lua_connected_readout_ref != LUA_NOREF) {
  312. NODE_DBG("timer armed\n");
  313. os_timer_disarm (&bme280_timer);
  314. os_timer_setfn (&bme280_timer, (os_timer_func_t *)bme280_readoutdone, L);
  315. os_timer_arm (&bme280_timer, delay, 0); // trigger callback when readout is ready
  316. }
  317. return 0;
  318. }
  319. // Return nothing on failure
  320. // Return T, QFE, H if no altitude given
  321. // Return T, QFE, H, QNH if altitude given
  322. static int bme280_lua_read(lua_State* L) {
  323. uint8_t buf[8];
  324. uint32_t qfe;
  325. uint8_t calc_qnh = lua_isnumber(L, 1);
  326. r8u_n(BME280_REGISTER_PRESS, 8, buf); // registers are P[3], T[3], H[2]
  327. // Must do Temp first since bme280_t_fine is used by the other compensation functions
  328. uint32_t adc_T = (uint32_t)(((buf[3] << 16) | (buf[4] << 8) | buf[5]) >> 4);
  329. if (adc_T == 0x80000 || adc_T == 0xfffff)
  330. return 0;
  331. lua_pushinteger(L, bme280_compensate_T(adc_T));
  332. uint32_t adc_P = (uint32_t)(((buf[0] << 16) | (buf[1] << 8) | buf[2]) >> 4);
  333. if (adc_P ==0x80000 || adc_P == 0xfffff) {
  334. lua_pushnil(L);
  335. calc_qnh = 0;
  336. } else {
  337. qfe = bme280_compensate_P(adc_P);
  338. lua_pushinteger(L, qfe);
  339. }
  340. uint32_t adc_H = (uint32_t)((buf[6] << 8) | buf[7]);
  341. if (!bme280_isbme || adc_H == 0x8000 || adc_H == 0xffff)
  342. lua_pushnil(L);
  343. else
  344. lua_pushinteger(L, bme280_compensate_H(adc_H));
  345. if (calc_qnh) { // have altitude
  346. int32_t h = luaL_checkinteger(L, 1);
  347. double qnh = bme280_qfe2qnh(qfe, h);
  348. lua_pushinteger(L, (int32_t)(qnh + 0.5));
  349. return 4;
  350. }
  351. return 3;
  352. }
  353. static int bme280_lua_temp(lua_State* L) {
  354. uint8_t buf[3];
  355. r8u_n(BME280_REGISTER_TEMP, 3, buf); // registers are P[3], T[3], H[2]
  356. uint32_t adc_T = (uint32_t)(((buf[0] << 16) | (buf[1] << 8) | buf[2]) >> 4);
  357. if (adc_T == 0x80000 || adc_T == 0xfffff)
  358. return 0;
  359. lua_pushinteger(L, bme280_compensate_T(adc_T));
  360. lua_pushinteger(L, bme280_t_fine);
  361. return 2;
  362. }
  363. static int bme280_lua_baro(lua_State* L) {
  364. uint8_t buf[6];
  365. r8u_n(BME280_REGISTER_PRESS, 6, buf); // registers are P[3], T[3], H[2]
  366. uint32_t adc_T = (uint32_t)(((buf[3] << 16) | (buf[4] << 8) | buf[5]) >> 4);
  367. uint32_t T = bme280_compensate_T(adc_T);
  368. uint32_t adc_P = (uint32_t)(((buf[0] << 16) | (buf[1] << 8) | buf[2]) >> 4);
  369. if (adc_T == 0x80000 || adc_T == 0xfffff || adc_P ==0x80000 || adc_P == 0xfffff)
  370. return 0;
  371. lua_pushinteger(L, bme280_compensate_P(adc_P));
  372. lua_pushinteger(L, T);
  373. return 2;
  374. }
  375. static int bme280_lua_humi(lua_State* L) {
  376. if (!bme280_isbme) return 0;
  377. uint8_t buf[5];
  378. r8u_n(BME280_REGISTER_TEMP, 5, buf); // registers are P[3], T[3], H[2]
  379. uint32_t adc_T = (uint32_t)(((buf[0] << 16) | (buf[1] << 8) | buf[2]) >> 4);
  380. uint32_t T = bme280_compensate_T(adc_T);
  381. uint32_t adc_H = (uint32_t)((buf[3] << 8) | buf[4]);
  382. if (adc_T == 0x80000 || adc_T == 0xfffff || adc_H == 0x8000 || adc_H == 0xffff)
  383. return 0;
  384. lua_pushinteger(L, bme280_compensate_H(adc_H));
  385. lua_pushinteger(L, T);
  386. return 2;
  387. }
  388. static int bme280_lua_qfe2qnh(lua_State* L) {
  389. if (!lua_isnumber(L, 2)) {
  390. return luaL_error(L, "wrong arg range");
  391. }
  392. int32_t qfe = luaL_checkinteger(L, 1);
  393. int32_t h = luaL_checkinteger(L, 2);
  394. double qnh = bme280_qfe2qnh(qfe, h);
  395. lua_pushinteger(L, (int32_t)(qnh + 0.5));
  396. return 1;
  397. }
  398. static int bme280_lua_altitude(lua_State* L) {
  399. if (!lua_isnumber(L, 2)) {
  400. return luaL_error(L, "wrong arg range");
  401. }
  402. int32_t P = luaL_checkinteger(L, 1);
  403. int32_t qnh = luaL_checkinteger(L, 2);
  404. double h = (1.0 - pow((double)P/(double)qnh, 1.0/5.25588)) / 2.25577e-5 * 100.0;
  405. lua_pushinteger(L, (int32_t)(h + (((h<0)?-1:(h>0)) * 0.5)));
  406. return 1;
  407. }
  408. static int bme280_lua_dewpoint(lua_State* L) {
  409. if (!lua_isnumber(L, 2)) {
  410. return luaL_error(L, "wrong arg range");
  411. }
  412. double H = luaL_checkinteger(L, 1)/100000.0;
  413. double T = luaL_checkinteger(L, 2)/100.0;
  414. const double c243 = 243.5;
  415. const double c17 = 17.67;
  416. double c = ln(H) + ((c17 * T) / (c243 + T));
  417. double d = (c243 * c)/(c17 - c) * 100.0;
  418. lua_pushinteger(L, (int32_t)(d + (((d<0)?-1:(d>0)) * 0.5)));
  419. return 1;
  420. }
  421. static const LUA_REG_TYPE bme280_map[] = {
  422. { LSTRKEY( "init" ), LFUNCVAL(bme280_lua_init)},
  423. { LSTRKEY( "temp" ), LFUNCVAL(bme280_lua_temp)},
  424. { LSTRKEY( "baro" ), LFUNCVAL(bme280_lua_baro)},
  425. { LSTRKEY( "humi" ), LFUNCVAL(bme280_lua_humi)},
  426. { LSTRKEY( "startreadout" ), LFUNCVAL(bme280_lua_startreadout)},
  427. { LSTRKEY( "qfe2qnh" ), LFUNCVAL(bme280_lua_qfe2qnh)},
  428. { LSTRKEY( "altitude" ), LFUNCVAL(bme280_lua_altitude)},
  429. { LSTRKEY( "dewpoint" ), LFUNCVAL(bme280_lua_dewpoint)},
  430. { LSTRKEY( "read" ), LFUNCVAL(bme280_lua_read)},
  431. { LNILKEY, LNILVAL}
  432. };
  433. NODEMCU_MODULE(BME280, "bme280", bme280_map, NULL);