bme280.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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_setup(lua_State* L) {
  206. uint8_t config;
  207. uint8_t ack;
  208. uint8_t full_init;
  209. uint8_t const bit3 = 0b111;
  210. uint8_t const bit2 = 0b11;
  211. bme280_mode = (!lua_isnumber(L, 4)?BME280_NORMAL_MODE:(luaL_checkinteger(L, 4)&bit2)) // 4-th parameter: power mode
  212. | ((!lua_isnumber(L, 2)?BME280_OVERSAMP_16X:(luaL_checkinteger(L, 2)&bit3)) << 2) // 2-nd parameter: pressure oversampling
  213. | ((!lua_isnumber(L, 1)?BME280_OVERSAMP_16X:(luaL_checkinteger(L, 1)&bit3)) << 5); // 1-st parameter: temperature oversampling
  214. bme280_ossh = (!lua_isnumber(L, 3))?BME280_OVERSAMP_16X:(luaL_checkinteger(L, 3)&bit3); // 3-rd parameter: humidity oversampling
  215. config = ((!lua_isnumber(L, 5)?BME280_STANDBY_TIME_20_MS:(luaL_checkinteger(L, 5)&bit3))<< 5) // 5-th parameter: inactive duration in normal mode
  216. | ((!lua_isnumber(L, 6)?BME280_FILTER_COEFF_16:(luaL_checkinteger(L, 6)&bit3)) << 2); // 6-th parameter: IIR filter
  217. full_init = !lua_isnumber(L, 7)?1:lua_tointeger(L, 7); // 7-th parameter: init the chip too
  218. NODE_DBG("mode: %x\nhumidity oss: %x\nconfig: %x\n", bme280_mode, bme280_ossh, config);
  219. bme280_i2c_addr = BME280_I2C_ADDRESS1;
  220. platform_i2c_send_start(bme280_i2c_id);
  221. ack = platform_i2c_send_address(bme280_i2c_id, bme280_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  222. platform_i2c_send_stop(bme280_i2c_id);
  223. if (!ack) {
  224. NODE_DBG("No ACK on address: %x\n", bme280_i2c_addr);
  225. bme280_i2c_addr = BME280_I2C_ADDRESS2;
  226. platform_i2c_send_start(bme280_i2c_id);
  227. ack = platform_i2c_send_address(bme280_i2c_id, bme280_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  228. platform_i2c_send_stop(bme280_i2c_id);
  229. if (!ack) {
  230. NODE_DBG("No ACK on address: %x\n", bme280_i2c_addr);
  231. return 0;
  232. }
  233. }
  234. uint8_t chipid = r8u(BME280_REGISTER_CHIPID);
  235. NODE_DBG("chip_id: %x\n", chipid);
  236. bme280_isbme = (chipid == 0x60);
  237. #define r16uLE_buf(reg) (uint16_t)((reg[1] << 8) | reg[0])
  238. #define r16sLE_buf(reg) (int16_t)(r16uLE_buf(reg))
  239. uint8_t buf[18], *reg;
  240. r8u_n(BME280_REGISTER_DIG_T, 6, buf);
  241. reg = buf;
  242. bme280_data.dig_T1 = r16uLE_buf(reg); reg+=2;
  243. bme280_data.dig_T2 = r16sLE_buf(reg); reg+=2;
  244. bme280_data.dig_T3 = r16sLE_buf(reg);
  245. //NODE_DBG("dig_T: %d\t%d\t%d\n", bme280_data.dig_T1, bme280_data.dig_T2, bme280_data.dig_T3);
  246. r8u_n(BME280_REGISTER_DIG_P, 18, buf);
  247. reg = buf;
  248. bme280_data.dig_P1 = r16uLE_buf(reg); reg+=2;
  249. bme280_data.dig_P2 = r16sLE_buf(reg); reg+=2;
  250. bme280_data.dig_P3 = r16sLE_buf(reg); reg+=2;
  251. bme280_data.dig_P4 = r16sLE_buf(reg); reg+=2;
  252. bme280_data.dig_P5 = r16sLE_buf(reg); reg+=2;
  253. bme280_data.dig_P6 = r16sLE_buf(reg); reg+=2;
  254. bme280_data.dig_P7 = r16sLE_buf(reg); reg+=2;
  255. bme280_data.dig_P8 = r16sLE_buf(reg); reg+=2;
  256. bme280_data.dig_P9 = r16sLE_buf(reg);
  257. // 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);
  258. if (full_init) w8u(BME280_REGISTER_CONFIG, config);
  259. if (bme280_isbme) {
  260. bme280_data.dig_H1 = r8u(BME280_REGISTER_DIG_H1);
  261. r8u_n(BME280_REGISTER_DIG_H2, 7, buf);
  262. reg = buf;
  263. bme280_data.dig_H2 = r16sLE_buf(reg); reg+=2;
  264. bme280_data.dig_H3 = reg[0]; reg++;
  265. 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
  266. 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
  267. bme280_data.dig_H6 = (int8_t)reg[0];
  268. // 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);
  269. if (full_init) w8u(BME280_REGISTER_CONTROL_HUM, bme280_ossh);
  270. lua_pushinteger(L, 2);
  271. } else {
  272. lua_pushinteger(L, 1);
  273. }
  274. #undef r16uLE_buf
  275. #undef r16sLE_buf
  276. if (full_init) w8u(BME280_REGISTER_CONTROL, bme280_mode);
  277. return 1;
  278. }
  279. static int bme280_lua_init(lua_State* L) {
  280. uint8_t sda;
  281. uint8_t scl;
  282. uint8_t config;
  283. uint8_t ack;
  284. uint8_t full_init;
  285. platform_print_deprecation_note("bme280.init() is replaced by bme280.setup()", "in the next version");
  286. if (!lua_isnumber(L, 1) || !lua_isnumber(L, 2)) {
  287. return luaL_error(L, "wrong arg range");
  288. }
  289. sda = luaL_checkinteger(L, 1);
  290. scl = luaL_checkinteger(L, 2);
  291. platform_i2c_setup(bme280_i2c_id, sda, scl, PLATFORM_I2C_SPEED_SLOW);
  292. // remove sda and scl parameters from stack
  293. lua_remove(L, 1);
  294. lua_remove(L, 1);
  295. return bme280_lua_setup(L);
  296. }
  297. static void bme280_readoutdone (void *arg)
  298. {
  299. NODE_DBG("timer out\n");
  300. lua_State *L = lua_getstate();
  301. lua_rawgeti (L, LUA_REGISTRYINDEX, lua_connected_readout_ref);
  302. lua_call (L, 0, 0);
  303. luaL_unref (L, LUA_REGISTRYINDEX, lua_connected_readout_ref);
  304. os_timer_disarm (&bme280_timer);
  305. }
  306. static int bme280_lua_startreadout(lua_State* L) {
  307. uint32_t delay;
  308. if (lua_isnumber(L, 1)) {
  309. delay = luaL_checkinteger(L, 1);
  310. if (!delay) {delay = BME280_SAMPLING_DELAY;} // if delay is 0 then set the default delay
  311. }
  312. if (!lua_isnoneornil(L, 2)) {
  313. lua_pushvalue(L, 2);
  314. lua_connected_readout_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  315. } else {
  316. lua_connected_readout_ref = LUA_NOREF;
  317. }
  318. w8u(BME280_REGISTER_CONTROL_HUM, bme280_ossh);
  319. w8u(BME280_REGISTER_CONTROL, (bme280_mode & 0xFC) | BME280_FORCED_MODE);
  320. NODE_DBG("control old: %x, control: %x, delay: %d\n", bme280_mode, (bme280_mode & 0xFC) | BME280_FORCED_MODE, delay);
  321. if (lua_connected_readout_ref != LUA_NOREF) {
  322. NODE_DBG("timer armed\n");
  323. os_timer_disarm (&bme280_timer);
  324. os_timer_setfn (&bme280_timer, (os_timer_func_t *)bme280_readoutdone, L);
  325. os_timer_arm (&bme280_timer, delay, 0); // trigger callback when readout is ready
  326. }
  327. return 0;
  328. }
  329. // Return nothing on failure
  330. // Return T, QFE, H if no altitude given
  331. // Return T, QFE, H, QNH if altitude given
  332. static int bme280_lua_read(lua_State* L) {
  333. uint8_t buf[8];
  334. uint32_t qfe;
  335. uint8_t calc_qnh = lua_isnumber(L, 1);
  336. r8u_n(BME280_REGISTER_PRESS, 8, buf); // registers are P[3], T[3], H[2]
  337. // Must do Temp first since bme280_t_fine is used by the other compensation functions
  338. uint32_t adc_T = (uint32_t)(((buf[3] << 16) | (buf[4] << 8) | buf[5]) >> 4);
  339. if (adc_T == 0x80000 || adc_T == 0xfffff)
  340. return 0;
  341. lua_pushinteger(L, bme280_compensate_T(adc_T));
  342. uint32_t adc_P = (uint32_t)(((buf[0] << 16) | (buf[1] << 8) | buf[2]) >> 4);
  343. if (adc_P ==0x80000 || adc_P == 0xfffff) {
  344. lua_pushnil(L);
  345. calc_qnh = 0;
  346. } else {
  347. qfe = bme280_compensate_P(adc_P);
  348. lua_pushinteger(L, qfe);
  349. }
  350. uint32_t adc_H = (uint32_t)((buf[6] << 8) | buf[7]);
  351. if (!bme280_isbme || adc_H == 0x8000 || adc_H == 0xffff)
  352. lua_pushnil(L);
  353. else
  354. lua_pushinteger(L, bme280_compensate_H(adc_H));
  355. if (calc_qnh) { // have altitude
  356. int32_t h = luaL_checkinteger(L, 1);
  357. double qnh = bme280_qfe2qnh(qfe, h);
  358. lua_pushinteger(L, (int32_t)(qnh + 0.5));
  359. return 4;
  360. }
  361. return 3;
  362. }
  363. static int bme280_lua_temp(lua_State* L) {
  364. uint8_t buf[3];
  365. r8u_n(BME280_REGISTER_TEMP, 3, buf); // registers are P[3], T[3], H[2]
  366. uint32_t adc_T = (uint32_t)(((buf[0] << 16) | (buf[1] << 8) | buf[2]) >> 4);
  367. if (adc_T == 0x80000 || adc_T == 0xfffff)
  368. return 0;
  369. lua_pushinteger(L, bme280_compensate_T(adc_T));
  370. lua_pushinteger(L, bme280_t_fine);
  371. return 2;
  372. }
  373. static int bme280_lua_baro(lua_State* L) {
  374. uint8_t buf[6];
  375. r8u_n(BME280_REGISTER_PRESS, 6, buf); // registers are P[3], T[3], H[2]
  376. uint32_t adc_T = (uint32_t)(((buf[3] << 16) | (buf[4] << 8) | buf[5]) >> 4);
  377. uint32_t T = bme280_compensate_T(adc_T);
  378. uint32_t adc_P = (uint32_t)(((buf[0] << 16) | (buf[1] << 8) | buf[2]) >> 4);
  379. if (adc_T == 0x80000 || adc_T == 0xfffff || adc_P ==0x80000 || adc_P == 0xfffff)
  380. return 0;
  381. lua_pushinteger(L, bme280_compensate_P(adc_P));
  382. lua_pushinteger(L, T);
  383. return 2;
  384. }
  385. static int bme280_lua_humi(lua_State* L) {
  386. if (!bme280_isbme) return 0;
  387. uint8_t buf[5];
  388. r8u_n(BME280_REGISTER_TEMP, 5, buf); // registers are P[3], T[3], H[2]
  389. uint32_t adc_T = (uint32_t)(((buf[0] << 16) | (buf[1] << 8) | buf[2]) >> 4);
  390. uint32_t T = bme280_compensate_T(adc_T);
  391. uint32_t adc_H = (uint32_t)((buf[3] << 8) | buf[4]);
  392. if (adc_T == 0x80000 || adc_T == 0xfffff || adc_H == 0x8000 || adc_H == 0xffff)
  393. return 0;
  394. lua_pushinteger(L, bme280_compensate_H(adc_H));
  395. lua_pushinteger(L, T);
  396. return 2;
  397. }
  398. static int bme280_lua_qfe2qnh(lua_State* L) {
  399. if (!lua_isnumber(L, 2)) {
  400. return luaL_error(L, "wrong arg range");
  401. }
  402. int32_t qfe = luaL_checkinteger(L, 1);
  403. int32_t h = luaL_checkinteger(L, 2);
  404. double qnh = bme280_qfe2qnh(qfe, h);
  405. lua_pushinteger(L, (int32_t)(qnh + 0.5));
  406. return 1;
  407. }
  408. static int bme280_lua_altitude(lua_State* L) {
  409. if (!lua_isnumber(L, 2)) {
  410. return luaL_error(L, "wrong arg range");
  411. }
  412. int32_t P = luaL_checkinteger(L, 1);
  413. int32_t qnh = luaL_checkinteger(L, 2);
  414. double h = (1.0 - pow((double)P/(double)qnh, 1.0/5.25588)) / 2.25577e-5 * 100.0;
  415. lua_pushinteger(L, (int32_t)(h + (((h<0)?-1:(h>0)) * 0.5)));
  416. return 1;
  417. }
  418. static int bme280_lua_dewpoint(lua_State* L) {
  419. if (!lua_isnumber(L, 2)) {
  420. return luaL_error(L, "wrong arg range");
  421. }
  422. double H = luaL_checkinteger(L, 1)/100000.0;
  423. double T = luaL_checkinteger(L, 2)/100.0;
  424. const double c243 = 243.5;
  425. const double c17 = 17.67;
  426. double c = ln(H) + ((c17 * T) / (c243 + T));
  427. double d = (c243 * c)/(c17 - c) * 100.0;
  428. lua_pushinteger(L, (int32_t)(d + (((d<0)?-1:(d>0)) * 0.5)));
  429. return 1;
  430. }
  431. static const LUA_REG_TYPE bme280_map[] = {
  432. // init() is deprecated
  433. { LSTRKEY( "init" ), LFUNCVAL(bme280_lua_init)},
  434. { LSTRKEY( "setup" ), LFUNCVAL(bme280_lua_setup)},
  435. { LSTRKEY( "temp" ), LFUNCVAL(bme280_lua_temp)},
  436. { LSTRKEY( "baro" ), LFUNCVAL(bme280_lua_baro)},
  437. { LSTRKEY( "humi" ), LFUNCVAL(bme280_lua_humi)},
  438. { LSTRKEY( "startreadout" ), LFUNCVAL(bme280_lua_startreadout)},
  439. { LSTRKEY( "qfe2qnh" ), LFUNCVAL(bme280_lua_qfe2qnh)},
  440. { LSTRKEY( "altitude" ), LFUNCVAL(bme280_lua_altitude)},
  441. { LSTRKEY( "dewpoint" ), LFUNCVAL(bme280_lua_dewpoint)},
  442. { LSTRKEY( "read" ), LFUNCVAL(bme280_lua_read)},
  443. { LNILKEY, LNILVAL}
  444. };
  445. NODEMCU_MODULE(BME280, "bme280", bme280_map, NULL);