bme280.c 18 KB

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